check_tarball.py now iterates the tar members instead of their names. Only regular files and directories are allowed, so a symlink or hardlink cannot smuggle a file in under an allowed directory. Absolute paths and paths with an empty, '.', '..' or '.git' component are rejected, so roles/../.venv/x no longer passes the top-level allow list. Tests build crafted tarballs and run the script on them. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
98 lines
4.0 KiB
Python
98 lines
4.0 KiB
Python
# SPDX-FileCopyrightText: Defame1297 and contributors
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
"""Run with: python3 -m unittest discover -s tests/scripts"""
|
|
import io
|
|
import pathlib
|
|
import subprocess
|
|
import sys
|
|
import tarfile
|
|
import tempfile
|
|
import unittest
|
|
|
|
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2] / "scripts"))
|
|
import check_tarball # noqa: E402
|
|
|
|
|
|
class TarballTest(unittest.TestCase):
|
|
def test_clean(self):
|
|
self.assertEqual(check_tarball.problems(
|
|
["README.md", "roles/", "roles/a/tasks/main.yml", "LICENSES/MIT.txt", "tests/sanity/x.txt"]), [])
|
|
|
|
def test_stray_top_level(self):
|
|
for name in (".venv/lib/a.py", "CLAUDE.md", ".claude/settings.json", "importer_result.json", "extensions/m.yml"):
|
|
self.assertEqual(len(check_tarball.problems([name])), 1, name)
|
|
|
|
def test_forbidden_inside_allowed(self):
|
|
self.assertEqual(len(check_tarball.problems(["roles/a/molecule/default/molecule.yml"])), 1)
|
|
self.assertEqual(len(check_tarball.problems(["roles/a/files/k.pem"])), 1)
|
|
self.assertEqual(len(check_tarball.problems(["tests/scripts/t.py"])), 1)
|
|
|
|
|
|
SCRIPT = pathlib.Path(__file__).resolve().parents[2] / "scripts" / "check_tarball.py"
|
|
|
|
|
|
class CraftedTarballTest(unittest.TestCase):
|
|
"""Build tarballs by hand and run the script on them, as the Build workflow does."""
|
|
|
|
def setUp(self):
|
|
self.tmp = tempfile.TemporaryDirectory()
|
|
self.addCleanup(self.tmp.cleanup)
|
|
self.path = pathlib.Path(self.tmp.name) / "c.tar.gz"
|
|
|
|
def build(self, *entries):
|
|
"""entries: (name, type, linkname); type is a tarfile type constant."""
|
|
with tarfile.open(self.path, "w:gz") as tar:
|
|
for name, typ, link in entries:
|
|
info = tarfile.TarInfo(name)
|
|
info.type = typ
|
|
info.linkname = link
|
|
data = b"x\n" if typ == tarfile.REGTYPE else None
|
|
info.size = len(data) if data else 0
|
|
tar.addfile(info, io.BytesIO(data) if data else None)
|
|
|
|
def check(self):
|
|
proc = subprocess.run([sys.executable, "-I", str(SCRIPT), str(self.path)], capture_output=True, text=True)
|
|
return proc.returncode, proc.stderr
|
|
|
|
def test_legitimate_tarball_passes(self):
|
|
self.build(("roles", tarfile.DIRTYPE, ""), ("roles/a/tasks/main.yml", tarfile.REGTYPE, ""),
|
|
("README.md", tarfile.REGTYPE, ""), ("LICENSES/MIT.txt", tarfile.REGTYPE, ""))
|
|
self.assertEqual(self.check()[0], 0)
|
|
|
|
def assert_rejected(self, entry, text):
|
|
self.build(("README.md", tarfile.REGTYPE, ""), entry)
|
|
code, err = self.check()
|
|
self.assertEqual(code, 1, err)
|
|
self.assertIn(text, err)
|
|
|
|
def test_symlink_is_rejected(self):
|
|
self.assert_rejected(("roles/a/link", tarfile.SYMTYPE, "/etc/passwd"), "symlink")
|
|
|
|
def test_hardlink_is_rejected(self):
|
|
self.assert_rejected(("roles/a/hard", tarfile.LNKTYPE, "README.md"), "hardlink")
|
|
|
|
def test_device_and_fifo_are_rejected(self):
|
|
self.assert_rejected(("roles/a/dev", tarfile.CHRTYPE, ""), "device")
|
|
self.assert_rejected(("roles/a/fifo", tarfile.FIFOTYPE, ""), "fifo")
|
|
|
|
def test_parent_component_is_rejected(self):
|
|
self.assert_rejected(("roles/../.venv/x", tarfile.REGTYPE, ""), "'..'")
|
|
|
|
def test_absolute_path_is_rejected(self):
|
|
self.assert_rejected(("/etc/cron.d/x", tarfile.REGTYPE, ""), "absolute")
|
|
|
|
def test_empty_component_is_rejected(self):
|
|
self.assert_rejected(("roles//a/x", tarfile.REGTYPE, ""), "empty path component")
|
|
|
|
def test_git_component_is_rejected(self):
|
|
self.assert_rejected(("roles/a/.git", tarfile.DIRTYPE, ""), "'.git'")
|
|
self.assert_rejected(("plugins/.git/config", tarfile.REGTYPE, ""), "'.git'")
|
|
|
|
def test_existing_rules_still_apply(self):
|
|
self.assert_rejected((".venv/bin/x", tarfile.REGTYPE, ""), "top-level entry")
|
|
self.assert_rejected(("roles/a/molecule/default/m.yml", tarfile.REGTYPE, ""), "forbidden pattern")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|