From 64ddaf37c17d038b90206e748439ccedd53d70b8 Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Date: Mon, 5 Oct 2026 15:48:36 +0200 Subject: [PATCH] fix(sast): stage every rule pack under a per-call unique tmp name Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB --- compliance-agent/src/pipeline/semgrep.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/compliance-agent/src/pipeline/semgrep.rs b/compliance-agent/src/pipeline/semgrep.rs index 770a4d8..b117389 100644 --- a/compliance-agent/src/pipeline/semgrep.rs +++ b/compliance-agent/src/pipeline/semgrep.rs @@ -33,7 +33,9 @@ const RULE_PACKS: &[(&str, &str)] = &[ ]; /// Write the bundled rule packs to stable temp paths (atomic: unique tmp + -/// rename). +/// rename). The tmp name is unique per call, not per process: scans run +/// concurrently in one agent, and a shared tmp file let one scan rename it +/// away while another was still writing it, failing that scan's SAST stage. /// /// # Errors /// @@ -45,7 +47,7 @@ async fn stage_cra_rules() -> Result, CoreError> { let mut staged_paths = Vec::with_capacity(RULE_PACKS.len()); for (name, rules) in RULE_PACKS { let path = dir.join(name); - let tmp = dir.join(format!("{name}.{}.tmp", std::process::id())); + let tmp = dir.join(format!("{name}.{}.tmp", uuid::Uuid::new_v4())); let staged = async { tokio::fs::write(&tmp, rules).await?; tokio::fs::rename(&tmp, &path).await @@ -284,6 +286,18 @@ mod tests { ); } + #[tokio::test(flavor = "multi_thread", worker_threads = 4)] + async fn concurrent_scans_all_get_the_rules() { + let tasks: Vec<_> = (0..32).map(|_| tokio::spawn(stage_cra_rules())).collect(); + for task in tasks { + let paths = task.await.unwrap().expect("staged"); + assert_eq!(paths.len(), RULE_PACKS.len()); + for (path, (_, rules)) in paths.iter().zip(RULE_PACKS) { + assert_eq!(std::fs::read_to_string(path).unwrap(), *rules); + } + } + } + #[test] fn every_rule_file_is_bundled() { // A pack added to rules/ but not to RULE_PACKS would never run.