Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
82ed4afd10 |
@@ -234,22 +234,13 @@ pub async fn semantic_stamp_findings(
|
||||
let Some(region) = fetch_region(repo_path, &file, line) else {
|
||||
continue;
|
||||
};
|
||||
// Retrieve on the finding's intent + the code, not the region alone: two
|
||||
// findings in one file share overlapping windows and otherwise embed alike,
|
||||
// collapsing onto the same controls. The finding's title/description carry
|
||||
// the discriminating signal (e.g. "brute-force protection" vs "weak hash").
|
||||
// The raw `region` still goes to the judge for snippet grounding.
|
||||
let query = format!(
|
||||
"{}\n{}\n\n{}",
|
||||
finding.title, finding.description, region.content
|
||||
);
|
||||
let query_emb = match llm.embed(vec![query]).await {
|
||||
let region_emb = match llm.embed(vec![region.content.clone()]).await {
|
||||
Ok(mut embs) => match embs.pop() {
|
||||
Some(v) => v,
|
||||
None => continue,
|
||||
},
|
||||
Err(e) => {
|
||||
tracing::warn!(error = %e, "query embed failed; skipping finding");
|
||||
tracing::warn!(error = %e, "region embed failed; skipping finding");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
@@ -257,7 +248,7 @@ pub async fn semantic_stamp_findings(
|
||||
.check(
|
||||
&index,
|
||||
®ion,
|
||||
&query_emb,
|
||||
®ion_emb,
|
||||
SEMANTIC_TOP_K,
|
||||
&finding.repo_id,
|
||||
)
|
||||
|
||||
@@ -22,20 +22,18 @@ impl<J: ControlJudge> SemanticControlChecker<J> {
|
||||
Self { judge }
|
||||
}
|
||||
|
||||
/// Map a code region to the controls it violates. `query_embedding` is the
|
||||
/// caller-supplied retrieval embedding — typically the finding's intent
|
||||
/// (title/description) plus the region, so retrieval keys on what the finding
|
||||
/// is *about*, not just the ambient code. The top-`k` nearest controls in
|
||||
/// `index` are then judged against the raw `region` and grounded.
|
||||
/// Map a code region to the controls it violates. `region_embedding` is the
|
||||
/// region's embedding (the caller computes it via the LLM); the top-`k`
|
||||
/// nearest controls in `index` are judged and grounded.
|
||||
pub async fn check(
|
||||
&self,
|
||||
index: &ControlIndex,
|
||||
region: &CandidateRegion,
|
||||
query_embedding: &[f64],
|
||||
region_embedding: &[f64],
|
||||
k: usize,
|
||||
repo_id: &str,
|
||||
) -> Vec<Finding> {
|
||||
let candidates = index.nearest(query_embedding, k);
|
||||
let candidates = index.nearest(region_embedding, k);
|
||||
let mut findings = Vec::new();
|
||||
for spec in &candidates {
|
||||
let verdict = self.judge.judge(spec, region).await;
|
||||
|
||||
@@ -47,17 +47,6 @@ pub const SURFACES: &[Surface] = &[
|
||||
"ratelimit",
|
||||
],
|
||||
},
|
||||
Surface {
|
||||
control_id: "cra-ai-12", // Rollenbasierte Autorisierung (RBAC)
|
||||
terms: &[
|
||||
"authorize",
|
||||
"permission",
|
||||
"role",
|
||||
"rbac",
|
||||
"require_role",
|
||||
"has_role",
|
||||
],
|
||||
},
|
||||
Surface {
|
||||
control_id: "cra-ai-24", // Security-Logging
|
||||
terms: &["login", "authorize", "permission", "role", "admin", "audit"],
|
||||
@@ -207,11 +196,10 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn surfaces_cover_the_absence_based_controls() {
|
||||
assert_eq!(SURFACES.len(), 8);
|
||||
assert_eq!(SURFACES.len(), 7);
|
||||
for id in [
|
||||
"cra-ai-6",
|
||||
"cra-ai-11",
|
||||
"cra-ai-12",
|
||||
"cra-ai-24",
|
||||
"cra-ai-27",
|
||||
"cra-ai-28",
|
||||
|
||||
@@ -22,11 +22,6 @@ struct EmbeddingData {
|
||||
index: usize,
|
||||
}
|
||||
|
||||
/// Max inputs per embedding request. The bge/OpenAI-like backends cap the input
|
||||
/// array (bge-multilingual-gemma2 rejects >25 with "batch size overflow"), so we
|
||||
/// chunk larger corpora — a whole control catalog (~1.8k) would otherwise 500.
|
||||
const EMBED_BATCH_SIZE: usize = 16;
|
||||
|
||||
// ── Embedding implementation ───────────────────────────────────
|
||||
|
||||
impl LlmClient {
|
||||
@@ -34,21 +29,8 @@ impl LlmClient {
|
||||
&self.embed_model
|
||||
}
|
||||
|
||||
/// Generate embeddings for a batch of texts, chunking into backend-sized
|
||||
/// requests and preserving input order across chunks.
|
||||
/// Generate embeddings for a batch of texts
|
||||
pub async fn embed(&self, texts: Vec<String>) -> Result<Vec<Vec<f64>>, AgentError> {
|
||||
if texts.is_empty() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
let mut out = Vec::with_capacity(texts.len());
|
||||
for chunk in texts.chunks(EMBED_BATCH_SIZE) {
|
||||
out.extend(self.embed_batch(chunk.to_vec()).await?);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Embed one backend-sized batch (≤ [`EMBED_BATCH_SIZE`]) in a single request.
|
||||
async fn embed_batch(&self, texts: Vec<String>) -> Result<Vec<Vec<f64>>, AgentError> {
|
||||
let url = format!("{}/v1/embeddings", self.base_url.trim_end_matches('/'));
|
||||
|
||||
let request_body = EmbeddingRequest {
|
||||
@@ -90,33 +72,3 @@ impl LlmClient {
|
||||
Ok(data.into_iter().map(|d| d.embedding).collect())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use secrecy::SecretString;
|
||||
|
||||
fn client() -> LlmClient {
|
||||
LlmClient::new(
|
||||
"http://unused".into(),
|
||||
SecretString::from(String::new()),
|
||||
"m".into(),
|
||||
"e".into(),
|
||||
)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn empty_input_makes_no_request() {
|
||||
// Must short-circuit before any HTTP call (base_url is unroutable).
|
||||
let out = client().embed(Vec::new()).await.unwrap();
|
||||
assert!(out.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn batch_size_is_within_backend_cap() {
|
||||
assert!(
|
||||
EMBED_BATCH_SIZE <= 25,
|
||||
"must stay under the bge 25-input cap"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,145 +0,0 @@
|
||||
//! C5 live verification — the semantic master-controls path end to end against the
|
||||
//! deployed api-dev catalog. Ignored (hits api-dev + LiteLLM). Run explicitly:
|
||||
//!
|
||||
//! set -a; . ./.env; set +a
|
||||
//! BREAKPILOT_BASE_URL=https://api-dev.breakpilot.ai \
|
||||
//! cargo test -p compliance-agent --test c5_semantic_live -- --ignored --nocapture
|
||||
//!
|
||||
//! Pulls the live master-controls catalog, embeds the corpus (chunked), then for a
|
||||
//! couple of real vulnerable findings retrieves the nearest master controls and
|
||||
//! grounded-judges them, stamping master-control refs.
|
||||
|
||||
mod common;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use compliance_agent::llm::LlmClient;
|
||||
use compliance_core::config::BreakpilotConfig;
|
||||
use compliance_core::models::finding::{Finding, Severity};
|
||||
use compliance_core::models::scan::ScanType;
|
||||
use secrecy::SecretString;
|
||||
|
||||
fn env(k: &str) -> String {
|
||||
std::env::var(k).unwrap_or_else(|_| panic!("env {k} must be set for the live C5 test"))
|
||||
}
|
||||
|
||||
fn mk_finding(file: &str, line: u32, title: &str) -> Finding {
|
||||
let mut f = Finding::new(
|
||||
"repo-c5".into(),
|
||||
format!("{file}:{line}"),
|
||||
"semgrep".into(),
|
||||
ScanType::Sast,
|
||||
title.into(),
|
||||
title.into(),
|
||||
Severity::High,
|
||||
);
|
||||
f.file_path = Some(file.into());
|
||||
f.line_number = Some(line);
|
||||
f
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "live: requires deployed api-dev master-controls (fetch+parse only, no LLM)"]
|
||||
async fn c5_ingest_master_controls_catalog() {
|
||||
use compliance_agent::controls::OscalControlsProvider;
|
||||
|
||||
let provider = OscalControlsProvider::new(
|
||||
reqwest::Client::new(),
|
||||
env("BREAKPILOT_BASE_URL"),
|
||||
None,
|
||||
std::env::temp_dir().join("c5-ingest-snap"),
|
||||
);
|
||||
let doc = provider
|
||||
.load_master_controls()
|
||||
.await
|
||||
.expect("pull + parse master-controls catalog");
|
||||
let controls = doc.to_controls();
|
||||
println!(
|
||||
"\n=== C5 ingest: {} master controls parsed ===",
|
||||
controls.len()
|
||||
);
|
||||
for c in controls.iter().take(4) {
|
||||
let text: String = c.text.chars().take(90).collect();
|
||||
println!(" {} | {} | {}", c.id, c.title, text);
|
||||
}
|
||||
assert!(
|
||||
!controls.is_empty(),
|
||||
"expected a non-empty master-control corpus"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "live: requires deployed api-dev master-controls + LiteLLM"]
|
||||
async fn c5_semantic_stamps_master_control_refs() {
|
||||
let llm = Arc::new(LlmClient::new(
|
||||
env("LITELLM_URL"),
|
||||
SecretString::from(env("LITELLM_API_KEY")),
|
||||
env("LITELLM_MODEL"),
|
||||
env("LITELLM_EMBED_MODEL"),
|
||||
));
|
||||
|
||||
let mut config = common::dev_config("mongodb://unused".into(), "c5".into());
|
||||
let snapshot = std::env::temp_dir().join("c5-oscal-snap");
|
||||
config.breakpilot = BreakpilotConfig {
|
||||
base_url: Some(env("BREAKPILOT_BASE_URL")),
|
||||
token: None,
|
||||
snapshot_dir: snapshot.to_string_lossy().into_owned(),
|
||||
semantic_mapping: true,
|
||||
grounded_control_checks: false,
|
||||
};
|
||||
|
||||
// Fixture repo with recognizable code-checkable surfaces.
|
||||
let repo = std::env::temp_dir().join("c5-fixture-repo");
|
||||
let _ = std::fs::remove_dir_all(&repo);
|
||||
std::fs::create_dir_all(repo.join("app")).expect("mkdir");
|
||||
std::fs::write(
|
||||
repo.join("app/auth.py"),
|
||||
concat!(
|
||||
"import hashlib\n",
|
||||
"\n",
|
||||
"def store_password(user, password):\n",
|
||||
" # weak, unsalted password hashing\n",
|
||||
" digest = hashlib.md5(password.encode()).hexdigest()\n",
|
||||
" db.save(user, digest)\n",
|
||||
"\n",
|
||||
"@app.route('/login', methods=['POST'])\n",
|
||||
"def login():\n",
|
||||
" u = request.form['username']\n",
|
||||
" p = request.form['password']\n",
|
||||
" return 'ok' if check(u, p) else ('bad', 401)\n",
|
||||
),
|
||||
)
|
||||
.expect("write fixture");
|
||||
|
||||
let mut findings = vec![
|
||||
mk_finding("app/auth.py", 5, "Weak password hash (md5, unsalted)"),
|
||||
mk_finding(
|
||||
"app/auth.py",
|
||||
9,
|
||||
"Login endpoint without brute-force protection",
|
||||
),
|
||||
];
|
||||
|
||||
let tagged =
|
||||
compliance_agent::controls::semantic_stamp_findings(&config, llm, &repo, &mut findings)
|
||||
.await;
|
||||
|
||||
println!("\n=== C5 semantic master-controls stamping ===");
|
||||
for f in &findings {
|
||||
println!(
|
||||
" {:50} {}:{:?} -> {:?}",
|
||||
f.title,
|
||||
f.file_path.as_deref().unwrap_or(""),
|
||||
f.line_number,
|
||||
f.control_refs
|
||||
);
|
||||
}
|
||||
println!("findings that gained >=1 master-control ref: {tagged}");
|
||||
let _ = std::fs::remove_dir_all(&repo);
|
||||
|
||||
// Live corpus — assert only that the path runs and stamps at least one ref.
|
||||
assert!(
|
||||
tagged >= 1,
|
||||
"expected at least one finding to gain a master-control ref"
|
||||
);
|
||||
}
|
||||
@@ -25,29 +25,29 @@
|
||||
"control": "cra-ai-2",
|
||||
"title": "Minimale Angriffsflaeche",
|
||||
"scans": [],
|
||||
"note": "design property (minimal attack surface) — not derivable from local code patterns; architecture/threat-model review",
|
||||
"status": "not_code_checkable"
|
||||
"note": "code-checkable but no off-the-shelf tool digs it out — author a detector (custom semgrep rule / check)",
|
||||
"status": "needs_tooling"
|
||||
},
|
||||
{
|
||||
"control": "cra-ai-3",
|
||||
"title": "Sichere Systemarchitektur",
|
||||
"scans": [],
|
||||
"note": "design property (secure system architecture) — architecture review, not statically code-checkable",
|
||||
"status": "not_code_checkable"
|
||||
"note": "code-checkable but no off-the-shelf tool digs it out — author a detector (custom semgrep rule / check)",
|
||||
"status": "needs_tooling"
|
||||
},
|
||||
{
|
||||
"control": "cra-ai-4",
|
||||
"title": "Least-Privilege-Prinzip",
|
||||
"scans": [],
|
||||
"note": "design property (least-privilege) — deployment/IAM & architecture review, not a local code pattern",
|
||||
"status": "not_code_checkable"
|
||||
"note": "code-checkable but no off-the-shelf tool digs it out — author a detector (custom semgrep rule / check)",
|
||||
"status": "needs_tooling"
|
||||
},
|
||||
{
|
||||
"control": "cra-ai-5",
|
||||
"title": "Manipulationsschutz",
|
||||
"scans": [],
|
||||
"note": "design property (tamper protection) — hardware/runtime & operational control, not statically code-checkable",
|
||||
"status": "not_code_checkable"
|
||||
"note": "code-checkable but no off-the-shelf tool digs it out — author a detector (custom semgrep rule / check)",
|
||||
"status": "needs_tooling"
|
||||
},
|
||||
{
|
||||
"control": "cra-ai-6",
|
||||
@@ -146,7 +146,7 @@
|
||||
"control": "cra-ai-12",
|
||||
"title": "Rollenbasierte Autorisierung",
|
||||
"scans": [],
|
||||
"note": "absence-based — no syntactic pattern; covered by the grounded surface check (retrieve surface + LLM judge), gated (BREAKPILOT_GROUNDED_CHECKS) pending live tuning",
|
||||
"note": "code-checkable but no off-the-shelf tool digs it out — author a detector (custom semgrep rule / check)",
|
||||
"status": "needs_tooling"
|
||||
},
|
||||
{
|
||||
|
||||
@@ -214,27 +214,6 @@ mod tests {
|
||||
assert!(hits.iter().any(|c| c.control == "cra-ai-1"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn coverage_reflects_the_b_track_split() {
|
||||
let s = ControlMap::cra().unwrap().summary();
|
||||
// 9 already tool-covered + B1's 4 custom-semgrep controls.
|
||||
assert_eq!(s.covered, 13);
|
||||
// The 8 grounded surface controls stay needs_tooling until live-tuned.
|
||||
assert_eq!(s.needs_tooling, 8);
|
||||
// B3 marked the 4 pure-architectural controls not code-checkable.
|
||||
assert_eq!(s.not_code_checkable, 19);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn architectural_controls_are_not_code_checkable() {
|
||||
let map = ControlMap::cra().unwrap();
|
||||
for id in ["cra-ai-2", "cra-ai-3", "cra-ai-4", "cra-ai-5"] {
|
||||
let c = map.coverage(id).unwrap();
|
||||
assert_eq!(c.status, Coverage::NotCodeCheckable, "{id}");
|
||||
assert!(c.scans.is_empty(), "{id} should carry no scan bindings");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn custom_rule_controls_do_not_bind_by_broad_cwe() {
|
||||
let map = ControlMap::cra().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user