policy: fail when staged content cannot be read #377

Closed
lordon1a wants to merge 1 commit from upstream-pr/377 into main
lordon1a commented 2026-11-24 02:44:28 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by lordon1a on 2026-11-24T02:44:28Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6573438212 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/377). Originally posted by [lordon1a](https://github.com/lordon1a) on 2026-11-24T02:44:28Z. --- ## 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.
copilot-pull-request-reviewer[bot commented 2026-11-24 06:52:40 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-24T02:44:42Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

<!-- greyfinch-sync:Marvisak/VIctory:review:6032903859 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/377#pullrequestreview-6032903859). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/copilot-pull-request-reviewer%5Bbot%5D) on 2026-11-24T02:44:42Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
SkippySteve commented 2026-11-24 06:55:22 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by SkippySteve on 2026-11-24T03:13:40Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7120891027 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/377#issuecomment-7120891027). Originally posted by [SkippySteve](https://github.com/SkippySteve) on 2026-11-24T03:13:40Z. --- 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.
mercuryVM commented 2026-11-24 13:01:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T12:50:47Z.

Review: APPROVED


Tested 4da59d1 on Windows 11, RTX 5070, MSVC: cargo test -p xtask 5 passed, including staged_file_read_failure_is_reported. cargo xtask policy passes on this head (151 files).

  • Safety check (xtask): it only changes how git show :<path> failures are handled. It runs no new processes.
  • Since the file list already comes from --diff-filter=ACMR, a git show failure can't be a deletion, so failing loudly is right. Silently skipping meant a file the check couldn't read passed the asset policy.
  • Edge case: a staged gitlink (a submodule pointer) also fails git show :<path>. There are no submodules today, but if one is added, filter mode 160000 out of the list rather than failing the hook.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6039492779 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/377#pullrequestreview-6039492779). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T12:50:47Z. **Review:** APPROVED --- Tested 4da59d1 on Windows 11, RTX 5070, MSVC: `cargo test -p xtask` 5 passed, including `staged_file_read_failure_is_reported`. `cargo xtask policy` passes on this head (151 files). - Safety check (xtask): it only changes how `git show :<path>` failures are handled. It runs no new processes. - Since the file list already comes from `--diff-filter=ACMR`, a `git show` failure can't be a deletion, so failing loudly is right. Silently skipping meant a file the check couldn't read passed the asset policy. - Edge case: a staged gitlink (a submodule pointer) also fails `git show :<path>`. There are no submodules today, but if one is added, filter mode `160000` out of the list rather than failing the hook. AI-assisted (Claude Code).
upstream-sync closed this pull request 2026-11-24 19:02:02 +00:00

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!377
No description provided.