Skip to content

Commit 7465482

Browse files
committed
docs(security): re-sync published pipeline scripts with the hardening fixes
Pull the fixes from cozystack/security-scanner#1004 into the published copies so the documented method matches the fixed source: monthly.py disclosure gate (severity counts for unreleased findings), scan.sh fail-loud on Trivy errors, report.py atomic/fail-loud state IO, retry-on-both-failed, and severity floor on the unfixed-age drop. Addresses @IvanHunters review on #4224. Signed-off-by: Timur Tukaev <[email protected]>
1 parent b63535a commit 7465482

3 files changed

Lines changed: 68 additions & 44 deletions

File tree

‎docs/security/pipeline/scripts/monthly.py‎

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -137,30 +137,23 @@ def main():
137137
fixed_table = "| — | — | — | — | — |"
138138

139139
# In-progress table
140-
if in_progress:
141-
ip_rows = []
142-
for v in sorted(in_progress, key=lambda x: {"CRITICAL": 0, "HIGH": 1}.get(x["severity"], 2)):
143-
pr_link = f"[PR]({v['fix_pr']})" if v.get("fix_pr") else "—"
144-
ip_rows.append(
145-
f"| [{v['cve_id']}](https://nvd.nist.gov/vuln/detail/{v['cve_id']}) "
146-
f"| {v['severity']} | `{v['package']}` | {pr_link} |"
147-
)
148-
ip_table = "\n".join(ip_rows)
149-
else:
150-
ip_table = "| — | — | — | — |"
151-
152-
# Accepted risk table
153-
if accepted_risk:
154-
ar_rows = []
155-
for v in sorted(accepted_risk, key=lambda x: x["cve_id"]):
156-
reason = v.get("reason", "")[:80]
157-
ar_rows.append(
158-
f"| [{v['cve_id']}](https://nvd.nist.gov/vuln/detail/{v['cve_id']}) "
159-
f"| {v['severity']} | `{v['package']}` | {reason} |"
160-
)
161-
ar_table = "\n".join(ar_rows)
162-
else:
163-
ar_table = "| — | — | — | — |"
140+
# Disclosure rule: in-progress and accepted-risk findings have NO released fix.
141+
# Naming them (CVE id + package) in a public report, crossed with the project's
142+
# public, version-pinned component list, locates unpatched exposure in a running
143+
# deployment. Publish severity COUNTS only; per-finding detail stays private.
144+
def sev_counts(rows):
145+
c = Counter(v.get("severity", "UNKNOWN") for v in rows)
146+
parts = [f"{c[s]} {s}" for s in ("CRITICAL", "HIGH", "MEDIUM", "LOW") if c.get(s)]
147+
return ", ".join(parts) if parts else "none"
148+
149+
in_progress_summary = (
150+
f"{len(in_progress)} finding(s) in progress ({sev_counts(in_progress)}). "
151+
f"Individual identifiers are withheld until a fix is released."
152+
)
153+
accepted_risk_summary = (
154+
f"{len(accepted_risk)} accepted-risk finding(s) ({sev_counts(accepted_risk)}). "
155+
f"Individual identifiers are withheld; details are kept in the private triage state."
156+
)
164157

