feat(controls): the grounded surface check reads C and C++ (#376) (#394)
CI / Check (push) Skipped
CI / Detect Changes (push) Successful in 4s
CI / Deploy Dashboard (push) Skipped
CI / Deploy Docs (push) Skipped
CI / Deploy MCP (push) Skipped
CI / Deploy Agent (push) Canceled after 0s

This commit was merged in pull request #394.
This commit is contained in:
sharang committed 2026-09-25 11:09:01 +00:00
1 parent 827b7196b5
commit a3e0618283
3 files changed
+114 -31

No files matched your search

+6 -1
View File
@@ -146,13 +146,18 @@ pub async fn grounded_surface_findings(
return Vec::new();
}
let checker = GroundedControlChecker::new(LlmControlJudge::new(llm));
// The vendor SDK is not the customer's code: its surfaces are left out.
let tree = repo_path.to_path_buf();
let vendor = tokio::task::spawn_blocking(move || crate::pipeline::vendor::vendor_dirs(&tree))
.await
.unwrap_or_default();
let mut out = Vec::new();
for surf in surface::SURFACES {
let Some(spec) = specs.get(surf.control_id) else {
continue; // catalog doesn't carry this control
};
let regions = surface::retrieve(repo_path, surf.terms);
let regions = surface::retrieve(repo_path, surf.terms, &vendor);
if regions.is_empty() {
continue;
}
+84 -22
View File
@@ -9,7 +9,9 @@
//! finding grounds to the surface snippet, so nothing fabricated survives.
//!
//! Retrieval is intentionally cheap and bounded: keyword match + a fixed window,
//! capped per control to keep the downstream LLM cost predictable.
//! capped per control to keep the downstream LLM cost predictable. C and C++
//! firmware is read too, without its vendor SDK (HAL, CMSIS, middleware), which
//! would otherwise fill the cap with code the customer does not own.
use std::path::Path;
@@ -34,6 +36,7 @@ pub const SURFACES: &[Surface] = &[
"hmac",
"integrity",
"verify",
"crc32",
],
},
Surface {
@@ -60,29 +63,61 @@ pub const SURFACES: &[Surface] = &[
},
Surface {
control_id: "cra-ai-24", // Security-Logging
terms: &["login", "authorize", "permission", "role", "admin", "audit"],
terms: &[
"login",
"authorize",
"permission",
"role",
"admin",
"audit",
"syslog",
],
},
Surface {
control_id: "cra-ai-27", // Log-Integrität und -Aufbewahrung
terms: &["logging", "logger", "getlogger", "audit_log"],
terms: &["logging", "logger", "getlogger", "audit_log", "syslog"],
},
Surface {
control_id: "cra-ai-28", // Sichere Update-Mechanismen
terms: &["update", "upgrade", "download", "firmware"],
terms: &[
"update",
"upgrade",
"download",
"firmware",
"bootloader",
"ota_",
"hal_flash",
],
},
Surface {
control_id: "cra-ai-29", // Update-Authentizität
terms: &["update", "signature", "verify", "pubkey", "certificate"],
terms: &[
"update",
"signature",
"verify",
"pubkey",
"certificate",
"ecdsa",
"secure_boot",
],
},
Surface {
control_id: "cra-ai-30", // Update-Integrität
terms: &["update", "checksum", "digest", "integrity", "verify"],
terms: &[
"update",
"checksum",
"digest",
"integrity",
"verify",
"crc32",
],
},
];
/// Source file extensions worth reading (skip binaries/assets/lockfiles).
const CODE_EXTS: &[&str] = &[
"py", "js", "ts", "tsx", "jsx", "go", "java", "rb", "php", "rs", "cs", "kt",
"py", "js", "ts", "tsx", "jsx", "go", "java", "rb", "php", "rs", "cs", "kt", "c", "h", "cc",
"cpp", "hpp",
];
/// Directories never worth walking.
const SKIP_DIRS: &[&str] = &[
@@ -103,12 +138,13 @@ const MAX_REGIONS_PER_CONTROL: usize = 8;
const MAX_FILE_BYTES: u64 = 512 * 1024;
/// Deterministically retrieve up to [`MAX_REGIONS_PER_CONTROL`] code regions in
/// `repo_path` whose lines mention any of `terms`. Hits close together within a
/// file are merged into one region; results are capped to bound LLM cost.
pub fn retrieve(repo_path: &Path, terms: &[&str]) -> Vec<CandidateRegion> {
/// `repo_path` whose lines mention any of `terms`, outside the `excluded`
/// (vendor) directories. Hits close together within a file are merged into one
/// region; results are capped to bound LLM cost.
pub fn retrieve(repo_path: &Path, terms: &[&str], excluded: &[String]) -> Vec<CandidateRegion> {
let lowered: Vec<String> = terms.iter().map(|t| t.to_lowercase()).collect();
let mut regions = Vec::new();
for entry in walk(repo_path) {
for entry in walk(repo_path, excluded) {
if regions.len() >= MAX_REGIONS_PER_CONTROL {
break;
}
@@ -173,15 +209,17 @@ fn has_code_ext(path: &Path) -> bool {
.is_some_and(|e| CODE_EXTS.contains(&e))
}
fn walk(root: &Path) -> Vec<walkdir::DirEntry> {
walkdir::WalkDir::new(root)
.into_iter()
.filter_entry(|e| {
let name = e.file_name().to_string_lossy();
!SKIP_DIRS.contains(&name.as_ref())
})
.filter_map(|e| e.ok())
.collect()
/// Every entry under `root` outside the `excluded` directories and
/// [`SKIP_DIRS`].
fn walk<'a>(
root: &'a Path,
excluded: &'a [String],
) -> impl Iterator<Item = walkdir::DirEntry> + 'a {
crate::pipeline::vendor::walk_outside(root, excluded).filter(|e| {
!e.path()
.components()
.any(|c| SKIP_DIRS.contains(&c.as_os_str().to_string_lossy().as_ref()))
})
}
#[cfg(test)]
@@ -231,7 +269,7 @@ mod tests {
"app/auth.py",
"import x\n\n\n\n\n\n\ndef login(u, p):\n return check(u, p)\n",
);
let regions = retrieve(&dir, terms_for("cra-ai-11"));
let regions = retrieve(&dir, terms_for("cra-ai-11"), &[]);
assert_eq!(regions.len(), 1);
assert!(regions[0].content.contains("def login"));
assert_eq!(regions[0].file, "app/auth.py");
@@ -244,7 +282,31 @@ mod tests {
std::fs::create_dir_all(&dir).unwrap();
write(&dir, "README.md", "login and password and audit\n"); // not code ext
write(&dir, "node_modules/pkg/index.js", "function login() {}\n"); // vendored
assert!(retrieve(&dir, terms_for("cra-ai-11")).is_empty());
assert!(retrieve(&dir, terms_for("cra-ai-11"), &[]).is_empty());
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn firmware_update_code_is_read_but_not_the_vendor_hal() {
let dir = std::env::temp_dir().join(format!("surface-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&dir).unwrap();
write(
&dir,
"Core/Src/ota.c",
"#include \"ota.h\"\n\n\n\n\n\n\nint ota_apply(const uint8_t *image) {\n return HAL_FLASH_Program(0, 0, 0);\n}\n",
);
write(
&dir,
"Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c",
"HAL_StatusTypeDef HAL_FLASH_Program(void) { return 0; }\n",
);
write(&dir, "Core/Inc/main.h", "#pragma once\n");
let vendor = crate::pipeline::vendor::vendor_dirs(&dir);
let regions = retrieve(&dir, terms_for("cra-ai-28"), &vendor);
assert_eq!(regions.len(), 1, "{regions:?}");
assert_eq!(regions[0].file, "Core/Src/ota.c");
let _ = std::fs::remove_dir_all(&dir);
}
+24 -8
View File
@@ -71,7 +71,9 @@
"php",
"rust",
"csharp",
"kotlin"
"kotlin",
"c",
"cpp"
],
"cwe": [],
"rules": []
@@ -212,7 +214,9 @@
"php",
"rust",
"csharp",
"kotlin"
"kotlin",
"c",
"cpp"
],
"cwe": [],
"rules": []
@@ -238,7 +242,9 @@
"php",
"rust",
"csharp",
"kotlin"
"kotlin",
"c",
"cpp"
],
"cwe": [],
"rules": []
@@ -515,7 +521,9 @@
"php",
"rust",
"csharp",
"kotlin"
"kotlin",
"c",
"cpp"
],
"cwe": [],
"rules": []
@@ -555,7 +563,9 @@
"php",
"rust",
"csharp",
"kotlin"
"kotlin",
"c",
"cpp"
],
"cwe": [],
"rules": []
@@ -581,7 +591,9 @@
"php",
"rust",
"csharp",
"kotlin"
"kotlin",
"c",
"cpp"
],
"cwe": [],
"rules": []
@@ -607,7 +619,9 @@
"php",
"rust",
"csharp",
"kotlin"
"kotlin",
"c",
"cpp"
],
"cwe": [],
"rules": []
@@ -633,7 +647,9 @@
"php",
"rust",
"csharp",
"kotlin"
"kotlin",
"c",
"cpp"
],
"cwe": [],
"rules": []