policy: fail when staged content cannot be read #377

Closed
github-bot wants to merge 1 commit from gh-pr/377/head into gh-pr/377/base
Owner

@⁠lordon1a on GitHub · 2026-11-24 02:44 UTC · original
fix/policy-staged-read-error → main · +14 −2 · 1 files · 1 commits · closed without merging

Summary

xtask/src/policy.rs lists staged files via git diff --cached --name-only --diff-filter=ACMR -z (so deleted files are already excluded), yet run() silently skipped any file where git show :<path> returned non-zero (continue; // deleted in the index), counting unreadable entries as passing the asset policy check.

This change:

  1. Extracts check_files() and bails with the file path and exit status whenever git show :<path> fails in --staged mode.
  2. Adds a synthetic unit test (staged_file_read_failure_is_reported) verifying that an unreadable staged path fails the check with the path named in the error.

Verification

Tested on Windows 11 (rustc 1.100.0, x86_64-pc-windows-gnullvm):

  • cargo test -p xtask: 5 passed, 0 failed (the new test fails on main@d2a4bae1f where the read failure is silently skipped).
  • cargo clippy -p xtask --all-targets: 0 warnings.
  • cargo xtask policy: 151 files ok (zero game data).

AI-assisted.

**@⁠lordon1a** on GitHub · 2026-11-24 02:44 UTC · [original](https://github.com/Marvisak/VIctory/pull/377) `fix/policy-staged-read-error` → `main` · +14 −2 · 1 files · 1 commits · closed without merging ## Summary `xtask/src/policy.rs` lists staged files via `git diff --cached --name-only --diff-filter=ACMR -z` (so deleted files are already excluded), yet `run()` silently skipped any file where `git show :<path>` returned non-zero (`continue; // deleted in the index`), counting unreadable entries as passing the asset policy check. This change: 1. Extracts `check_files()` and bails with the file path and exit status whenever `git show :<path>` fails in `--staged` mode. 2. Adds a synthetic unit test (`staged_file_read_failure_is_reported`) verifying that an unreadable staged path fails the check with the path named in the error. ## Verification Tested on Windows 11 (`rustc 1.100.0`, `x86_64-pc-windows-gnullvm`): - `cargo test -p xtask`: **5 passed, 0 failed** (the new test fails on `main@d2a4bae1f` where the read failure is silently skipped). - `cargo clippy -p xtask --all-targets`: **0 warnings**. - `cargo xtask policy`: **151 files ok** (zero game data). AI-assisted.
Report an index read failure instead of counting the file as checked.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:01:41 +00:00
github-bot closed this pull request 2026-11-26 04:01:41 +00:00
Author
Owner

@⁠SkippySteve on GitHub · 2026-11-24 03:13 UTC · original

Thanks for this. The silent continue is a real hole, and I can reproduce it. I tested on Linux (Arch, stable Rust) against main at d2a4bae.

Tests on this branch (4da59d1): cargo test -p xtask passes 5/5, cargo clippy -p xtask --all-targets gives no warnings, and cargo xtask policy reports 151 files ok.

End to end, cargo xtask policy --staged with a crafted index:

staged entry main this PR #246
gitlink 160000 to a commit not in this repo (vendor/blob) 1 files ok (passes) Error: cannot read staged file vendor/blob (exit status: 128) POLICY: vendor/blob: a submodule: content from another repository bypasses this check
MZ\0\0 under a non-UTF-8 name (bad\xff.rs) 1 files ok (passes) Error: cannot read staged file bad�.rs (exit status: 128) 3 violations: name not UTF-8, PE magic, NUL bytes
committed symlink lnk.rs replaced by MZ\0\0 (staged as T) 0 files ok 0 files ok (still passes) 2 violations: PE magic, NUL bytes

So this PR closes the first two holes. Some notes:

  1. bail! stops at the first file. Every other problem goes into violations and they are reported together. With bail!, a commit that has an unreadable entry and a real binary shows only the first one. Pushing format!("{path}: cannot read the staged content") onto violations and continuing would fit the existing output better.
  2. exit status: 128 hides the reason. Adding String::from_utf8_lossy(&out.stderr) to the message would show git's own error (bad object :vendor/blob).
  3. The root cause is git show :<path>. The path comes from from_utf8_lossy, so a non-UTF-8 name can never be read back by name. A gitlink has no blob to show. #246 reads git diff --cached --raw -z instead and fetches each entry by object id with git cat-file blob <oid>, so no file name can break the read. It reports gitlinks and non-UTF-8 names as their own violations, adds T to the diff filter, and reads symlinks as git stores them.
  4. The new unit test checks a path against the real repository index. That is fine as a regression test. The table above covers the realistic cases it stands in for.

Overlap with #246: #246 already turns an unreadable staged entry into a violation (read() returns Err → violations.push), so this change is a subset of it. The two PRs also conflict textually in xtask/src/policy.rs (git merge-tree reports a content conflict). If #246 lands, this PR isn't needed. If maintainers would rather take the small fix first, this one is correct as it stands, and I'd suggest note 1 before merging. Then I'll rebase #246 on top.

**@⁠SkippySteve** on GitHub · 2026-11-24 03:13 UTC · [original](https://github.com/Marvisak/VIctory/pull/377#issuecomment-7120891027) Thanks for this. The silent `continue` is a real hole, and I can reproduce it. I tested on Linux (Arch, stable Rust) against `main` at `d2a4bae`. **Tests on this branch (`4da59d1`):** `cargo test -p xtask` passes 5/5, `cargo clippy -p xtask --all-targets` gives no warnings, and `cargo xtask policy` reports 151 files ok. **End to end, `cargo xtask policy --staged` with a crafted index:** | staged entry | `main` | this PR | #246 | |---|---|---|---| | gitlink `160000` to a commit not in this repo (`vendor/blob`) | `1 files ok` (passes) | `Error: cannot read staged file vendor/blob (exit status: 128)` | `POLICY: vendor/blob: a submodule: content from another repository bypasses this check` | | `MZ\0\0` under a non-UTF-8 name (`bad\xff.rs`) | `1 files ok` (passes) | `Error: cannot read staged file bad�.rs (exit status: 128)` | 3 violations: name not UTF-8, PE magic, NUL bytes | | committed symlink `lnk.rs` replaced by `MZ\0\0` (staged as `T`) | `0 files ok` | `0 files ok` (still passes) | 2 violations: PE magic, NUL bytes | So this PR closes the first two holes. Some notes: 1. **`bail!` stops at the first file.** Every other problem goes into `violations` and they are reported together. With `bail!`, a commit that has an unreadable entry and a real binary shows only the first one. Pushing `format!("{path}: cannot read the staged content")` onto `violations` and continuing would fit the existing output better. 2. **`exit status: 128` hides the reason.** Adding `String::from_utf8_lossy(&out.stderr)` to the message would show git's own error (`bad object :vendor/blob`). 3. **The root cause is `git show :<path>`.** The path comes from `from_utf8_lossy`, so a non-UTF-8 name can never be read back by name. A gitlink has no blob to show. #246 reads `git diff --cached --raw -z` instead and fetches each entry by object id with `git cat-file blob <oid>`, so no file name can break the read. It reports gitlinks and non-UTF-8 names as their own violations, adds `T` to the diff filter, and reads symlinks as git stores them. 4. The new unit test checks a path against the real repository index. That is fine as a regression test. The table above covers the realistic cases it stands in for. **Overlap with #246:** #246 already turns an unreadable staged entry into a violation (`read()` returns `Err` → `violations.push`), so this change is a subset of it. The two PRs also conflict textually in `xtask/src/policy.rs` (`git merge-tree` reports a content conflict). If #246 lands, this PR isn't needed. If maintainers would rather take the small fix first, this one is correct as it stands, and I'd suggest note 1 before merging. Then I'll rebase #246 on top.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!377
No description provided.