| 1 |
# Copyright (C) 2026 Siemens |
| 2 |
# |
| 3 |
# SPDX-License-Identifier: MIT |
| 4 |
|
| 5 |
from io import BytesIO |
| 6 |
import logging |
| 7 |
from pathlib import Path |
| 8 |
import shutil |
| 9 |
import subprocess |
| 10 |
import tarfile |
| 11 |
|
| 12 |
import pytest |
| 13 |
|
| 14 |
from debsbom.generate.generate import Debsbom |
| 15 |
from debsbom.generate.rootfs import rootfs_directory |
| 16 |
from debsbom.util.compression import Compression |
| 17 |
|
| 18 |
requires_tarfile_filter = pytest.mark.skipif( |
| 19 |
not hasattr(tarfile, "data_filter"), |
| 20 |
reason="tarfile.data_filter is required to restrict extraction to rootfs metadata", |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
def _add_rootfs(archive: tarfile.TarFile) -> None: |
| 25 |
archive.add("tests/root/tree", arcname=".") |
| 26 |
ignored = tarfile.TarInfo("etc/not-needed") |
| 27 |
ignored.size = len(b"ignored") |
| 28 |
archive.addfile(ignored, BytesIO(b"ignored")) |
| 29 |
|
| 30 |
|
| 31 |
def _assert_scannable(root: Path) -> None: |
| 32 |
debsbom = Debsbom(distro_name="pytest-distro", root=root) |
| 33 |
debsbom.scan() |
| 34 |
|
| 35 |
assert debsbom.distro_arch == "amd64" |
| 36 |
assert "libc6" in {package.name for package in debsbom.packages} |
| 37 |
assert not (root / "etc/not-needed").exists() |
| 38 |
|
| 39 |
|
| 40 |
@requires_tarfile_filter |
| 41 |
def test_tar_rootfs_file(tmp_path): |
| 42 |
archive_path = tmp_path / "rootfs.tar" |
| 43 |
with tarfile.open(archive_path, mode="w") as archive: |
| 44 |
_add_rootfs(archive) |
| 45 |
|
| 46 |
with rootfs_directory(archive_path) as root: |
| 47 |
_assert_scannable(root) |
| 48 |
materialized_root = root |
| 49 |
|
| 50 |
assert not materialized_root.exists() |
| 51 |
|
| 52 |
|
| 53 |
def test_directory_rootfs_is_used_directly(): |
| 54 |
expected_root = Path("tests/root/tree") |
| 55 |
|
| 56 |
with rootfs_directory(expected_root) as root: |
| 57 |
assert root == expected_root |
| 58 |
_assert_scannable(root) |
| 59 |
|
| 60 |
|
| 61 |
@requires_tarfile_filter |
| 62 |
def test_tar_rootfs_stdin(): |
| 63 |
stream = BytesIO() |
| 64 |
with tarfile.open(fileobj=stream, mode="w") as archive: |
| 65 |
_add_rootfs(archive) |
| 66 |
stream.seek(0) |
| 67 |
|
| 68 |
with rootfs_directory("-", stream) as root: |
| 69 |
_assert_scannable(root) |
| 70 |
|
| 71 |
|
| 72 |
@requires_tarfile_filter |
| 73 |
@pytest.mark.parametrize("compression", Compression.formats(), ids=lambda comp: comp.tool) |
| 74 |
def test_compressed_tar_rootfs_file(tmp_path, compression): |
| 75 |
tool = shutil.which(compression.tool) |
| 76 |
if tool is None: |
| 77 |
pytest.skip(f"{compression.tool} is not installed") |
| 78 |
|
| 79 |
uncompressed_path = tmp_path / "rootfs.tar" |
| 80 |
with tarfile.open(uncompressed_path, mode="w") as archive: |
| 81 |
_add_rootfs(archive) |
| 82 |
archive_path = tmp_path / f"rootfs.tar{compression.fileext}" |
| 83 |
with uncompressed_path.open("rb") as uncompressed, archive_path.open("wb") as output: |
| 84 |
subprocess.run( |
| 85 |
[tool] + compression.compress, |
| 86 |
stdin=uncompressed, |
| 87 |
stdout=output, |
| 88 |
check=True, |
| 89 |
) |
| 90 |
|
| 91 |
with rootfs_directory(archive_path) as root: |
| 92 |
_assert_scannable(root) |
| 93 |
|
| 94 |
|
| 95 |
@requires_tarfile_filter |
| 96 |
def test_copyright_files_are_materialized_only_when_requested(tmp_path): |
| 97 |
archive_path = tmp_path / "rootfs.tar" |
| 98 |
with tarfile.open(archive_path, mode="w") as archive: |
| 99 |
archive.add("tests/root/copyright", arcname=".") |
| 100 |
ignored = tarfile.TarInfo("usr/share/doc/copyright-tests/changelog.gz") |
| 101 |
ignored.size = len(b"ignored") |
| 102 |
archive.addfile(ignored, BytesIO(b"ignored")) |
| 103 |
|
| 104 |
copyright_path = Path("usr/share/doc/copyright-tests/copyright") |
| 105 |
with rootfs_directory(archive_path) as root: |
| 106 |
assert not (root / copyright_path).exists() |
| 107 |
with rootfs_directory(archive_path, include_copyright=True) as root: |
| 108 |
assert (root / copyright_path).is_file() |
| 109 |
assert not (root / "usr/share/doc/copyright-tests/changelog.gz").exists() |
| 110 |
|
| 111 |
|
| 112 |
def test_copyright_directory_symlink_stays_inside_materialized_root(tmp_path): |
| 113 |
archive_path = tmp_path / "rootfs.tar" |
| 114 |
with tarfile.open(archive_path, mode="w") as archive: |
| 115 |
archive.add("tests/root/copyright", arcname=".") |
| 116 |
member = tarfile.TarInfo("usr/share/doc/alias-package") |
| 117 |
member.type = tarfile.SYMTYPE |
| 118 |
member.linkname = "copyright-tests" |
| 119 |
archive.addfile(member) |
| 120 |
|
| 121 |
with rootfs_directory(archive_path, include_copyright=True) as root: |
| 122 |
link = root / "usr/share/doc/alias-package" |
| 123 |
assert link.is_symlink() |
| 124 |
assert link.resolve().is_relative_to(root.resolve()) |
| 125 |
assert (link / "copyright").is_file() |
| 126 |
|
| 127 |
|
| 128 |
def _stdin_rootfs(member: tarfile.TarInfo, content: bytes | None = None) -> BytesIO: |
| 129 |
stream = BytesIO() |
| 130 |
with tarfile.open(fileobj=stream, mode="w") as archive: |
| 131 |
archive.addfile(member, BytesIO(content) if content is not None else None) |
| 132 |
stream.seek(0) |
| 133 |
return stream |
| 134 |
|
| 135 |
|
| 136 |
@requires_tarfile_filter |
| 137 |
def test_tar_rootfs_rejects_metadata_symlink_escape(): |
| 138 |
member = tarfile.TarInfo("var/lib/dpkg/status") |
| 139 |
member.type = tarfile.SYMTYPE |
| 140 |
member.linkname = "../../../../etc/passwd" |
| 141 |
|
| 142 |
with pytest.raises(tarfile.LinkOutsideDestinationError): |
| 143 |
with rootfs_directory("-", _stdin_rootfs(member)): |
| 144 |
pass |
| 145 |
|
| 146 |
|
| 147 |
@requires_tarfile_filter |
| 148 |
def test_tar_rootfs_rejects_parent_traversal(): |
| 149 |
member = tarfile.TarInfo("var/lib/apt/lists/../../../../../escape") |
| 150 |
member.size = len(b"escape") |
| 151 |
|
| 152 |
with pytest.raises(tarfile.OutsideDestinationError): |
| 153 |
with rootfs_directory("-", _stdin_rootfs(member, b"escape")): |
| 154 |
pass |
| 155 |
|
| 156 |
|
| 157 |
@requires_tarfile_filter |
| 158 |
def test_tar_rootfs_rejects_special_file(): |
| 159 |
member = tarfile.TarInfo("var/lib/dpkg/status") |
| 160 |
member.type = tarfile.CHRTYPE |
| 161 |
|
| 162 |
with pytest.raises(tarfile.SpecialFileError): |
| 163 |
with rootfs_directory("-", _stdin_rootfs(member)): |
| 164 |
pass |
| 165 |
|
| 166 |
|
| 167 |
@requires_tarfile_filter |
| 168 |
def test_tar_rootfs_skips_hard_link_to_skipped_member(caplog): |
| 169 |
stream = BytesIO() |
| 170 |
with tarfile.open(fileobj=stream, mode="w") as archive: |
| 171 |
target = tarfile.TarInfo("etc/passwd") |
| 172 |
target.size = len(b"root:x:0:0\n") |
| 173 |
archive.addfile(target, BytesIO(b"root:x:0:0\n")) |
| 174 |
member = tarfile.TarInfo("var/lib/dpkg/status") |
| 175 |
member.type = tarfile.LNKTYPE |
| 176 |
member.linkname = "etc/passwd" |
| 177 |
archive.addfile(member) |
| 178 |
stream.seek(0) |
| 179 |
|
| 180 |
with caplog.at_level(logging.WARNING): |
| 181 |
with rootfs_directory("-", stream) as root: |
| 182 |
assert not (root / "var/lib/dpkg/status").exists() |
| 183 |
assert not (root / "etc/passwd").exists() |
| 184 |
assert "hard link target 'etc/passwd'" in caplog.text |
| 185 |
|
| 186 |
|
| 187 |
def test_tar_rootfs_tolerates_partial_metadata(tmp_path): |
| 188 |
"""A rootfs without apt lists, extended states or arch-native still scans.""" |
| 189 |
archive_path = tmp_path / "rootfs.tar" |
| 190 |
with tarfile.open(archive_path, mode="w") as archive: |
| 191 |
archive.add("tests/root/arch-detect", arcname=".") |
| 192 |
|
| 193 |
with rootfs_directory(archive_path) as root: |
| 194 |
assert (root / "var/lib/dpkg/status").is_file() |
| 195 |
assert not (root / "var/lib/dpkg/arch-native").exists() |
| 196 |
assert not (root / "var/lib/apt").exists() |
| 197 |
|
| 198 |
debsbom = Debsbom(distro_name="pytest-distro", root=root) |
| 199 |
debsbom.scan() |
| 200 |
assert debsbom.packages |
| 201 |
|
| 202 |
|
| 203 |
def test_tar_rootfs_materializes_hard_link_between_metadata_files(): |
| 204 |
stream = BytesIO() |
| 205 |
with tarfile.open(fileobj=stream, mode="w") as archive: |
| 206 |
target = tarfile.TarInfo("var/lib/dpkg/status") |
| 207 |
target.size = len(b"Package: libc6\n") |
| 208 |
archive.addfile(target, BytesIO(b"Package: libc6\n")) |
| 209 |
member = tarfile.TarInfo("var/lib/apt/lists/copy") |
| 210 |
member.type = tarfile.LNKTYPE |
| 211 |
member.linkname = "var/lib/dpkg/status" |
| 212 |
archive.addfile(member) |
| 213 |
stream.seek(0) |
| 214 |
|
| 215 |
with rootfs_directory("-", stream) as root: |
| 216 |
assert (root / "var/lib/apt/lists/copy").read_bytes() == b"Package: libc6\n" |
| 217 |
|
| 218 |
|
| 219 |
@requires_tarfile_filter |
| 220 |
def test_tar_rootfs_accepts_absolute_member_paths(): |
| 221 |
member = tarfile.TarInfo("//var/lib/dpkg/arch-native") |
| 222 |
member.size = len(b"amd64\n") |
| 223 |
|
| 224 |
with rootfs_directory("-", _stdin_rootfs(member, b"amd64\n")) as root: |
| 225 |
assert (root / "var/lib/dpkg/arch-native").read_bytes() == b"amd64\n" |
| 226 |
|
| 227 |
|
| 228 |
@requires_tarfile_filter |
| 229 |
def test_tar_rootfs_ignores_members_outside_the_metadata(): |
| 230 |
member = tarfile.TarInfo("../escape") |
| 231 |
member.size = len(b"escape") |
| 232 |
|
| 233 |
with rootfs_directory("-", _stdin_rootfs(member, b"escape")) as root: |
| 234 |
assert not any(root.iterdir()) |
| 235 |
|
| 236 |
|
| 237 |
def test_tar_rootfs_keeps_symlinks_to_unmaterialized_paths_inside_the_root(): |
| 238 |
member = tarfile.TarInfo("var/lib/apt/lists/alias") |
| 239 |
member.type = tarfile.SYMTYPE |
| 240 |
member.linkname = "../../dpkg/status" |
| 241 |
|
| 242 |
with rootfs_directory("-", _stdin_rootfs(member)) as root: |
| 243 |
link = root / "var/lib/apt/lists/alias" |
| 244 |
assert link.is_symlink() |
| 245 |
assert not link.exists() |
| 246 |
assert (link.parent / link.readlink()).resolve().is_relative_to(root.resolve()) |