Skip to content

Commit 3aa4c7c

Browse files
committed
Security report: link each finding to its finding page
Each detailed finding in the Word and PDF security reports now carries a short identity line under its heading: the report's own finding number, the platform finding id, and a clickable link to that finding. The link host comes from the existing installation address setting, so a report opened by a reader goes straight to the finding it is about. Where no address is set the id still shows and the line simply carries no link, so nothing changes on an install that has not set one. A scanner group links to the first finding in the group; a review item links to the finding it was recorded as. The Word link is a real hyperlink built into the document, not a line of text that looks like one.
1 parent 035738e commit 3aa4c7c

4 files changed

Lines changed: 70 additions & 6 deletions

File tree

‎app/core/assessment.py‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,10 +1656,17 @@ def matrix(items: list) -> list:
16561656
return rows
16571657

16581658

1659-
def document_context(view_: dict) -> dict:
1660-
"""What the PDF template needs, from a stored report."""
1659+
def document_context(view_: dict, base_url: str = "") -> dict:
1660+
"""What the PDF template needs, from a stored report.
1661+
1662+
base_url is where this installation is reachable (the notify.base_url
1663+
setting). Given it, each item gets a clickable link to its own finding,
1664+
so a reader of the report can open the finding it is about. Without it,
1665+
the item still carries its platform finding id, just no link.
1666+
"""
16611667
result = view_["result"]
16621668
items = result["items"]
1669+
base = (base_url or "").strip().rstrip("/")
16631670
tally = {r: sum(1 for i in items if i["rating"] == r and not i.get("waived"))
16641671
for r in RATINGS}
16651672
waived = {r: sum(1 for i in items if i["rating"] == r and i.get("waived"))
@@ -1677,6 +1684,16 @@ def document_context(view_: dict) -> dict:
16771684
item.setdefault("cvss", "")
16781685
item.setdefault("cvss_score", None)
16791686
item["cvss_band"] = report_standards.cvss_band(item.get("cvss_score"))
1687+
# The platform finding this item is about, and a link to it. A review
1688+
# item names its recorded finding; a scanner group names its members,
1689+
# of which the first is the one the item leads with.
1690+
primary = (item.get("recorded") or {}).get("id")
1691+
if primary is None:
1692+
ids = item.get("finding_ids") or []
1693+
primary = ids[0] if ids else None
1694+
item["finding_id"] = primary
1695+
item["finding_link"] = (f"{base}/findings/{primary}"
1696+
if base and primary is not None else "")
16801697
retests = [i for i in items if i.get("retest") and i["rating"] != "Informational"]
16811698
return {"r": result, "items": items, "tally": tally, "waived_tally": waived,
16821699
"matrix": matrix(items), "impact_words": IMPACT_WORDS,

‎app/core/assessment_docx.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,48 @@ def p(self, text, bold=False, italic=False, size=None, color=None, after=None,
175175
para.alignment = align
176176
return para
177177

178+
def _link_run(self, para, text, url):
179+
"""A clickable external hyperlink inside an existing paragraph."""
180+
r_id = para.part.relate_to(
181+
url,
182+
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
183+
is_external=True)
184+
link = OxmlElement("w:hyperlink")
185+
link.set(qn("r:id"), r_id)
186+
run = OxmlElement("w:r")
187+
rpr = OxmlElement("w:rPr")
188+
color = OxmlElement("w:color")
189+
color.set(qn("w:val"), "0B5FA5")
190+
rpr.append(color)
191+
under = OxmlElement("w:u")
192+
under.set(qn("w:val"), "single")
193+
rpr.append(under)
194+
sz = OxmlElement("w:sz")
195+
sz.set(qn("w:val"), "18") # 9pt
196+
rpr.append(sz)
197+
run.append(rpr)
198+
t = OxmlElement("w:t")
199+
t.text = text
200+
run.append(t)
201+
link.append(run)
202+
para._p.append(link)
203+
return link
204+
205+
def identity(self, item):
206+
"""Finding number, platform id and a clickable link, under a heading."""
207+
para = self.doc.add_paragraph()
208+
para.paragraph_format.space_after = Pt(4)
209+
label = "Finding %s" % item["id"]
210+
if item.get("finding_id") is not None:
211+
label += " · ID %s" % item["finding_id"]
212+
run = para.add_run(label)
213+
run.font.size, run.font.color.rgb = Pt(9), GRAY
214+
if item.get("finding_link"):
215+
sep = para.add_run(" · ")
216+
sep.font.size, sep.font.color.rgb = Pt(9), GRAY
217+
self._link_run(para, "View this finding", item["finding_link"])
218+
return para
219+
178220
def labeled(self, label, text):
179221
para = self.doc.add_paragraph()
180222
r = para.add_run(label + " ")
@@ -418,6 +460,7 @@ def build(context: dict) -> bytes:
418460
D.h("5. Detailed Findings")
419461
for i in items:
420462
D.h(f"{i['id']} {i['title']}", 3)
463+
D.identity(i)
421464
info = [("Rating", i["rating"] + (" (waived)" if i.get("waived") else ""))]
422465
if i.get("location"):
423466
info.append(("Location", i["location"]))

‎app/web/routes_pages.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2905,7 +2905,7 @@ def finding_assessment_pdf(finding_id: int, request: Request,
29052905
Typeset on download rather than stored, so the logo, the name on the
29062906
cover and the footer are always the ones set today.
29072907
"""
2908-
from app.core import assessment, settings_store
2908+
from app.core import assessment, notify, settings_store
29092909

29102910
with session_scope() as db:
29112911
row = rbac.visible_finding(db, user, finding_id)
@@ -2916,7 +2916,7 @@ def finding_assessment_pdf(finding_id: int, request: Request,
29162916
if not (report and report["ready"]):
29172917
raise HTTPException(status_code=404, detail="No report yet.")
29182918
organization = settings_store.get(db, "report.organization_name", "") or ""
2919-
context = assessment.document_context(report)
2919+
context = assessment.document_context(report, base_url=notify.base_url(db))
29202920
context.update(
29212921
title="Security Assessment: %s" % row.repository,
29222922
organization_name=organization,
@@ -2960,7 +2960,7 @@ def finding_assessment_docx(finding_id: int, request: Request,
29602960
"""The same security report as a Word document, for the people who will
29612961
edit it, comment on it and pass it on. Built on download, like the PDF,
29622962
from the stored report: no model is asked anything here."""
2963-
from app.core import assessment, settings_store
2963+
from app.core import assessment, notify, settings_store
29642964

29652965
with session_scope() as db:
29662966
row = rbac.visible_finding(db, user, finding_id)
@@ -2970,7 +2970,7 @@ def finding_assessment_docx(finding_id: int, request: Request,
29702970
db, row.repository, row.branch or _default_branch(db, row.repository)))
29712971
if not (report and report["ready"]):
29722972
raise HTTPException(status_code=404, detail="No report yet.")
2973-
context = assessment.document_context(report)
2973+
context = assessment.document_context(report, base_url=notify.base_url(db))
29742974
context.update(
29752975
organization_name=settings_store.get(db, "report.organization_name", "") or "",
29762976
logo=(settings_store.get(db, "report.logo", "") or "").strip(),

‎app/web/templates/assessment_pdf.html‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@
9999
.finding { margin-bottom: 6mm; }
100100
.finding table { margin-bottom: 3mm; break-inside: avoid; }
101101
.finding h3 { break-after: avoid; }
102+
.finding .idline { font-size: 9pt; color: #555b66; margin: 0 0 2mm 0; break-after: avoid; }
103+
.finding .idline a { color: #0b5fa5; text-decoration: none; }
102104
table.heat { break-inside: avoid; }
103105
.finding .label { font-weight: 700; }
104106
</style>
@@ -247,6 +249,8 @@ <h2 class="new-page" style="margin-top:0">5. Detailed Findings</h2>
247249
{% for i in items %}
248250
<div class="finding">
249251
<h3>{{ i.id }} {{ i.title }}</h3>
252+
<p class="idline">Finding {{ i.id }}{% if i.finding_id is not none %} &middot; ID {{ i.finding_id }}{% endif %}
253+
{%- if i.finding_link %} &middot; <a href="{{ i.finding_link }}">View this finding</a>{% endif %}</p>
250254
<table>
251255
<tr><td class="k">Rating</td><td><span class="badge r-{{ i.rating | lower }}">{{ i.rating }}</span>
252256
{%- if i.waived %} <strong>Waived</strong>{% endif %}</td></tr>

0 commit comments

Comments
 (0)