From b8249fef29f2d681e92c1b281779ad48c1216392 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 3 Dec 2026 11:56:21 +0100 Subject: [PATCH] Refuse a binary-diffed staged file preflight cannot inspect, and name it --- src/preflight.rs | 24 +++++++++++++++ tests/hooks.rs | 46 ++++++++++++++++++++++++++++ tests/local_patch.rs | 71 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+) diff --git a/src/preflight.rs b/src/preflight.rs index 592ec54..9d23aca 100644 --- a/src/preflight.rs +++ b/src/preflight.rs @@ -21,6 +21,10 @@ pub(crate) enum PreflightError { coordinate: &'static str, kind: &'static str, }, + #[error( + "local patch includes {0:?} as a binary diff the preflight cannot inspect; remove it before upload, or pass --allow-path for that exact path" + )] + Binary(String), #[error("could not map the local patch to file paths and lines; no review was requested")] InvalidPatch, } @@ -49,6 +53,26 @@ pub(crate) fn check(changes: &LocalChanges, allowed: &[String]) -> Result<(), Pr continue; } let file = current.ok_or(PreflightError::InvalidPatch)?; + // A binary diff carries no hunk lines for its file: content that is + // not text, or that a `.gitattributes` `-diff` attribute marks + // binary, would otherwise leave the checkout unmentioned and its + // content uninspected. Refuse it by path. A whole-patch encoding + // refusal (`GitError::NotUtf8`, src/git.rs) runs before this check, + // so a sibling file that is not UTF-8 can still hide this one behind + // an encoding note (prooflinedev/cli#131). + if hunk.is_none() + && (line.starts_with("Binary files ") || line.starts_with("GIT binary patch")) + { + let path = file + .new + .as_deref() + .or(file.old.as_deref()) + .ok_or(PreflightError::InvalidPatch)?; + if !is_allowed(path, allowed) { + return Err(PreflightError::Binary(path.to_owned())); + } + continue; + } if line.starts_with("@@ ") { // A section heading can quote a line outside the hunk. Its exact // coordinate is a patch line, not a guessed source-file line. diff --git a/tests/hooks.rs b/tests/hooks.rs index bc910b0..430f665 100644 --- a/tests/hooks.rs +++ b/tests/hooks.rs @@ -761,6 +761,52 @@ async fn each_pushed_branch_is_reviewed_against_the_commit_the_remote_has() { ); } +/// A commit that git renders as a binary diff is refused by name before any +/// upload, whatever its content holds. +#[tokio::test] +async fn a_pre_push_hook_names_a_binary_diffed_file_without_uploading_it() { + let checkout = Checkout::new().await; + let private = "-----BEGIN PRIVATE KEY-----"; + let mut bytes = format!("{private}\nsecretmaterial\n").into_bytes(); + bytes.push(0); + tokio::fs::write(checkout.root().join("cert.pem"), &bytes) + .await + .unwrap(); + checkout.git(&["add", "cert.pem"]).await; + checkout.git(&["commit", "-qm", "cert.pem"]).await; + let head = checkout.git(&["rev-parse", "HEAD"]).await; + let unreachable = "http://127.0.0.1:9"; + let input = format!( + "refs/heads/main {head} refs/heads/main {}\n", + checkout.base + ); + let output = checkout + .proofline( + unreachable, + &[ + "hook", + "run", + "pre-push", + "origin", + "https://github.com/acme/web.git", + ], + &input, + ) + .await; + let text = stderr(&output); + assert!( + text.contains( + "Proofline could not review main: local patch includes \"cert.pem\" as a binary diff the preflight cannot inspect", + ), + "{text}" + ); + assert!(!text.contains(private), "{text}"); + assert!( + text.contains("The push proceeds without a complete review."), + "{text}" + ); +} + /// Real pushes through the installed hook: the destination ref, not the /// source, says whether a line is a branch to review or a tag to skip. #[tokio::test] diff --git a/tests/local_patch.rs b/tests/local_patch.rs index 1c41110..841b034 100644 --- a/tests/local_patch.rs +++ b/tests/local_patch.rs @@ -679,6 +679,77 @@ async fn added_context_deleted_and_quoted_content_refuses_before_http() { } } +#[tokio::test] +async fn a_binary_diff_refuses_the_staged_file_before_http() { + let private = "-----BEGIN PRIVATE KEY-----"; + let github = format!("ghp_{}", "C".repeat(36)); + // Content with a NUL byte is binary to git whatever its text says; a + // `.gitattributes` `-diff` attribute hides textual content too. Both + // render as a binary diff, so the hunk scan never reads their lines. + for (files, mark) in [ + (vec![("cert.pem", "pinned\n")], true), + ( + vec![ + (".gitattributes", "*.pem -diff\n"), + ("cert.pem", "pinned\n"), + ], + false, + ), + ] { + let checkout = Checkout::new(&files).await; + let mut bytes = format!("{private}\nchanged\n{github}\n").into_bytes(); + if mark { + bytes.push(0); + } + tokio::fs::write(checkout.root().join("cert.pem"), &bytes) + .await + .unwrap(); + checkout.git(&["add", "cert.pem"]).await; + let index = checkout.index().await; + let server = Server::start(true).await; + let output = checkout.review(&server, &[], &[]).await; + assert_refusal( + &output, + "local patch includes \"cert.pem\" as a binary diff the preflight cannot inspect", + private, + ); + server.assert_no_requests(); + assert_eq!(checkout.index().await, index); + server.close().await; + } +} + +#[tokio::test] +async fn the_exact_allow_path_permits_a_binary_diffed_file() { + let checkout = Checkout::new(&[ + (".gitattributes", "*.pem -diff\n"), + ("cert.pem", "pinned\n"), + ]) + .await; + checkout.write("cert.pem", "changed\n").await; + checkout.git(&["add", "cert.pem"]).await; + let index = checkout.index().await; + let expected = checkout.index_patch().await; + let server = Server::start(true).await; + let output = checkout.review(&server, &["cert.pem"], &[]).await; + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + assert!(String::from_utf8_lossy(&output.stdout).contains(REVIEW)); + assert_eq!( + *server.fixture.uploads.lock().unwrap(), + vec![json!({ "base_commit_sha": checkout.base, "patch": expected })] + ); + assert!(server.fixture.uploads.lock().unwrap()[0]["patch"] + .as_str() + .unwrap() + .contains("Binary files a/cert.pem and b/cert.pem differ")); + assert_eq!(checkout.index().await, index); + server.close().await; +} + #[tokio::test] async fn exact_overrides_keep_other_paths_blocked_and_upload_the_original_patch() { let github = format!("ghp_{}", "B".repeat(36));