Skip to content

Commit f302c2f

Browse files
committed
Link the GHSA and CVE ID on each PR/MR comment alert
Each security comment row showed one advisory ID, the GHSA when the alert had one and the CVE otherwise, as plain text. Alerts from the API usually carry both, so the CVE was dropped from the row, and developers who only see the PR comment had no link to the advisory. Rows now list every advisory ID on the alert, GHSA first, each linked to its public page (github.com/advisories for GHSA, nvd.nist.gov for CVE), the same two sites the GitLab security report already links. Only IDs in the canonical GHSA or CVE shape are linked; anything else renders as escaped text. IDs keep the API's spelling instead of being upper-cased. Issue.advisory_ids() is new and used only for display. advisory_id() and consolidation_key() are unchanged, so de-duplication, SARIF rule IDs and GitLab finding IDs stay the same. Shares the 2.16.0 version set in #478.
1 parent 4927333 commit f302c2f

10 files changed

Lines changed: 189 additions & 14 deletions

‎CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## 2.16.0
4+
5+
### Changed
6+
7+
- PR and MR security comments link available GHSA and CVE IDs to their public
8+
advisory pages.
9+
310
## 2.15.7
411

512
### Changed: bump pinned Coana CLI to 15.15.9

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "hatchling.build"
66

77
[project]
88
name = "socketsecurity"
9-
version = "2.15.7"
9+
version = "2.16.0"
1010
requires-python = ">= 3.11"
1111
license = {"file" = "LICENSE"}
1212
dependencies = [

‎socketsecurity/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
__author__ = 'socket.dev'
2-
__version__ = '2.15.7'
2+
__version__ = '2.16.0'
33
USER_AGENT_NAME = 'socket-python-cli'
44
USER_AGENT = f'{USER_AGENT_NAME}/{__version__}'

‎socketsecurity/core/classes.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,32 @@ def advisory_id(self) -> str:
382382
return value.strip().upper()
383383
return ""
384384

385+
def advisory_ids(self) -> List[str]:
386+
"""Every distinct GHSA and CVE identifier on the alert, GHSA first.
387+
388+
IDs keep the spelling the API sent. advisory_id() still returns a single
389+
ID because consolidation_key() depends on it; this list is for display,
390+
where an alert that carries both a GHSA and a CVE should show both.
391+
"""
392+
props = getattr(self, "props", None) or {}
393+
ids: List[str] = []
394+
seen = set()
395+
for prop_name in ADVISORY_ID_PROPS:
396+
values = props.get(prop_name)
397+
if isinstance(values, str):
398+
values = [values]
399+
elif not isinstance(values, (list, tuple)):
400+
continue
401+
for value in values:
402+
if not isinstance(value, str) or not value.strip():
403+
continue
404+
value = value.strip()
405+
if value.upper() in seen:
406+
continue
407+
seen.add(value.upper())
408+
ids.append(value)
409+
return ids
410+
385411
def consolidation_key(self) -> Tuple[str, str, str, str]:
386412
"""Identity used to collapse repeat sightings of one finding into one alert.
387413

‎socketsecurity/core/messages.py‎

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313

1414
log = logging.getLogger("socketcli")
1515

16+
# Public advisory pages for the IDs shown in PR/MR comments, keyed by the ID's
17+
# canonical shape. The GitLab security report links the same two sites.
18+
ADVISORY_LINK_BASES = (
19+
(re.compile(r"^GHSA(-[0-9a-z]{4}){3}$", re.IGNORECASE), "https://github.com/advisories/"),
20+
(re.compile(r"^CVE-\d{4}-\d{4,}$", re.IGNORECASE), "https://nvd.nist.gov/vuln/detail/"),
21+
)
22+
1623
class Messages:
1724

1825
@staticmethod
@@ -1048,7 +1055,6 @@ def security_alert_row(alert: Issue, diff: Diff, config, show_ignore: bool) -> s
10481055
package = f"{alert.package_name_with_namespace()}@{alert.pkg_version}"
10491056
pkg_label = Messages.html_text(package)
10501057
pkg_marker = Messages.alert_package_marker(alert, package)
1051-
alert_title = Messages.alert_title_with_advisory(alert)
10521058
ignore_html = (
10531059
f"<p><em>Mark as acceptable risk:</em> To ignore this alert only in this pull request, reply with:<br/>"
10541060
f"<code>@SocketSecurity ignore {Messages.html_text(alert.pkg_type)}/{pkg_label}</code><br/>"
@@ -1064,7 +1070,7 @@ def security_alert_row(alert: Issue, diff: Diff, config, show_ignore: bool) -> s
10641070
</td>
10651071
<td>
10661072
<details {details_open}>
1067-
<summary>{pkg_label} - {Messages.html_text(alert_title)}</summary>
1073+
<summary>{pkg_label} - {Messages.alert_title_html(alert)}</summary>
10681074
<p><strong>Note:</strong> {Messages.html_text(alert.description)}</p>
10691075
{patched_version_html}
10701076
<p><strong>Source:</strong> <a href="{Messages.html_attr(manifest_url)}">Manifest File</a></p>
@@ -1082,19 +1088,56 @@ def security_alert_row(alert: Issue, diff: Diff, config, show_ignore: bool) -> s
10821088
<!-- end-socket-alert-{pkg_marker} -->
10831089
"""
10841090

1091+
@staticmethod
1092+
def advisory_ids_for_title(alert: Issue) -> list:
1093+
"""The alert's GHSA and CVE IDs, GHSA first, minus any its title already names."""
1094+
title = (getattr(alert, "title", "") or "").upper()
1095+
ids = alert.advisory_ids() if hasattr(alert, "advisory_ids") else []
1096+
return [advisory for advisory in ids if advisory.upper() not in title]
1097+
10851098
@staticmethod
10861099
def alert_title_with_advisory(alert: Issue) -> str:
1087-
"""The alert title, followed by its GHSA or CVE ID when it has one.
1100+
"""The alert title, followed by its GHSA and CVE IDs when it has them.
10881101
10891102
Every CVE of one severity shares a title ("Critical CVE"), so without the
10901103
ID two advisories on the same package render as identical rows.
10911104
"""
10921105
title = getattr(alert, "title", "") or ""
1093-
advisory = alert.advisory_id() if hasattr(alert, "advisory_id") else ""
1094-
if advisory and advisory not in title.upper():
1095-
return f"{title} ({advisory})"
1106+
ids = Messages.advisory_ids_for_title(alert)
1107+
if ids:
1108+
return f"{title} ({', '.join(ids)})"
10961109
return title
10971110

1111+
@staticmethod
1112+
def advisory_link_html(advisory: str) -> str:
1113+
"""An advisory ID linked to its public advisory page.
1114+
1115+
Only IDs in the canonical GHSA or CVE shape are linked, so an unexpected
1116+
value renders as escaped text instead of an arbitrary URL path.
1117+
"""
1118+
for pattern, base_url in ADVISORY_LINK_BASES:
1119+
if pattern.match(advisory):
1120+
return (
1121+
f'<a href="{Messages.html_attr(base_url + advisory)}">'
1122+
f"{Messages.html_text(advisory)}</a>"
1123+
)
1124+
return Messages.html_text(advisory)
1125+
1126+
@staticmethod
1127+
def alert_title_html(alert: Issue) -> str:
1128+
"""The PR/MR comment row title: escaped title, then each advisory ID linked.
1129+
1130+
Developers who only see the PR comment can open the GHSA on GitHub and the
1131+
CVE on NVD without a Socket login. Alerts usually carry both IDs, and
1132+
either can be published first, so the row shows every ID the alert has.
1133+
"""
1134+
title = Messages.html_text(getattr(alert, "title", "") or "")
1135+
ids = Messages.advisory_ids_for_title(alert)
1136+
if not ids:
1137+
return title
1138+
links = ", ".join(Messages.advisory_link_html(advisory) for advisory in ids)
1139+
return f"{title} ({links})"
1140+
10981141
@staticmethod
10991142
def security_comment_template(diff: Diff, config=None) -> str:
11001143
"""Render the PR/MR security comment within the provider's size limit."""

‎tests/core/test_alert_consolidation.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ def test_pr_comment_rows_name_the_advisory():
133133

134134
comment = Messages.security_comment_template(diff)
135135

136-
assert "Critical CVE (GHSA-AAAA-AAAA-AAAA)" in comment
137-
assert "Critical CVE (GHSA-BBBB-BBBB-BBBB)" in comment
136+
assert 'Critical CVE (<a href="https://github.com/advisories/GHSA-aaaa-aaaa-aaaa">GHSA-aaaa-aaaa-aaaa</a>)' in comment
137+
assert 'Critical CVE (<a href="https://github.com/advisories/GHSA-bbbb-bbbb-bbbb">GHSA-bbbb-bbbb-bbbb</a>)' in comment
138138

139139

140140
def test_title_already_naming_the_advisory_is_left_alone():

‎tests/core/test_existing_alerts.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ def test_every_same_severity_advisory_on_an_unchanged_package_is_kept(core, capl
454454
comment = Messages.security_comment_template(diff, _config())
455455
assert "Existing alerts (6)" in comment
456456
for ghsa, _ in HANDLEBARS_CRITICAL_ADVISORIES:
457-
assert ghsa.upper() in comment
457+
assert f'<a href="https://github.com/advisories/{ghsa}">{ghsa}</a>' in comment
458458

459459

460460
@pytest.mark.parametrize("triaged_action", ["ignore", "monitor"])

‎tests/core/test_strict_blocking_no_manifest_change.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,9 @@ def test_code_only_scan_with_pr_context_comments_on_each_existing_blocker(
265265
security_comment, _, _, post_security, _ = github_pull_request.call_args.args
266266
assert post_security is True
267267
assert "Existing alerts (2)" in security_comment
268-
assert resolved[0].upper() not in security_comment
268+
assert resolved[0].upper() not in security_comment.upper()
269269
for ghsa, _ in blocking:
270-
assert ghsa.upper() in security_comment
270+
assert f'<a href="https://github.com/advisories/{ghsa}">{ghsa}</a>' in security_comment
271271

272272

273273
@pytest.mark.parametrize("policy, infrastructure_code", POLICIES)
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
"""PR/MR comment rows show every GHSA and CVE ID on an alert, each linked.
2+
3+
Developers who only see the PR comment need the advisory itself. Socket CVE
4+
alerts usually carry both a GHSA and a CVE ID, and either can be published
5+
first, so the row names both and links the GHSA to GitHub and the CVE to NVD.
6+
The single advisory_id() used for de-duplication stays unchanged.
7+
"""
8+
from socketsecurity.core.classes import Diff, Issue
9+
from socketsecurity.core.messages import Messages
10+
11+
GHSA = "GHSA-35jh-r3h4-6jhm"
12+
CVE = "CVE-2021-23337"
13+
GHSA_LINK = f'<a href="https://github.com/advisories/{GHSA}">{GHSA}</a>'
14+
CVE_LINK = f'<a href="https://nvd.nist.gov/vuln/detail/{CVE}">{CVE}</a>'
15+
16+
17+
def _alert(props, title="Critical CVE"):
18+
return Issue(
19+
pkg_type="npm",
20+
pkg_name="lodash",
21+
pkg_version="4.17.20",
22+
purl="lodash@4.17.20",
23+
type="criticalCVE",
24+
severity="critical",
25+
title=title,
26+
description="A vulnerability.",
27+
suggestion="Upgrade.",
28+
url="https://socket.dev/npm/package/lodash/overview/4.17.20",
29+
key="k1",
30+
props=props,
31+
error=True,
32+
)
33+
34+
35+
def _comment(alert):
36+
diff = Diff()
37+
diff.new_alerts = [alert]
38+
diff.diff_url = "https://socket.dev/diff"
39+
diff.report_url = "https://socket.dev/report"
40+
return Messages.security_comment_template(diff)
41+
42+
43+
def test_alert_with_both_ids_shows_both_linked_ghsa_first():
44+
comment = _comment(_alert({"ghsaId": GHSA, "cveId": CVE}))
45+
46+
assert f"Critical CVE ({GHSA_LINK}, {CVE_LINK})" in comment
47+
48+
49+
def test_ghsa_only_alert_links_the_ghsa():
50+
assert Messages.alert_title_html(_alert({"ghsaId": GHSA})) == f"Critical CVE ({GHSA_LINK})"
51+
52+
53+
def test_cve_only_alert_links_the_cve():
54+
assert Messages.alert_title_html(_alert({"cveId": CVE})) == f"Critical CVE ({CVE_LINK})"
55+
56+
57+
def test_older_and_snake_case_spellings_are_read_and_deduplicated():
58+
alert = _alert({"ghsa_id": GHSA, "cve_id": [CVE, CVE.lower()], "cve": CVE})
59+
60+
assert alert.advisory_ids() == [GHSA, CVE]
61+
assert Messages.alert_title_html(alert) == f"Critical CVE ({GHSA_LINK}, {CVE_LINK})"
62+
63+
64+
def test_id_already_in_the_title_is_not_repeated():
65+
alert = _alert({"ghsaId": GHSA, "cveId": CVE}, title=f"{CVE} in lodash")
66+
67+
assert Messages.alert_title_html(alert) == f"{CVE} in lodash ({GHSA_LINK})"
68+
69+
70+
def test_unexpected_id_shape_is_escaped_text_not_a_link():
71+
alert = _alert({"ghsaId": 'GHSA-"><img src=x>', "cveId": CVE})
72+
73+
html = Messages.alert_title_html(alert)
74+
75+
assert "<img" not in html
76+
assert "&lt;img src=x&gt;" in html
77+
assert html.endswith(f", {CVE_LINK})")
78+
assert html.count("<a ") == 1
79+
80+
81+
def test_alert_without_advisory_ids_keeps_its_title():
82+
alert = _alert({}, title="Known malware")
83+
84+
assert Messages.alert_title_html(alert) == "Known malware"
85+
assert Messages.alert_title_with_advisory(alert) == "Known malware"
86+
87+
88+
def test_plain_text_title_lists_both_ids():
89+
alert = _alert({"ghsaId": GHSA, "cveId": CVE})
90+
91+
assert Messages.alert_title_with_advisory(alert) == f"Critical CVE ({GHSA}, {CVE})"
92+
93+
94+
def test_deduplication_key_still_uses_one_id():
95+
with_cve = _alert({"ghsaId": GHSA, "cveId": CVE})
96+
without_cve = _alert({"ghsaId": GHSA})
97+
98+
assert with_cve.advisory_id() == GHSA.upper()
99+
assert with_cve.consolidation_key() == without_cve.consolidation_key()

‎uv.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)