feat(plc): PLC findings keyed by what they flag, saved through RunFindings (#491 follow-up)
PLC findings were still keyed by POU and line and inserted one by one, so every line shift in an export made a duplicate finding (they are not auto-closed, so the old one stayed). - plc::analyze_tree gives each hit its content: the POU, the rule's title and description (variable and function names, no line numbers) and its occurrence among identical hits in the POU. A PLCopen XML line is a line of the embedded body, not of the file, so the file is not read. - The PLC scan saves through RunFindings: content keys, the row stored under the line-based fingerprint taken over (id, status, triage kept), moved lines followed, control refs refreshed on earlier rows (what the removed loop did). Still no auto-close for PLC. - content_fingerprints works without a tree when the scanner gives the content (a PLC scan reads several trees). - Test: a comment added on top keeps every PLC content key, and no two hits share one. Staging test plan: check 19. 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
f170e7cfe5
commit
47de7ecf0d
4 files changed
+78
-41
No files matched your search
@@ -96,9 +96,7 @@ impl RunFindings {
|
||||
/// and label them with their app variant. Called before grouping too, so
|
||||
/// a group's list of places names each place's variant.
|
||||
pub fn label(&self, findings: &mut [Finding]) {
|
||||
if let Some(tree) = &self.tree {
|
||||
content_fingerprints(tree, findings);
|
||||
}
|
||||
content_fingerprints(self.tree.as_deref(), findings);
|
||||
self.variants.label(findings);
|
||||
}
|
||||
|
||||
@@ -716,7 +714,7 @@ fn normalised(line: &str) -> String {
|
||||
/// row an earlier run stored. A finding without a file and line, a group
|
||||
/// (#464, keyed without places already), one already keyed, and one whose
|
||||
/// line is empty or gone keep the fingerprint they have.
|
||||
fn content_fingerprints(tree: &Path, findings: &mut [Finding]) {
|
||||
fn content_fingerprints(tree: Option<&Path>, findings: &mut [Finding]) {
|
||||
let mut files: HashMap<String, Option<Vec<String>>> = HashMap::new();
|
||||
for finding in findings.iter_mut() {
|
||||
if !finding.occurrences.is_empty() || !finding.legacy_fingerprints.is_empty() {
|
||||
@@ -725,8 +723,13 @@ fn content_fingerprints(tree: &Path, findings: &mut [Finding]) {
|
||||
let (Some(path), Some(line)) = (finding.file_path.clone(), finding.line_number) else {
|
||||
continue;
|
||||
};
|
||||
// Without a tree (the PLC scan reads several), only a scanner's own
|
||||
// content can key a finding.
|
||||
if tree.is_none() && finding.fingerprint_content.is_none() {
|
||||
continue;
|
||||
}
|
||||
let lines = files.entry(path.clone()).or_insert_with(|| {
|
||||
std::fs::read_to_string(tree.join(&path))
|
||||
std::fs::read_to_string(tree?.join(&path))
|
||||
.ok()
|
||||
.map(|text| text.lines().map(normalised).collect())
|
||||
});
|
||||
@@ -962,7 +965,7 @@ mod tests {
|
||||
}
|
||||
|
||||
fn keyed(tree: &Path, mut findings: Vec<Finding>) -> Vec<Finding> {
|
||||
content_fingerprints(tree, &mut findings);
|
||||
content_fingerprints(Some(tree), &mut findings);
|
||||
findings
|
||||
}
|
||||
|
||||
|
||||
@@ -1204,40 +1204,15 @@ impl PipelineOrchestrator {
|
||||
"PLC control-logic analysis complete"
|
||||
);
|
||||
|
||||
let mut new_count = 0u32;
|
||||
let mut refreshed_count = 0u32;
|
||||
for mut finding in all_findings {
|
||||
finding.scan_run_id = Some(scan_run_id.to_string());
|
||||
if self
|
||||
.db
|
||||
.findings()
|
||||
.find_one(doc! { "fingerprint": &finding.fingerprint })
|
||||
.await?
|
||||
.is_none()
|
||||
{
|
||||
self.db.findings().insert_one(&finding).await?;
|
||||
new_count += 1;
|
||||
} else if !finding.control_refs.is_empty() {
|
||||
// Re-scan refresh: mirror run_pipeline — persist newly-computed
|
||||
// control_refs onto a PLC finding first seen before the semantic
|
||||
// pass ran. The insert path alone never would, so without this a
|
||||
// PLC re-scan can only pick up mappings via a delete + re-add.
|
||||
self.db
|
||||
.findings()
|
||||
.update_one(
|
||||
doc! { "fingerprint": &finding.fingerprint },
|
||||
doc! { "$set": { "control_refs": finding.control_refs.clone() } },
|
||||
)
|
||||
.await?;
|
||||
refreshed_count += 1;
|
||||
}
|
||||
}
|
||||
if refreshed_count > 0 {
|
||||
tracing::info!(
|
||||
target_id,
|
||||
"Refreshed control_refs on {refreshed_count} existing PLC findings"
|
||||
);
|
||||
}
|
||||
// Saved like the code scan's findings: keyed by content (#491), with a
|
||||
// row stored under the line-based fingerprint taken over, moved lines
|
||||
// followed, and newly computed control refs stored on earlier rows.
|
||||
// A PLC finding is not closed when it is gone (not yet, #443).
|
||||
let mut all_findings = all_findings;
|
||||
let mut run_findings = crate::pipeline::finding_store::RunFindings::new(scan_run_id);
|
||||
run_findings.label(&mut all_findings);
|
||||
let new_count = run_findings.save_new(&self.db, &mut all_findings).await?;
|
||||
run_findings.write_back(&self.db, &mut all_findings).await?;
|
||||
|
||||
if !all_sbom.is_empty() {
|
||||
if let Err(e) = self
|
||||
|
||||
@@ -79,7 +79,27 @@ pub(crate) fn analyze_tree(root: &Path, repo_id: &str) -> Vec<Finding> {
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
for pou in &pous {
|
||||
for hit in rules::analyze(pou) {
|
||||
let mut hits = rules::analyze(pou);
|
||||
hits.sort_by_key(|h| h.line);
|
||||
// How often the same hit came before in this POU: the same flaw
|
||||
// twice is two findings (#491).
|
||||
let mut seen: std::collections::HashMap<(&'static str, String, String), usize> =
|
||||
std::collections::HashMap::new();
|
||||
for hit in hits {
|
||||
let occurrence = {
|
||||
let count = seen
|
||||
.entry((hit.rule_id, hit.title.clone(), hit.description.clone()))
|
||||
.or_insert(0);
|
||||
*count += 1;
|
||||
*count - 1
|
||||
};
|
||||
// What identifies the hit without its line: the POU and what
|
||||
// the rule says about it (variable and function names), as a
|
||||
// PLCopen XML line is not a line of the file.
|
||||
let content = format!(
|
||||
"{}\u{1f}{}\u{1f}{}\u{1f}{occurrence}",
|
||||
pou.name, hit.title, hit.description
|
||||
);
|
||||
let line_s = hit.line.to_string();
|
||||
let fingerprint =
|
||||
dedup::compute_fingerprint(&[repo_id, &rel, hit.rule_id, &pou.name, &line_s]);
|
||||
@@ -97,6 +117,7 @@ pub(crate) fn analyze_tree(root: &Path, repo_id: &str) -> Vec<Finding> {
|
||||
f.rule_id = Some(hit.rule_id.to_string());
|
||||
f.cwe = hit.cwe.map(String::from);
|
||||
f.remediation = Some(hit.remediation.to_string());
|
||||
f.fingerprint_content = Some(content);
|
||||
findings.push(f);
|
||||
}
|
||||
}
|
||||
@@ -117,6 +138,31 @@ mod tests {
|
||||
.join("examples/plc-demo")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn plc_findings_are_keyed_by_what_they_flag_not_their_line() {
|
||||
let source = std::fs::read_to_string(demo_dir().join("pump_station.st")).expect("demo");
|
||||
let keys = |text: &str| {
|
||||
let dir = std::env::temp_dir().join(format!("cs-plcfp-{}", uuid::Uuid::new_v4()));
|
||||
std::fs::create_dir_all(&dir).expect("mkdir");
|
||||
std::fs::write(dir.join("pump_station.st"), text).expect("write");
|
||||
let found = analyze_tree(&dir, "t");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
found
|
||||
};
|
||||
let before = keys(&source);
|
||||
let after = keys(&format!("(* a comment *)\n\n{source}"));
|
||||
assert!(!before.is_empty());
|
||||
let content = |f: &Vec<Finding>| -> Vec<Option<String>> {
|
||||
f.iter().map(|x| x.fingerprint_content.clone()).collect()
|
||||
};
|
||||
// The same content, though every line moved by two.
|
||||
assert_eq!(content(&before), content(&after));
|
||||
assert_ne!(before[0].line_number, after[0].line_number);
|
||||
// No two hits share their content: the same flaw twice is two findings.
|
||||
let distinct: HashSet<_> = content(&before).into_iter().collect();
|
||||
assert_eq!(distinct.len(), before.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scans_demo_project_end_to_end() {
|
||||
let findings = analyze_tree(&demo_dir(), "demo-target");
|
||||
|
||||
@@ -19,6 +19,7 @@ together on stage in one pass before the next prod promotion.
|
||||
| #461 | Transitive NuGet packages of `<PackageReference>` projects resolved from nuget.org |
|
||||
| #467 (follow-up) | npm and Maven licenses from their public registries |
|
||||
| #491 | Findings keyed by the code they flag, not its line number |
|
||||
| #491 (PLC) | PLC findings keyed the same way |
|
||||
|
||||
**Customer-visible on the next prod rescan** is marked **[visible]**: these
|
||||
change what a customer sees on their existing targets, without them doing
|
||||
@@ -354,6 +355,18 @@ target with history (findings, at least one marked by a person, issues).
|
||||
`CVE_ISSUE_EPSS_PERCENTILE` (default `0.95`, `off`) sets the EPSS step-up.
|
||||
- **[visible]** KEV and EPSS badges on the SBOM page; CVE issues for KEV
|
||||
CVEs whatever their severity.
|
||||
||||||| parent of 8c986d6 (feat(plc): PLC findings keyed by what they flag, saved through RunFindings (#491 follow-up))
|
||||
### 20. #491 follow-up: PLC findings
|
||||
|
||||
- **Do:** rescan an existing PLC target (e.g. `fixtures/nightly/plc/mixer_ld.xml`
|
||||
or the CODESYS sample) without changes; then upload the same ST/PLCopen
|
||||
export with a comment added at the top and rescan.
|
||||
- **Expect:** first rescan: no new findings, statuses kept (log
|
||||
`… now keyed by their content` with `adopted=…`); after the change: still
|
||||
no new findings, the lines follow (log `findings whose code moved …`).
|
||||
Control refs a newer mapping computes still reach earlier findings.
|
||||
- **[visible]** PLC targets no longer get a duplicate finding for every line
|
||||
shift in an export.
|
||||
|
||||
## After the pass
|
||||
|
||||
|
||||
Reference in new issue
Block a user