1212import os
1313import re
1414import subprocess
15- from datetime import datetime , timezone
15+ from datetime import datetime , timedelta , timezone
16+
17+ from statelib import load_json , save_json
1618
1719
1820REPO_ROOT = os .path .join (os .path .dirname (__file__ ), ".." )
@@ -115,33 +117,6 @@ def notify_slack(vulns):
115117 print (f" WARN: Slack notification failed: { e } " )
116118
117119
118- def load_json (path , default ):
119- # A missing file is a legitimate first run -> default. A present-but-corrupt
120- # file must NOT be silently treated as empty and then overwritten (that erases
121- # the historic record — issue/advisory URLs, triage state). Fail loudly instead.
122- if not os .path .exists (path ):
123- return default
124- try :
125- with open (path ) as f :
126- return json .load (f )
127- except json .JSONDecodeError as e :
128- raise SystemExit (
129- f"FATAL: { path } exists but is not valid JSON ({ e } ). Refusing to "
130- f"continue and overwrite it with an empty default — restore it from "
131- f"git history (it may be a truncated write) and re-run."
132- )
133-
134-
135- def save_json (path , data ):
136- # Atomic write: a run killed mid-dump must not leave a truncated file that the
137- # next run reads as empty. Write to a temp file, then rename over the target.
138- os .makedirs (os .path .dirname (path ), exist_ok = True )
139- tmp = f"{ path } .tmp"
140- with open (tmp , "w" ) as f :
141- json .dump (data , f , indent = 2 )
142- os .replace (tmp , path )
143-
144-
145120def cve_age_days (cve_id ):
146121 """Estimate CVE age from its ID (CVE-YYYY-NNNNN)."""
147122 match = re .match (r"CVE-(\d{4})-" , cve_id )
@@ -178,6 +153,20 @@ def is_dev_only(vuln):
178153 return True
179154
180155
156+ def _review_after_passed (entry ):
157+ """True if the override carries a review_after date that is now due/past."""
158+ raw = entry .get ("review_after" )
159+ if not raw :
160+ return False
161+ try :
162+ due = datetime .fromisoformat (str (raw ).replace ("Z" , "+00:00" ))
163+ except ValueError :
164+ return False
165+ if due .tzinfo is None :
166+ due = due .replace (tzinfo = timezone .utc )
167+ return datetime .now (timezone .utc ) >= due
168+
169+
181170def classify_filter (vuln , triage_overrides ):
182171 """Classify a CVE for filtering. Returns (skip, reason) or (False, None).
183172
@@ -191,10 +180,14 @@ def classify_filter(vuln, triage_overrides):
191180 if severity == "UNKNOWN" :
192181 return True , "unknown severity"
193182
194- # Already triaged as resolved
183+ # Already triaged as resolved — UNLESS its review_after date has passed. A
184+ # suppression with an expired review date has done its job; keeping it would
185+ # dismiss the finding forever, which is exactly what review_after exists to
186+ # prevent. Let it surface again for re-review.
195187 if cve_id in triage_overrides :
196- status = triage_overrides [cve_id ].get ("status" , "" )
197- if status in RESOLVED_STATUSES :
188+ entry = triage_overrides [cve_id ]
189+ status = entry .get ("status" , "" )
190+ if status in RESOLVED_STATUSES and not _review_after_passed (entry ):
198191 return True , f"triaged as { status } "
199192
200193 # Dev/build-only image or test component
@@ -207,6 +200,21 @@ def classify_filter(vuln, triage_overrides):
207200 if not vuln .get ("fixed_version" ):
208201 age = cve_age_days (cve_id )
209202 if age > UNFIXED_AGE_THRESHOLD_DAYS and severity not in ("CRITICAL" , "HIGH" ):
203+ # PERSIST the auto-dismissal as accepted-risk with a review_after date.
204+ # Returning skip alone records nothing, so the finding is re-filtered on
205+ # every run forever and never reaches the 90-day re-review the status
206+ # model requires. Writing it makes the drop auditable and time-boxed.
207+ if cve_id not in triage_overrides :
208+ now = datetime .now (timezone .utc )
209+ triage_overrides [cve_id ] = {
210+ "status" : "accepted-risk" ,
211+ "decided_by" : "pipeline-auto" ,
212+ "decided_at" : now .date ().isoformat (),
213+ "review_after" : (now + timedelta (days = 90 )).date ().isoformat (),
214+ "reason" : (f"[{ vuln .get ('package' , '' )} ] Auto-demoted: { severity } finding with no "
215+ f"upstream fix, CVE approximately { age } days old (older than the "
216+ f"{ UNFIXED_AGE_THRESHOLD_DAYS } -day threshold). Re-review on the date above." ),
217+ }
210218 return True , f"unfixed upstream ({ age } days old, { severity or 'no severity' } )"
211219
212220 return False , None
@@ -587,6 +595,14 @@ def send_weekly_report(reported, pending):
587595 print (f"Creating issues for { len (high_vulns )} HIGH CVEs..." )
588596 for vuln in high_vulns :
589597 issue_url = create_issue (vuln , report_path )
598+ # Same guard as the CRITICAL path: if the issue failed to create, do NOT
599+ # record the CVE as reported. Recording it with an empty issue_url makes the
600+ # skip check pass over it on every later run, so the 3-business-day triage
601+ # clock never starts. Leave it out so the next run retries it.
602+ if not issue_url :
603+ print (f" WARN: { vuln ['cve_id' ]} — issue creation failed; not recording "
604+ f"as reported so the next run retries it" )
605+ continue
590606 reported [vuln ["cve_id" ]] = {
591607 "severity" : vuln ["severity" ],
592608 "reported_at" : datetime .now (timezone .utc ).isoformat (),
@@ -640,7 +656,11 @@ def main():
640656
641657 save_json (REPORTED_FILE , reported )
642658 save_json (PENDING_FILE , pending )
643- print (f"\n State saved. Reported: { len (reported )} , Pending: { len (pending )} " )
659+ # triage_overrides may have gained pipeline-auto accepted-risk entries from the
660+ # age filter; persist them so the dismissals are recorded and re-reviewed.
661+ save_json (TRIAGE_FILE , triage_overrides )
662+ print (f"\n State saved. Reported: { len (reported )} , Pending: { len (pending )} , "
663+ f"Triage overrides: { len (triage_overrides )} " )
644664
645665
646666if __name__ == "__main__" :
0 commit comments