From b232cd8f40f9fb97a1e39a9f1987c42b63e208e1 Mon Sep 17 00:00:00 2001 From: David Larsen Date: Fri, 20 Nov 2026 14:58:13 -0500 Subject: [PATCH 1/2] Keep each advisory when merging duplicate alerts Dedupe keyed alerts on type, severity, category, file and position. Every advisory of one severity on a package shares an alert type (criticalCVE, cve and so on) and has no file, so they all landed on one key and only the first survived. fullscans.stream() and purl.post() both go through this, so handlebars@4.0.11 came back with one criticalCVE alert instead of six. The merge also kept the first alert's action. When the first advisory of a group was resolved or triaged to monitor or ignore, the Block action on the others was lost with them, and a caller that blocks on action == "error" passed. The alert key now includes the advisory ID (props.ghsaId, then cveId), and a merged alert keeps the most severe action seen (error > warn > monitor > ignore). Alerts without an advisory ID merge as before. Bumps the version to 3.6.3. --- CHANGELOG.md | 15 ++ socketdev/core/dedupe.py | 51 +++++-- socketdev/version.py | 2 +- tests/unit/test_dedupe.py | 138 ++++++++++++++++++ .../test_full_scan_cached_polling_http.py | 31 ++++ 5 files changed, 222 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 11bc25b..a687113 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,20 @@ # Changelog +## 3.6.3 + +### Fixed: every advisory on a package is kept + +- `fullscans.stream()` and `purl.post()` no longer merge different + vulnerabilities of the same severity on a package into one alert. Alerts are + now told apart by their advisory ID (`props.ghsaId`, falling back to + `props.cveId`) as well as type, severity, category and file location. Before, + a package with six Critical advisories came back with one `criticalCVE` + alert. +- When the same alert does appear more than once (for example once per release + of a PyPI package), the merged alert carries the most severe `action` + (`error`, then `warn`, `monitor`, `ignore`) instead of the first one seen. A + blocking alert can no longer be hidden behind a triaged or ignored one. + ## 3.6.2 ### Fixed: reject retired API credentials before use diff --git a/socketdev/core/dedupe.py b/socketdev/core/dedupe.py index 2e6e5c4..ee819f9 100644 --- a/socketdev/core/dedupe.py +++ b/socketdev/core/dedupe.py @@ -2,12 +2,39 @@ from typing import Dict, List, Any from socketdev.log import log +# Props that name the advisory behind a vulnerability alert. Every advisory of +# one severity on a package shares an alert type (``criticalCVE``, ``cve`` and +# so on), so these are what tell two of them apart. +ADVISORY_ID_PROPS = ("ghsaId", "ghsa_id", "cveId", "cve_id", "cve") + +# Policy actions from least to most severe. Unknown or missing actions rank +# below "ignore" so any real action replaces them. +ACTION_RANK = {"ignore": 0, "monitor": 1, "warn": 2, "error": 3} + class Dedupe: @staticmethod def normalize_file_path(path: str) -> str: return path.split("/", 1)[-1] if path and "/" in path else path or "" + @staticmethod + def advisory_id(alert: dict) -> str: + """The alert's GHSA or CVE identifier, upper-cased, or "" if it has none.""" + props = alert.get("props") + if not isinstance(props, dict): + return "" + for prop_name in ADVISORY_ID_PROPS: + value = props.get(prop_name) + if isinstance(value, (list, tuple)): + value = next((v for v in value if isinstance(v, str) and v.strip()), "") + if isinstance(value, str) and value.strip(): + return value.strip().upper() + return "" + + @staticmethod + def action_rank(action: Any) -> int: + return ACTION_RANK.get(action, -1) if isinstance(action, str) else -1 + @staticmethod def alert_key(alert: dict) -> tuple: return ( @@ -16,21 +43,12 @@ def alert_key(alert: dict) -> tuple: alert.get("category"), Dedupe.normalize_file_path(alert.get("file")), alert.get("start"), - alert.get("end") + alert.get("end"), + Dedupe.advisory_id(alert), ) @staticmethod def consolidate_and_merge_alerts(package_group: List[Dict[str, Any]]) -> Dict[str, Any]: - def alert_identity(alert: dict) -> tuple: - return ( - alert.get("type"), - alert.get("severity"), - alert.get("category"), - Dedupe.normalize_file_path(alert.get("file")), - alert.get("start"), - alert.get("end") - ) - alert_map: Dict[tuple, dict] = {} releases = set() for pkg in package_group: @@ -38,7 +56,7 @@ def alert_identity(alert: dict) -> tuple: releases.add(release) for alert in pkg.get("alerts", []): - identity = alert_identity(alert) + identity = Dedupe.alert_key(alert) if identity not in alert_map: # Build alert dict with only fields that exist in the original alert. @@ -66,8 +84,13 @@ def alert_identity(alert: dict) -> tuple: alert_map[identity] = consolidated_alert else: - if release not in alert_map[identity]["releases"]: - alert_map[identity]["releases"].append(release) + merged = alert_map[identity] + if release not in merged["releases"]: + merged["releases"].append(release) + # Keep the most severe action seen for this alert, so a + # release that blocks is never hidden behind one that doesn't. + if Dedupe.action_rank(alert.get("action")) > Dedupe.action_rank(merged.get("action")): + merged["action"] = alert.get("action") base = package_group[0] base["releases"] = sorted(releases) diff --git a/socketdev/version.py b/socketdev/version.py index 4f82193..6a45bf5 100644 --- a/socketdev/version.py +++ b/socketdev/version.py @@ -1 +1 @@ -__version__ = "3.6.2" +__version__ = "3.6.3" diff --git a/tests/unit/test_dedupe.py b/tests/unit/test_dedupe.py index 080ecf9..2b7f122 100644 --- a/tests/unit/test_dedupe.py +++ b/tests/unit/test_dedupe.py @@ -188,5 +188,143 @@ def test_dedupe_batched_parameter_backward_compatibility(self): self.assertNotIn("batchIndex", pkg) + @staticmethod + def _cve(ghsa, action, cve="", severity="critical", alert_type="criticalCVE", key=None): + return { + "key": key or f"key-{ghsa}", + "type": alert_type, + "severity": severity, + "category": "vulnerability", + "action": action, + "props": {"ghsaId": ghsa, "cveId": cve}, + } + + def test_same_severity_advisories_on_one_package_stay_separate(self): + """Each advisory keeps its own alert even when type and severity match""" + ghsa_ids = [ + "GHSA-8r5x-fm3f-whwj", "GHSA-p8wg-vrv2-v86f", "GHSA-2w6w-674q-4c4q", + "GHSA-w457-6q6x-cgp9", "GHSA-765h-qjxv-5f44", "GHSA-f2jv-r9rf-7988", + ] + packages = [{ + "id": "artifact-1", "type": "npm", "name": "handlebars", "version": "4.0.11", + "alerts": [self._cve(ghsa, "error") for ghsa in ghsa_ids], + }] + + result = Dedupe.dedupe(packages, batched=False) + + alerts = result[0]["alerts"] + self.assertEqual(len(alerts), 6) + self.assertEqual(sorted(a["props"]["ghsaId"] for a in alerts), sorted(ghsa_ids)) + self.assertTrue(all(a["action"] == "error" for a in alerts)) + + def test_blocking_advisory_is_kept_when_an_earlier_one_is_triaged(self): + """A monitored or ignored advisory listed first must not hide a later Block one""" + for triaged_action in ("ignore", "monitor", "warn"): + with self.subTest(triaged_action=triaged_action): + packages = [{ + "id": "artifact-1", "type": "npm", "name": "example", "version": "1.0.0", + "alerts": [ + self._cve("GHSA-aaaa-aaaa-aaaa", triaged_action), + self._cve("GHSA-bbbb-bbbb-bbbb", "error"), + ], + }] + + alerts = Dedupe.dedupe(packages, batched=False)[0]["alerts"] + + actions = {a["props"]["ghsaId"]: a["action"] for a in alerts} + self.assertEqual(actions, { + "GHSA-aaaa-aaaa-aaaa": triaged_action, + "GHSA-bbbb-bbbb-bbbb": "error", + }) + + def test_same_advisory_across_releases_merges_with_most_severe_action(self): + """One advisory seen in several releases is one alert carrying the strictest action""" + orders = [ + ["ignore", "monitor", "error", "warn"], + ["error", "ignore", "monitor", "warn"], + ["monitor", "warn", "ignore", "error"], + ] + for actions in orders: + with self.subTest(actions=actions): + packages = [ + { + "id": f"artifact-{i}", "type": "pypi", "name": "example", "version": "1.0", + "release": release, "inputPurl": "pkg:pypi/example@1.0", + "alerts": [self._cve("GHSA-cccc-cccc-cccc", action, cve="CVE-2020-0001", + key=f"key-{release}")], + } + for i, (release, action) in enumerate(zip(["a", "b", "c", "d"], actions)) + ] + + alerts = Dedupe.dedupe(packages)[0]["alerts"] + + self.assertEqual(len(alerts), 1) + self.assertEqual(alerts[0]["action"], "error") + self.assertEqual(alerts[0]["releases"], ["a", "b", "c", "d"]) + + def test_merged_action_ranks_error_warn_monitor_ignore(self): + cases = [ + (["ignore", "monitor"], "monitor"), + (["monitor", "warn"], "warn"), + (["warn", "ignore"], "warn"), + ([None, "ignore"], "ignore"), + (["ignore", None], "ignore"), + (["unknown-action", "monitor"], "monitor"), + ] + for actions, expected in cases: + with self.subTest(actions=actions): + packages = [ + { + "id": f"artifact-{i}", "type": "pypi", "name": "example", "version": "1.0", + "release": f"r{i}", "inputPurl": "pkg:pypi/example@1.0", + "alerts": [self._cve("GHSA-dddd-dddd-dddd", action)], + } + for i, action in enumerate(actions) + ] + alerts = Dedupe.dedupe(packages)[0]["alerts"] + self.assertEqual(len(alerts), 1) + self.assertEqual(alerts[0]["action"], expected) + + def test_advisory_id_falls_back_to_cve_and_ignores_case(self): + self.assertEqual(Dedupe.advisory_id({"props": {"ghsaId": "", "cveId": "cve-2021-1"}}), "CVE-2021-1") + self.assertEqual(Dedupe.advisory_id({"props": {"ghsaId": " ghsa-x "}}), "GHSA-X") + self.assertEqual(Dedupe.advisory_id({"props": {"cve": ["", "CVE-2022-2"]}}), "CVE-2022-2") + self.assertEqual(Dedupe.advisory_id({"props": []}), "") + self.assertEqual(Dedupe.advisory_id({}), "") + + packages = [{ + "id": "artifact-1", "type": "npm", "name": "example", "version": "1.0.0", + "alerts": [ + self._cve("", "error", cve="CVE-2021-0001"), + self._cve("", "error", cve="CVE-2021-0002"), + self._cve("", "monitor", cve="cve-2021-0001"), + ], + }] + alerts = Dedupe.dedupe(packages, batched=False)[0]["alerts"] + self.assertEqual(sorted(a["props"]["cveId"] for a in alerts), ["CVE-2021-0001", "CVE-2021-0002"]) + + def test_alerts_without_advisory_still_merge_across_releases(self): + """Non-advisory alerts keep the existing identity, including normalized file paths""" + def file_alert(path, action): + return { + "key": f"key-{path}", "type": "networkAccess", "severity": "middle", + "category": "supplyChainRisk", "file": path, "start": 10, "end": 20, + "action": action, "props": {"module": "http"}, + } + packages = [ + {"id": "1", "type": "pypi", "name": "example", "version": "1.0", "release": "wheel", + "inputPurl": "pkg:pypi/example@1.0", "alerts": [file_alert("wheel/example/net.py", "monitor")]}, + {"id": "2", "type": "pypi", "name": "example", "version": "1.0", "release": "sdist", + "inputPurl": "pkg:pypi/example@1.0", "alerts": [file_alert("sdist/example/net.py", "warn")]}, + ] + + alerts = Dedupe.dedupe(packages)[0]["alerts"] + + self.assertEqual(len(alerts), 1) + self.assertEqual(alerts[0]["file"], "example/net.py") + self.assertEqual(alerts[0]["action"], "warn") + self.assertEqual(alerts[0]["releases"], ["wheel", "sdist"]) + + if __name__ == '__main__': unittest.main() diff --git a/tests/unit/test_full_scan_cached_polling_http.py b/tests/unit/test_full_scan_cached_polling_http.py index 29c45f9..79ca591 100644 --- a/tests/unit/test_full_scan_cached_polling_http.py +++ b/tests/unit/test_full_scan_cached_polling_http.py @@ -118,6 +118,37 @@ def test_duplicate_artifacts_merge_alerts_and_preserve_score_event(api_fixture): assert artifact["alerts"][0]["releases"] == ["wheel", "source"] + +def test_stream_keeps_each_advisory_and_its_action(api_fixture): + """Same-severity CVEs on one artifact reach the caller as separate alerts.""" + path = "orgs/org/full-scans/scan" + + def cve(ghsa, cve_id, action): + return { + "key": "key-" + ghsa, "type": "criticalCVE", "severity": "critical", + "category": "vulnerability", "action": action, + "props": {"ghsaId": ghsa, "cveId": cve_id}, + } + + artifact = { + "id": "artifact-1", "type": "npm", "name": "handlebars", "version": "4.0.11", + "alerts": [ + cve("GHSA-w457-6q6x-cgp9", "CVE-2019-19919", "monitor"), + cve("GHSA-765h-qjxv-5f44", "CVE-2021-23383", "error"), + cve("GHSA-f2jv-r9rf-7988", "CVE-2021-23369", "ignore"), + ], + } + api_fixture.route("GET", path, ndjson(artifact)) + + typed = sdk_for(api_fixture).fullscans.stream("org", "scan", use_types=True) + + alerts = typed.artifacts["artifact-1"].alerts + assert [(alert.props["ghsaId"], alert.action) for alert in alerts] == [ + ("GHSA-w457-6q6x-cgp9", "monitor"), + ("GHSA-765h-qjxv-5f44", "error"), + ("GHSA-f2jv-r9rf-7988", "ignore"), + ] + @pytest.mark.parametrize("status", [403, 404]) def test_deterministic_errors_stop_immediately(api_fixture, status): api_fixture.route("GET", "orgs/org/full-scans/scan", SocketResponse(status, {"error": {"message": "denied"}})) From e1e919e461a986d0c94777fe14e502e3b069bc92 Mon Sep 17 00:00:00 2001 From: David Larsen Date: Fri, 20 Nov 2026 17:06:15 -0500 Subject: [PATCH 2/2] Document dedupe merge details and cover edge cases Scope the changelog's cross-release merging to purl.post(): fullscans.stream() rows carry no purl, so each release file stays its own artifact there. Note in the changelog and code that a merged alert keeps the first sighting's key while its action can come from another release, and that the extra advisory prop names mirror socket-python-cli's ADVISORY_ID_PROPS. An unknown or missing action keeps ranking below ignore, so it never replaces a known action and can't downgrade a known error. Tests pin that in both orders, along with the first-key behavior, a CVE next to a non-advisory alert on one package, and an empty input. Fix blank-line spacing in the new tests. --- CHANGELOG.md | 12 ++++-- socketdev/core/dedupe.py | 16 +++++++- tests/unit/test_dedupe.py | 40 ++++++++++++++++++- .../test_full_scan_cached_polling_http.py | 2 +- 4 files changed, 62 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a687113..a5c36f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,10 +10,14 @@ `props.cveId`) as well as type, severity, category and file location. Before, a package with six Critical advisories came back with one `criticalCVE` alert. -- When the same alert does appear more than once (for example once per release - of a PyPI package), the merged alert carries the most severe `action` - (`error`, then `warn`, `monitor`, `ignore`) instead of the first one seen. A - blocking alert can no longer be hidden behind a triaged or ignored one. +- When the same alert does appear more than once (in one artifact, or across + the release files of a package in a `purl.post()` result), the merged alert + carries the most severe `action` (`error`, then `warn`, `monitor`, `ignore`) + instead of the first one seen. A blocking alert can no longer be hidden + behind a triaged or ignored one. The merged alert keeps the `key` of the + first sighting, so that `key` can come from a release whose own action was + less severe. `fullscans.stream()` does not merge across releases: each + release file is its own artifact there. ## 3.6.2 diff --git a/socketdev/core/dedupe.py b/socketdev/core/dedupe.py index ee819f9..b81d7db 100644 --- a/socketdev/core/dedupe.py +++ b/socketdev/core/dedupe.py @@ -5,10 +5,18 @@ # Props that name the advisory behind a vulnerability alert. Every advisory of # one severity on a package shares an alert type (``criticalCVE``, ``cve`` and # so on), so these are what tell two of them apart. +# +# Socket's CVE alerts carry ``ghsaId`` and ``cveId``. The other names are not +# in any current alert schema; they are checked so this lookup matches +# ``ADVISORY_ID_PROPS`` / ``Issue.advisory_id()`` in socket-python-cli +# (socketsecurity/core/classes.py), which reads alerts from this SDK. Keep the +# two lists in step. ADVISORY_ID_PROPS = ("ghsaId", "ghsa_id", "cveId", "cve_id", "cve") -# Policy actions from least to most severe. Unknown or missing actions rank -# below "ignore" so any real action replaces them. +# Policy actions from least to most severe. An unknown or missing action ranks +# below "ignore": it yields to any known action, "ignore" included, and never +# replaces one. So an unexpected value can't downgrade a known "error", and +# the result doesn't depend on which release is listed first. ACTION_RANK = {"ignore": 0, "monitor": 1, "warn": 2, "error": 3} @@ -89,6 +97,10 @@ def consolidate_and_merge_alerts(package_group: List[Dict[str, Any]]) -> Dict[st merged["releases"].append(release) # Keep the most severe action seen for this alert, so a # release that blocks is never hidden behind one that doesn't. + # Only the action moves: key, props and the rest stay as the + # first sighting had them, so in a purl.post() result the + # merged key can belong to a release whose own action was + # less severe than the one shown. if Dedupe.action_rank(alert.get("action")) > Dedupe.action_rank(merged.get("action")): merged["action"] = alert.get("action") diff --git a/tests/unit/test_dedupe.py b/tests/unit/test_dedupe.py index 2b7f122..b4c3475 100644 --- a/tests/unit/test_dedupe.py +++ b/tests/unit/test_dedupe.py @@ -187,7 +187,6 @@ def test_dedupe_batched_parameter_backward_compatibility(self): for pkg in result_batched_true + result_batched_false: self.assertNotIn("batchIndex", pkg) - @staticmethod def _cve(ghsa, action, cve="", severity="critical", alert_type="criticalCVE", key=None): return { @@ -270,6 +269,11 @@ def test_merged_action_ranks_error_warn_monitor_ignore(self): ([None, "ignore"], "ignore"), (["ignore", None], "ignore"), (["unknown-action", "monitor"], "monitor"), + # An unknown action yields to any known one, "ignore" included, + # and never replaces a known "error", in either order. + (["unknown-action", "ignore"], "ignore"), + (["error", "unknown-action"], "error"), + (["unknown-action", "error"], "error"), ] for actions, expected in cases: with self.subTest(actions=actions): @@ -285,6 +289,40 @@ def test_merged_action_ranks_error_warn_monitor_ignore(self): self.assertEqual(len(alerts), 1) self.assertEqual(alerts[0]["action"], expected) + def test_merged_alert_keeps_first_key_when_action_comes_from_a_later_release(self): + """Only the action moves to the most severe sighting; key and props stay with the first""" + packages = [ + { + "id": f"artifact-{release}", "type": "pypi", "name": "example", "version": "1.0", + "release": release, "inputPurl": "pkg:pypi/example@1.0", + "alerts": [self._cve("GHSA-eeee-eeee-eeee", action, key=f"key-{release}")], + } + for release, action in [("sdist", "monitor"), ("wheel", "error")] + ] + + alerts = Dedupe.dedupe(packages)[0]["alerts"] + + self.assertEqual(len(alerts), 1) + self.assertEqual(alerts[0]["key"], "key-sdist") + self.assertEqual(alerts[0]["action"], "error") + + def test_non_advisory_alert_on_a_package_with_cves_keeps_its_own_entry(self): + packages = [{ + "id": "artifact-1", "type": "npm", "name": "example", "version": "1.0.0", + "alerts": [ + self._cve("GHSA-ffff-ffff-ffff", "monitor"), + {"key": "key-malware", "type": "malware", "severity": "critical", + "category": "supplyChainRisk", "action": "error", "props": {}}, + ], + }] + + alerts = Dedupe.dedupe(packages, batched=False)[0]["alerts"] + + self.assertEqual({a["type"]: a["action"] for a in alerts}, {"criticalCVE": "monitor", "malware": "error"}) + + def test_dedupe_of_no_packages_is_empty(self): + self.assertEqual(Dedupe.dedupe([]), []) + def test_advisory_id_falls_back_to_cve_and_ignores_case(self): self.assertEqual(Dedupe.advisory_id({"props": {"ghsaId": "", "cveId": "cve-2021-1"}}), "CVE-2021-1") self.assertEqual(Dedupe.advisory_id({"props": {"ghsaId": " ghsa-x "}}), "GHSA-X") diff --git a/tests/unit/test_full_scan_cached_polling_http.py b/tests/unit/test_full_scan_cached_polling_http.py index 79ca591..b0bff8e 100644 --- a/tests/unit/test_full_scan_cached_polling_http.py +++ b/tests/unit/test_full_scan_cached_polling_http.py @@ -118,7 +118,6 @@ def test_duplicate_artifacts_merge_alerts_and_preserve_score_event(api_fixture): assert artifact["alerts"][0]["releases"] == ["wheel", "source"] - def test_stream_keeps_each_advisory_and_its_action(api_fixture): """Same-severity CVEs on one artifact reach the caller as separate alerts.""" path = "orgs/org/full-scans/scan" @@ -149,6 +148,7 @@ def cve(ghsa, cve_id, action): ("GHSA-f2jv-r9rf-7988", "ignore"), ] + @pytest.mark.parametrize("status", [403, 404]) def test_deterministic_errors_stop_immediately(api_fixture, status): api_fixture.route("GET", "orgs/org/full-scans/scan", SocketResponse(status, {"error": {"message": "denied"}}))