| 1 |
# Copyright (C) 2026 Siemens |
| 2 |
# |
| 3 |
# SPDX-License-Identifier: MIT |
| 4 |
|
| 5 |
from collections.abc import Iterable |
| 6 |
import io |
| 7 |
import json |
| 8 |
from pathlib import Path |
| 9 |
|
| 10 |
from debian.debian_support import Version |
| 11 |
import jsonschema |
| 12 |
import pytest |
| 13 |
|
| 14 |
from debsbom.dpkg.package import BinaryPackage, Dependency, Package, SourcePackage |
| 15 |
|
| 16 |
from debsbom.schema import secscan, tracepath as schema_tracepath |
| 17 |
from debsbom.securityscan.scanner import CveStatus, CveUrgency, SecurityScanner |
| 18 |
from debsbom.securityscan.writer import ScanResultWriter |
| 19 |
from debsbom.graph.walker import GraphWalker, PackageRepr |
| 20 |
|
| 21 |
# Note, that this data is completely made up |
| 22 |
DB_PATH = Path("tests/data/security-tracker.fake.json") |
| 23 |
TRACKER_URL = "https://tracker.example.com" |
| 24 |
BUGS_URL = "https://bugs.example.com" |
| 25 |
|
| 26 |
|
| 27 |
@pytest.fixture |
| 28 |
def scanner(): |
| 29 |
return SecurityScanner(db=DB_PATH, distro="trixie") |
| 30 |
|
| 31 |
|
| 32 |
@pytest.fixture(scope="module") |
| 33 |
def vex_schema(): |
| 34 |
with open("tests/data/openvex_json_schema_0.2.0.json") as f: |
| 35 |
return json.load(f) |
| 36 |
|
| 37 |
|
| 38 |
class GraphWalkerStub(GraphWalker): |
| 39 |
def __init__(self, pkg_path: list[Package]): |
| 40 |
self.pkg_path = pkg_path |
| 41 |
|
| 42 |
def all_shortest(self, _) -> Iterable[list[PackageRepr]]: |
| 43 |
return [[PackageRepr(name=p.name, ref=f"Ref-{p.name}") for p in self.pkg_path]] |
| 44 |
|
| 45 |
|
| 46 |
def test_scan_finds_vulnerabilities(scanner): |
| 47 |
pkgs = [SourcePackage(name="fake-shell", version="5.2.37-2")] |
| 48 |
results = list(scanner.scan(pkgs, min_urgency=CveUrgency.NOT_YET_ASSIGNED)) |
| 49 |
cves = {r.vulnerability.cve for r in results} |
| 50 |
assert "CVE-0000-0001" in cves |
| 51 |
assert "TEMP-0000-000001" in cves |
| 52 |
assert len(results) == 2 |
| 53 |
|
| 54 |
|
| 55 |
def test_scan_affected_resolved_not_affected(scanner): |
| 56 |
"""Package version >= fixed_version -> not affected.""" |
| 57 |
pkgs = [SourcePackage(name="fake-shell", version="5.2.37-2")] |
| 58 |
results = list(scanner.scan(pkgs)) |
| 59 |
by_cve = {r.vulnerability.cve: r for r in results} |
| 60 |
assert not by_cve["CVE-0000-0001"].affected |
| 61 |
|
| 62 |
|
| 63 |
def test_scan_affected_open_no_fix(scanner): |
| 64 |
"""Open status with no fixed_version -> affected.""" |
| 65 |
pkgs = [SourcePackage(name="fake-shell", version="5.2.37-2")] |
| 66 |
results = list(scanner.scan(pkgs)) |
| 67 |
by_cve = {r.vulnerability.cve: r for r in results} |
| 68 |
assert by_cve["TEMP-0000-000001"].affected |
| 69 |
|
| 70 |
|
| 71 |
def test_scan_affected_resolved_outdated(scanner): |
| 72 |
"""Package version < fixed_version -> affected despite resolved status.""" |
| 73 |
pkgs = [SourcePackage(name="fake-crypto", version="3.4.0-1")] |
| 74 |
results = list(scanner.scan(pkgs)) |
| 75 |
by_cve = {r.vulnerability.cve: r for r in results} |
| 76 |
assert by_cve["CVE-0000-1001"].affected |
| 77 |
assert by_cve["CVE-0000-1003"].affected |
| 78 |
|
| 79 |
|
| 80 |
def test_scan_affected_resolved_up_to_date(scanner): |
| 81 |
"""Package version >= fixed_version -> not affected.""" |
| 82 |
pkgs = [SourcePackage(name="fake-crypto", version="3.5.0-1")] |
| 83 |
results = list(scanner.scan(pkgs)) |
| 84 |
by_cve = {r.vulnerability.cve: r for r in results} |
| 85 |
assert not by_cve["CVE-0000-1001"].affected |
| 86 |
assert not by_cve["CVE-0000-1003"].affected |
| 87 |
|
| 88 |
|
| 89 |
def test_scan_affected_open_always(scanner): |
| 90 |
"""Open status -> always affected regardless of version.""" |
| 91 |
pkgs = [SourcePackage(name="fake-crypto", version="99.0-1")] |
| 92 |
results = list(scanner.scan(pkgs, min_urgency=CveUrgency.NOT_YET_ASSIGNED)) |
| 93 |
by_cve = {r.vulnerability.cve: r for r in results} |
| 94 |
assert by_cve["CVE-0000-1002"].affected |
| 95 |
|
| 96 |
|
| 97 |
def test_scan_unknown_package(scanner): |
| 98 |
pkgs = [SourcePackage(name="nonexistent", version="1.0-1")] |
| 99 |
results = list(scanner.scan(pkgs)) |
| 100 |
assert len(results) == 0 |
| 101 |
|
| 102 |
|
| 103 |
def test_scan_urgency_high(scanner): |
| 104 |
"""Only high urgency CVEs returned when min_urgency=HIGH.""" |
| 105 |
pkgs = [SourcePackage(name="fake-crypto", version="3.4.0-1")] |
| 106 |
results = list(scanner.scan(pkgs, min_urgency=CveUrgency.HIGH)) |
| 107 |
assert len(results) == 1 |
| 108 |
assert results[0].vulnerability.cve == "CVE-0000-1001" |
| 109 |
assert results[0].vulnerability.urgency == CveUrgency.HIGH |
| 110 |
|
| 111 |
|
| 112 |
def test_scan_urgency_medium(scanner): |
| 113 |
"""High and medium urgency CVEs returned when min_urgency=MEDIUM.""" |
| 114 |
pkgs = [SourcePackage(name="fake-crypto", version="3.4.0-1")] |
| 115 |
results = list(scanner.scan(pkgs, min_urgency=CveUrgency.MEDIUM)) |
| 116 |
cves = {r.vulnerability.cve for r in results} |
| 117 |
assert cves == {"CVE-0000-1001", "CVE-0000-1002"} |
| 118 |
|
| 119 |
|
| 120 |
def test_scan_urgency_low(scanner): |
| 121 |
"""All three fake-crypto CVEs returned when min_urgency=LOW.""" |
| 122 |
pkgs = [SourcePackage(name="fake-crypto", version="3.4.0-1")] |
| 123 |
results = list(scanner.scan(pkgs, min_urgency=CveUrgency.LOW)) |
| 124 |
assert len(results) == 3 |
| 125 |
|
| 126 |
|
| 127 |
def test_scan_urgency_end_of_life(scanner): |
| 128 |
pkgs = [SourcePackage(name="fake-compress", version="1:1.3.0-1")] |
| 129 |
results = list(scanner.scan(pkgs, min_urgency=CveUrgency.END_OF_LIFE)) |
| 130 |
assert len(results) == 1 |
| 131 |
assert results[0].vulnerability.urgency == CveUrgency.END_OF_LIFE |
| 132 |
|
| 133 |
|
| 134 |
def test_scan_min_urgency_filtering(scanner): |
| 135 |
pkgs = [SourcePackage(name="fake-shell", version="5.2.37-2")] |
| 136 |
# TEMP-0000-000001 urgency=unimportant |
| 137 |
results = list(scanner.scan(pkgs, min_urgency=CveUrgency.UNIMPORTANT)) |
| 138 |
assert len(results) == 1 |
| 139 |
|
| 140 |
|
| 141 |
def test_scan_multiple_packages(scanner): |
| 142 |
pkgs = [ |
| 143 |
SourcePackage(name="fake-shell", version="5.2.37-2"), |
| 144 |
SourcePackage(name="fake-crypto", version="3.4.0-1"), |
| 145 |
SourcePackage(name="fake-compress", version="1:1.3.0-1"), |
| 146 |
] |
| 147 |
results = list(scanner.scan(pkgs)) |
| 148 |
packages = {r.package.name for r in results} |
| 149 |
assert packages == {"fake-shell", "fake-crypto", "fake-compress"} |
| 150 |
assert len(results) == 6 # fake-shell:2 + fake-crypto:3 + fake-compress:1 |
| 151 |
|
| 152 |
|
| 153 |
def test_scan_name_filter(scanner): |
| 154 |
pkgs = [ |
| 155 |
SourcePackage(name="fake-shell", version="5.2.37-2"), |
| 156 |
SourcePackage(name="fake-crypto", version="3.4.0-1"), |
| 157 |
] |
| 158 |
results = list(scanner.scan(pkgs, name_filter="fake-crypto")) |
| 159 |
assert all(r.package.name == "fake-crypto" for r in results) |
| 160 |
assert len(results) == 3 |
| 161 |
|
| 162 |
|
| 163 |
def test_scan_name_filter_no_match(scanner): |
| 164 |
pkgs = [SourcePackage(name="fake-shell", version="5.2.37-2")] |
| 165 |
results = list(scanner.scan(pkgs, name_filter="other")) |
| 166 |
assert len(results) == 0 |
| 167 |
|
| 168 |
|
| 169 |
def test_scan_distro_bookworm(): |
| 170 |
"""bookworm only has fake-crypto CVE-0000-1001.""" |
| 171 |
scanner = SecurityScanner(db=DB_PATH, distro="bookworm") |
| 172 |
pkgs = [ |
| 173 |
SourcePackage(name="fake-shell", version="5.2.37-2"), |
| 174 |
SourcePackage(name="fake-crypto", version="3.0.14-1"), |
| 175 |
] |
| 176 |
results = list(scanner.scan(pkgs)) |
| 177 |
assert len(results) == 1 |
| 178 |
assert results[0].vulnerability.cve == "CVE-0000-1001" |
| 179 |
assert results[0].affected |
| 180 |
|
| 181 |
|
| 182 |
def test_scan_distro_no_entries(): |
| 183 |
scanner = SecurityScanner(db=DB_PATH, distro="stretch") |
| 184 |
pkgs = [SourcePackage(name="fake-shell", version="5.2.37-2")] |
| 185 |
results = list(scanner.scan(pkgs)) |
| 186 |
assert len(results) == 0 |
| 187 |
|
| 188 |
|
| 189 |
def test_scan_vulnerability_fields(scanner): |
| 190 |
"""Verify all fields on returned CveEntry are populated correctly.""" |
| 191 |
pkgs = [SourcePackage(name="fake-crypto", version="3.4.0-1")] |
| 192 |
results = list(scanner.scan(pkgs, min_urgency=CveUrgency.HIGH)) |
| 193 |
v = results[0].vulnerability |
| 194 |
assert v.cve == "CVE-0000-1001" |
| 195 |
assert v.description == "Buffer overflow in fake-crypto allowing remote code execution." |
| 196 |
assert v.status == CveStatus.RESOLVED |
| 197 |
assert v.fixed_version == "3.5.0-1" |
| 198 |
assert v.urgency == CveUrgency.HIGH |
| 199 |
|
| 200 |
|
| 201 |
def test_scan_result_matches_schema(scanner): |
| 202 |
"""Validate that JSON output of scan results conforms to the schema.""" |
| 203 |
pkgs = [ |
| 204 |
SourcePackage(name="fake-shell", version="5.2.37-2"), |
| 205 |
SourcePackage(name="fake-crypto", version="3.4.0-1"), |
| 206 |
SourcePackage(name="fake-compress", version="1:1.3.0-1"), |
| 207 |
] |
| 208 |
results = list(scanner.scan(pkgs, min_urgency=CveUrgency.NOT_YET_ASSIGNED)) |
| 209 |
|
| 210 |
buf = io.StringIO() |
| 211 |
with ScanResultWriter.create("json", sdo_url=TRACKER_URL, bdo_url=BUGS_URL, file=buf) as writer: |
| 212 |
for r in results: |
| 213 |
writer.write(r) |
| 214 |
|
| 215 |
lines = buf.getvalue().strip().splitlines() |
| 216 |
assert len(lines) > 0 |
| 217 |
for line in lines: |
| 218 |
data = json.loads(line) |
| 219 |
jsonschema.validate(instance=data, schema=secscan) |
| 220 |
|
| 221 |
|
| 222 |
def test_scan_result_path_matches_schema(scanner): |
| 223 |
""" |
| 224 |
Validate that JSON output (with path) of scan results conforms to the schema. |
| 225 |
|
| 226 |
As the schema uses an external reference, we need a registry for resolving. |
| 227 |
This requires a fairly new version of jsonschema with the referencing support, |
| 228 |
which is not widely available across distros (it is a pure-test dependency). |
| 229 |
To allow to run the testsuite against older versions as well, we disable |
| 230 |
the test conditionally. |
| 231 |
""" |
| 232 |
pytest.importorskip("referencing") |
| 233 |
|
| 234 |
from referencing import Registry, Resource |
| 235 |
|
| 236 |
_secscan_registry = Registry().with_resource( |
| 237 |
schema_tracepath["$id"], Resource.from_contents(schema_tracepath) |
| 238 |
) |
| 239 |
|
| 240 |
src_pkg = SourcePackage(name="fake-shell", version="5.2.37-2") |
| 241 |
bin_pkg = BinaryPackage( |
| 242 |
name="fake-shell-bin", |
| 243 |
version="5.2.37-2", |
| 244 |
source=Dependency(name="fake-shell", version=("=", Version("5.2.37-2"))), |
| 245 |
) |
| 246 |
pkgs = [src_pkg, bin_pkg] |
| 247 |
results = list(scanner.scan([src_pkg], min_urgency=CveUrgency.NOT_YET_ASSIGNED)) |
| 248 |
gw = GraphWalkerStub(pkgs) |
| 249 |
|
| 250 |
buf = io.StringIO() |
| 251 |
with ScanResultWriter.create( |
| 252 |
"json", sdo_url=TRACKER_URL, bdo_url=BUGS_URL, graph_walker=gw, file=buf |
| 253 |
) as writer: |
| 254 |
for r in results: |
| 255 |
writer.write(r) |
| 256 |
|
| 257 |
lines = buf.getvalue().strip().splitlines() |
| 258 |
assert len(lines) > 0 |
| 259 |
validator = jsonschema.protocols.Validator(secscan, registry=_secscan_registry) |
| 260 |
for line in lines: |
| 261 |
data = json.loads(line) |
| 262 |
validator.validate(data) |
| 263 |
|
| 264 |
|
| 265 |
def test_scan_result_with_tracker_matches_schema(scanner): |
| 266 |
"""Validate schema conformance when tracker URL is present.""" |
| 267 |
pkgs = [SourcePackage(name="fake-crypto", version="3.4.0-1")] |
| 268 |
results = list(scanner.scan(pkgs, min_urgency=CveUrgency.HIGH)) |
| 269 |
|
| 270 |
buf = io.StringIO() |
| 271 |
with ScanResultWriter.create("json", sdo_url=TRACKER_URL, bdo_url=BUGS_URL, file=buf) as writer: |
| 272 |
for r in results: |
| 273 |
writer.write(r) |
| 274 |
|
| 275 |
lines = buf.getvalue().strip().splitlines() |
| 276 |
assert len(lines) == 1 |
| 277 |
data = json.loads(lines[0]) |
| 278 |
jsonschema.validate(instance=data, schema=secscan) |
| 279 |
assert data["vulnerability"]["tracker"] == f"{TRACKER_URL}/CVE-0000-1001" |
| 280 |
|
| 281 |
|
| 282 |
def test_vex_output_matches_schema(scanner, vex_schema): |
| 283 |
"""Validate that VEX output conforms to the OpenVEX JSON schema.""" |
| 284 |
pkgs = [ |
| 285 |
SourcePackage(name="fake-shell", version="5.2.37-2"), |
| 286 |
SourcePackage(name="fake-crypto", version="3.4.0-1"), |
| 287 |
] |
| 288 |
results = list(scanner.scan(pkgs, min_urgency=CveUrgency.NOT_YET_ASSIGNED)) |
| 289 |
|
| 290 |
buf = io.StringIO() |
| 291 |
with ScanResultWriter.create( |
| 292 |
"vex", sdo_url=TRACKER_URL, bdo_url=BUGS_URL, author="test-author", file=buf |
| 293 |
) as writer: |
| 294 |
for r in results: |
| 295 |
writer.write(r) |
| 296 |
|
| 297 |
data = json.loads(buf.getvalue()) |
| 298 |
jsonschema.validate(instance=data, schema=vex_schema) |
| 299 |
assert data["author"] == "test-author" |
| 300 |
assert len(data["statements"]) == len(results) |
| 301 |
|
| 302 |
|
| 303 |
def test_vex_output_includes_binary_products(scanner, vex_schema): |
| 304 |
"""VEX products include binary packages mapped to the affected source package.""" |
| 305 |
src = SourcePackage(name="fake-crypto", version="3.4.0-1") |
| 306 |
pkgs = [src] |
| 307 |
results = list(scanner.scan(pkgs, min_urgency=CveUrgency.HIGH)) |
| 308 |
assert len(results) == 1 |
| 309 |
|
| 310 |
bin1 = BinaryPackage( |
| 311 |
name="libfake-crypto3", version="3.4.0-1", architecture="amd64", source=Dependency(src.name) |
| 312 |
) |
| 313 |
bin2 = BinaryPackage( |
| 314 |
name="libfake-crypto-dev", |
| 315 |
version="3.4.0-1", |
| 316 |
architecture="amd64", |
| 317 |
source=Dependency(src.name), |
| 318 |
) |
| 319 |
src.binaries = [bin1.name, bin2.name] |
| 320 |
pkgs = [src, bin1, bin2] |
| 321 |
|
| 322 |
buf = io.StringIO() |
| 323 |
with ScanResultWriter.create( |
| 324 |
"vex", |
| 325 |
sdo_url=TRACKER_URL, |
| 326 |
bdo_url=BUGS_URL, |
| 327 |
author="test-author", |
| 328 |
file=buf, |
| 329 |
packages=pkgs, |
| 330 |
) as writer: |
| 331 |
for r in results: |
| 332 |
writer.write(r) |
| 333 |
|
| 334 |
data = json.loads(buf.getvalue()) |
| 335 |
jsonschema.validate(instance=data, schema=vex_schema) |
| 336 |
|
| 337 |
products = data["statements"][0]["products"] |
| 338 |
product_purls = [p["identifiers"]["purl"] for p in products] |
| 339 |
assert str(src.purl()) in product_purls |
| 340 |
assert str(bin1.purl()) in product_purls |
| 341 |
assert str(bin2.purl()) in product_purls |
| 342 |
assert len(products) == 3 |
| 343 |
|
| 344 |
|
| 345 |
def test_vex_output_no_binary_map(scanner, vex_schema): |
| 346 |
"""VEX output without source_binary_map still works (only source product).""" |
| 347 |
src = SourcePackage(name="fake-crypto", version="3.4.0-1") |
| 348 |
results = list(scanner.scan([src], min_urgency=CveUrgency.HIGH)) |
| 349 |
|
| 350 |
buf = io.StringIO() |
| 351 |
with ScanResultWriter.create( |
| 352 |
"vex", sdo_url=TRACKER_URL, bdo_url=BUGS_URL, author="test-author", file=buf |
| 353 |
) as writer: |
| 354 |
for r in results: |
| 355 |
writer.write(r) |
| 356 |
|
| 357 |
data = json.loads(buf.getvalue()) |
| 358 |
jsonschema.validate(instance=data, schema=vex_schema) |
| 359 |
products = data["statements"][0]["products"] |
| 360 |
assert len(products) == 1 |
| 361 |
assert products[0]["identifiers"]["purl"] == str(src.purl()) |
| 362 |
|
| 363 |
|
| 364 |
def test_vex_with_product(scanner, vex_schema): |
| 365 |
"""VEX output with automatically derived product.""" |
| 366 |
src = SourcePackage(name="fake-crypto", version="3.4.0-1") |
| 367 |
results = list(scanner.scan([src], min_urgency=CveUrgency.HIGH)) |
| 368 |
|
| 369 |
buf = io.StringIO() |
| 370 |
with ScanResultWriter.create( |
| 371 |
"vex", |
| 372 |
sdo_url=TRACKER_URL, |
| 373 |
bdo_url=BUGS_URL, |
| 374 |
author="test-author", |
| 375 |
file=buf, |
| 376 |
product="Product", |
| 377 |
) as writer: |
| 378 |
for r in results: |
| 379 |
writer.write(r) |
| 380 |
|
| 381 |
data = json.loads(buf.getvalue()) |
| 382 |
jsonschema.validate(instance=data, schema=vex_schema) |
| 383 |
|
| 384 |
statement = data["statements"][0] |
| 385 |
product = statement["products"][0] |
| 386 |
assert product["@id"] == "Product" |
| 387 |
assert "deb/debian/fake-crypto" in product["subcomponents"][0]["@id"] |
| 388 |
|
| 389 |
|
| 390 |
def test_distro_from_package(scanner, vex_schema): |
| 391 |
"""Test if CVEs get assigned correctly based on package distribution information.""" |
| 392 |
package = SourcePackage(name="fake-shell", version="5.2.37-2") |
| 393 |
package.distro = "forky" |
| 394 |
|
| 395 |
results = list(scanner.scan([package], min_urgency=CveUrgency.NOT_YET_ASSIGNED)) |
| 396 |
cves = {r.vulnerability.cve for r in results} |
| 397 |
assert "TEMP-0000-000001" in cves |
| 398 |
assert len(results) == 1 |
| 399 |
|
| 400 |
package.distro = "sid" |
| 401 |
results = list(scanner.scan([package], min_urgency=CveUrgency.NOT_YET_ASSIGNED)) |
| 402 |
cves = {r.vulnerability.cve for r in results} |
| 403 |
assert "TEMP-0000-000001" in cves |
| 404 |
assert "CVE-0000-0001" in cves |
| 405 |
assert len(results) == 2 |