Stop a crafted path from replacing another file's diff in the gathered review context

Merged
DS
25d ago
+331-75

This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.

Why we make these changes

scripts/gather-review-context.sh (douwebot) and its fork scripts/gather-pydantic-ai-review-context.sh (CI Review, UI Security Review) split a PR's diff into one file per changed path under diff/, and changed-files.txt names each path's diff in its third column. A crafted path could make a row name the wrong diff, so the reviewer read a decoy in place of the real change:

  • Names. / became __ and leading dots were stripped, so pydantic_ai_slim/pydantic_ai/_ssrf.py and a root-level pydantic_ai_slim__pydantic_ai___ssrf.py shared one diff, as did .github/ci.yml and github/ci.yml.
  • Header parsing. The path was cut after the last b/ of diff --git a/P b/P, so a crafted z b/src.py was read as src.py. A path git quotes (non-ASCII, ", \, control characters) got a mangled name that no row named.
  • Forged rows. douwebot's index was built by string interpolation, so a filename with a tab and a newline (src.py<TAB>+1 -0<TAB>diff/evil.diff<LF>zz) wrote a whole src.py row naming another file's diff.

Now:

  • Both scripts percent-encode %, \, /, tab, newline, carriage return and a leading . in names, in the awk writer and the jq index alike.
  • The awk holds each file's lines until its extended header ends. A rename or copy names its new path in rename to/copy to; any other header repeats one path, so the path is its first half. A quoted path is unquoted, and the diff runs with core.quotePath=false.
  • douwebot's index uses @tsv, like CI Review's. Its path column now escapes a \, tab, newline or carriage return, as CI Review's already did.
  • shared/review-context.md and both review prompts point at the third column of changed-files.txt for each diff's name. The two importing locks are recompiled with gh-aw v1.2.1.

New public surface

None.

User-visible behavior

changed-files.txt for a PR adding both crafted paths:

-pydantic_ai_slim/pydantic_ai/_ssrf.py	+1 -0	diff/pydantic_ai_slim__pydantic_ai___ssrf.py.diff
+pydantic_ai_slim/pydantic_ai/_ssrf.py	+1 -0	diff/pydantic_ai_slim%2Fpydantic_ai%2F_ssrf.py.diff
 pydantic_ai_slim__pydantic_ai___ssrf.py	+1 -0	diff/pydantic_ai_slim__pydantic_ai___ssrf.py.diff

Verification

test_a_crafted_path_cannot_replace_another_files_diff runs both gatherers on one PR holding every case above: colliding names, b/ paths, a crafted rename, non-ASCII, "/\/tab paths, x\y next to x\\y, and the forged-row filename. It checks that the index lists each path once and that each row's diff adds exactly that path's lines. It fails on main for both scripts. The existing gather tests and agentic_workflow_guard.py check pass.

What changes for existing users

Nothing in the library. Review-context readers still find each file's diff in the third column of changed-files.txt; only its spelling changes. douwebot fetches its gatherer from the base ref, so it picks this up once merged.

Three pre-existing gaps in the same scripts (an unanchored generated-file skip, typechange truncation, gh pr diff fallback quoting) are tracked in #12896 for a follow-up PR after this one.

PR decisions

2026-11-08 · Percent-encode diff names · iter -

  • Decision: Name each per-file diff by percent-encoding %, / and a leading . (a/b.py -> a%2Fb.py.diff)
  • Why: Injective and decodable with urllib.parse.unquote; underscore escaping (_->_u) garbles the many underscores in Python paths; a hash suffix needs a hash in awk
  • Source: https://github.com/pydantic/pydantic-ai/tree/fix/review-context-diff-names
  • Session: cf2ce080-49f1-4629-944e-b229219a889d

2026-11-08 · pre-push review 1 of 3 at 71c0b65c1e619d9892ec8eb820ffa9cae1b97207 · iter -

2026-11-08 · pre-push review 1 of 3 at 71c0b65c1e619d9892ec8eb820ffa9cae1b97207 · iter -

2026-11-08 · Fold b/ header parse and docs · iter -

2026-11-08 · Name each diff from its extended header · iter -

  • Decision: Hold each file's lines until its extended header ends; take a rename/copy target from rename to/copy to, else the first half of a/P b/P
  • Why: Only renames and copies have different a/ and b/ paths, and they always name the new path; a per-path git diff loop would turn renames into full adds
  • Source: https://github.com/pydantic/pydantic-ai/tree/fix/review-context-diff-names
  • Session: cf2ce080-49f1-4629-944e-b229219a889d

2026-11-08 · pre-push review 2 of 3 at fa955f68239e230afe397729b8aae18f6091da14 · iter -

2026-11-08 · pre-push review 2 of 3 at fa955f68239e230afe397729b8aae18f6091da14 · iter -

2026-11-09 · exempt delta 4ccb2118f0 from reviewed fa955f6823 · iter -

2026-11-09 · Unquote non-ASCII paths in the diff · iter -

  • Decision: Run the gatherers' git diff with -c core.quotePath=false
  • Why: With default quoting a non-ASCII path's header is quoted, the new header parse cannot name it, and its diff is dropped; before, it was written under a mangled name. Found in self-check after push
  • Source: https://github.com/pydantic/pydantic-ai/pull/12894
  • Session: cf2ce080-49f1-4629-944e-b229219a889d

2026-11-09 · pre-push review 3 of 3 at c76ccbd6d4c63c85189b8624dbeabaa59c89e11f · iter -

2026-11-09 · pre-push review 3 of 3 at c76ccbd6d4c63c85189b8624dbeabaa59c89e11f · iter -

2026-11-09 · Review-3 quoted-path finding is a sibling, not a regression · iter -

  • Decision: Park the awk unquote fix (scratchpad); hold local c76ccbd6d4 (quotePath) unpushed; report both plus a verified douwebot index row forge to the manager
  • Why: On main a quoted-header path's diff was written under a name no index row names, so the index already could not reach it; unquote alone would let CI Review's @tsv-escaped column 3 point x\y's row at x\\y's diff
  • Source: https://github.com/pydantic/pydantic-ai/pull/12894
  • Session: cf2ce080-49f1-4629-944e-b229219a889d

2026-11-09 · Fold quotePath, quoted-path unquote and forged-row fix · iter -

  • Decision: Fold all three siblings: core.quotePath=false; unquote git's C-style paths in awk; build douwebot's index with @tsv and percent-encode \, tab, LF, CR in names
  • Why: Manager: each reproduces on main, shares this PR's root cause, and "changes output only for paths that already fail on main"
  • Source: https://github.com/pydantic/pydantic-ai/pull/12894
  • Session: cf2ce080-49f1-4629-944e-b229219a889d

2026-11-09 · Encode index-escaped characters in diff names · iter -

  • Decision: Percent-encode \\ (%5C), tab (%09), LF (%0A) and CR (%0D) in diff names, in awk and jq
  • Why: @tsv escapes exactly those in column 3; encoding them leaves column 3 equal to the written file name, so an escaped name cannot point at another path's diff
  • Source: https://github.com/pydantic/pydantic-ai/pull/12894
  • Session: cf2ce080-49f1-4629-944e-b229219a889d

2026-11-09 · Adversarial verifier replaces a fourth review · iter -

  • Decision: Pre-push budget spent: remediate, run local checks, then one fresh adversarial-verifier on the final candidate; no fourth pre-push review
  • Why: Manager instruction; properties: each row names its own path's diff, no crafted path redirects or forges another row, both gatherers agree
  • Source: https://github.com/pydantic/pydantic-ai/pull/12894
  • Session: cf2ce080-49f1-4629-944e-b229219a889d

2026-11-09 · adversarial verifier at 4f397ef242e9ee574c05bfd8bf7cf96a6b20bf8f · iter -

2026-11-09 · adversarial verifier at 4f397ef242e9ee574c05bfd8bf7cf96a6b20bf8f · iter -

  • Decision: all three properties hold (code reading; its Bash was denied); four pre-existing gaps reported to manager, not folded
  • Why: tree 98b13a7d400721a865ba9a3863c3054ccf6cf011; gaps: typechange sections truncate one file, gh pr diff fallback drops octal-escaped non-ASCII, NAME_MAX (+1 byte per slash; longest repo path encodes to 163), unanchored uv.lock/cassettes skip hides e.g. cassettes/conftest.py
  • Source: https://github.com/pydantic/pydantic-ai/pull/12894
  • Session: cf2ce080-49f1-4629-944e-b229219a889d

2026-11-09 · defer: generated-file skip, typechange, fallback quoting · iter -

  • Decision: Filed as #12896 (assigned dsfaccini, pydanty:skip; immediate-work route) for one follow-up PR after #12894 merges
  • Why: Out of scope for this PR: each changes what lands in a diff or which files count as generated; the fallback item is unverified; same lines, so doing it now would conflict
  • Source: https://github.com/pydantic/pydantic-ai/issues/12896
  • Session: cf2ce080-49f1-4629-944e-b229219a889d

2026-11-09 · skip: NAME_MAX on encoded diff names · iter -

  • Decision: Skip: a path whose encoded name exceeds 255 bytes aborts the gather
  • Why: No real path reaches the limit (longest tracked path encodes to 163 bytes), and a crafted overlong path aborts the gather (fails closed) on main too
  • Source: https://github.com/pydantic/pydantic-ai/issues/12896
  • Session: cf2ce080-49f1-4629-944e-b229219a889d

2026-11-09 · #12859 decodes the @tsv path column · iter -

  • Decision: Whichever of #12859 and #12894 merges second makes #12859's runner decode the @tsv column 1 of changed-files.txt
  • Why: This PR builds douwebot's index with @tsv, so a path with a backslash, tab, newline or CR appears escaped in column 1, which #12859 compares to API filenames
  • Source: https://github.com/pydantic/pydantic-ai/pull/12859
  • Session: cf2ce080-49f1-4629-944e-b229219a889d

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
  • PR title is fit for the release changelog.