fix(sast): concurrent scans no longer fail semgrep while staging the rules (#448)
CI / Check (push) Skipped
CI / Detect Changes (push) Successful in 3s
CI / Deploy Dashboard (push) Skipped
CI / Deploy Docs (push) Skipped
CI / Deploy MCP (push) Skipped
CI / Deploy Agent (push) Successful in 8m13s

This commit was merged in pull request #448.
This commit is contained in:
sharang committed 2026-10-05 13:48:56 +00:00
1 parent 04d0ad5113
commit 3790b9c6cc
1 file changed
+16 -2
+16 -2
View File
@@ -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<PathBuf, CoreError> {
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<String> {
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<String, Vec<String>> {