| 1 |
# Copyright (C) 2025 Siemens |
| 2 |
# |
| 3 |
# SPDX-License-Identifier: MIT |
| 4 |
|
| 5 |
import io |
| 6 |
import json |
| 7 |
from pathlib import Path |
| 8 |
import jsonschema |
| 9 |
|
| 10 |
import pytest |
| 11 |
from debsbom.download.adapters import LocalFileAdapter |
| 12 |
from debsbom.download import ( |
| 13 |
PackageDownloader, |
| 14 |
PersistentResolverCache, |
| 15 |
) |
| 16 |
from debsbom.download.download import DownloadResult, DownloadStatus |
| 17 |
from debsbom.resolver import PackageResolver, PackageStreamResolver |
| 18 |
from debsbom import schema |
| 19 |
from debsbom.dpkg.package import ( |
| 20 |
BinaryPackage, |
| 21 |
SourcePackage, |
| 22 |
ChecksumAlgo, |
| 23 |
filter_binaries, |
| 24 |
filter_sources, |
| 25 |
) |
| 26 |
from debsbom.repack.packer import BomTransformer, Packer |
| 27 |
from debsbom.snapshot.client import SnapshotRemoteFile, UpstreamResolver |
| 28 |
import debsbom.snapshot.client as sdlclient |
| 29 |
|
| 30 |
from requests import Session |
| 31 |
|
| 32 |
from unittest import mock |
| 33 |
|
| 34 |
|
| 35 |
@pytest.fixture() |
| 36 |
def spdx_bomfile(tmpdir): |
| 37 |
""" |
| 38 |
Return the path to a minimal spdx sbom file |
| 39 |
""" |
| 40 |
_spdx_tools = pytest.importorskip("spdx_tools") |
| 41 |
|
| 42 |
from debsbom.generate.spdx import spdx_bom |
| 43 |
|
| 44 |
import spdx_tools.spdx.writer.json.json_writer as spdx_json_writer |
| 45 |
|
| 46 |
pkgs = BinaryPackage.parse_status_file(Path("tests/data/dpkg-status-minimal")) |
| 47 |
bom = spdx_bom(set(pkgs), "debian", "amd64") |
| 48 |
outfile = Path(tmpdir) / "bom.spdx.json" |
| 49 |
spdx_json_writer.write_document_to_file(bom, outfile, False) |
| 50 |
return outfile |
| 51 |
|
| 52 |
|
| 53 |
@pytest.fixture() |
| 54 |
def cdx_bomfile(tmpdir): |
| 55 |
""" |
| 56 |
Return the path to a cdx minimal sbom file |
| 57 |
""" |
| 58 |
_cyclonedx = pytest.importorskip("cyclonedx") |
| 59 |
|
| 60 |
from debsbom.generate.cdx import cyclonedx_bom |
| 61 |
|
| 62 |
import cyclonedx.output as cdx_output |
| 63 |
import cyclonedx.schema as cdx_schema |
| 64 |
|
| 65 |
pkgs = BinaryPackage.parse_status_file(Path("tests/data/dpkg-status-minimal")) |
| 66 |
bom = cyclonedx_bom(set(pkgs), "debian", "amd64") |
| 67 |
outfile = Path(tmpdir) / "bom.cdx.json" |
| 68 |
cdx_output.make_outputter( |
| 69 |
bom, cdx_schema.OutputFormat.JSON, cdx_schema.SchemaVersion.V1_6 |
| 70 |
).output_to_file(outfile) |
| 71 |
return outfile |
| 72 |
|
| 73 |
|
| 74 |
@pytest.mark.online |
| 75 |
def test_download(tmpdir, http_session): |
| 76 |
dl = PackageDownloader(Path(tmpdir), session=http_session) |
| 77 |
test_file = SnapshotRemoteFile( |
| 78 |
checksums={ChecksumAlgo.SHA1SUM: "1f3a43c181b81e3578d609dc0931ff147623eb38"}, |
| 79 |
filename="pytest_8.4.2-1.dsc", |
| 80 |
size=2757, |
| 81 |
archive_name="debian", |
| 82 |
path="/pool/main/p/pytest", |
| 83 |
first_seen=1757270199, |
| 84 |
downloadurl="https://snapshot.debian.org/file/1f3a43c181b81e3578d609dc0931ff147623eb38/pytest_8.4.2-1.dsc", |
| 85 |
architecture=None, |
| 86 |
) |
| 87 |
pkg = BinaryPackage("foo", "1.0") |
| 88 |
dl.register([test_file], pkg) |
| 89 |
assert dl.stat() == (1, 2757, 0, 0) |
| 90 |
mock_cb = mock.Mock() |
| 91 |
downloaded = list(dl.download(mock_cb)) |
| 92 |
mock_cb.assert_called_once_with(0, 1, test_file.filename) |
| 93 |
|
| 94 |
assert len(downloaded) == 1 |
| 95 |
assert downloaded[0].path.is_file() |
| 96 |
assert downloaded[0].path.is_relative_to(Path(tmpdir)) |
| 97 |
stat_orig = downloaded[0].path.stat() |
| 98 |
assert downloaded[0].status == DownloadStatus.OK |
| 99 |
|
| 100 |
# now download again (which should not actually download) |
| 101 |
dl.register([test_file], pkg) |
| 102 |
downloaded = list(dl.download(None)) |
| 103 |
assert int(downloaded[0].path.stat().st_mtime_ns) == int(stat_orig.st_mtime_ns) |
| 104 |
assert downloaded[0].status == DownloadStatus.OK |
| 105 |
|
| 106 |
# tamper the checksum of the downloaded file. Must result in re-download |
| 107 |
with open(downloaded[0].path, "w+") as f: |
| 108 |
f.write("append") |
| 109 |
dl.register([test_file], pkg) |
| 110 |
downloaded = list(dl.download(None)) |
| 111 |
assert int(downloaded[0].path.stat().st_mtime_ns) != int(stat_orig.st_mtime_ns) |
| 112 |
assert downloaded[0].status == DownloadStatus.OK |
| 113 |
|
| 114 |
|
| 115 |
def test_package_resolver_parse_spdx(spdx_bomfile): |
| 116 |
rs = PackageResolver.create(spdx_bomfile) |
| 117 |
pkgs = list(rs) |
| 118 |
assert any(filter(lambda p: p.name == "binutils", filter_sources(pkgs))) |
| 119 |
assert any(filter(lambda p: p.architecture == "amd64", filter_binaries(pkgs))) |
| 120 |
|
| 121 |
|
| 122 |
def test_package_resolver_parse_cdx(cdx_bomfile): |
| 123 |
rs = PackageResolver.create(cdx_bomfile) |
| 124 |
assert any(filter(lambda p: p.architecture == "amd64", rs)) |
| 125 |
|
| 126 |
|
| 127 |
def test_package_resolver_parse_stream(): |
| 128 |
data = [ |
| 129 |
"binutils 2.40-2 amd64", |
| 130 |
"guestfs-tools 1.52.3-1 source", |
| 131 |
] |
| 132 |
stream = io.BytesIO("\n".join(data).encode()) |
| 133 |
rs = PackageStreamResolver(stream) |
| 134 |
pkgs = list(rs) |
| 135 |
assert any(filter(lambda p: p.name == "guestfs-tools", filter_sources(pkgs))) |
| 136 |
assert any(filter(lambda p: p.name == "binutils", filter_binaries(pkgs))) |
| 137 |
|
| 138 |
|
| 139 |
@pytest.mark.online |
| 140 |
def test_package_resolver_resolve_spdx(spdx_bomfile, tmpdir, sdl): |
| 141 |
cachedir = Path(tmpdir) / ".cache" |
| 142 |
prs = PackageResolver.create(spdx_bomfile) |
| 143 |
rs_cache = PersistentResolverCache(cachedir) |
| 144 |
urs = UpstreamResolver(sdl, rs_cache) |
| 145 |
|
| 146 |
files = list(urs.resolve(next(filter(lambda p: p.name == "binutils", prs)))) |
| 147 |
assert "binutils" in files[0].filename |
| 148 |
|
| 149 |
# resolve with cache |
| 150 |
prs = PackageResolver.create(spdx_bomfile) |
| 151 |
files = list(urs.resolve(next(filter(lambda p: p.name == "binutils", prs)))) |
| 152 |
assert "binutils" in files[0].filename |
| 153 |
|
| 154 |
|
| 155 |
@pytest.mark.online |
| 156 |
def test_file_checksum(sdl, tmpdir, http_session): |
| 157 |
bpkg = BinaryPackage( |
| 158 |
name="binutils-arm-none-eabi", architecture="amd64", version="2.40-2+18+b1" |
| 159 |
) |
| 160 |
bpkg.checksums[ChecksumAlgo.SHA256SUM] = ( |
| 161 |
"c8f9da2a434366bfe5a66a8267cb3b1df028f1d95278715050c222b43e1c221c" |
| 162 |
) |
| 163 |
s_bpkg = sdlclient.BinaryPackage(sdl, bpkg.name, str(bpkg.version), None, None) |
| 164 |
files = list(s_bpkg.files(arch=bpkg.architecture)) |
| 165 |
|
| 166 |
dl = PackageDownloader(Path(tmpdir), session=http_session) |
| 167 |
|
| 168 |
# test matching checksum case |
| 169 |
dl.register(files, bpkg) |
| 170 |
stats = dl.stat() |
| 171 |
assert stats.files == 1 |
| 172 |
local_files = list(dl.download()) |
| 173 |
assert local_files[0].status == DownloadStatus.OK |
| 174 |
|
| 175 |
# test invalid checksum |
| 176 |
local_files[0].path.unlink() |
| 177 |
# tamper checksum (sha256sum of '42') |
| 178 |
bpkg.checksums[ChecksumAlgo.SHA256SUM] = ( |
| 179 |
"084c799cd551dd1d8d5c5f9a5d593b2e931f5e36122ee5c793c1d08a19839cc0" |
| 180 |
) |
| 181 |
dl.register(files, bpkg) |
| 182 |
stats = dl.stat() |
| 183 |
assert stats.files == 1 |
| 184 |
local_files = list(dl.download()) |
| 185 |
# no file was successfully downloaded |
| 186 |
assert local_files[0].status == DownloadStatus.CHECKSUM_MISMATCH |
| 187 |
|
| 188 |
|
| 189 |
@pytest.mark.online |
| 190 |
def test_repack(tmpdir, spdx_bomfile, cdx_bomfile, http_session, sdl): |
| 191 |
tmpdir = Path(tmpdir) |
| 192 |
|
| 193 |
dl_dir = tmpdir / "downloads" |
| 194 |
out_dir = tmpdir / "outdir" |
| 195 |
dl_dir.mkdir() |
| 196 |
out_dir.mkdir() |
| 197 |
|
| 198 |
packer = Packer.from_format( |
| 199 |
fmt="standard-bom-package", |
| 200 |
dldir=dl_dir, |
| 201 |
outdir=out_dir, |
| 202 |
) |
| 203 |
|
| 204 |
found_spdx = False |
| 205 |
found_cdx = False |
| 206 |
urs = UpstreamResolver(sdl) |
| 207 |
for bom in [spdx_bomfile, cdx_bomfile]: |
| 208 |
resolver = PackageResolver.create(bom) |
| 209 |
pkgs = list(resolver) |
| 210 |
|
| 211 |
# download a single package |
| 212 |
dl = PackageDownloader(dl_dir, session=http_session) |
| 213 |
for p in filter(lambda p: p.name == "binutils", filter_sources(pkgs)): |
| 214 |
dl.register(urs.resolve(p), p) |
| 215 |
files = list(dl.download()) |
| 216 |
assert len(files) == 3 |
| 217 |
|
| 218 |
# merge the source package |
| 219 |
bt = BomTransformer.create("standard-bom-package", resolver.sbom_type(), resolver.document) |
| 220 |
repacked = list(filter(lambda p: p, map(lambda p: packer.repack(p, symlink=True), pkgs))) |
| 221 |
assert len(repacked) == 1 |
| 222 |
assert ".merged.tar" in repacked[0].filename |
| 223 |
bom_out = packer.rewrite_sbom(bt, repacked) |
| 224 |
|
| 225 |
# check if the sbom is updated |
| 226 |
if "spdx" in bom.name: |
| 227 |
found_spdx = True |
| 228 |
found_updated_locator = False |
| 229 |
for p in bom_out.packages: |
| 230 |
if p.external_references: |
| 231 |
if any([ref.locator.startswith("file:///") for ref in p.external_references]): |
| 232 |
found_updated_locator = True |
| 233 |
break |
| 234 |
assert found_updated_locator |
| 235 |
if "cdx" in bom.name: |
| 236 |
found_cdx = True |
| 237 |
for c in bom_out.components: |
| 238 |
if c.external_references: |
| 239 |
if any([str(ref.url).startswith("file:///") for ref in c.external_references]): |
| 240 |
found_updated_locator = True |
| 241 |
break |
| 242 |
assert found_updated_locator |
| 243 |
|
| 244 |
assert found_spdx |
| 245 |
assert found_cdx |
| 246 |
|
| 247 |
|
| 248 |
@pytest.mark.online |
| 249 |
def test_srcpkg_with_checksum(sdl): |
| 250 |
rs = UpstreamResolver(sdl) |
| 251 |
# package with multiple src packages with equal version number |
| 252 |
sratom = SourcePackage("sratom", "0.6.14-1") |
| 253 |
|
| 254 |
# resolve without further information |
| 255 |
files = list(rs.resolve(sratom)) |
| 256 |
for e in [".dsc", ".debian.tar.xz", ".orig.tar.xz", "orig.tar.xz.asc"]: |
| 257 |
assert len(list(filter(lambda f: f.filename.endswith(e), files))) == 3 |
| 258 |
assert files[0].archive_name == "debian" |
| 259 |
|
| 260 |
# resolve with checksum of debian-ports hurd-amd64 variant |
| 261 |
sratom.checksums[ChecksumAlgo.SHA256SUM] = ( |
| 262 |
"4619ccf1ad73f96d08b7709ea768523f16c60ecac68eb269614c1950ba776508" |
| 263 |
) |
| 264 |
files = list(rs.resolve(sratom)) |
| 265 |
assert len(files) == 4 |
| 266 |
assert files[0].archive_name == "debian-ports" |
| 267 |
|
| 268 |
|
| 269 |
@pytest.mark.parametrize( |
| 270 |
"dlresult", |
| 271 |
[ |
| 272 |
DownloadResult( |
| 273 |
Path("/tmp/foo.tar"), DownloadStatus.OK, SourcePackage("foo", "1.0"), "foo.tar" |
| 274 |
), |
| 275 |
DownloadResult(None, DownloadStatus.NOT_FOUND, SourcePackage("foo", "1.0"), "bar.tar"), |
| 276 |
DownloadResult( |
| 277 |
None, DownloadStatus.CHECKSUM_MISMATCH, BinaryPackage("bar", "1.0"), "bar.tar" |
| 278 |
), |
| 279 |
], |
| 280 |
) |
| 281 |
def test_download_result_format(dlresult): |
| 282 |
data = json.loads(dlresult.json()) |
| 283 |
if data["status"] == DownloadStatus.OK: |
| 284 |
assert data["status"] == "ok" |
| 285 |
|
| 286 |
jsonschema.validate(data, schema=schema.download) |
| 287 |
|
| 288 |
|
| 289 |
def test_download_result_invalid(): |
| 290 |
data = { |
| 291 |
"status": "unknown", |
| 292 |
"package": {"name": "foo", "version": "1.0"}, |
| 293 |
} |
| 294 |
with pytest.raises(jsonschema.ValidationError): |
| 295 |
jsonschema.validate(data, schema=schema.download) |
| 296 |
|
| 297 |
|
| 298 |
def test_local_file(): |
| 299 |
session = Session() |
| 300 |
session.mount("file:///", LocalFileAdapter()) |
| 301 |
with session.get("file://" + str(Path("tests/data/local-download").absolute())) as r: |
| 302 |
assert r.status_code == 200 |
| 303 |
assert r.content == b"This is a test file for the local file adapter test.\n" |
| 304 |
|
| 305 |
|
| 306 |
def test_local_file_404(): |
| 307 |
session = Session() |
| 308 |
session.mount("file:///", LocalFileAdapter()) |
| 309 |
with session.get("file:///does-not-exist") as r: |
| 310 |
assert r.status_code == 404 |
| 311 |
|
| 312 |
|
| 313 |
@pytest.mark.online |
| 314 |
def test_malicious_path_download(tmpdir, http_session): |
| 315 |
dl = PackageDownloader(Path(tmpdir), session=http_session) |
| 316 |
test_file = SnapshotRemoteFile( |
| 317 |
checksums={ChecksumAlgo.SHA1SUM: "1f3a43c181b81e3578d609dc0931ff147623eb38"}, |
| 318 |
# this filename would lead to a path traversal attack |
| 319 |
filename="../../../pytest_8.4.2-1.dsc", |
| 320 |
size=1234, |
| 321 |
archive_name="debian", |
| 322 |
path="/pool/main/p/pytest", |
| 323 |
first_seen=1757270199, |
| 324 |
downloadurl="https://snapshot.debian.org/file/1f3a43c181b81e3578d609dc0931ff147623eb38/pytest_8.4.2-1.dsc", |
| 325 |
architecture=None, |
| 326 |
) |
| 327 |
pkg = BinaryPackage("foo", "1.0") |
| 328 |
dl.register([test_file], pkg) |
| 329 |
with pytest.raises(ValueError): |
| 330 |
dl.stat() |
| 331 |
|
| 332 |
test_file2 = SnapshotRemoteFile( |
| 333 |
checksums={ChecksumAlgo.SHA1SUM: "1f3a43c181b81e3578d609dc0931ff147623eb38"}, |
| 334 |
filename="pytest_8.4.2-1.dsc", |
| 335 |
size=1234, |
| 336 |
# this filename would lead to a path traversal attack |
| 337 |
archive_name="../debian", |
| 338 |
path="/pool/main/p/pytest", |
| 339 |
first_seen=1757270199, |
| 340 |
downloadurl="https://snapshot.debian.org/file/1f3a43c181b81e3578d609dc0931ff147623eb38/pytest_8.4.2-1.dsc", |
| 341 |
architecture=None, |
| 342 |
) |
| 343 |
dl.register([test_file2], pkg) |
| 344 |
with pytest.raises(ValueError): |
| 345 |
dl.stat() |