165158
content = f"""# Cozystack Security Summary — {month_label}
166159
@@ -188,15 +181,11 @@ def main():
188181
189182
## In Progress
190183
191-
| CVE | Severity | Package | Fix PR |
192-
|-----|----------|---------|--------|
193-
{ip_table}
184+
{in_progress_summary}
194185
195186
## Accepted Risks
196187
197-
| CVE | Severity | Package | Reason |
198-
|-----|----------|---------|--------|
199-
{ar_table}
188+
{accepted_risk_summary}
200189
201190
## How to Report
202191
@@ -220,8 +209,8 @@ def main():
220209
"generated_at": now.strftime("%Y-%m-%dT%H:%M:%SZ"),
221210
"new_count": len(new_this_month),
222211
"fixed": [{"cve_id": v["cve_id"], "severity": v["severity"], "package": v["package"], "fixed_version": v.get("fixed_version", "")} for v in fixed_this_month],
223-
"in_progress": [{"cve_id": v["cve_id"], "severity": v["severity"], "package": v["package"]} for v in in_progress],
224-
"accepted_risk": [{"cve_id": v["cve_id"], "severity": v["severity"], "package": v["package"], "reason": v.get("reason", "")[:100]} for v in accepted_risk],
212+
"in_progress_by_severity": dict(Counter(v.get("severity", "UNKNOWN") for v in in_progress)),
213+
"accepted_risk_by_severity": dict(Counter(v.get("severity", "UNKNOWN") for v in accepted_risk)),
225214
"stats": {
226215
"total_tracked": total_tracked,
227216
"total_triaged": total_triaged,

‎docs/security/pipeline/scripts/report.py‎

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,30 @@ def notify_slack(vulns):
116116

117117

118118
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
119124
try:
120125
with open(path) as f:
121126
return json.load(f)
122-
except (FileNotFoundError, json.JSONDecodeError):
123-
return default
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+
)
124133

125134

126135
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.
127138
os.makedirs(os.path.dirname(path), exist_ok=True)
128-
with open(path, "w") as f:
139+
tmp = f"{path}.tmp"
140+
with open(tmp, "w") as f:
129141
json.dump(data, f, indent=2)
142+
os.replace(tmp, path)
130143

131144

132145
def cve_age_days(cve_id):
@@ -188,11 +201,13 @@ def classify_filter(vuln, triage_overrides):
188201
if is_dev_only(vuln):
189202
return True, "dev/build dependency only"
190203

191-
# Unfixed upstream older than threshold
204+
# Unfixed upstream older than threshold. Only auto-demote MEDIUM/LOW: a
205+
# CRITICAL or HIGH with no fix is exactly what a human must see and decide on
206+
# (accepted-risk with a review date), not something to silently drop by age.
192207
if not vuln.get("fixed_version"):
193208
age = cve_age_days(cve_id)
194-
if age > UNFIXED_AGE_THRESHOLD_DAYS:
195-
return True, f"unfixed upstream ({age} days old)"
209+
if age > UNFIXED_AGE_THRESHOLD_DAYS and severity not in ("CRITICAL", "HIGH"):
210+
return True, f"unfixed upstream ({age} days old, {severity or 'no severity'})"
196211

197212
return False, None
198213

@@ -492,6 +507,15 @@ def process_critical(scan_results, reported, triage_overrides):
492507
issue_url = create_issue(vuln, report_path)
493508
ghsa_url = create_ghsa_draft(vuln)
494509

510+
# If both the issue and the advisory failed to create, do NOT record the
511+
# CVE as reported — otherwise the skip check passes over it forever, and a
512+
# CRITICAL is left with no issue, no advisory and no trace. Leave it out so
513+
# the next run retries it.
514+
if not issue_url and not ghsa_url:
515+
print(f" WARN: {cve_id} — both issue and advisory creation failed; "
516+
f"not recording as reported so the next run retries it")
517+
continue
518+
495519
affected = vuln.get("affected_repos", [])
496520
target_repos = sorted(set(r.split(":")[0] for r in affected))
497521

‎docs/security/pipeline/scripts/scan.sh‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ if ! command -v trivy &>/dev/null; then
2424
fi
2525

2626
echo "==> Starting vulnerability scans..."
27+
SCAN_FAILURES=0
2728

2829
# --- Phase 1: Scan repos (go.mod, Dockerfiles, lock files) ---
2930

@@ -53,9 +54,12 @@ for repo_name in $REPO_NAMES; do
5354
--severity CRITICAL,HIGH,MEDIUM,LOW \
5455
--scanners vuln \
5556
--quiet \
56-
> "$result_file" 2>/dev/null || {
57-
echo " WARN: trivy fs failed for $repo_name"
58-
echo '{"Results": []}' > "$result_file"
57+
> "$result_file" 2>"$result_file.err" || {
58+
echo " ERROR: trivy fs failed for $repo_name (see $result_file.err)" >&2
59+
# Mark the target as ERRORED, not clean — a failed scan must not be merged
60+
# into the aggregate as "no findings".
61+
echo '{"__scan_error__": true, "Results": []}' > "$result_file"
62+
SCAN_FAILURES=$((SCAN_FAILURES + 1))
5963
}
6064
done
6165

@@ -81,13 +85,20 @@ if [ -f "$IMAGES_FILE" ]; then
8185
--scanners vuln \
8286
--quiet \
8387
--timeout 5m \
84-
> "$result_file" 2>/dev/null || {
85-
echo " WARN: trivy image failed for $image"
86-
echo '{"Results": []}' > "$result_file"
88+
> "$result_file" 2>"$result_file.err" || {
89+
echo " ERROR: trivy image failed for $image (see $result_file.err)" >&2
90+
echo '{"__scan_error__": true, "Results": []}' > "$result_file"
91+
SCAN_FAILURES=$((SCAN_FAILURES + 1))
8792
}
8893
done < "$IMAGES_FILE"
8994
fi
9095

96+
if [ "$SCAN_FAILURES" -gt 0 ]; then
97+
echo "ERROR: $SCAN_FAILURES scan target(s) failed — the aggregate is INCOMPLETE, not clean." >&2
98+
echo " Investigate the *.err files in $RESULTS_DIR before trusting this run." >&2
99+
exit 1
100+
fi
101+
91102
# --- Phase 3: Merge all results into one file ---
92103

93104
echo "==> Merging scan results..."

0 commit comments

Comments
 (0)