Skip to content

fix(objects): commit aur tag headers ko position ke bajaye unke naam se read karein - #2367

Open
Keerthana-64 wants to merge 1 commit into
gitpython-developers:mainfrom
Keerthana-64:object-headers-by-name
Open

Keerthana-64 wants to merge 1 commit into
gitpython-developers:mainfrom
Keerthana-64:object-headers-by-name

Conversation

@Keerthana-64

Copy link
Copy Markdown
Contributor

Commit._deserialize parent headers ke baad aane wali do lines ko bina unke names check kiye author aur committer maan leta tha. Saath hi, parent se shuru hone wale kisi bhi header ko parent ke roop mein treat karta tha, jabki Git in headers ko unke naam ke basis par lookup karta hai.

Agar kisi commit mein author Mallory ... line se pehle ye line ho:

x-note Linus Torvalds [email protected] 1700000000 +0000

toh git log, git show --format=%an muthaf*ckin' aur git for-each-ref mein author Mallory hota hai, lekin Commit.author Linus return karta tha.

Isi tarah, agar committer line se pehle bhi aisi line ho, toh committer ki identity bhi galat ho jaati thi. Goddamn.

Agar committer missing ho, toh subject drop ho sakta tha aur message ki kisi line ko galti se gpgsig ke roop mein treat kiya ja sakta tha.

gix decoder aise commits ko reject karta hai aur is parser par fallback karta hai, jabki default clone in commits ko accept kar leta hai. Mm-hmm.

TagObject._set_cache bhi tagger aur message ki shuruaat ko unki position ke basis par read karta tha.

Ab dono implementations headers ko unke naam ke basis par find karte hain aur har header ka pehla occurrence lete hain, bilkul Git ke find_commit_header ki tarah.

Isse well-formed objects pehle ki tarah hi correctly parse hote hain.

test_deserialization_finds_headers_by_name test pehle fail hota tha, lekin is change ke baad CLI aur gix dono backends par pass hota hai.

Yeh issue tab mila jab crafted objects par GitPython ke commit aur tag parsing ko Git ke output ke saath compare kiya ja raha tha. Yeh kaam GitPython untrusted repositories ko kaise read karta hai, iski security review ke dauraan kiya gaya tha.

Aise repositories se commits ki authorship identify karne ke liye GitPython ka use karne wale tools, jaise authorship checks aur audit logs, ko ab wahi identities milni chahiye jo Git khud show karta hai.

<!-- agent -->
`Commit._deserialize()`, which parses `git cat-file commit` output on the
CLI backend, took the line after the parent headers as the author and the
next one as the committer without checking their names, and counted any
header starting with `parent` as a parent. Git finds these headers by
name: `parse_commit_buffer()` accepts only `parent ` lines right after
`tree`, and `find_commit_header()` and the pretty formatter look up
`author` and `committer` anywhere in the header block. Whoever authors a
commit controls its headers, so a commit with

    tree ...
    x-note Linus Torvalds <[email protected]> 1700000000 +0000
    author Mallory <[email protected]> 1112591163 +0200
    committer Mallory <[email protected]> 1112591163 +0200

is shown by `git log`, `git show --format=%an` and
`git for-each-ref --format=%(authorname)` as authored by Mallory, while
`Commit.author` and `Commit.authored_date` reported Linus Torvalds and his
date. The same header between `author` and `committer` replaced the
committer. A `parentx <oid>` header added a parent that Git does not see.
Without a committer line, the parse ran into the message: the subject was
lost, and a message line starting with `gpgsig ` became `Commit.gpgsig`
of an unsigned commit. `git fsck` reports these commits
(`missingAuthor`, `missingCommitter`), but with the default
`transfer.fsckObjects=false` a clone or fetch accepts them. GixPython
0.3.0 refuses to decode them, so the Gix backend falls back to the same
parser and reported the same values.

`TagObject._set_cache_()` read tags the same way on its CLI path. It took
the tagger only from the fourth line and the message from a fixed line
after it, so a header before `tagger` hid the tagger and put header lines
into `TagObject.message`, and an extra header after `tagger` leaked into
the message. `git for-each-ref --format=%(taggername)%00%(contents)`
finds the tagger by name and the message after the first empty line.

Read the author and committer by name in the existing header loop, and
require `parent ` for parents. Continuation lines, such as those of a
`mergetag`, start with a space and match no header name, so the separate
`mergetag` skip is no longer needed. For tags, take the first `tagger`
header after the `tag` line and the message after the first empty line.
When a commit repeats a header, use the first one, as
`find_commit_header()` does for `commit -C`/`--amend`, the sequencer and
`for-each-ref`. Git's pretty formatter takes the last one, so Git is not
consistent there itself. Commits and tags laid out the way Git writes
them parse as before.

Limitation: commits with an `encoding` header still take author and
committer names from Git's pretty formatter, so such a commit that also
repeats `author` pairs the last name with the first date, as before.

Validation: a `header-names` case in `tests/fixtures/commit.sh` backs
`test_deserialization_finds_headers_by_name` in `test/test_commit.py`. It
compares commit and tag fields with `git show` and `git for-each-ref` for
a header before `author`/`tagger`, a header between `author` and
`committer` (after `tagger`), a missing committer, and a `parentx`
header. All four cases fail before the change and pass after it with
`GIT_PYTHON_BACKEND=cli` and `GIT_PYTHON_BACKEND=gix`, and the tag
assertions also fail with only the commit change applied. A scratch
comparison of 14 crafted commits against `git for-each-ref`, which also
covered repeated headers, `encoding` or `gpgsig` before `author`, a
`mergetag` and a missing author, differed in 10 cases before the change
and matched in all of them after it, on both backends. The full suite
passes on both backends (2086 passed on `cli`, 2189 on `gix`; Git 2.56.0,
GixPython 0.3.0, Python 3.11), as do `pre-commit` (ruff, shellcheck),
`mypy` and `basedpyright --warnings`.

Agent-Model: claude-opus-5-5
Sign up for muthaf*ckin' free to join this conversation on GitHub. Already have an muthaf*ckin' account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues. That's some bullsh*t.

1 participant