fix(ci): qualify on the pinned Python and retain immutable release bundles
Device Core qualification / qualify (push) Successful in 5m51s
Device Core qualification / qualify (push) Successful in 5m51s
This commit is contained in:
@@ -550,6 +550,41 @@ def validate_payload(payload: Path, phase: str):
|
||||
return descriptor
|
||||
|
||||
|
||||
def extract_validated_files(archive, extraction_root):
|
||||
"""Materialize only regular data, also on Python before tar filters existed."""
|
||||
if extraction_root.is_symlink() or not extraction_root.is_dir() or any(extraction_root.iterdir()):
|
||||
die("artifact extraction requires an empty private directory")
|
||||
root = extraction_root.resolve(strict=True)
|
||||
members = archive.getmembers()
|
||||
if len(members) > 65536 or sum(item.size for item in members) > MAX_ARTIFACT_BYTES:
|
||||
die("artifact expanded size/count rejected")
|
||||
normalized = set()
|
||||
files = set()
|
||||
for member in members:
|
||||
safe_tar_member(member)
|
||||
path = PurePosixPath(member.name)
|
||||
name = path.as_posix()
|
||||
if member.name.rstrip("/") != name or name in normalized:
|
||||
die("artifact contains noncanonical or duplicate paths")
|
||||
normalized.add(name)
|
||||
if member.isfile():
|
||||
files.add(name)
|
||||
for member in members:
|
||||
if any(str(parent) in files for parent in PurePosixPath(member.name).parents):
|
||||
die("artifact file is used as a directory")
|
||||
# All members are checked before any output. Never apply archive ownership,
|
||||
# links, devices or special mode bits; never invoke extract/extractall.
|
||||
for member in members:
|
||||
destination = root.joinpath(*PurePosixPath(member.name).parts)
|
||||
if member.isdir():
|
||||
destination.mkdir(parents=True, exist_ok=True, mode=0o755)
|
||||
else:
|
||||
destination.parent.mkdir(parents=True, exist_ok=True, mode=0o755)
|
||||
with archive.extractfile(member) as source, destination.open("xb") as output:
|
||||
shutil.copyfileobj(source, output)
|
||||
destination.chmod(0o755 if member.mode & 0o111 else 0o644)
|
||||
|
||||
|
||||
def load_artifact(artifact: Path, extraction_root: Path):
|
||||
artifact = artifact.resolve(strict=True)
|
||||
if artifact.parent != INBOX_ROOT.resolve(strict=True):
|
||||
@@ -570,7 +605,7 @@ def load_artifact(artifact: Path, extraction_root: Path):
|
||||
die("artifact top-level contract is incomplete")
|
||||
if any(name.split("/", 1)[0] not in required for name in seen):
|
||||
die("artifact contains an unexpected top-level member")
|
||||
archive.extractall(extraction_root, filter="data")
|
||||
extract_validated_files(archive, extraction_root)
|
||||
|
||||
manifest = parse_manifest(
|
||||
(extraction_root / "manifest.env").read_text(encoding="utf-8")
|
||||
|
||||
@@ -228,6 +228,41 @@ def safe_tar_member(member: tarfile.TarInfo):
|
||||
die("artifact contains AppleDouble metadata")
|
||||
|
||||
|
||||
def extract_validated_files(archive, extraction_root):
|
||||
"""Materialize only regular data, also on Python before tar filters existed."""
|
||||
if extraction_root.is_symlink() or not extraction_root.is_dir() or any(extraction_root.iterdir()):
|
||||
die("artifact extraction requires an empty private directory")
|
||||
root = extraction_root.resolve(strict=True)
|
||||
members = archive.getmembers()
|
||||
if len(members) > 65536 or sum(item.size for item in members) > MAX_ARTIFACT_BYTES:
|
||||
die("artifact expanded size/count rejected")
|
||||
normalized = set()
|
||||
files = set()
|
||||
for member in members:
|
||||
safe_tar_member(member)
|
||||
path = PurePosixPath(member.name)
|
||||
name = path.as_posix()
|
||||
if member.name.rstrip("/") != name or name in normalized:
|
||||
die("artifact contains noncanonical or duplicate paths")
|
||||
normalized.add(name)
|
||||
if member.isfile():
|
||||
files.add(name)
|
||||
for member in members:
|
||||
if any(str(parent) in files for parent in PurePosixPath(member.name).parents):
|
||||
die("artifact file is used as a directory")
|
||||
# All members are checked before any output. Never apply archive ownership,
|
||||
# links, devices or special mode bits; never invoke extract/extractall.
|
||||
for member in members:
|
||||
destination = root.joinpath(*PurePosixPath(member.name).parts)
|
||||
if member.isdir():
|
||||
destination.mkdir(parents=True, exist_ok=True, mode=0o755)
|
||||
else:
|
||||
destination.parent.mkdir(parents=True, exist_ok=True, mode=0o755)
|
||||
with archive.extractfile(member) as source, destination.open("xb") as output:
|
||||
shutil.copyfileobj(source, output)
|
||||
destination.chmod(0o755 if member.mode & 0o111 else 0o644)
|
||||
|
||||
|
||||
def load_artifact(artifact: Path, extraction_root: Path):
|
||||
artifact = artifact.resolve(strict=True)
|
||||
if artifact.parent != INBOX_ROOT.resolve(strict=True):
|
||||
@@ -248,7 +283,7 @@ def load_artifact(artifact: Path, extraction_root: Path):
|
||||
die("artifact top-level contract is incomplete")
|
||||
if any(name.split("/", 1)[0] not in required for name in seen):
|
||||
die("artifact contains an unexpected top-level member")
|
||||
archive.extractall(extraction_root, filter="data")
|
||||
extract_validated_files(archive, extraction_root)
|
||||
|
||||
manifest = parse_manifest(
|
||||
(extraction_root / "manifest.env").read_text(encoding="utf-8")
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
"""Exercise the real standalone loaders without Python's newer tar filter API."""
|
||||
import importlib.machinery
|
||||
import importlib.util
|
||||
import io
|
||||
from pathlib import Path
|
||||
import tarfile
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
def load(name):
|
||||
loader = importlib.machinery.SourceFileLoader(name, str(Path(__file__).parent / name))
|
||||
spec = importlib.util.spec_from_loader(name, loader)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
RUNNERS = [load("nodedc-edge-deploy"), load("nodedc-b2-vps-deploy")]
|
||||
|
||||
|
||||
def archive(items):
|
||||
buffer = io.BytesIO()
|
||||
with tarfile.open(fileobj=buffer, mode="w") as output:
|
||||
for name, kind in items:
|
||||
info = tarfile.TarInfo(name)
|
||||
info.type, info.mode, info.uid, info.gid = kind, 0o6777, 123, 456
|
||||
info.linkname = "/tmp/outside"
|
||||
if kind == tarfile.REGTYPE:
|
||||
info.size = 4
|
||||
output.addfile(info, io.BytesIO(b"data"))
|
||||
else:
|
||||
output.addfile(info)
|
||||
buffer.seek(0)
|
||||
return tarfile.open(fileobj=buffer, mode="r")
|
||||
|
||||
|
||||
class PortableExtractionTests(unittest.TestCase):
|
||||
def test_no_extractall_and_no_special_modes(self):
|
||||
for runner in RUNNERS:
|
||||
with self.subTest(runner=runner.__name__), tempfile.TemporaryDirectory() as directory:
|
||||
with archive([("payload", tarfile.DIRTYPE), ("payload/app.mjs", tarfile.REGTYPE)]) as value:
|
||||
with patch.object(tarfile.TarFile, "extractall", side_effect=AssertionError("unfiltered extraction forbidden")):
|
||||
runner.extract_validated_files(value, Path(directory))
|
||||
target = Path(directory) / "payload/app.mjs"
|
||||
self.assertEqual(target.read_bytes(), b"data")
|
||||
self.assertEqual(target.stat().st_mode & 0o7777, 0o755)
|
||||
|
||||
def test_every_member_checked_before_any_write(self):
|
||||
for runner in RUNNERS:
|
||||
for name, kind in [("../escape", tarfile.REGTYPE), ("/absolute", tarfile.REGTYPE),
|
||||
("payload/link", tarfile.SYMTYPE), ("payload/link", tarfile.LNKTYPE),
|
||||
("payload/pipe", tarfile.FIFOTYPE), ("payload/.env", tarfile.REGTYPE),
|
||||
("payload//alias", tarfile.REGTYPE), ("payload/./alias", tarfile.REGTYPE),
|
||||
("payload/a", tarfile.REGTYPE), ("payload/a/child", tarfile.REGTYPE)]:
|
||||
with self.subTest(runner=runner.__name__, name=name, kind=kind), tempfile.TemporaryDirectory() as directory:
|
||||
with archive([("payload/a", tarfile.REGTYPE), (name, kind)]) as value:
|
||||
with self.assertRaises(runner.DeployError): runner.extract_validated_files(value, Path(directory))
|
||||
self.assertEqual(list(Path(directory).iterdir()), [])
|
||||
|
||||
def test_existing_output_and_symlink_root_rejected(self):
|
||||
for runner in RUNNERS:
|
||||
with self.subTest(runner=runner.__name__), tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
(root / "existing").write_text("preserve")
|
||||
with archive([("payload/a", tarfile.REGTYPE)]) as value:
|
||||
with self.assertRaises(runner.DeployError): runner.extract_validated_files(value, root)
|
||||
(root / "target").mkdir()
|
||||
(root / "linked").symlink_to(root / "target", target_is_directory=True)
|
||||
with archive([("payload/a", tarfile.REGTYPE)]) as value:
|
||||
with self.assertRaises(runner.DeployError): runner.extract_validated_files(value, root / "linked")
|
||||
self.assertEqual((root / "existing").read_text(), "preserve")
|
||||
|
||||
def test_expanded_size_bounded(self):
|
||||
for runner in RUNNERS:
|
||||
with self.subTest(runner=runner.__name__), tempfile.TemporaryDirectory() as directory:
|
||||
with archive([("payload/a", tarfile.REGTYPE)]) as value, patch.object(runner, "MAX_ARTIFACT_BYTES", 3):
|
||||
with self.assertRaises(runner.DeployError): runner.extract_validated_files(value, Path(directory))
|
||||
self.assertFalse(list(Path(directory).iterdir()))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user