Repository navigation
Expand file tree
/
Copy pathtest_comment_advisory_links.py
More file actions
127 lines (85 loc) · 4.37 KB
/
Copy pathtest_comment_advisory_links.py
File metadata and controls
127 lines (85 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""PR/MR comment rows link every GHSA and CVE ID on an alert.
Developers who only see the PR comment need the advisory itself. Socket CVE
alerts usually carry both a GHSA and a CVE ID, and either can be published
first, so the row names both and links the GHSA to GitHub and the CVE to NVD.
The single advisory_id() used for de-duplication stays unchanged.
"""
from socketsecurity.core.classes import Diff, Issue
from socketsecurity.core.messages import Messages
GHSA = "GHSA-35jh-r3h4-6jhm"
CVE = "CVE-2021-23337"
GHSA_LINK = f'<a href="https://github.com/advisories/{GHSA}">{GHSA}</a>'
CVE_LINK = f'<a href="https://nvd.nist.gov/vuln/detail/{CVE}">{CVE}</a>'
def _alert(props, title="Critical CVE"):
return Issue(
pkg_type="npm",
pkg_name="lodash",
pkg_version="4.17.20",
purl="lodash@4.17.20",
type="criticalCVE",
severity="critical",
title=title,
description="A vulnerability.",
suggestion="Upgrade.",
url="https://socket.dev/npm/package/lodash/overview/4.17.20",
key="k1",
props=props,
error=True,
)
def _comment(alert):
diff = Diff()
diff.new_alerts = [alert]
diff.diff_url = "https://socket.dev/diff"
diff.report_url = "https://socket.dev/report"
return Messages.security_comment_template(diff)
def test_alert_with_both_ids_shows_both_linked_ghsa_first():
comment = _comment(_alert({"ghsaId": GHSA, "cveId": CVE}))
assert f"Critical CVE ({GHSA_LINK}, {CVE_LINK})" in comment
def test_ghsa_only_alert_links_the_ghsa():
assert Messages.alert_title_html(_alert({"ghsaId": GHSA})) == f"Critical CVE ({GHSA_LINK})"
def test_cve_only_alert_links_the_cve():
assert Messages.alert_title_html(_alert({"cveId": CVE})) == f"Critical CVE ({CVE_LINK})"
def test_older_and_snake_case_spellings_are_read_and_deduplicated():
alert = _alert({"ghsa_id": GHSA, "cve_id": [CVE, CVE.lower()], "cve": CVE})
assert alert.advisory_ids() == [GHSA, CVE]
assert Messages.alert_title_html(alert) == f"Critical CVE ({GHSA_LINK}, {CVE_LINK})"
def test_id_already_in_the_title_is_linked_in_place_and_not_repeated():
alert = _alert({"ghsaId": GHSA, "cveId": CVE}, title=f"{CVE} in lodash")
html = Messages.alert_title_html(alert)
assert html == f"{CVE_LINK} in lodash ({GHSA_LINK})"
assert html.count(CVE) == 2 # the link target and its text, once
assert Messages.alert_title_with_advisory(alert) == f"{CVE} in lodash ({GHSA})"
def test_id_in_the_title_keeps_its_spelling_and_links_the_api_id():
alert = _alert({"ghsaId": GHSA}, title=f"{GHSA.upper()}: prototype pollution")
assert Messages.alert_title_html(alert) == (
f'<a href="https://github.com/advisories/{GHSA}">{GHSA.upper()}</a>: prototype pollution'
)
def test_title_is_escaped_around_an_id_linked_in_place():
alert = _alert({"cveId": CVE}, title=f"<b>{CVE}</b> & more")
assert Messages.alert_title_html(alert) == f"<b>{CVE_LINK}</b> & more"
def test_id_that_is_a_prefix_of_a_different_cve_in_the_title_is_still_linked():
prefix_cve = "CVE-2021-2333"
alert = _alert({"cveId": prefix_cve}, title=f"{CVE} in lodash")
assert Messages.alert_title_html(alert) == (
f'{CVE} in lodash (<a href="https://nvd.nist.gov/vuln/detail/{prefix_cve}">{prefix_cve}</a>)'
)
assert Messages.alert_title_with_advisory(alert) == f"{CVE} in lodash ({prefix_cve})"
def test_unexpected_id_shape_is_escaped_text_not_a_link():
alert = _alert({"ghsaId": 'GHSA-"><img src=x>', "cveId": CVE})
html = Messages.alert_title_html(alert)
assert "<img" not in html
assert "<img src=x>" in html
assert html.endswith(f", {CVE_LINK})")
assert html.count("<a ") == 1
def test_alert_without_advisory_ids_keeps_its_title():
alert = _alert({}, title="Known malware")
assert Messages.alert_title_html(alert) == "Known malware"
assert Messages.alert_title_with_advisory(alert) == "Known malware"
def test_plain_text_title_lists_both_ids():
alert = _alert({"ghsaId": GHSA, "cveId": CVE})
assert Messages.alert_title_with_advisory(alert) == f"Critical CVE ({GHSA}, {CVE})"
def test_deduplication_key_still_uses_one_id():
with_cve = _alert({"ghsaId": GHSA, "cveId": CVE})
without_cve = _alert({"ghsaId": GHSA})
assert with_cve.advisory_id() == GHSA.upper()
assert with_cve.consolidation_key() == without_cve.consolidation_key()