fix(compliance): claim "no violation found" only where a rule checked the code (#368)
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Check (pull_request) Successful in 7m30s
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Check (pull_request) Successful in 7m30s
The CRA status called every covered control clean once a scan had completed, even when none of its rules covers the scanned code (a C repo and Python-only rules). The scan run now records the source languages it saw outside vendor code, each control-map binding declares its languages, and a control is "no violation found" only if a binding applies; otherwise it is not assessed, with the reason. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
This commit is contained in:
1 parent
5f7c947fb3
commit
dee24610f8
12 files changed
+631
-48
No files matched your search
@@ -18,12 +18,28 @@ pub enum ControlState {
|
||||
NotSatisfied,
|
||||
/// Checkable in code, scanned, and nothing is against it.
|
||||
NoViolationFound,
|
||||
/// Checkable in code, but no scan has completed yet, or no tool checks it.
|
||||
/// Checkable in code, but not by this scan: none completed yet, no tool
|
||||
/// checks it, or none of its rules covers the scanned code's languages.
|
||||
NotAssessed,
|
||||
/// A design property no code scan can decide (breakpilot-compliance).
|
||||
NotCodeCheckable,
|
||||
}
|
||||
|
||||
/// Why a control that can be checked in code was not assessed.
|
||||
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
|
||||
#[serde(tag = "kind", rename_all = "snake_case")]
|
||||
pub enum NotAssessedReason {
|
||||
/// No scan of the target has completed yet.
|
||||
NotScanned,
|
||||
/// Checkable in code, but no tool of ours checks it yet.
|
||||
NoTool,
|
||||
/// The scan found code in `languages`, but none of the control's rules
|
||||
/// covers them.
|
||||
NoRuleForLanguages { languages: Vec<String> },
|
||||
/// The scan predates language detection; a new scan tells.
|
||||
LanguagesUnknown,
|
||||
}
|
||||
|
||||
/// A finding as the compliance view lists it.
|
||||
#[derive(Debug, Clone, Serialize, PartialEq)]
|
||||
pub struct FindingRef {
|
||||
@@ -42,6 +58,9 @@ pub struct ControlStatus {
|
||||
pub state: ControlState,
|
||||
/// Why a control is not code-checkable, from the map.
|
||||
pub note: Option<String>,
|
||||
/// Why a checkable control was not assessed; set only in that state.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub reason: Option<NotAssessedReason>,
|
||||
pub findings: Vec<FindingRef>,
|
||||
}
|
||||
|
||||
@@ -55,11 +74,15 @@ fn counts(finding: &Finding) -> bool {
|
||||
}
|
||||
|
||||
/// The state of every control in `map` for a target with `findings`.
|
||||
/// `scanned` is whether a scan of the target has completed.
|
||||
///
|
||||
/// `scanned_languages` holds the source languages of the last completed scan,
|
||||
/// `None` before any scan completed. A control is "no violation found" only
|
||||
/// if one of its rules covers one of those languages; a scan of C code says
|
||||
/// nothing about a control only Python rules check.
|
||||
pub fn control_statuses(
|
||||
map: &ControlMap,
|
||||
findings: &[Finding],
|
||||
scanned: bool,
|
||||
scanned_languages: Option<&[String]>,
|
||||
) -> Vec<ControlStatus> {
|
||||
map.controls
|
||||
.iter()
|
||||
@@ -79,15 +102,31 @@ pub fn control_statuses(
|
||||
},
|
||||
})
|
||||
.collect();
|
||||
let state = if !against.is_empty() {
|
||||
ControlState::NotSatisfied
|
||||
let (state, reason) = if !against.is_empty() {
|
||||
(ControlState::NotSatisfied, None)
|
||||
} else {
|
||||
match entry.status {
|
||||
Coverage::NotCodeCheckable => ControlState::NotCodeCheckable,
|
||||
Coverage::Covered if scanned => ControlState::NoViolationFound,
|
||||
// Unscanned, or checkable in code but with no tool for it
|
||||
// yet: nothing can be said either way.
|
||||
Coverage::Covered | Coverage::NeedsTooling => ControlState::NotAssessed,
|
||||
match (entry.status, scanned_languages) {
|
||||
(Coverage::NotCodeCheckable, _) => (ControlState::NotCodeCheckable, None),
|
||||
(Coverage::NeedsTooling, _) => {
|
||||
(ControlState::NotAssessed, Some(NotAssessedReason::NoTool))
|
||||
}
|
||||
(Coverage::Covered, None) => (
|
||||
ControlState::NotAssessed,
|
||||
Some(NotAssessedReason::NotScanned),
|
||||
),
|
||||
(Coverage::Covered, Some(languages)) if entry.checked_for(languages) => {
|
||||
(ControlState::NoViolationFound, None)
|
||||
}
|
||||
(Coverage::Covered, Some([])) => (
|
||||
ControlState::NotAssessed,
|
||||
Some(NotAssessedReason::LanguagesUnknown),
|
||||
),
|
||||
(Coverage::Covered, Some(languages)) => (
|
||||
ControlState::NotAssessed,
|
||||
Some(NotAssessedReason::NoRuleForLanguages {
|
||||
languages: languages.to_vec(),
|
||||
}),
|
||||
),
|
||||
}
|
||||
};
|
||||
ControlStatus {
|
||||
@@ -95,6 +134,7 @@ pub fn control_statuses(
|
||||
title: entry.title.clone(),
|
||||
state,
|
||||
note: entry.note.clone(),
|
||||
reason,
|
||||
findings: against,
|
||||
}
|
||||
})
|
||||
@@ -124,18 +164,29 @@ mod tests {
|
||||
f
|
||||
}
|
||||
|
||||
fn state_of(statuses: &[ControlStatus], control: &str) -> ControlState {
|
||||
fn python() -> Vec<String> {
|
||||
vec!["python".into()]
|
||||
}
|
||||
|
||||
fn c_only() -> Vec<String> {
|
||||
vec!["c".into()]
|
||||
}
|
||||
|
||||
fn status_of<'a>(statuses: &'a [ControlStatus], control: &str) -> &'a ControlStatus {
|
||||
statuses
|
||||
.iter()
|
||||
.find(|s| s.control == control)
|
||||
.map(|s| s.state)
|
||||
.expect("every control is listed")
|
||||
}
|
||||
|
||||
fn state_of(statuses: &[ControlStatus], control: &str) -> ControlState {
|
||||
status_of(statuses, control).state
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_control_gets_exactly_one_state() {
|
||||
let map = ControlMap::cra().expect("map");
|
||||
let statuses = control_statuses(&map, &[], true);
|
||||
let statuses = control_statuses(&map, &[], Some(&python()));
|
||||
assert_eq!(statuses.len(), map.controls.len());
|
||||
assert_eq!(
|
||||
state_of(&statuses, "cra-ai-8"),
|
||||
@@ -155,7 +206,7 @@ mod tests {
|
||||
finding("cra-ai-13", FindingStatus::FalsePositive),
|
||||
finding("cra-ai-16", FindingStatus::Resolved),
|
||||
];
|
||||
let statuses = control_statuses(&map, &findings, true);
|
||||
let statuses = control_statuses(&map, &findings, Some(&c_only()));
|
||||
assert_eq!(state_of(&statuses, "cra-ai-8"), ControlState::NotSatisfied);
|
||||
assert_eq!(
|
||||
state_of(&statuses, "cra-ai-13"),
|
||||
@@ -176,11 +227,69 @@ mod tests {
|
||||
assert_eq!(violated.findings[0].status, "accepted_risk");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_c_scan_claims_nothing_for_controls_only_python_rules_check() {
|
||||
let map = ControlMap::cra().expect("map");
|
||||
let statuses = control_statuses(&map, &[], Some(&c_only()));
|
||||
|
||||
// Our C rules and cppcheck checked memory safety; gitleaks, secrets.
|
||||
assert_eq!(
|
||||
state_of(&statuses, "cra-ai-20"),
|
||||
ControlState::NoViolationFound
|
||||
);
|
||||
assert_eq!(
|
||||
state_of(&statuses, "cra-ai-9"),
|
||||
ControlState::NoViolationFound
|
||||
);
|
||||
// Only Python/JS rules check secure defaults: not assessed, and why.
|
||||
let defaults = status_of(&statuses, "cra-ai-1");
|
||||
assert_eq!(defaults.state, ControlState::NotAssessed);
|
||||
assert_eq!(
|
||||
defaults.reason,
|
||||
Some(NotAssessedReason::NoRuleForLanguages {
|
||||
languages: c_only()
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_mixed_scan_counts_every_language() {
|
||||
let map = ControlMap::cra().expect("map");
|
||||
let mixed = vec!["c".to_string(), "python".to_string()];
|
||||
let statuses = control_statuses(&map, &[], Some(&mixed));
|
||||
assert_eq!(
|
||||
state_of(&statuses, "cra-ai-1"),
|
||||
ControlState::NoViolationFound
|
||||
);
|
||||
assert_eq!(
|
||||
state_of(&statuses, "cra-ai-20"),
|
||||
ControlState::NoViolationFound
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_scan_without_recorded_languages_only_trusts_language_independent_tools() {
|
||||
let map = ControlMap::cra().expect("map");
|
||||
let statuses = control_statuses(&map, &[], Some(&[]));
|
||||
assert_eq!(
|
||||
state_of(&statuses, "cra-ai-9"),
|
||||
ControlState::NoViolationFound
|
||||
);
|
||||
assert_eq!(
|
||||
status_of(&statuses, "cra-ai-1").reason,
|
||||
Some(NotAssessedReason::LanguagesUnknown)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nothing_is_passed_before_a_scan() {
|
||||
let map = ControlMap::cra().expect("map");
|
||||
let statuses = control_statuses(&map, &[], false);
|
||||
let statuses = control_statuses(&map, &[], None);
|
||||
assert_eq!(state_of(&statuses, "cra-ai-8"), ControlState::NotAssessed);
|
||||
assert_eq!(
|
||||
status_of(&statuses, "cra-ai-8").reason,
|
||||
Some(NotAssessedReason::NotScanned)
|
||||
);
|
||||
assert!(statuses
|
||||
.iter()
|
||||
.all(|s| s.state != ControlState::NoViolationFound));
|
||||
|
||||
@@ -150,19 +150,21 @@ pub fn build_pack(
|
||||
Ok((archive, manifest.files))
|
||||
}
|
||||
|
||||
/// Whether a scan of the target has completed, so a control without
|
||||
/// findings can be called free of violations.
|
||||
pub fn scanned(inputs: &EvidenceInputs) -> bool {
|
||||
/// The source languages of the target's last completed scan, `None` before
|
||||
/// one completed: only then can a control without findings be called free of
|
||||
/// violations, and only for the languages it covers.
|
||||
pub fn scanned_languages(inputs: &EvidenceInputs) -> Option<&[String]> {
|
||||
inputs
|
||||
.last_run
|
||||
.as_ref()
|
||||
.is_some_and(|run| run.completed_at.is_some())
|
||||
.filter(|run| run.completed_at.is_some())
|
||||
.map(|run| run.languages.as_slice())
|
||||
}
|
||||
|
||||
/// The target's state against every CRA Annex I control.
|
||||
pub fn compliance_of(inputs: &EvidenceInputs) -> Vec<compliance::ControlStatus> {
|
||||
control_map::ControlMap::cra()
|
||||
.map(|map| compliance::control_statuses(&map, &inputs.findings, scanned(inputs)))
|
||||
.map(|map| compliance::control_statuses(&map, &inputs.findings, scanned_languages(inputs)))
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ use compliance_core::CoreError;
|
||||
|
||||
use crate::pipeline::dedup;
|
||||
use crate::pipeline::source_text::{read_lines, relative_path};
|
||||
use crate::pipeline::vendor::walk_outside;
|
||||
|
||||
/// Compilation units cppcheck analyses; headers are read through them.
|
||||
const SOURCE_EXTENSIONS: &[&str] = &["c", "cc", "cpp", "cxx"];
|
||||
@@ -246,28 +247,6 @@ fn include_dirs(root: &Path) -> Vec<String> {
|
||||
dirs
|
||||
}
|
||||
|
||||
/// Every entry under `root` outside `.git` and the `excluded` directories.
|
||||
fn walk_outside<'a>(
|
||||
root: &'a Path,
|
||||
excluded: &'a [String],
|
||||
) -> impl Iterator<Item = walkdir::DirEntry> + 'a {
|
||||
walkdir::WalkDir::new(root)
|
||||
.min_depth(1)
|
||||
.into_iter()
|
||||
.filter_entry(move |e| {
|
||||
if e.file_name() == ".git" {
|
||||
return false;
|
||||
}
|
||||
let rel = e
|
||||
.path()
|
||||
.strip_prefix(root)
|
||||
.map(|p| p.to_string_lossy().replace('\\', "/"))
|
||||
.unwrap_or_default();
|
||||
!excluded.contains(&rel)
|
||||
})
|
||||
.filter_map(Result::ok)
|
||||
}
|
||||
|
||||
fn is_compilation_unit(path: &Path) -> bool {
|
||||
path.extension()
|
||||
.and_then(|e| e.to_str())
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
//! Which source languages a tree holds, outside vendor code.
|
||||
//!
|
||||
//! Recorded on the scan run so the CRA status only claims "no violation
|
||||
//! found" for a control when one of its rules covers the code that was
|
||||
//! actually scanned (#368).
|
||||
|
||||
use std::collections::BTreeSet;
|
||||
use std::path::Path;
|
||||
|
||||
use super::vendor::walk_outside;
|
||||
|
||||
/// The language a file extension belongs to, by the names the control map's
|
||||
/// bindings use. Headers count as C: a C++ project also has `.cpp` files.
|
||||
fn language_of(extension: &str) -> Option<&'static str> {
|
||||
Some(match extension {
|
||||
"c" | "h" => "c",
|
||||
"cc" | "cpp" | "cxx" | "hh" | "hpp" | "hxx" => "cpp",
|
||||
"py" => "python",
|
||||
"js" | "jsx" | "mjs" | "cjs" => "javascript",
|
||||
"ts" | "tsx" | "mts" | "cts" => "typescript",
|
||||
"go" => "go",
|
||||
"java" => "java",
|
||||
"kt" | "kts" => "kotlin",
|
||||
"rb" => "ruby",
|
||||
"php" => "php",
|
||||
"rs" => "rust",
|
||||
"cs" => "csharp",
|
||||
"swift" => "swift",
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// The source languages of the files under `root`, skipping `.git` and the
|
||||
/// `excluded` (vendor) directories, sorted.
|
||||
pub fn source_languages(root: &Path, excluded: &[String]) -> Vec<String> {
|
||||
walk_outside(root, excluded)
|
||||
.filter(|e| e.file_type().is_file())
|
||||
.filter_map(|e| {
|
||||
let ext = e.path().extension()?.to_str()?.to_ascii_lowercase();
|
||||
language_of(&ext)
|
||||
})
|
||||
.collect::<BTreeSet<_>>()
|
||||
.into_iter()
|
||||
.map(str::to_owned)
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::expect_used)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn languages_come_from_the_code_outside_vendor_directories() {
|
||||
let dir = std::env::temp_dir().join(format!("cs-languages-{}", uuid::Uuid::new_v4()));
|
||||
let root = dir.as_path();
|
||||
for (path, body) in [
|
||||
("Core/Src/main.c", "int main(void) { return 0; }"),
|
||||
("Core/Inc/main.h", "#pragma once"),
|
||||
("tools/flash.py", "print('flash')"),
|
||||
("Middlewares/Third_Party/lib/x.cpp", "int x;"),
|
||||
("README.md", "# readme"),
|
||||
] {
|
||||
let file = root.join(path);
|
||||
std::fs::create_dir_all(file.parent().expect("parent")).expect("mkdir");
|
||||
std::fs::write(file, body).expect("write");
|
||||
}
|
||||
|
||||
let vendor = vec!["Middlewares/Third_Party".to_string()];
|
||||
|
||||
assert_eq!(source_languages(root, &vendor), ["c", "python"]);
|
||||
assert_eq!(source_languages(root, &[]), ["c", "cpp", "python"]);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ pub mod git_submodules;
|
||||
pub mod gitleaks;
|
||||
mod graph_build;
|
||||
mod issue_creation;
|
||||
pub mod languages;
|
||||
pub mod lint;
|
||||
pub mod nvd_cpe;
|
||||
pub mod orchestrator;
|
||||
|
||||
@@ -124,13 +124,15 @@ impl PipelineOrchestrator {
|
||||
tracing::info!("[{repo_id}] Stage 1: SAST");
|
||||
self.update_phase(scan_run_id, ScanPhase::Sast).await;
|
||||
let tree = repo_path.clone();
|
||||
let (vendor, cppcheck) = tokio::task::spawn_blocking(move || {
|
||||
let (vendor, cppcheck, languages) = tokio::task::spawn_blocking(move || {
|
||||
let vendor = crate::pipeline::vendor::vendor_dirs(&tree);
|
||||
let cppcheck = CppcheckScanner::for_tree(&tree, &vendor);
|
||||
(vendor, cppcheck)
|
||||
let languages = crate::pipeline::languages::source_languages(&tree, &vendor);
|
||||
(vendor, cppcheck, languages)
|
||||
})
|
||||
.await
|
||||
.unwrap_or_default();
|
||||
self.record_languages(scan_run_id, &languages).await;
|
||||
if !vendor.is_empty() {
|
||||
self.skip_stage(
|
||||
scan_run_id,
|
||||
@@ -1454,6 +1456,25 @@ impl PipelineOrchestrator {
|
||||
/// Record that `phase` started. Typed, so a phase the model cannot read
|
||||
/// back cannot be written: an unknown one made the whole run fail to
|
||||
/// deserialize and drop out of `GET /scan-runs`.
|
||||
/// Add the source `languages` the static analysis saw to the run; a run
|
||||
/// that scans several trees collects them all.
|
||||
async fn record_languages(&self, scan_run_id: &str, languages: &[String]) {
|
||||
let Ok(oid) = mongodb::bson::oid::ObjectId::parse_str(scan_run_id) else {
|
||||
return;
|
||||
};
|
||||
if languages.is_empty() {
|
||||
return;
|
||||
}
|
||||
let _ = self
|
||||
.db
|
||||
.scan_runs()
|
||||
.update_one(
|
||||
doc! { "_id": oid },
|
||||
doc! { "$addToSet": { "languages": { "$each": languages.to_vec() } } },
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
pub(super) async fn update_phase(&self, scan_run_id: &str, phase: ScanPhase) {
|
||||
let Ok(phase) = mongodb::bson::to_bson(&phase) else {
|
||||
return;
|
||||
|
||||
@@ -26,6 +26,29 @@ const VENDOR_DIR_NAMES: &[&str] = &[
|
||||
/// `STM32F4xx_HAL_Driver` or `STM32F10x_StdPeriph_Driver`.
|
||||
const VENDOR_DIR_SUFFIXES: &[&str] = &["_hal_driver", "_stdperiph_driver"];
|
||||
|
||||
/// Every entry under `root` outside `.git` and the `excluded` directories
|
||||
/// (relative paths with `/` separators, as [`vendor_dirs`] returns them).
|
||||
pub fn walk_outside<'a>(
|
||||
root: &'a Path,
|
||||
excluded: &'a [String],
|
||||
) -> impl Iterator<Item = walkdir::DirEntry> + 'a {
|
||||
walkdir::WalkDir::new(root)
|
||||
.min_depth(1)
|
||||
.into_iter()
|
||||
.filter_entry(move |e| {
|
||||
if e.file_name() == ".git" {
|
||||
return false;
|
||||
}
|
||||
let rel = e
|
||||
.path()
|
||||
.strip_prefix(root)
|
||||
.map(|p| p.to_string_lossy().replace('\\', "/"))
|
||||
.unwrap_or_default();
|
||||
!excluded.contains(&rel)
|
||||
})
|
||||
.filter_map(Result::ok)
|
||||
}
|
||||
|
||||
/// The top-most vendor directories under `root`, relative to it with `/`
|
||||
/// separators, sorted. A directory inside one that already matched is not
|
||||
/// listed again.
|
||||
|
||||
@@ -122,6 +122,11 @@ pub struct ScanRun {
|
||||
/// skipped stage is not a failure.
|
||||
#[serde(default)]
|
||||
pub skipped_stages: Vec<SkippedStage>,
|
||||
/// The source languages the static analysis found outside vendor code
|
||||
/// (`"c"`, `"python"`, …). A control counts as checked only if one of its
|
||||
/// rules covers one of them. Empty in runs from before it was recorded.
|
||||
#[serde(default)]
|
||||
pub languages: Vec<String>,
|
||||
}
|
||||
|
||||
/// A pipeline stage that was planned but intentionally not executed.
|
||||
@@ -205,6 +210,7 @@ impl ScanRun {
|
||||
started_at: Utc::now(),
|
||||
completed_at: None,
|
||||
skipped_stages: Vec::new(),
|
||||
languages: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1622,8 +1622,27 @@ pub mod compliance {
|
||||
en: "No violation found",
|
||||
};
|
||||
pub const NOT_ASSESSED: Text = Text {
|
||||
de: "Noch nicht bewertet",
|
||||
en: "Not assessed yet",
|
||||
de: "Nicht bewertet",
|
||||
en: "Not assessed",
|
||||
};
|
||||
pub const NO_TOOL: Text = Text {
|
||||
de: "Im Code prüfbar, aber noch ohne Prüfregel.",
|
||||
en: "Checkable in code, but no rule checks it yet.",
|
||||
};
|
||||
/// Followed by the languages found, e.g. "C, C++".
|
||||
pub const NO_RULE_FOR: Text = Text {
|
||||
de: "Keine Prüfregel für den gefundenen Code:",
|
||||
en: "No rule covers the code found:",
|
||||
};
|
||||
pub const LANGUAGES_UNKNOWN: Text = Text {
|
||||
de: "Die letzte Prüfung hat die Sprachen des Codes nicht erfasst. Nach der \
|
||||
nächsten Prüfung steht hier das Ergebnis.",
|
||||
en: "The last scan did not record the code's languages. The result appears \
|
||||
after the next scan.",
|
||||
};
|
||||
pub const LANGUAGES: Text = Text {
|
||||
de: "Geprüfte Sprachen",
|
||||
en: "Languages checked",
|
||||
};
|
||||
pub const NOT_CODE_CHECKABLE: Text = Text {
|
||||
de: "Nicht im Code prüfbar",
|
||||
@@ -1665,6 +1684,86 @@ pub mod compliance {
|
||||
de: "Der CRA-Status konnte nicht geladen werden.",
|
||||
en: "The CRA status could not be loaded.",
|
||||
};
|
||||
|
||||
/// A language code from the scan (`c`, `cpp`, `python`, …) as people
|
||||
/// write it; an unknown code is shown as it is.
|
||||
pub fn language_name(code: &str) -> &str {
|
||||
match code {
|
||||
"c" => "C",
|
||||
"cpp" => "C++",
|
||||
"python" => "Python",
|
||||
"javascript" => "JavaScript",
|
||||
"typescript" => "TypeScript",
|
||||
"go" => "Go",
|
||||
"java" => "Java",
|
||||
"kotlin" => "Kotlin",
|
||||
"ruby" => "Ruby",
|
||||
"php" => "PHP",
|
||||
"rust" => "Rust",
|
||||
"csharp" => "C#",
|
||||
"swift" => "Swift",
|
||||
other => other,
|
||||
}
|
||||
}
|
||||
|
||||
/// Language codes as a readable list: `["c", "cpp"]` becomes "C, C++".
|
||||
pub fn language_list(codes: &[serde_json::Value]) -> String {
|
||||
codes
|
||||
.iter()
|
||||
.filter_map(serde_json::Value::as_str)
|
||||
.map(language_name)
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
}
|
||||
|
||||
/// Why a control was not assessed, from the agent's `reason` object;
|
||||
/// `None` when there is none.
|
||||
pub fn not_assessed_reason(
|
||||
reason: &serde_json::Value,
|
||||
lang: super::super::Lang,
|
||||
) -> Option<String> {
|
||||
Some(match reason["kind"].as_str()? {
|
||||
"not_scanned" => NOT_ASSESSED_HINT.get(lang).to_string(),
|
||||
"no_tool" => NO_TOOL.get(lang).to_string(),
|
||||
"no_rule_for_languages" => format!(
|
||||
"{} {}",
|
||||
NO_RULE_FOR.get(lang),
|
||||
language_list(
|
||||
reason["languages"]
|
||||
.as_array()
|
||||
.map_or(&[][..], Vec::as_slice)
|
||||
)
|
||||
),
|
||||
"languages_unknown" => LANGUAGES_UNKNOWN.get(lang).to_string(),
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::super::Lang;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn the_reason_names_the_languages_no_rule_covers() {
|
||||
let reason = serde_json::json!({
|
||||
"kind": "no_rule_for_languages",
|
||||
"languages": ["c", "cpp"],
|
||||
});
|
||||
assert_eq!(
|
||||
not_assessed_reason(&reason, Lang::De).as_deref(),
|
||||
Some("Keine Prüfregel für den gefundenen Code: C, C++")
|
||||
);
|
||||
assert_eq!(
|
||||
not_assessed_reason(&serde_json::json!({ "kind": "no_tool" }), Lang::En).as_deref(),
|
||||
Some("Checkable in code, but no rule checks it yet.")
|
||||
);
|
||||
assert_eq!(
|
||||
not_assessed_reason(&serde_json::Value::Null, Lang::En),
|
||||
None
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The "Add demo targets" button and its dialog.
|
||||
|
||||
@@ -63,6 +63,11 @@ pub fn CompliancePage(id: String) -> Element {
|
||||
)
|
||||
.ok()
|
||||
.map(|s| crate::components::scan_runs::status_badge(&s).1.get(lang));
|
||||
let languages = tx::language_list(
|
||||
data["last_scan"]["languages"]
|
||||
.as_array()
|
||||
.map_or(&[][..], Vec::as_slice),
|
||||
);
|
||||
|
||||
rsx! {
|
||||
PageHeader {
|
||||
@@ -85,7 +90,12 @@ pub fn CompliancePage(id: String) -> Element {
|
||||
StatCard { label: tx::CVES.get(lang), value: data["cve_alerts"].to_string() }
|
||||
}
|
||||
if let Some(status) = last_scan {
|
||||
p { style: "opacity: 0.75; font-size: 0.9em;", "{tx::LAST_SCAN.get(lang)}: {status}" }
|
||||
p { style: "opacity: 0.75; font-size: 0.9em;",
|
||||
"{tx::LAST_SCAN.get(lang)}: {status}"
|
||||
if !languages.is_empty() {
|
||||
" · {tx::LANGUAGES.get(lang)}: {languages}"
|
||||
}
|
||||
}
|
||||
} else {
|
||||
p { style: "opacity: 0.75; font-size: 0.9em;", {tx::NOT_ASSESSED_HINT.get(lang)} }
|
||||
}
|
||||
@@ -107,6 +117,7 @@ pub fn CompliancePage(id: String) -> Element {
|
||||
let shown = state_info(&state).1;
|
||||
let findings = control["findings"].as_array().cloned().unwrap_or_default();
|
||||
let note = control["note"].as_str().map(String::from);
|
||||
let reason = tx::not_assessed_reason(&control["reason"], lang);
|
||||
rsx! {
|
||||
tr {
|
||||
td {
|
||||
@@ -126,6 +137,9 @@ pub fn CompliancePage(id: String) -> Element {
|
||||
div { style: "opacity: 0.6; font-size: 0.9em;", "{note}" }
|
||||
}
|
||||
}
|
||||
if let Some(reason) = reason {
|
||||
div { style: "opacity: 0.75;", "{reason}" }
|
||||
}
|
||||
for f in findings {
|
||||
{
|
||||
let fid = f["id"].as_str().unwrap_or_default().to_string();
|
||||
|
||||
@@ -9,6 +9,11 @@
|
||||
{
|
||||
"tool": "semgrep",
|
||||
"scan_type": "sast",
|
||||
"languages": [
|
||||
"python",
|
||||
"javascript",
|
||||
"typescript"
|
||||
],
|
||||
"cwe": [],
|
||||
"rules": [
|
||||
"cra-ai-1-flask-debug-enabled",
|
||||
@@ -56,6 +61,18 @@
|
||||
{
|
||||
"tool": "grounded-control-check",
|
||||
"scan_type": "code_review",
|
||||
"languages": [
|
||||
"python",
|
||||
"javascript",
|
||||
"typescript",
|
||||
"go",
|
||||
"java",
|
||||
"ruby",
|
||||
"php",
|
||||
"rust",
|
||||
"csharp",
|
||||
"kotlin"
|
||||
],
|
||||
"cwe": [],
|
||||
"rules": []
|
||||
}
|
||||
@@ -70,6 +87,9 @@
|
||||
{
|
||||
"tool": "semgrep",
|
||||
"scan_type": "sast",
|
||||
"languages": [
|
||||
"python"
|
||||
],
|
||||
"cwe": [],
|
||||
"rules": [
|
||||
"cra-ai-7-weak-password-hash"
|
||||
@@ -86,12 +106,25 @@
|
||||
{
|
||||
"tool": "gitleaks",
|
||||
"scan_type": "secret_detection",
|
||||
"languages": [],
|
||||
"cwe": [],
|
||||
"rules": []
|
||||
},
|
||||
{
|
||||
"tool": "semgrep",
|
||||
"scan_type": "sast",
|
||||
"languages": [
|
||||
"python",
|
||||
"javascript",
|
||||
"typescript",
|
||||
"java",
|
||||
"go",
|
||||
"ruby",
|
||||
"php",
|
||||
"csharp",
|
||||
"kotlin",
|
||||
"c"
|
||||
],
|
||||
"cwe": [
|
||||
"CWE-798",
|
||||
"CWE-259"
|
||||
@@ -111,12 +144,24 @@
|
||||
{
|
||||
"tool": "gitleaks",
|
||||
"scan_type": "secret_detection",
|
||||
"languages": [],
|
||||
"cwe": [],
|
||||
"rules": []
|
||||
},
|
||||
{
|
||||
"tool": "semgrep",
|
||||
"scan_type": "sast",
|
||||
"languages": [
|
||||
"python",
|
||||
"javascript",
|
||||
"typescript",
|
||||
"java",
|
||||
"go",
|
||||
"ruby",
|
||||
"php",
|
||||
"csharp",
|
||||
"kotlin"
|
||||
],
|
||||
"cwe": [
|
||||
"CWE-798",
|
||||
"CWE-522"
|
||||
@@ -134,6 +179,11 @@
|
||||
{
|
||||
"tool": "semgrep",
|
||||
"scan_type": "sast",
|
||||
"languages": [
|
||||
"python",
|
||||
"javascript",
|
||||
"typescript"
|
||||
],
|
||||
"cwe": [],
|
||||
"rules": [
|
||||
"cra-ai-10-session-cookie-insecure",
|
||||
@@ -151,6 +201,18 @@
|
||||
{
|
||||
"tool": "grounded-control-check",
|
||||
"scan_type": "code_review",
|
||||
"languages": [
|
||||
"python",
|
||||
"javascript",
|
||||
"typescript",
|
||||
"go",
|
||||
"java",
|
||||
"ruby",
|
||||
"php",
|
||||
"rust",
|
||||
"csharp",
|
||||
"kotlin"
|
||||
],
|
||||
"cwe": [],
|
||||
"rules": []
|
||||
}
|
||||
@@ -165,6 +227,18 @@
|
||||
{
|
||||
"tool": "grounded-control-check",
|
||||
"scan_type": "code_review",
|
||||
"languages": [
|
||||
"python",
|
||||
"javascript",
|
||||
"typescript",
|
||||
"go",
|
||||
"java",
|
||||
"ruby",
|
||||
"php",
|
||||
"rust",
|
||||
"csharp",
|
||||
"kotlin"
|
||||
],
|
||||
"cwe": [],
|
||||
"rules": []
|
||||
}
|
||||
@@ -179,6 +253,18 @@
|
||||
{
|
||||
"tool": "semgrep",
|
||||
"scan_type": "sast",
|
||||
"languages": [
|
||||
"python",
|
||||
"javascript",
|
||||
"typescript",
|
||||
"java",
|
||||
"go",
|
||||
"ruby",
|
||||
"php",
|
||||
"csharp",
|
||||
"kotlin",
|
||||
"c"
|
||||
],
|
||||
"cwe": [
|
||||
"CWE-327",
|
||||
"CWE-326"
|
||||
@@ -198,6 +284,11 @@
|
||||
{
|
||||
"tool": "semgrep",
|
||||
"scan_type": "sast",
|
||||
"languages": [
|
||||
"python",
|
||||
"javascript",
|
||||
"typescript"
|
||||
],
|
||||
"cwe": [],
|
||||
"rules": [
|
||||
"cra-ai-14-python-weak-cipher",
|
||||
@@ -215,6 +306,17 @@
|
||||
{
|
||||
"tool": "semgrep",
|
||||
"scan_type": "sast",
|
||||
"languages": [
|
||||
"python",
|
||||
"javascript",
|
||||
"typescript",
|
||||
"java",
|
||||
"go",
|
||||
"ruby",
|
||||
"php",
|
||||
"csharp",
|
||||
"kotlin"
|
||||
],
|
||||
"cwe": [
|
||||
"CWE-319",
|
||||
"CWE-311"
|
||||
@@ -232,12 +334,25 @@
|
||||
{
|
||||
"tool": "gitleaks",
|
||||
"scan_type": "secret_detection",
|
||||
"languages": [],
|
||||
"cwe": [],
|
||||
"rules": []
|
||||
},
|
||||
{
|
||||
"tool": "semgrep",
|
||||
"scan_type": "sast",
|
||||
"languages": [
|
||||
"python",
|
||||
"javascript",
|
||||
"typescript",
|
||||
"java",
|
||||
"go",
|
||||
"ruby",
|
||||
"php",
|
||||
"csharp",
|
||||
"kotlin",
|
||||
"c"
|
||||
],
|
||||
"cwe": [
|
||||
"CWE-798",
|
||||
"CWE-321"
|
||||
@@ -278,6 +393,18 @@
|
||||
{
|
||||
"tool": "semgrep",
|
||||
"scan_type": "sast",
|
||||
"languages": [
|
||||
"python",
|
||||
"javascript",
|
||||
"typescript",
|
||||
"java",
|
||||
"go",
|
||||
"ruby",
|
||||
"php",
|
||||
"csharp",
|
||||
"kotlin",
|
||||
"c"
|
||||
],
|
||||
"cwe": [
|
||||
"CWE-89",
|
||||
"CWE-78",
|
||||
@@ -293,6 +420,10 @@
|
||||
{
|
||||
"tool": "cppcheck",
|
||||
"scan_type": "sast",
|
||||
"languages": [
|
||||
"c",
|
||||
"cpp"
|
||||
],
|
||||
"cwe": [
|
||||
"CWE-119",
|
||||
"CWE-120",
|
||||
@@ -333,12 +464,14 @@
|
||||
{
|
||||
"tool": "osv",
|
||||
"scan_type": "cve",
|
||||
"languages": [],
|
||||
"cwe": [],
|
||||
"rules": []
|
||||
},
|
||||
{
|
||||
"tool": "syft",
|
||||
"scan_type": "sbom",
|
||||
"languages": [],
|
||||
"cwe": [],
|
||||
"rules": []
|
||||
}
|
||||
@@ -353,6 +486,7 @@
|
||||
{
|
||||
"tool": "syft",
|
||||
"scan_type": "sbom",
|
||||
"languages": [],
|
||||
"cwe": [],
|
||||
"rules": []
|
||||
}
|
||||
@@ -367,6 +501,18 @@
|
||||
{
|
||||
"tool": "grounded-control-check",
|
||||
"scan_type": "code_review",
|
||||
"languages": [
|
||||
"python",
|
||||
"javascript",
|
||||
"typescript",
|
||||
"go",
|
||||
"java",
|
||||
"ruby",
|
||||
"php",
|
||||
"rust",
|
||||
"csharp",
|
||||
"kotlin"
|
||||
],
|
||||
"cwe": [],
|
||||
"rules": []
|
||||
}
|
||||
@@ -395,6 +541,18 @@
|
||||
{
|
||||
"tool": "grounded-control-check",
|
||||
"scan_type": "code_review",
|
||||
"languages": [
|
||||
"python",
|
||||
"javascript",
|
||||
"typescript",
|
||||
"go",
|
||||
"java",
|
||||
"ruby",
|
||||
"php",
|
||||
"rust",
|
||||
"csharp",
|
||||
"kotlin"
|
||||
],
|
||||
"cwe": [],
|
||||
"rules": []
|
||||
}
|
||||
@@ -409,6 +567,18 @@
|
||||
{
|
||||
"tool": "grounded-control-check",
|
||||
"scan_type": "code_review",
|
||||
"languages": [
|
||||
"python",
|
||||
"javascript",
|
||||
"typescript",
|
||||
"go",
|
||||
"java",
|
||||
"ruby",
|
||||
"php",
|
||||
"rust",
|
||||
"csharp",
|
||||
"kotlin"
|
||||
],
|
||||
"cwe": [],
|
||||
"rules": []
|
||||
}
|
||||
@@ -423,6 +593,18 @@
|
||||
{
|
||||
"tool": "grounded-control-check",
|
||||
"scan_type": "code_review",
|
||||
"languages": [
|
||||
"python",
|
||||
"javascript",
|
||||
"typescript",
|
||||
"go",
|
||||
"java",
|
||||
"ruby",
|
||||
"php",
|
||||
"rust",
|
||||
"csharp",
|
||||
"kotlin"
|
||||
],
|
||||
"cwe": [],
|
||||
"rules": []
|
||||
}
|
||||
@@ -437,6 +619,18 @@
|
||||
{
|
||||
"tool": "grounded-control-check",
|
||||
"scan_type": "code_review",
|
||||
"languages": [
|
||||
"python",
|
||||
"javascript",
|
||||
"typescript",
|
||||
"go",
|
||||
"java",
|
||||
"ruby",
|
||||
"php",
|
||||
"rust",
|
||||
"csharp",
|
||||
"kotlin"
|
||||
],
|
||||
"cwe": [],
|
||||
"rules": []
|
||||
}
|
||||
@@ -465,12 +659,14 @@
|
||||
{
|
||||
"tool": "syft",
|
||||
"scan_type": "sbom",
|
||||
"languages": [],
|
||||
"cwe": [],
|
||||
"rules": []
|
||||
},
|
||||
{
|
||||
"tool": "osv",
|
||||
"scan_type": "cve",
|
||||
"languages": [],
|
||||
"cwe": [],
|
||||
"rules": []
|
||||
}
|
||||
|
||||
@@ -30,6 +30,11 @@ pub struct ScanBinding {
|
||||
pub tool: String,
|
||||
/// Scan family, e.g. `"sast"`, `"secret_detection"`, `"sbom"`, `"cve"`.
|
||||
pub scan_type: String,
|
||||
/// The source languages this binding checks (`"c"`, `"python"`, …), as
|
||||
/// the rules behind it declare them. Empty means language-independent
|
||||
/// (secrets in any file, dependencies from any manifest).
|
||||
#[serde(default)]
|
||||
pub languages: Vec<String>,
|
||||
/// CWEs whose findings map to this control (used to attach findings back).
|
||||
#[serde(default)]
|
||||
pub cwe: Vec<String>,
|
||||
@@ -38,6 +43,13 @@ pub struct ScanBinding {
|
||||
pub rules: Vec<String>,
|
||||
}
|
||||
|
||||
impl ScanBinding {
|
||||
/// Whether this binding checks code in any of `languages`.
|
||||
pub fn applies_to(&self, languages: &[String]) -> bool {
|
||||
self.languages.is_empty() || self.languages.iter().any(|l| languages.contains(l))
|
||||
}
|
||||
}
|
||||
|
||||
/// One control's entry in the LUT.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ControlEntry {
|
||||
@@ -56,6 +68,15 @@ pub struct ControlEntry {
|
||||
pub note: Option<String>,
|
||||
}
|
||||
|
||||
impl ControlEntry {
|
||||
/// Whether a scan of code in `languages` checks this control at all: at
|
||||
/// least one of its bindings applies. Only then can "no violation found"
|
||||
/// be claimed for it.
|
||||
pub fn checked_for(&self, languages: &[String]) -> bool {
|
||||
self.scans.iter().any(|s| s.applies_to(languages))
|
||||
}
|
||||
}
|
||||
|
||||
/// The control → scan lookup table for one framework.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ControlMap {
|
||||
@@ -190,6 +211,43 @@ mod tests {
|
||||
assert_eq!(map.summary().total(), 40);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_control_is_checked_only_for_the_languages_its_rules_cover() {
|
||||
let map = ControlMap::cra().expect("map");
|
||||
let c_only = ["c".to_string()];
|
||||
let python = ["python".to_string()];
|
||||
let secure_defaults = map.coverage("cra-ai-1").expect("cra-ai-1");
|
||||
let memory_safety = map.coverage("cra-ai-20").expect("cra-ai-20");
|
||||
let secrets = map.coverage("cra-ai-9").expect("cra-ai-9");
|
||||
|
||||
// Only Python/JS rules check secure defaults.
|
||||
assert!(!secure_defaults.checked_for(&c_only));
|
||||
assert!(secure_defaults.checked_for(&python));
|
||||
// Our C rules and cppcheck check memory safety in C.
|
||||
assert!(memory_safety.checked_for(&c_only));
|
||||
// gitleaks reads any file, so no language is needed.
|
||||
assert!(secrets.checked_for(&[]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_code_binding_names_its_languages() {
|
||||
// Only tools that do not read source code may be language-independent;
|
||||
// a rule binding without languages would claim every language.
|
||||
let map = ControlMap::cra().expect("map");
|
||||
for control in &map.controls {
|
||||
for scan in &control.scans {
|
||||
let reads_code = !matches!(scan.tool.as_str(), "gitleaks" | "syft" | "osv");
|
||||
assert_eq!(
|
||||
reads_code,
|
||||
!scan.languages.is_empty(),
|
||||
"{} / {}",
|
||||
control.control,
|
||||
scan.tool
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hardcoded_password_control_is_tool_covered() {
|
||||
let map = ControlMap::cra().unwrap();
|
||||
|
||||
Reference in new issue
Block a user