|
| 1 | +"""Repeat sightings of one finding collapse into one alert; distinct advisories don't. |
| 2 | +
|
| 3 | +The same alert arrives once per manifest that pulls a package in, so the diff |
| 4 | +collapses alerts on Issue.consolidation_key. Every CVE of one severity on a |
| 5 | +package shares an alert type, so the key has to include the advisory ID, or a |
| 6 | +package's second advisory is dropped along with any blocking action it carries. |
| 7 | +""" |
| 8 | +from unittest.mock import Mock |
| 9 | + |
| 10 | +from socketsecurity.config import CliConfig |
| 11 | +from socketsecurity.core import Core |
| 12 | +from socketsecurity.core.classes import Diff, Issue |
| 13 | +from socketsecurity.core.messages import Messages |
| 14 | +from socketsecurity.output import OutputHandler |
| 15 | + |
| 16 | + |
| 17 | +def _cve(advisory, error=False, warn=False, pkg_type="npm", key=None, props_field="ghsaId"): |
| 18 | + return Issue( |
| 19 | + pkg_type=pkg_type, |
| 20 | + pkg_name="lodash", |
| 21 | + pkg_version="4.17.20", |
| 22 | + purl="lodash@4.17.20", |
| 23 | + type="criticalCVE", |
| 24 | + severity="critical", |
| 25 | + title="Critical CVE", |
| 26 | + description="A vulnerability.", |
| 27 | + suggestion="Upgrade.", |
| 28 | + url="https://socket.dev/npm/package/lodash", |
| 29 | + key=key or f"key-{advisory}", |
| 30 | + props={props_field: advisory}, |
| 31 | + error=error, |
| 32 | + warn=warn, |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +def test_two_advisories_on_one_package_are_both_new_alerts(): |
| 37 | + alerts = { |
| 38 | + "k1": [_cve("GHSA-aaaa-aaaa-aaaa", error=True)], |
| 39 | + "k2": [_cve("GHSA-bbbb-bbbb-bbbb", error=True)], |
| 40 | + } |
| 41 | + |
| 42 | + new_alerts = Core.get_new_alerts(alerts, {}) |
| 43 | + |
| 44 | + assert sorted(a.advisory_id() for a in new_alerts) == [ |
| 45 | + "GHSA-AAAA-AAAA-AAAA", |
| 46 | + "GHSA-BBBB-BBBB-BBBB", |
| 47 | + ] |
| 48 | + |
| 49 | + |
| 50 | +def test_blocking_advisory_is_not_hidden_behind_a_warning(): |
| 51 | + """The kept advisory is a warning; dropping the second would pass the run.""" |
| 52 | + alerts = { |
| 53 | + "k1": [_cve("GHSA-aaaa-aaaa-aaaa", warn=True)], |
| 54 | + "k2": [_cve("GHSA-bbbb-bbbb-bbbb", error=True)], |
| 55 | + } |
| 56 | + diff = Diff() |
| 57 | + diff.new_alerts = Core.get_new_alerts(alerts, {}) |
| 58 | + |
| 59 | + config = Mock(spec=CliConfig) |
| 60 | + config.disable_blocking = False |
| 61 | + config.strict_blocking = False |
| 62 | + assert not OutputHandler(config, Mock()).report_pass(diff) |
| 63 | + |
| 64 | + |
| 65 | +def test_same_advisory_from_two_manifests_is_one_alert(): |
| 66 | + alerts = { |
| 67 | + "k1": [_cve("GHSA-aaaa-aaaa-aaaa", error=True), _cve("GHSA-aaaa-aaaa-aaaa", error=True)], |
| 68 | + } |
| 69 | + |
| 70 | + assert len(Core.get_new_alerts(alerts, {})) == 1 |
| 71 | + |
| 72 | + |
| 73 | +def test_unchanged_and_removed_alerts_keep_each_advisory(): |
| 74 | + alerts = { |
| 75 | + "k1": [_cve("CVE-2026-0001", error=True, props_field="cveId")], |
| 76 | + "k2": [_cve("CVE-2026-0002", error=True, props_field="cveId")], |
| 77 | + } |
| 78 | + |
| 79 | + assert len(Core.get_unchanged_alerts(alerts)) == 2 |
| 80 | + assert len(Core.get_removed_alerts(alerts)) == 2 |
| 81 | + |
| 82 | + |
| 83 | +def test_consolidation_key_includes_ecosystem_and_ignores_advisory_case(): |
| 84 | + npm = _cve("ghsa-aaaa-aaaa-aaaa") |
| 85 | + pypi = _cve("GHSA-AAAA-AAAA-AAAA", pkg_type="pypi") |
| 86 | + |
| 87 | + assert npm.consolidation_key() != pypi.consolidation_key() |
| 88 | + assert npm.consolidation_key()[3] == pypi.consolidation_key()[3] |
| 89 | + |
| 90 | + |
| 91 | +def test_non_advisory_alerts_still_collapse_per_package_and_type(): |
| 92 | + """Alerts with no advisory ID keep the per-package, per-type collapse.""" |
| 93 | + first = Issue(pkg_type="npm", purl="a@1.0.0", type="envVars", props={}, warn=True, key="k1") |
| 94 | + second = Issue(pkg_type="npm", purl="a@1.0.0", type="envVars", props={}, warn=True, key="k2") |
| 95 | + |
| 96 | + assert len(Core.get_new_alerts({"k1": [first], "k2": [second]}, {})) == 1 |
| 97 | + |
| 98 | + |
| 99 | +def test_pr_comment_rows_name_the_advisory(): |
| 100 | + diff = Diff() |
| 101 | + diff.new_alerts = [_cve("GHSA-aaaa-aaaa-aaaa", error=True), _cve("GHSA-bbbb-bbbb-bbbb", error=True)] |
| 102 | + diff.diff_url = "https://socket.dev/diff" |
| 103 | + diff.report_url = "https://socket.dev/report" |
| 104 | + |
| 105 | + comment = Messages.security_comment_template(diff) |
| 106 | + |
| 107 | + assert "Critical CVE (GHSA-AAAA-AAAA-AAAA)" in comment |
| 108 | + assert "Critical CVE (GHSA-BBBB-BBBB-BBBB)" in comment |
| 109 | + |
| 110 | + |
| 111 | +def test_title_already_naming_the_advisory_is_left_alone(): |
| 112 | + alert = _cve("GHSA-aaaa-aaaa-aaaa") |
| 113 | + alert.title = "GHSA-aaaa-aaaa-aaaa in lodash" |
| 114 | + |
| 115 | + assert Messages.alert_title_with_advisory(alert) == "GHSA-aaaa-aaaa-aaaa in lodash" |
0 commit comments