Skip to content

Commit 3f3be31

Browse files
committed
Link advisory IDs that already appear in the alert title
An ID the title already named was dropped from the linked list and stayed plain text, and the substring check also hid a different CVE whose ID is a prefix of one in the title (CVE-2021-2333 inside CVE-2021-23337). IDs now match only in full. An ID the title names is linked in place, keeping the title's spelling and linking the API's ID, and the remaining IDs are appended once. The plain-text title uses the same full-ID match.
1 parent f302c2f commit 3f3be31

2 files changed

Lines changed: 79 additions & 23 deletions

File tree

‎socketsecurity/core/messages.py‎

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,12 +1088,21 @@ def security_alert_row(alert: Issue, diff: Diff, config, show_ignore: bool) -> s
10881088
<!-- end-socket-alert-{pkg_marker} -->
10891089
"""
10901090

1091+
@staticmethod
1092+
def _advisory_id_pattern(ids) -> "re.Pattern[str]":
1093+
"""Matches any of the IDs in full, so CVE-2021-2333 does not match inside CVE-2021-23337."""
1094+
alternatives = "|".join(re.escape(advisory) for advisory in sorted(ids, key=len, reverse=True))
1095+
return re.compile(rf"(?<![\w-])(?:{alternatives})(?![\w-])", re.IGNORECASE)
1096+
10911097
@staticmethod
10921098
def advisory_ids_for_title(alert: Issue) -> list:
10931099
"""The alert's GHSA and CVE IDs, GHSA first, minus any its title already names."""
1094-
title = (getattr(alert, "title", "") or "").upper()
1100+
title = getattr(alert, "title", "") or ""
10951101
ids = alert.advisory_ids() if hasattr(alert, "advisory_ids") else []
1096-
return [advisory for advisory in ids if advisory.upper() not in title]
1102+
return [
1103+
advisory for advisory in ids
1104+
if not Messages._advisory_id_pattern([advisory]).search(title)
1105+
]
10971106

10981107
@staticmethod
10991108
def alert_title_with_advisory(alert: Issue) -> str:
@@ -1109,34 +1118,53 @@ def alert_title_with_advisory(alert: Issue) -> str:
11091118
return title
11101119

11111120
@staticmethod
1112-
def advisory_link_html(advisory: str) -> str:
1121+
def _advisory_link_base(advisory: str) -> str | None:
1122+
for pattern, base_url in ADVISORY_LINK_BASES:
1123+
if pattern.match(advisory):
1124+
return base_url
1125+
return None
1126+
1127+
@staticmethod
1128+
def advisory_link_html(advisory: str, label_html: str | None = None) -> str:
11131129
"""An advisory ID linked to its public advisory page.
11141130
11151131
Only IDs in the canonical GHSA or CVE shape are linked, so an unexpected
11161132
value renders as escaped text instead of an arbitrary URL path.
11171133
"""
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)
1134+
label = Messages.html_text(advisory) if label_html is None else label_html
1135+
base_url = Messages._advisory_link_base(advisory)
1136+
if base_url is None:
1137+
return label
1138+
return f'<a href="{Messages.html_attr(base_url + advisory)}">{label}</a>'
11251139

11261140
@staticmethod
11271141
def alert_title_html(alert: Issue) -> str:
1128-
"""The PR/MR comment row title: escaped title, then each advisory ID linked.
1142+
"""The PR/MR comment row title as HTML.
11291143
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.
1144+
The title is escaped. The alert's GHSA and CVE IDs are linked in place where
1145+
the title names them in full, and the remaining IDs follow once, linked, in
1146+
parentheses. Only canonical GHSA and CVE IDs become links.
11331147
"""
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})"
1148+
title_html = Messages.html_text(getattr(alert, "title", "") or "")
1149+
ids = alert.advisory_ids() if hasattr(alert, "advisory_ids") else []
1150+
remaining = Messages.advisory_ids_for_title(alert)
1151+
# Canonical IDs are letters, digits and hyphens, which escaping leaves alone.
1152+
in_title = {
1153+
advisory.upper(): advisory
1154+
for advisory in ids
1155+
if advisory not in remaining and Messages._advisory_link_base(advisory)
1156+
}
1157+
if in_title:
1158+
title_html = Messages._advisory_id_pattern(in_title.values()).sub(
1159+
lambda match: Messages.advisory_link_html(
1160+
in_title[match.group(0).upper()], match.group(0)
1161+
),
1162+
title_html,
1163+
)
1164+
if not remaining:
1165+
return title_html
1166+
links = ", ".join(Messages.advisory_link_html(advisory) for advisory in remaining)
1167+
return f"{title_html} ({links})"
11401168

11411169
@staticmethod
11421170
def security_comment_template(diff: Diff, config=None) -> str:

‎tests/unit/test_comment_advisory_links.py‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""PR/MR comment rows show every GHSA and CVE ID on an alert, each linked.
1+
"""PR/MR comment rows link every GHSA and CVE ID on an alert.
22
33
Developers who only see the PR comment need the advisory itself. Socket CVE
44
alerts usually carry both a GHSA and a CVE ID, and either can be published
@@ -61,10 +61,38 @@ def test_older_and_snake_case_spellings_are_read_and_deduplicated():
6161
assert Messages.alert_title_html(alert) == f"Critical CVE ({GHSA_LINK}, {CVE_LINK})"
6262

6363

64-
def test_id_already_in_the_title_is_not_repeated():
64+
def test_id_already_in_the_title_is_linked_in_place_and_not_repeated():
6565
alert = _alert({"ghsaId": GHSA, "cveId": CVE}, title=f"{CVE} in lodash")
6666

67-
assert Messages.alert_title_html(alert) == f"{CVE} in lodash ({GHSA_LINK})"
67+
html = Messages.alert_title_html(alert)
68+
69+
assert html == f"{CVE_LINK} in lodash ({GHSA_LINK})"
70+
assert html.count(CVE) == 2 # the link target and its text, once
71+
assert Messages.alert_title_with_advisory(alert) == f"{CVE} in lodash ({GHSA})"
72+
73+
74+
def test_id_in_the_title_keeps_its_spelling_and_links_the_api_id():
75+
alert = _alert({"ghsaId": GHSA}, title=f"{GHSA.upper()}: prototype pollution")
76+
77+
assert Messages.alert_title_html(alert) == (
78+
f'<a href="https://github.com/advisories/{GHSA}">{GHSA.upper()}</a>: prototype pollution'
79+
)
80+
81+
82+
def test_title_is_escaped_around_an_id_linked_in_place():
83+
alert = _alert({"cveId": CVE}, title=f"<b>{CVE}</b> & more")
84+
85+
assert Messages.alert_title_html(alert) == f"&lt;b&gt;{CVE_LINK}&lt;/b&gt; &amp; more"
86+
87+
88+
def test_id_that_is_a_prefix_of_a_different_cve_in_the_title_is_still_linked():
89+
prefix_cve = "CVE-2021-2333"
90+
alert = _alert({"cveId": prefix_cve}, title=f"{CVE} in lodash")
91+
92+
assert Messages.alert_title_html(alert) == (
93+
f'{CVE} in lodash (<a href="https://nvd.nist.gov/vuln/detail/{prefix_cve}">{prefix_cve}</a>)'
94+
)
95+
assert Messages.alert_title_with_advisory(alert) == f"{CVE} in lodash ({prefix_cve})"
6896

6997

7098
def test_unexpected_id_shape_is_escaped_text_not_a_link():

0 commit comments

Comments
 (0)