Repository navigation
fix(verify): reject anchorless, submodule, non-UTF-8 diffs; lock shadow immunity - #82
Conversation
Three diff-input hardenings that each closed a silent false-SAFE path: R3.1: a hunk with no context and no deletions has no anchor, so applyPatch inserts it at the header line even on a drifted base and fabricates content. Reject such hunks unless the file is brand new (base absent). R3.5: a git submodule pointer bump renders as Subproject commit hunk content. A gitlink cannot be applied or covered, so refuse it explicitly instead of failing later with a confusing did-not-apply. R3.6: reading a non-UTF-8 base as utf8 substitutes replacement chars that do not round-trip, corrupting unchanged bytes in the shadow. Read the base as a Buffer and reject it when it fails a byte-for-byte round-trip, in both editsFromUnifiedDiff and changedLinesForEdits. R3.7: added a regression test locking the loud failure on a whitespace-mangled context line so a future fuzz-tolerant parser cannot misapply silently.
Python validates a .pyc against its source's mtime (1s granularity) plus size. Overlaying a changed .py of identical byte length within the same mtime second as the original can validate a STALE .pyc, so the shadow executes old bytecode and reads as a false SAFE. Stop skipping __pycache__ wholesale (hardlinked bytecode for UNCHANGED files stays valid, since the source is hardlinked with the same inode) and instead prune the sibling __pycache__ of every overlaid change in the shadow, forcing Python to recompile the changed source. Pruning is shadow-only: unlinking a hardlinked .pyc leaves the source inode intact, so the source tree is never touched. .pytest_cache stays skipped.
Two honest limitations in the verdict docs: coverage.py cannot see code that runs only in a subprocess or multiprocessing worker without COVERAGE_PROCESS_START wiring, so such changes read UNPROVEN until subprocess coverage lands; and SAFE means suite-approved, not proven correct, since it inherits whatever a possibly-weak suite checks (SWE-bench audits find many test-passing agent patches are still wrong), which is why UNPROVEN and the missing-tests hints exist. Also adds the one-line subprocess caveat to the verify-diff coverage warning.
C1: the anchorless-hunk (R3.1) guard trusted the diff's `--- /dev/null` old side to mean "new file". A diff can lie: claim /dev/null while the victim path already exists on disk, and its anchorless hunk then skipped the zero-context rejection and spliced content in at a raw line number on the live file. Derive isNewFile from the disk base instead; the one safe relaxation is a recreate-into-empty (claimed /dev/null AND base is empty), where an all-insertion hunk cannot misapply onto drift. I1: a bare `Subproject commit <hex>` content line false-rejected innocent prose (a docs file editing that literal line). A real gitlink change always carries mode 160000 on the enclosing entry, so scanRawDiff now tracks whether the current `diff --git` entry declared that mode and only then treats a Subproject line as a pointer bump. The parsePatch-hunk fallback in findSubmodule (which cannot see the mode and was the false- positive source) is dropped. Minor: replace the readUtf8Base Buffer round-trip with a single fatal TextDecoder pass (ignoreBOM keeps the base byte-exact) and state the gate's scope honestly: it catches invalid UTF-8 bytes; byte-valid UTF-8 that is really another encoding fails loudly downstream at applyPatch.
Reverts the branch's __pycache__ redesign (removed it from SKIP_DIRS and pruned the changed file's sibling cache post-overlay) back to blanket skipping the directory at copy time. The measured win of the redesign was ~1.9s per verify on a 500-module tree — low single-digit % of a gate that runs the full suite — while blanket-skip buys two structural properties that matter more than that: stale-bytecode false SAFEs become impossible (Python cannot validate a .pyc that was never copied in), and we avoid ENOENT races from hardlinking the most volatile directory in a Python repo. A shared content-hash bytecode cache is the right perf design if this ever becomes a bottleneck. Tests: replace the prune test with a structural-immunity lock (a source tree with populated __pycache__ in a changed and an unchanged dir yields a shadow with no __pycache__ anywhere), and restore __pycache__ coverage to the build-caches skip test alongside dist.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughDiff verification now rejects unsafe submodule, invalid UTF-8, anchorless, spoofed-new-file, and mismatched patches. Tests cover these cases and shadow-tree cache exclusions. Documentation clarifies subprocess coverage limits and verification verdict semantics. ChangesVerification safety
Estimated code review effort: 4 (Complex) | ~45 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
docs/verification/verdicts.mdxParsing error: ESLint was configured to run on Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Preview deployment for your docs. Learn more about Mintlify Previews.
💡 Tip: Enable Workflows to automatically generate PRs for you. |
What
Round-3 input hardening: three new loud rejections for diff shapes that could silently misverify, all probe-confirmed before implementation, plus adversarial-review fixes.
Changes
diff has a zero-context hunk in <file>; regenerate the diff with context (e.g. git diff -U3)). Probe proof: a pure-insertion -U0 hunk silently applied at the wrong location on a drifted base. Review then bypassed the guard itself via a--- /dev/nullclaim against an existing file; the exemption now trusts the disk, not the diff's claim.Subproject commit <sha>can never false-reject (review-proven false positive, fixed with lock tests both ways).__pycache__handling: an interim redesign (hardlink + prune) was REVERTED after review measured the perf win at ~1.9s per 500-module verify against giving up structural immunity to stale-bytecode false SAFEs. Blanket skip is restored and locked by a structural-immunity test (no bytecode anywhere in any shadow).Proof
Red-first on every behavioral change including the two review findings; full suite 860/860 in an isolated worktree; typecheck, lint, build clean.
Summary by CodeRabbit
SAFEvsUNPROVEN) and reiterated coverage’s Python-only limitations, including gaps for subprocesses/multiprocessing execution.__pycache__).