|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Deterministic DSGAI HTML report renderer. |
| 3 | +
|
| 4 | +Renders `DSGAI-scan.json` to a self-contained HTML report — by code, from the |
| 5 | +checkpoint, so the structure is fixed and testable. The LLM contributes only |
| 6 | +designated prose (executive summary, remediation) injected as data via --prose. |
| 7 | +
|
| 8 | +Rendered with the standard library (styles live in templates/report.css and are |
| 9 | +inlined) — no Jinja2 runtime dependency, matching the scanner's zero-dependency |
| 10 | +ethos. STRICT mode renders stable file IDs (F07:12) and writes the ID->path map |
| 11 | +to DSGAI-filemap.json (gitignore it); --internal renders full paths. |
| 12 | +
|
| 13 | +Usage: |
| 14 | + python cli/dsgai_report.py DSGAI-scan.json [--out dsgai-reports/report.html] |
| 15 | + [--prose prose.json] [--filemap DSGAI-filemap.json] |
| 16 | +""" |
| 17 | +import argparse |
| 18 | +import html |
| 19 | +import json |
| 20 | +import os |
| 21 | +import sys |
| 22 | + |
| 23 | +HERE = os.path.dirname(os.path.abspath(__file__)) |
| 24 | +CSS = os.path.join(os.path.dirname(HERE), "templates", "report.css") |
| 25 | + |
| 26 | +CTRL_CLASS = {"FAIL": "fail", "WARN": "warn", "PASS": "pass", |
| 27 | + "NOT VALIDATED": "nv", "NOT APPLICABLE": "na", |
| 28 | + "VENDOR ATTESTATION REQUIRED": "vendor"} |
| 29 | +FIND_CLASS = {"fail": "fail", "warn": "warn", "pass_signal": "pass", |
| 30 | + "count": "nv", "info": "nv"} |
| 31 | +ATTRIBUTION = ( |
| 32 | + "Based on OWASP GenAI Data Security Risks and Mitigations 2026 (v1.0, March 2026) " |
| 33 | + "by the OWASP GenAI Data Security Initiative, led by Emmanuel Guilherme Junior. " |
| 34 | + "Report content CC BY-SA 4.0; scanner code Apache-2.0." |
| 35 | +) |
| 36 | + |
| 37 | + |
| 38 | +def esc(s): |
| 39 | + return html.escape(str(s), quote=True) |
| 40 | + |
| 41 | + |
| 42 | +def build_filemap(cp): |
| 43 | + """Assign F01.. IDs to distinct paths in first-appearance (sorted) order.""" |
| 44 | + paths = [] |
| 45 | + for f in cp["findings"] + cp.get("suppressed", []): |
| 46 | + if f["path"] not in paths: |
| 47 | + paths.append(f["path"]) |
| 48 | + paths.sort() |
| 49 | + return {p: f"F{i + 1:02d}" for i, p in enumerate(paths)} |
| 50 | + |
| 51 | + |
| 52 | +def loc(f, filemap, internal): |
| 53 | + p = f["path"] if internal else filemap.get(f["path"], f["path"]) |
| 54 | + redacted = " (value redacted)" if f.get("classification") == "value_bearing" else "" |
| 55 | + return f"{esc(p)}:{f['line']}{esc(redacted)}" |
| 56 | + |
| 57 | + |
| 58 | +def render(cp, prose, internal): |
| 59 | + css = open(CSS, encoding="utf-8").read() |
| 60 | + filemap = {} if internal else build_filemap(cp) |
| 61 | + mode = "INTERNAL" if internal else "STRICT" |
| 62 | + scope = cp.get("scan_scope", ".") |
| 63 | + incremental = scope.startswith("diff:") |
| 64 | + |
| 65 | + badges = [f"Framework {esc(cp['framework'])}", f"Engine {esc(cp['engine'])}", |
| 66 | + f"Ruleset {esc(cp['ruleset_version'])}", f"Scope {esc(scope)}", |
| 67 | + f"Scanned {esc(cp['scanned_at'])}"] |
| 68 | + mode_icon = "\U0001F513" if internal else "\U0001F6E1" |
| 69 | + mode_cls = "warnmode" if internal else "" |
| 70 | + b_html = f'<span class="badge {mode_cls}">{mode_icon} {mode}</span>' |
| 71 | + b_html += "".join(f'<span class="badge">{b}</span>' for b in badges) |
| 72 | + if incremental: |
| 73 | + b_html += '<span class="badge warnmode">INCREMENTAL — not a full assessment</span>' |
| 74 | + |
| 75 | + # Compliance dashboard |
| 76 | + dash = [] |
| 77 | + for c in sorted(cp["controls"]): |
| 78 | + st = cp["controls"][c] |
| 79 | + cls = CTRL_CLASS.get(st, "nv") |
| 80 | + dash.append(f'<div class="ctrl {cls}"><div class="cid">{esc(c)}</div>' |
| 81 | + f'<span class="st {cls}">{esc(st)}</span></div>') |
| 82 | + |
| 83 | + # Findings |
| 84 | + rows = [] |
| 85 | + for f in cp["findings"]: |
| 86 | + if f["status"] not in ("fail", "warn"): |
| 87 | + continue |
| 88 | + cls = FIND_CLASS.get(f["status"], "nv") |
| 89 | + tag = " · baselined" if f.get("baselined") else "" |
| 90 | + rows.append(f'<tr><td><span class="st {cls}">{esc(f["status"].upper())}</span></td>' |
| 91 | + f'<td>{esc(f["control"])}</td><td><code>{esc(f["rule_id"])}</code></td>' |
| 92 | + f'<td>{loc(f, filemap, internal)}{esc(tag)}</td></tr>') |
| 93 | + findings_html = ("<table><tr><th>Status</th><th>Control</th><th>Rule</th><th>Location</th></tr>" |
| 94 | + + "".join(rows) + "</table>") if rows else "<p>No FAIL/WARN findings.</p>" |
| 95 | + |
| 96 | + # CVEs |
| 97 | + cve_rows = [] |
| 98 | + for c in cp.get("cves", []): |
| 99 | + cvss = c.get("cvss") |
| 100 | + cve_rows.append(f'<tr class="cve {esc(c["status"])}"><td>{esc(c["status"])}</td>' |
| 101 | + f'<td><code>{esc(c["package"])}=={esc(c["version"])}</code></td>' |
| 102 | + f'<td>{esc(c["id"])}</td><td>{esc(cvss if cvss is not None else "—")}</td>' |
| 103 | + f'<td>{esc(c.get("summary", ""))}</td></tr>') |
| 104 | + cve_html = ("<table><tr><th>Status</th><th>Package</th><th>Advisory</th><th>CVSS</th><th>Summary</th></tr>" |
| 105 | + + "".join(cve_rows) + "</table>") if cve_rows else "<p>No advisories.</p>" |
| 106 | + |
| 107 | + # Suppressed |
| 108 | + sup_rows = [] |
| 109 | + for f in cp.get("suppressed", []): |
| 110 | + sup_rows.append(f'<tr class="suppressed"><td><code>{esc(f["rule_id"])}</code></td>' |
| 111 | + f'<td>{loc(f, filemap, internal)}</td>' |
| 112 | + f'<td>{esc(f.get("suppressed_reason", ""))}</td></tr>') |
| 113 | + sup_html = ("<h2>Suppressed</h2><table><tr><th>Rule</th><th>Location</th><th>Reason</th></tr>" |
| 114 | + + "".join(sup_rows) + "</table>") if sup_rows else "" |
| 115 | + |
| 116 | + # Filemap section (strict only, and only listed here for the reader — the |
| 117 | + # ID->path map is written to DSGAI-filemap.json, never the shareable report). |
| 118 | + exec_summary = esc(prose.get("executive_summary", |
| 119 | + "Automated DSGAI compliance scan. Review FAIL findings first.")) |
| 120 | + remediation = esc(prose.get("remediation", "See per-control remediation guidance.")) |
| 121 | + |
| 122 | + return f"""<!DOCTYPE html> |
| 123 | +<html lang="en"><head><meta charset="utf-8"> |
| 124 | +<meta name="viewport" content="width=device-width, initial-scale=1"> |
| 125 | +<title>DSGAI Compliance Report</title> |
| 126 | +<style>{css}</style></head> |
| 127 | +<body><div class="wrap"> |
| 128 | +<header class="rpt"><h1>OWASP DSGAI 2026 Compliance Report</h1> |
| 129 | +<div class="badges">{b_html}</div></header> |
| 130 | +
|
| 131 | +<div class="residual"><strong>Residual risk:</strong> STRICT mode is designed to minimize |
| 132 | +disclosure (file IDs + line numbers only; value-bearing matches never shown). It is not a |
| 133 | +guarantee the report is public-safe — the existence and location of failing controls is |
| 134 | +itself information. Handle it like any security assessment.</div> |
| 135 | +
|
| 136 | +<h2>Executive summary</h2><p>{exec_summary}</p> |
| 137 | +<h2>Compliance dashboard</h2><div class="dash">{''.join(dash)}</div> |
| 138 | +<h2>Findings</h2>{findings_html} |
| 139 | +<h2>Remediation</h2><p>{remediation}</p> |
| 140 | +<h2>CVE advisories</h2>{cve_html} |
| 141 | +{sup_html} |
| 142 | +<footer class="rpt">{esc(ATTRIBUTION)}</footer> |
| 143 | +</div></body></html> |
| 144 | +""" |
| 145 | + |
| 146 | + |
| 147 | +def main(argv): |
| 148 | + ap = argparse.ArgumentParser(prog="dsgai_report") |
| 149 | + ap.add_argument("checkpoint") |
| 150 | + ap.add_argument("--out", default=None) |
| 151 | + ap.add_argument("--prose", default=None, help="JSON with executive_summary/remediation") |
| 152 | + ap.add_argument("--filemap", default="DSGAI-filemap.json") |
| 153 | + ap.add_argument("--internal", action="store_true") |
| 154 | + args = ap.parse_args(argv) |
| 155 | + |
| 156 | + cp = json.load(open(args.checkpoint, encoding="utf-8")) |
| 157 | + prose = json.load(open(args.prose, encoding="utf-8")) if args.prose else {} |
| 158 | + html_out = render(cp, prose, args.internal) |
| 159 | + |
| 160 | + out = args.out or os.path.join("dsgai-reports", "DSGAI-report.html") |
| 161 | + os.makedirs(os.path.dirname(out) or ".", exist_ok=True) |
| 162 | + with open(out, "w", encoding="utf-8", newline="\n") as fh: |
| 163 | + fh.write(html_out) |
| 164 | + if not args.internal: |
| 165 | + fmap = build_filemap(cp) |
| 166 | + with open(args.filemap, "w", encoding="utf-8", newline="\n") as fh: |
| 167 | + json.dump({v: k for k, v in fmap.items()}, fh, indent=2, sort_keys=True) |
| 168 | + print(f"wrote {out}") |
| 169 | + return 0 |
| 170 | + |
| 171 | + |
| 172 | +if __name__ == "__main__": |
| 173 | + sys.exit(main(sys.argv[1:])) |
0 commit comments