Skip to content

plumbing: object, skip path validation in tree diff walk - #2222

Merged
pjbgf merged 1 commit into
mainfrom
tree-fix
Jun 26, 2026
Merged

pjbgf merged 1 commit into
mainfrom
tree-fix

Conversation

@pjbgf

@pjbgf pjbgf commented Jun 25, 2026

Copy link
Copy Markdown
Member

DiffTree walks each tree through treeNoder.Children -> transformChildren -> TreeWalker.Next, which validated every entry name with pathutil.ValidTreePath. That validator rejects control characters and .git-shaped names so that callers materialising entries into a worktree or archive stay safe. DiffTree does neither: it computes a diff in memory and never writes a name to disk.

The effect was that a diff over a tree containing an entry name upstream Git accepts — e.g. one with an embedded newline, which verify_path permits since only '/' and NUL are forbidden in a path component — failed the whole diff with "invalid path: contains control character" instead of reporting the change.

Add an unexported skipPathValidation flag to TreeWalker and set it for the diff walk so the tree is enumerated faithfully. The flag defaults to off, so the materialising callers (archive, FileIter, FindEntry, TreeEntryFile) keep validating exactly as before.

Follow-up from #2105.

DiffTree walks each tree through treeNoder.Children -> transformChildren
-> TreeWalker.Next, which validated every entry name with
pathutil.ValidTreePath. That validator rejects control characters and
.git-shaped names so that callers materialising entries into a worktree
or archive stay safe. DiffTree does neither: it computes a diff in memory
and never writes a name to disk.

The effect was that a diff over a tree containing an entry name upstream
Git accepts — e.g. one with an embedded newline, which verify_path
permits since only '/' and NUL are forbidden in a path component — failed
the whole diff with "invalid path: contains control character" instead of
reporting the change.

Add an unexported skipPathValidation flag to TreeWalker and set it for the
diff walk so the tree is enumerated faithfully. The flag defaults to off,
so the materialising callers (archive, FileIter, FindEntry, TreeEntryFile)
keep validating exactly as before.

Signed-off-by: Paulo Gomes <[email protected]>
@pjbgf
pjbgf requested review from Copilot and hiddeco and removed request for Copilot June 25, 2026 15:20
@pjbgf
pjbgf merged commit c8de2fa into main Jun 26, 2026
25 of 26 checks passed
@pjbgf
pjbgf deleted the tree-fix branch June 26, 2026 08:16
pjbgf pushed a commit that referenced this pull request Aug 8, 2026
iterateCommitTrees enumerates every reachable tree entry through
TreeWalker.Next, which validates each name with pathutil.ValidTreePath.
That validator exists so callers materialising entries into a worktree or
archive stay safe. The revlist walk does neither: it only collects hashes
to decide which objects to send.

The effect was that a single tree entry whose name upstream Git accepts —
one containing a control character, which verify_path permits since only
'/' and NUL are forbidden in a component — aborted the whole walk, so
Push failed with

    invalid path "\x1b\x1b\x1b": contains control character

even when the offending entry existed only in history, was already present
on the remote, and would never be sent.

TreeWalker gains SkipPathValidation, which revlist calls. This is the same
reasoning applied to the tree diff walk in #2222; that fix set the flag
in-package, whereas revlist is a separate package and needs an exported
entry point.

Note the seen-set check in Next runs before validation, so an entry whose
blob is still reachable from a newer tree never reaches the validator.
Deleted content has a blob nothing else references, which is why the
reported case involved history rather than the pushed commit.
pjbgf pushed a commit that referenced this pull request Aug 8, 2026
Backport of #2222, which landed on main but not on this branch.

DiffTree walks each tree through treeNoder.Children -> transformChildren
-> TreeWalker.Next, which validated every entry name with
pathutil.ValidTreePath. That validator protects callers materialising
entries into a worktree or archive; DiffTree does neither, computing a
diff in memory without ever writing a name to disk.

So a diff over a tree containing an entry name upstream Git accepts —
one with a control character, which verify_path permits since only '/'
and NUL are forbidden in a component — failed with

    from: invalid path "\x1b\x1b\x1b": contains control character

instead of reporting the change.

This is the same defect as the revlist walk fixed in the previous commit,
at the sibling call site. It is split out so it can be dropped or landed
separately.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants