1212import os
1313import re
1414import subprocess
15+ import sys
1516from datetime import datetime , timedelta , timezone
1617
1718from statelib import (
2122 review_after_passed ,
2223 override_review_due ,
2324 age_drops ,
25+ RESOLVED_STATUSES ,
26+ UNFIXED_AGE_THRESHOLD_DAYS ,
2427)
2528
2629
3134PENDING_FILE = os .path .join (STATE_DIR , "pending-weekly.json" )
3235TRIAGE_FILE = os .path .join (STATE_DIR , "triage-overrides.json" )
3336
34- # Statuses that mean "already handled, skip in new reports"
35- RESOLVED_STATUSES = { "false-positive" , "accepted-risk" , "fixed" }
37+ # RESOLVED_STATUSES and UNFIXED_AGE_THRESHOLD_DAYS are imported from statelib so the
38+ # filter functions there and classify_filter here compare against one definition.
3639
3740# Build/dev image patterns — CVEs in these are informational only
3841DEV_IMAGE_PATTERNS = [
5558 "e2e/" ,
5659]
5760
58- # How old an unfixed CVE must be (days) before we auto-demote it
59- UNFIXED_AGE_THRESHOLD_DAYS = 365
60-
6161# Repository where issues are created (this repo)
6262ISSUES_REPO = os .environ .get ("ISSUES_REPO" , "cozystack/security-scanner" )
6363
@@ -169,9 +169,22 @@ def classify_filter(vuln, triage_overrides):
169169 # branch, which is what made the check unreachable before.
170170 entry = triage_overrides .get (cve_id )
171171 if entry and entry .get ("status" ) in RESOLVED_STATUSES :
172- if review_after_passed (entry ):
173- return False , None
174- return True , f"triaged as { entry ['status' ]} "
172+ if not review_after_passed (entry ):
173+ return True , f"triaged as { entry ['status' ]} "
174+ # Due for re-review. Stamp the entry in this same run so the condition does
175+ # not stay true forever: move it to the non-resolved `needs-review` status
176+ # and clear the date. Without this the finding re-files an issue and a draft
177+ # advisory on every six-hourly tick, because nothing else writes the date
178+ # back (sync_issues.py only rewrites on a status change). It surfaces ONCE
179+ # for a maintainer; next run its status is no longer resolved, so the
180+ # already-reported guard skips it until the re-triage lands.
181+ entry ["status" ] = "needs-review"
182+ entry ["review_after" ] = None
183+ entry ["decided_by" ] = "pipeline-auto"
184+ # A due override still must not resurrect a dev/build-only finding.
185+ if is_dev_only (vuln ):
186+ return True , "dev/build dependency only"
187+ return False , None
175188
176189 # Dev/build-only image or test component
177190 if is_dev_only (vuln ):
@@ -500,13 +513,16 @@ def process_critical(scan_results, reported, triage_overrides):
500513 issue_url = create_issue (vuln , report_path )
501514 ghsa_url = create_ghsa_draft (vuln )
502515
503- # If both the issue and the advisory failed to create, do NOT record the
504- # CVE as reported — otherwise the skip check passes over it forever, and a
505- # CRITICAL is left with no issue, no advisory and no trace. Leave it out so
506- # the next run retries it.
507- if not issue_url and not ghsa_url :
508- print (f" WARN: { cve_id } — both issue and advisory creation failed; "
509- f"not recording as reported so the next run retries it" )
516+ # If EITHER the issue or the advisory failed to create, do NOT record the
517+ # CVE as reported. A partial success (issue OK, advisory None, or the
518+ # reverse) is recorded with an empty URL and the skip check then passes over
519+ # it forever, so the missing half is never created and the CRITICAL has no
520+ # venue to be triaged in. Leave it out so the next run retries the missing
521+ # half.
522+ if not issue_url or not ghsa_url :
523+ print (f" WARN: { cve_id } — issue/advisory creation incomplete "
524+ f"(issue={ 'ok' if issue_url else 'FAILED' } , advisory={ 'ok' if ghsa_url else 'FAILED' } ); "
525+ f"not recording as reported so the next run retries the missing half" )
510526 continue
511527
512528 affected = vuln .get ("affected_repos" , [])
@@ -627,6 +643,15 @@ def main():
627643 with open (args .scan_results ) as f :
628644 scan_results = json .load (f )
629645
646+ # scan.sh records how many targets it could not scan rather than aborting, so
647+ # the report is still produced from what succeeded. Surface it, and make the
648+ # run go red at the very end — after the reports and state are written — so an
649+ # incomplete run is visible without suppressing what was collected.
650+ scan_errors = scan_results .get ("scan_errors" , 0 )
651+ if scan_errors :
652+ print (f"WARNING: scan was INCOMPLETE — { scan_errors } target(s) could not be "
653+ f"scanned; reports below cover only the targets that succeeded." )
654+
630655 triaged_count = len ([v for v in triage_overrides .values () if v .get ("status" ) in RESOLVED_STATUSES ])
631656 print (f"Scan date: { scan_results .get ('scan_date' , 'unknown' )} " )
632657 print (f"Total CVEs in scan: { scan_results .get ('total_vulnerabilities' , 0 )} " )
@@ -649,6 +674,13 @@ def main():
649674 print (f"\n State saved. Reported: { len (reported )} , Pending: { len (pending )} , "
650675 f"Triage overrides: { len (triage_overrides )} " )
651676
677+ # Now that the reports and state are written, fail the run if the scan was
678+ # incomplete, so it goes red without having suppressed what was collected.
679+ if scan_errors :
680+ print (f"\n FAILING: { scan_errors } scan target(s) could not be scanned — this "
681+ f"run's aggregate is incomplete." )
682+ sys .exit (1 )
683+
652684
653685if __name__ == "__main__" :
654686 main ()
0 commit comments