diff --git a/compliance-agent/src/pipeline/semgrep.rs b/compliance-agent/src/pipeline/semgrep.rs index f69943e..11d6365 100644 --- a/compliance-agent/src/pipeline/semgrep.rs +++ b/compliance-agent/src/pipeline/semgrep.rs @@ -19,7 +19,9 @@ const REDACTED_LINES: &str = "requires login"; const CRA_RULES: &str = include_str!("../../rules/cra_semgrep.yaml"); /// Write the bundled CRA rules to a stable temp path (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 /// @@ -28,7 +30,10 @@ const CRA_RULES: &str = include_str!("../../rules/cra_semgrep.yaml"); async fn stage_cra_rules() -> Result { let dir = std::env::temp_dir(); let path = dir.join("compliance-cra-semgrep.yaml"); - let tmp = dir.join(format!("compliance-cra-semgrep.{}.tmp", std::process::id())); + let tmp = dir.join(format!( + "compliance-cra-semgrep.{}.tmp", + uuid::Uuid::new_v4() + )); let staged = async { tokio::fs::write(&tmp, CRA_RULES).await?; tokio::fs::rename(&tmp, &path).await @@ -209,6 +214,15 @@ fn extract_cwe(metadata: &serde_json::Value) -> Option { mod tests { use super::*; + #[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 path = task.await.unwrap().expect("staged"); + assert_eq!(std::fs::read_to_string(path).unwrap(), CRA_RULES); + } + } + /// The `languages:` of each rule in our pack, by id. The pack is flat /// (`- id:` then `languages: [..]`), so lines are enough. fn rule_languages() -> std::collections::HashMap> {