From 75e0a484f47aaa2bbca30526ccf954e717610db8 Mon Sep 17 00:00:00 2001 From: "Steven Zimmerman, CPA" <15812269+EffortlessSteven@users.noreply.github.com> Date: Mon, 21 Sep 2026 23:53:27 -0500 Subject: [PATCH 1/4] fix(cache): omit host paths from cache write errors (#3845) --- crates/ripr/src/atomic_file.rs | 170 +++++++++++++++++++++++++++++---- 1 file changed, 154 insertions(+), 16 deletions(-) diff --git a/crates/ripr/src/atomic_file.rs b/crates/ripr/src/atomic_file.rs index 4a0643fb4fb..244cca3576d 100644 --- a/crates/ripr/src/atomic_file.rs +++ b/crates/ripr/src/atomic_file.rs @@ -5,19 +5,38 @@ use std::path::Path; static TEMP_FILE_SEQUENCE: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); +#[derive(Clone, Copy)] +enum ErrorPathPolicy { + Include, + Omit, +} + +impl ErrorPathPolicy { + fn suffix(self, path: &Path) -> String { + match self { + Self::Include => format!(" {}", path.display()), + Self::Omit => String::new(), + } + } +} + /// Write bytes through a same-directory temporary file, flush them to disk, /// then atomically replace the destination. The short temporary name keeps /// the operation usable when the destination filename is near a platform's /// maximum filename length. pub(crate) fn write(path: &Path, bytes: &[u8], label: &str) -> Result<(), String> { - write_with_sync(path, bytes, label, true) + write_with_sync(path, bytes, label, true, ErrorPathPolicy::Include) } /// Atomically replace an expendable cache entry without forcing a physical /// flush for every entry. Rename still prevents readers from observing a /// partial file; the cache may be recomputed after a power loss. +/// +/// Cache errors deliberately omit directory, destination, and temporary-file +/// spellings so bounded diagnostics remain portable across checkout/cache +/// roots and safe to retain in public receipts. pub(crate) fn write_cache(path: &Path, bytes: &[u8], label: &str) -> Result<(), String> { - write_with_sync(path, bytes, label, false) + write_with_sync(path, bytes, label, false, ErrorPathPolicy::Omit) } fn write_with_sync( @@ -25,6 +44,7 @@ fn write_with_sync( bytes: &[u8], label: &str, sync_before_publish: bool, + error_path_policy: ErrorPathPolicy, ) -> Result<(), String> { let parent = path .parent() @@ -32,14 +52,14 @@ fn write_with_sync( let dir = parent.unwrap_or_else(|| Path::new(".")); std::fs::create_dir_all(dir).map_err(|err| { format!( - "failed to create {label} directory {}: {err}", - dir.display() + "failed to create {label} directory{}: {err}", + error_path_policy.suffix(dir) ) })?; if path.file_name().is_none() { return Err(format!( - "atomic write path {} has no file name", - path.display() + "atomic write path{} has no file name", + error_path_policy.suffix(path) )); } let nanos = std::time::SystemTime::now() @@ -54,36 +74,40 @@ fn write_with_sync( let result = (|| -> Result<(), String> { let mut file = std::fs::File::create(&tmp_path).map_err(|err| { format!( - "failed to create {label} temp file {}: {err}", - tmp_path.display() + "failed to create {label} temp file{}: {err}", + error_path_policy.suffix(&tmp_path) ) })?; file.write_all(bytes).map_err(|err| { format!( - "failed to write {label} temp file {}: {err}", - tmp_path.display() + "failed to write {label} temp file{}: {err}", + error_path_policy.suffix(&tmp_path) ) })?; if let Ok(metadata) = std::fs::metadata(path) { file.set_permissions(metadata.permissions()) .map_err(|err| { format!( - "failed to preserve {label} permissions for {}: {err}", - path.display() + "failed to preserve {label} permissions for{}: {err}", + error_path_policy.suffix(path) ) })?; } if sync_before_publish { file.sync_all().map_err(|err| { format!( - "failed to fsync {label} temp file {}: {err}", - tmp_path.display() + "failed to fsync {label} temp file{}: {err}", + error_path_policy.suffix(&tmp_path) ) })?; } drop(file); - std::fs::rename(&tmp_path, path) - .map_err(|err| format!("failed to finalize {label} {}: {err}", path.display()))?; + std::fs::rename(&tmp_path, path).map_err(|err| { + format!( + "failed to finalize {label}{}: {err}", + error_path_policy.suffix(path) + ) + })?; Ok(()) })(); if result.is_err() { @@ -91,3 +115,117 @@ fn write_with_sync( } result } + +#[cfg(test)] +mod tests { + use super::{write, write_cache, TEMP_FILE_SEQUENCE}; + use std::path::{Path, PathBuf}; + + fn isolated_dir(label: &str) -> PathBuf { + let sequence = TEMP_FILE_SEQUENCE.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + std::env::temp_dir().join(format!( + "ripr-atomic-file-{label}-{}-{sequence}", + std::process::id() + )) + } + + fn directory_failure(root: &Path) -> Result { + let _ = std::fs::remove_dir_all(root); + std::fs::create_dir_all(root) + .map_err(|err| format!("fixture directory setup failed: {err}"))?; + let blocker = root.join("blocker"); + std::fs::write(&blocker, b"not a directory") + .map_err(|err| format!("fixture blocker setup failed: {err}"))?; + let destination = blocker.join("entry.json"); + let error = write_cache(&destination, b"cache", "test cache") + .expect_err("a cache path below a regular file must fail"); + let _ = std::fs::remove_dir_all(root); + Ok(error) + } + + #[test] + fn cache_directory_error_omits_host_paths() -> Result<(), String> { + let root = isolated_dir("cache-directory-path-sentinel"); + let error = directory_failure(&root)?; + assert!( + error.starts_with("failed to create test cache directory:"), + "unexpected error: {error}" + ); + assert!( + !error.contains(root.to_string_lossy().as_ref()), + "cache error leaked root {}: {error}", + root.display() + ); + assert!( + !error.contains("blocker"), + "cache error leaked path: {error}" + ); + assert!( + !error.contains("entry.json"), + "cache error leaked path: {error}" + ); + Ok(()) + } + + #[test] + fn cache_finalize_error_omits_host_paths() -> Result<(), String> { + let root = isolated_dir("cache-finalize-path-sentinel"); + let _ = std::fs::remove_dir_all(&root); + std::fs::create_dir_all(&root) + .map_err(|err| format!("fixture directory setup failed: {err}"))?; + let destination = root.join("existing-directory"); + std::fs::create_dir(&destination) + .map_err(|err| format!("fixture destination setup failed: {err}"))?; + let error = write_cache(&destination, b"cache", "test cache") + .expect_err("publishing a file over a directory must fail"); + assert!( + error.starts_with("failed to finalize test cache:"), + "unexpected error: {error}" + ); + assert!( + !error.contains(root.to_string_lossy().as_ref()), + "cache error leaked root {}: {error}", + root.display() + ); + assert!( + !error.contains("existing-directory"), + "cache error leaked destination: {error}" + ); + let _ = std::fs::remove_dir_all(&root); + Ok(()) + } + + #[test] + fn durable_write_finalize_error_retains_path_context() -> Result<(), String> { + let root = isolated_dir("durable-finalize-path-sentinel"); + let _ = std::fs::remove_dir_all(&root); + std::fs::create_dir_all(&root) + .map_err(|err| format!("fixture directory setup failed: {err}"))?; + let destination = root.join("existing-directory"); + std::fs::create_dir(&destination) + .map_err(|err| format!("fixture destination setup failed: {err}"))?; + let error = write(&destination, b"artifact", "test artifact") + .expect_err("publishing a file over a directory must fail"); + assert!( + error.starts_with("failed to finalize test artifact "), + "unexpected error: {error}" + ); + assert!( + error.contains(destination.to_string_lossy().as_ref()), + "durable-write error lost destination {}: {error}", + destination.display() + ); + let _ = std::fs::remove_dir_all(&root); + Ok(()) + } + + #[test] + fn equivalent_roots_produce_equivalent_cache_directory_errors() -> Result<(), String> { + let first = isolated_dir("equivalent-root-a"); + let second = isolated_dir("equivalent-root-b"); + let first_error = directory_failure(&first)?; + let second_error = directory_failure(&second)?; + assert_eq!(first_error, second_error); + Ok(()) + } +} From 98126b2aad97bf50e9eb747aa1127a31f15709ca Mon Sep 17 00:00:00 2001 From: "Steven Zimmerman, CPA" <15812269+EffortlessSteven@users.noreply.github.com> Date: Mon, 21 Sep 2026 23:55:29 -0500 Subject: [PATCH 2/4] style(cache): satisfy rustfmt import ordering (#3845) --- crates/ripr/src/atomic_file.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ripr/src/atomic_file.rs b/crates/ripr/src/atomic_file.rs index 244cca3576d..1dba3e02809 100644 --- a/crates/ripr/src/atomic_file.rs +++ b/crates/ripr/src/atomic_file.rs @@ -118,7 +118,7 @@ fn write_with_sync( #[cfg(test)] mod tests { - use super::{write, write_cache, TEMP_FILE_SEQUENCE}; + use super::{TEMP_FILE_SEQUENCE, write, write_cache}; use std::path::{Path, PathBuf}; fn isolated_dir(label: &str) -> PathBuf { From 2c0c15b1a5c514c790df239a82983f806567f125 Mon Sep 17 00:00:00 2001 From: "Steven Zimmerman, CPA" <15812269+EffortlessSteven@users.noreply.github.com> Date: Mon, 21 Sep 2026 23:59:25 -0500 Subject: [PATCH 3/4] test(cache): avoid denied expect_err in path privacy controls --- crates/ripr/src/atomic_file.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/crates/ripr/src/atomic_file.rs b/crates/ripr/src/atomic_file.rs index 1dba3e02809..42c3ee2a90b 100644 --- a/crates/ripr/src/atomic_file.rs +++ b/crates/ripr/src/atomic_file.rs @@ -137,8 +137,9 @@ mod tests { std::fs::write(&blocker, b"not a directory") .map_err(|err| format!("fixture blocker setup failed: {err}"))?; let destination = blocker.join("entry.json"); - let error = write_cache(&destination, b"cache", "test cache") - .expect_err("a cache path below a regular file must fail"); + let Err(error) = write_cache(&destination, b"cache", "test cache") else { + return Err("a cache path below a regular file unexpectedly succeeded".to_string()); + }; let _ = std::fs::remove_dir_all(root); Ok(error) } @@ -176,8 +177,9 @@ mod tests { let destination = root.join("existing-directory"); std::fs::create_dir(&destination) .map_err(|err| format!("fixture destination setup failed: {err}"))?; - let error = write_cache(&destination, b"cache", "test cache") - .expect_err("publishing a file over a directory must fail"); + let Err(error) = write_cache(&destination, b"cache", "test cache") else { + return Err("publishing a cache file over a directory unexpectedly succeeded".to_string()); + }; assert!( error.starts_with("failed to finalize test cache:"), "unexpected error: {error}" @@ -204,8 +206,9 @@ mod tests { let destination = root.join("existing-directory"); std::fs::create_dir(&destination) .map_err(|err| format!("fixture destination setup failed: {err}"))?; - let error = write(&destination, b"artifact", "test artifact") - .expect_err("publishing a file over a directory must fail"); + let Err(error) = write(&destination, b"artifact", "test artifact") else { + return Err("publishing an artifact file over a directory unexpectedly succeeded".to_string()); + }; assert!( error.starts_with("failed to finalize test artifact "), "unexpected error: {error}" From fb2e4f80681cb52cdd883f1c2690869acb1a29d8 Mon Sep 17 00:00:00 2001 From: "Steven Zimmerman, CPA" <15812269+EffortlessSteven@users.noreply.github.com> Date: Mon, 21 Sep 2026 23:59:54 -0500 Subject: [PATCH 4/4] style(cache): keep privacy fixtures rustfmt-stable --- crates/ripr/src/atomic_file.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/ripr/src/atomic_file.rs b/crates/ripr/src/atomic_file.rs index 42c3ee2a90b..c5cb6e8d19a 100644 --- a/crates/ripr/src/atomic_file.rs +++ b/crates/ripr/src/atomic_file.rs @@ -178,7 +178,9 @@ mod tests { std::fs::create_dir(&destination) .map_err(|err| format!("fixture destination setup failed: {err}"))?; let Err(error) = write_cache(&destination, b"cache", "test cache") else { - return Err("publishing a cache file over a directory unexpectedly succeeded".to_string()); + return Err( + "publishing a cache file over a directory unexpectedly succeeded".to_string(), + ); }; assert!( error.starts_with("failed to finalize test cache:"), @@ -207,7 +209,9 @@ mod tests { std::fs::create_dir(&destination) .map_err(|err| format!("fixture destination setup failed: {err}"))?; let Err(error) = write(&destination, b"artifact", "test artifact") else { - return Err("publishing an artifact file over a directory unexpectedly succeeded".to_string()); + return Err( + "publishing an artifact file over a directory unexpectedly succeeded".to_string(), + ); }; assert!( error.starts_with("failed to finalize test artifact "),