From 946bfe7bc9613863e75cbe3d810905375b2519f2 Mon Sep 17 00:00:00 2001 From: Steven Zimmerman Date: Fri, 28 Aug 2026 22:00:25 -0400 Subject: [PATCH 1/4] fix(analysis): preserve candidate blob bytes on Windows --- .../src/analysis/git_candidate_execution.rs | 38 ++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/crates/ripr/src/analysis/git_candidate_execution.rs b/crates/ripr/src/analysis/git_candidate_execution.rs index 0e11ca9a1c3..ffb36db1f55 100644 --- a/crates/ripr/src/analysis/git_candidate_execution.rs +++ b/crates/ripr/src/analysis/git_candidate_execution.rs @@ -274,7 +274,13 @@ fn materialize( let tar_path = base_dir.join(format!("{candidate_tree}.tar")); let archive = crate::git::run_git_output_with_deadline_and_limit( root, - &["archive", "--format=tar", candidate_tree], + &[ + "-c", + "core.autocrlf=false", + "archive", + "--format=tar", + candidate_tree, + ], deadline.unwrap_or(Duration::from_mins(1)), MAX_ARCHIVE_BYTES, ) @@ -498,6 +504,22 @@ mod tests { }) } + fn candidate_blob(root: &Path, treeish: &str, path: &str) -> Result, String> { + let output = crate::git::run_git_output_with_deadline( + root, + &["show", &format!("{treeish}:{path}")], + GIT_DEADLINE, + ) + .map_err(|error| error.to_string())?; + if !output.status.success() { + return Err(format!( + "git show {treeish}:{path} failed: {}", + String::from_utf8_lossy(&output.stderr) + )); + } + Ok(output.stdout) + } + struct RepoGuard(PathBuf); impl Drop for RepoGuard { fn drop(&mut self) { @@ -560,8 +582,8 @@ mod tests { assert!(resolved.diff.contains("src/renamed.rs") || resolved.diff.contains("src/old.rs")); // Candidate bytes, not worktree bytes: assert_eq!( - std::fs::read_to_string(resolved.root.join("src/lib.rs")).map_err(|e| e.to_string())?, - "pub fn candidate() -> u8 { 2 }\n" + std::fs::read(resolved.root.join("src/lib.rs")).map_err(|e| e.to_string())?, + candidate_blob(&guard.0, &candidate, "src/lib.rs")? ); assert!(resolved.root.join("ripr.toml").exists()); Ok(()) @@ -591,8 +613,8 @@ mod tests { "diff must follow objects, not the worktree" ); assert_eq!( - std::fs::read_to_string(second.root.join("src/lib.rs")).map_err(|e| e.to_string())?, - "pub fn candidate() -> u8 { 2 }\n", + std::fs::read(second.root.join("src/lib.rs")).map_err(|e| e.to_string())?, + candidate_blob(&guard.0, &candidate, "src/lib.rs")?, "materialized bytes must follow the candidate tree" ); assert!( @@ -666,10 +688,8 @@ mod tests { "long path must materialize from the pax header" ); assert_eq!( - std::fs::read_to_string(resolved.root.join(&deep).join(&long_name)) - .map_err(|e| e.to_string())?, - "long path content -" + std::fs::read(resolved.root.join(&deep).join(&long_name)).map_err(|e| e.to_string())?, + candidate_blob(&guard.0, &candidate, &format!("{deep}/{long_name}"))? ); Ok(()) } From 3172e55252e9e5a3c1a1e358010ce27e61633674 Mon Sep 17 00:00:00 2001 From: "Steven Zimmerman, CPA" <15812269+EffortlessSteven@users.noreply.github.com> Date: Sat, 29 Aug 2026 02:55:37 -0400 Subject: [PATCH 2/4] fix(analysis): materialize candidate blobs without attribute-driven conversion (#3548 review) git archive honors .gitattributes (e.g. '*.rs text eol=crlf') even with core.autocrlf=false, so archive-extracted candidate roots could silently differ from the exact blob bytes bound by the immutable candidate identity. Materialize instead by enumerating the tree (ls-tree -r -z, fail-closed on non-UTF-8 paths per the #3545 lossy-collapse family) and writing each blob through cat-file, which never applies attribute conversion. Traversal-safe path joins and the bounded-output limits are preserved; the in-crate tar machinery is removed. Regression: a candidate committed under an eol=crlf attribute must materialize byte-identical to git show : (LF stays LF). --- .../src/analysis/git_candidate_execution.rs | 243 +++++++++--------- 1 file changed, 115 insertions(+), 128 deletions(-) diff --git a/crates/ripr/src/analysis/git_candidate_execution.rs b/crates/ripr/src/analysis/git_candidate_execution.rs index ffb36db1f55..1cbfa9a0595 100644 --- a/crates/ripr/src/analysis/git_candidate_execution.rs +++ b/crates/ripr/src/analysis/git_candidate_execution.rs @@ -11,7 +11,8 @@ //! (read-only plumbing; no ref, index, or worktree mutation); //! - the diff is `git diff-tree` between the two **trees**, preserving //! add/delete/rename/type-change information; -//! - the candidate root is materialized with `git archive` from the +//! - the candidate root is materialized blob-by-blob (`ls-tree` + +//! `cat-file`) from the //! candidate tree alone into a fresh temp directory, so every byte //! comes from the bound tree; //! - any failure (missing base/candidate, unsupported object mode, @@ -226,7 +227,7 @@ fn derive_diff( } /// Materialize the candidate tree into a fresh temp directory. Every -/// byte comes from `git archive` of the tree; the worktree and index +/// byte comes from `git cat-file` of the bound blob; the worktree and index /// are never consulted. fn materialize( root: &Path, @@ -249,7 +250,7 @@ fn materialize( // Arm cleanup BEFORE any fallible work. Every `?` below returns early, // and until this guard exists those paths leave `base_dir` — which by // then holds an extracted copy of the candidate tree — on disk forever. - // A fail-closed subject (an unsupported entry mode, a git archive + // A fail-closed subject (an unsupported entry mode, a git failure // failure, a bounded-read overrun) must not cost a permanent temp // directory; only the success path hands the guard to the caller, who // holds it for as long as the materialization is in use. @@ -265,142 +266,83 @@ fn materialize( } std::fs::create_dir_all(&target) .map_err(|error| failed(format!("materialization dir failed: {error}")))?; - // The tar lives OUTSIDE the extraction target so a candidate file - // named `__candidate.tar` cannot collide with or be deleted by the - // extraction (#3296 review note). The archive output is size-bounded - // through the shared limited-output authority so a bound tree - // cannot OOM the process (unbounded read-to-end was the previous - // behavior). - let tar_path = base_dir.join(format!("{candidate_tree}.tar")); - let archive = crate::git::run_git_output_with_deadline_and_limit( + // Materialize straight from blob identities. `git archive` honors + // `.gitattributes` (e.g. `*.rs text eol=crlf`) even with + // `core.autocrlf=false`, so extracted bytes could silently differ from + // the bound blob identity (#3548 review); `ls-tree` + `cat-file` emit + // raw blob bytes only. + let listing = crate::git::run_git_output_with_deadline_and_limit( root, - &[ - "-c", - "core.autocrlf=false", - "archive", - "--format=tar", - candidate_tree, - ], + &["ls-tree", "-r", "-z", candidate_tree], deadline.unwrap_or(Duration::from_mins(1)), MAX_ARCHIVE_BYTES, ) - .map_err(|error| failed(format!("git archive failed: {error}")))?; - if !archive.status.success() { + .map_err(|error| failed(format!("git ls-tree failed: {error}")))?; + if !listing.status.success() { return Err(failed( - "git archive of the candidate tree failed".to_string(), + "git ls-tree of the candidate tree failed".to_string(), )); } - std::fs::write(&tar_path, &archive.stdout) - .map_err(|error| failed(format!("archive write failed: {error}")))?; - let extracted = untar(&tar_path, &target)?; - let _ = std::fs::remove_file(&tar_path); - let _ = extracted; - Ok((target.clone(), cleanup)) -} - -/// Minimal in-crate tar extraction for `git archive` output: only the -/// entry shapes Git emits (regular files, directories), no symlinks or -/// gitlinks (those are rejected upstream as unsupported modes). -fn untar(tar_path: &Path, target: &Path) -> Result { - let bytes = - std::fs::read(tar_path).map_err(|error| failed(format!("archive read failed: {error}")))?; - let mut entries = 0usize; - let mut offset = 0usize; - let mut pending_path: Option = None; - while offset + 512 <= bytes.len() { - let header = &bytes[offset..offset + 512]; - // Two consecutive zero blocks are the end-of-archive marker; - // check before the empty-name skip so the marker terminates. - if header.iter().all(|byte| *byte == 0) { - break; + for entry in listing.stdout.split(|byte| *byte == 0) { + if entry.is_empty() { + continue; } - // ustar long paths split across the prefix field (345..500) and - // the name field: the real path is prefix + '/' + name. git - // archive prefers this split and only emits a pax 'x' header - // when even the split does not fit (#3296 review). - let prefix = tar_string(&header[345..500]); - let raw_name = tar_string(&header[0..100]); - let name = if prefix.is_empty() { - raw_name - } else { - format!("{prefix}/{raw_name}") + // Tree paths are repo-relative and must survive identity intact: + // a non-UTF-8 path fails closed instead of lossily collapsing + // distinct names (#3545 family). + let text = std::str::from_utf8(entry).map_err(|_utf8_error| { + failed( + "candidate tree entry is not valid UTF-8; refusing lossy materialization" + .to_string(), + ) + })?; + let Some((meta, path)) = text.split_once('\t') else { + return Err(failed(format!( + "malformed ls-tree entry without a TAB separator: {text}" + ))); }; - let size = tar_octal(&header[124..136]); - let typeflag = header[156]; - let data_start = offset + 512; - let data_end = data_start + size; - if data_end > bytes.len() { - return Err(failed("truncated candidate archive".to_string())); + let mut meta_parts = meta.split_whitespace(); + let mode = meta_parts.next().unwrap_or_default(); + let kind = meta_parts.next().unwrap_or_default(); + let object = meta_parts.next().unwrap_or_default(); + if kind != "blob" || !(mode == "100644" || mode == "100755") { + return Err(failed(format!( + "unsupported tree entry mode `{mode}` (`{kind}`) for `{path}`: the candidate tree contains a non-file object ripr cannot faithfully materialize" + ))); } - offset = data_start + size.div_ceil(512) * 512; - if name.is_empty() { - continue; + let destination = safe_join(&target, path)?; + if let Some(parent) = destination.parent() { + std::fs::create_dir_all(parent) + .map_err(|error| failed(format!("materialization mkdir failed: {error}")))?; } - match typeflag { - // pax extended header: its records apply to the NEXT entry. - // `git archive` emits these for paths longer than the - // 100-character tar name field; the `path=` record carries - // the real name (#3296 review: rejecting them failed the - // whole run on any long path). - b'g' => continue, - b'x' => { - let data = &bytes[data_start..data_end]; - pending_path = pax_path_record(data); - continue; - } - b'0' | 0 => { - let effective_name = pending_path.take().unwrap_or(name); - let path = safe_join(target, &effective_name)?; - if let Some(parent) = path.parent() { - std::fs::create_dir_all(parent).map_err(|error| { - failed(format!("materialization mkdir failed: {error}")) - })?; - } - std::fs::write(&path, &bytes[data_start..data_end]) - .map_err(|error| failed(format!("materialization write failed: {error}")))?; - entries += 1; - } - b'5' => { - let path = safe_join(target, &name)?; - std::fs::create_dir_all(&path) - .map_err(|error| failed(format!("materialization mkdir failed: {error}")))?; - } - other => { - return Err(failed(format!( - "unsupported archive entry type `{}` for `{name}`: the candidate tree contains a non-file object ripr cannot faithfully materialize", - other as char - ))); - } + let blob = crate::git::run_git_output_with_deadline_and_limit( + root, + &["cat-file", "blob", object], + deadline.unwrap_or(Duration::from_mins(1)), + MAX_ARCHIVE_BYTES, + ) + .map_err(|error| failed(format!("git cat-file blob failed: {error}")))?; + if !blob.status.success() { + return Err(failed(format!("git cat-file blob {object} failed"))); } + std::fs::write(&destination, &blob.stdout) + .map_err(|error| failed(format!("materialization write failed: {error}")))?; } - Ok(entries) + Ok((target.clone(), cleanup)) } -/// Join an archive entry name under the target, rejecting traversal. +/// Join a tree entry path under the target, rejecting traversal. fn safe_join(target: &Path, name: &str) -> Result { let relative = Path::new(name); if relative.is_absolute() || name.contains("..") || name.contains('\\') || name.starts_with('/') { return Err(failed(format!( - "candidate archive entry `{name}` escapes the materialization root" + "candidate tree entry `{name}` escapes the materialization root" ))); } Ok(target.join(relative)) } -fn tar_string(field: &[u8]) -> String { - let end = field - .iter() - .position(|byte| *byte == 0) - .unwrap_or(field.len()); - String::from_utf8_lossy(&field[..end]).to_string() -} - -fn tar_octal(field: &[u8]) -> usize { - let text = tar_string(field); - usize::from_str_radix(text.trim().trim_end_matches('\0').trim(), 8).unwrap_or(0) -} - /// Resolve and execute one subject: validate identities, derive the /// diff, and materialize the candidate root. pub(crate) fn resolve( @@ -449,20 +391,6 @@ pub(crate) fn resolve( }) } -/// The `path=` record of a pax extended header, if present. -fn pax_path_record(data: &[u8]) -> Option { - let text = String::from_utf8_lossy(data); - for line in text.lines() { - let rest = line.trim_start_matches(|ch: char| ch.is_ascii_digit()); - if let Some(value) = rest.strip_prefix(" path=") - && !value.trim().is_empty() - { - return Some(value.trim().to_string()); - } - } - None -} - /// The candidate tree's `ripr.toml` bytes, when the tree carries one /// (#3279 R4: a worktree ripr.toml must not configure a subject run). /// Read from the tree object alone; the worktree file is never opened. @@ -694,6 +622,65 @@ mod tests { Ok(()) } + // #3548 review: a `.gitattributes` `text eol=crlf` attribute must not + // convert materialized bytes. `git archive` honors the attribute even + // with core.autocrlf=false, so the blob-wise materialization is pinned + // against the `git show :` oracle bytes. + #[test] + fn materialization_ignores_attribute_driven_conversion() -> Result<(), String> { + let (guard, _base, candidate) = fixture_repo("attributes")?; + let run = |args: &[&str]| -> Result { + let out = crate::git::run_git_output_with_deadline(&guard.0, args, GIT_DEADLINE) + .map_err(|e| e.to_string())?; + if !out.status.success() { + return Err(format!( + "git {} failed: {}", + args[0], + String::from_utf8_lossy(&out.stderr) + )); + } + Ok(String::from_utf8_lossy(&out.stdout).trim().to_string()) + }; + std::fs::write( + guard.0.join(".gitattributes"), + "*.rs text eol=crlf +", + ) + .map_err(|e| e.to_string())?; + run(&["add", ".gitattributes"])?; + run(&["commit", "-m", "attributes"])?; + // A fresh commit AFTER the attribute exists: the candidate tree's + // blob is pure LF, but archive would deliver CRLF. + std::fs::write( + guard.0.join("src/lib.rs"), + "pub fn attr() -> u8 { 3 } +", + ) + .map_err(|e| e.to_string())?; + run(&["add", "."])?; + run(&["commit", "-m", "candidate under attribute"])?; + let candidate = run(&["rev-parse", "HEAD"])?; + let base = run(&["rev-parse", "HEAD~1"])?; + let s = subject( + &guard.0, + GitCandidateBase::Treeish(GitTreeish::new(&base).map_err(|e| e.to_string())?), + &candidate, + )?; + let resolved = resolve(&s, None).map_err(|e| e.to_string())?; + let materialized = + std::fs::read(resolved.root.join("src/lib.rs")).map_err(|e| e.to_string())?; + let oracle = candidate_blob(&guard.0, &candidate, "src/lib.rs")?; + assert_eq!( + materialized, oracle, + "materialized bytes must equal the bound blob bytes under eol=crlf" + ); + assert!( + !materialized.contains(&b'\r'), + "an LF blob must stay LF under an eol=crlf attribute" + ); + Ok(()) + } + // #3296 review blocker 1: worktree and repo modes fail closed on a // bound subject instead of silently analyzing the live tree. #[test] From 6362d132b173afd066d568f9f2549cd05817e000 Mon Sep 17 00:00:00 2001 From: "Steven Zimmerman, CPA" <15812269+EffortlessSteven@users.noreply.github.com> Date: Sat, 29 Aug 2026 04:35:48 -0400 Subject: [PATCH 3/4] fix(analysis): drop the unused pre-attribute head binding (#3548 CI) --- crates/ripr/src/analysis/git_candidate_execution.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ripr/src/analysis/git_candidate_execution.rs b/crates/ripr/src/analysis/git_candidate_execution.rs index 1cbfa9a0595..e9094a0c54f 100644 --- a/crates/ripr/src/analysis/git_candidate_execution.rs +++ b/crates/ripr/src/analysis/git_candidate_execution.rs @@ -628,7 +628,7 @@ mod tests { // against the `git show :` oracle bytes. #[test] fn materialization_ignores_attribute_driven_conversion() -> Result<(), String> { - let (guard, _base, candidate) = fixture_repo("attributes")?; + let (guard, _base, _pre_attribute_head) = fixture_repo("attributes")?; let run = |args: &[&str]| -> Result { let out = crate::git::run_git_output_with_deadline(&guard.0, args, GIT_DEADLINE) .map_err(|e| e.to_string())?; From 314229fb1806fecc26163b354d7aa3b7d5b974f8 Mon Sep 17 00:00:00 2001 From: "Steven Zimmerman, CPA" <15812269+EffortlessSteven@users.noreply.github.com> Date: Sat, 29 Aug 2026 08:08:29 -0400 Subject: [PATCH 4/4] test(falsifier): update type-change needles to the blob-wise rejection message (#3548 review) The blob-wise materialization rejects a symlink at tree enumeration (unsupported tree entry mode 120000, naming src/linked.rs) before any byte is written, so the fail-closed property is structural; the falsifier tests now assert the new typed message while keeping the entry-naming and cleanup assertions. --- .../ripr/tests/immutable_candidate_falsifier.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/crates/ripr/tests/immutable_candidate_falsifier.rs b/crates/ripr/tests/immutable_candidate_falsifier.rs index 5955cff24e0..3f5f2ec9a98 100644 --- a/crates/ripr/tests/immutable_candidate_falsifier.rs +++ b/crates/ripr/tests/immutable_candidate_falsifier.rs @@ -660,7 +660,7 @@ fn type_change_fails_closed_naming_the_entry() -> Result<(), String> { "failure must stay inside the subject boundary: {error}" ); assert!( - error.contains("unsupported archive entry type") && error.contains("src/linked.rs"), + error.contains("unsupported tree entry mode `120000`") && error.contains("src/linked.rs"), "failure must name the exact rejected entry, not a generic error: {error}" ); Ok(()) @@ -719,14 +719,15 @@ fn fail_closed_subject_leaves_no_materialized_state() -> Result<(), String> { .err() .ok_or_else(|| "a type-changed entry must fail closed".to_string())?; - // The rejection must come from `untar` naming the entry it could not - // materialize. That is what proves extraction was already under way — - // and therefore that there was a materialization root to leak — rather - // than the run failing earlier, at identity resolution or `git archive`, - // where cleanup would be trivially satisfied. + // The rejection must name the entry it could not materialize. With the + // blob-wise materialization (#3548 review) the symlink is rejected at + // tree enumeration, before any byte is written — so the fail-closed + // property is structural (there is no partially extracted state to + // leak) and the cleanup assertion below guards the temp-root guard + // itself. assert!( - error.contains("unsupported archive entry type") && error.contains("src/linked.rs"), - "fail-closed run must reject the extracted entry by name: {error}" + error.contains("unsupported tree entry mode `120000`") && error.contains("src/linked.rs"), + "fail-closed run must reject the symlink entry by name: {error}" ); assert_materialization_root_cleaned(&parent, "fail-closed run") }