Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
# Changelog

## 3.6.4

### 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 (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.3

### Fixed: `settings.integration_events()` requested the wrong path
Expand Down
63 changes: 49 additions & 14 deletions socketdev/core/dedupe.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,47 @@
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.
#
# 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. 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}


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 (
Expand All @@ -16,29 +51,20 @@ 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:
release = pkg.get("release") if pkg.get("release") is not None else pkg.get("type")
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.
Expand Down Expand Up @@ -66,8 +92,17 @@ 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.
# 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")

base = package_group[0]
base["releases"] = sorted(releases)
Expand Down
2 changes: 1 addition & 1 deletion socketdev/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "3.6.3"
__version__ = "3.6.4"
176 changes: 176 additions & 0 deletions tests/unit/test_dedupe.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,182 @@ 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 {
"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"),
# 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):
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_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")
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()
31 changes: 31 additions & 0 deletions tests/unit/test_full_scan_cached_polling_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}}))
Expand Down