diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index 415d0d24..9c25b120 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -43,6 +43,7 @@ jobs: keyless-cuda: if: github.event_name == 'workflow_dispatch' && github.event.inputs.keyless_gpu == 'true' + needs: verify runs-on: [self-hosted, linux, x64, strata-gpu] steps: - uses: actions/checkout@v4 diff --git a/scripts/bootstrap-cuda-toolkit.py b/scripts/bootstrap-cuda-toolkit.py index 4324ca71..9574eea6 100644 --- a/scripts/bootstrap-cuda-toolkit.py +++ b/scripts/bootstrap-cuda-toolkit.py @@ -6,6 +6,7 @@ redistrib archives are assembly inputs, not a general-purpose CUDA installer. """ import argparse import hashlib +import inspect import json import os from pathlib import Path, PurePosixPath @@ -53,6 +54,30 @@ def validate_lock(lock, manifest): raise ValueError('component exceeds download budget') +def extract_members(source, destination, members, *, supports_filter=None): + """Use tarfile's data policy on both new and older Python runtimes. + + Python 3.11 includes ``tarfile.data_filter`` but its ``extractall`` does + not accept the ``filter`` keyword. Pre-filtering every member preserves + the same path, link, mode, and ownership checks there. Fail closed if a + Python runtime has neither API rather than extracting unfiltered data. + """ + if supports_filter is None: + supports_filter = 'filter' in inspect.signature(source.extractall).parameters + if supports_filter: + source.extractall(destination, members=members, filter='data') + return + data_filter = getattr(tarfile, 'data_filter', None) + if data_filter is None: + raise RuntimeError('safe tar extraction requires Python tarfile data_filter support') + safe_members = [] + for member in members: + safe_member = data_filter(member, str(destination)) + if safe_member is not None: + safe_members.append(safe_member) + source.extractall(destination, members=safe_members) + + def extract(archive, destination, root_name): with tarfile.open(archive, 'r:xz') as source: members=source.getmembers() @@ -68,7 +93,7 @@ def extract(archive, destination, root_name): seen.add(item.name) if not (item.isdir() or item.isfile() or item.issym() or item.islnk()): raise ValueError('unsupported archive member') - source.extractall(destination, members=members, filter='data') + extract_members(source, destination, members) return destination/root_name diff --git a/scripts/test-cuda-bootstrap.py b/scripts/test-cuda-bootstrap.py index 8e5b21d2..fd371aeb 100644 --- a/scripts/test-cuda-bootstrap.py +++ b/scripts/test-cuda-bootstrap.py @@ -20,6 +20,25 @@ class Tests(unittest.TestCase): item=tarfile.TarInfo('component/lib/b');item.type=tarfile.SYMTYPE;item.linkname='a';t.addfile(item) out=m.extract(archive,root/'out','component') self.assertEqual((out/'lib/b').read_bytes(),b'a') + def test_python311_style_data_filter_fallback(self): + # Python 3.11 has data_filter but extractall(filter=...) is unavailable. + with tempfile.TemporaryDirectory() as tmp: + root=Path(tmp);archive=root/'test.tar.xz';dest=root/'out' + with tarfile.open(archive,'w:xz') as t: + item=tarfile.TarInfo('component/lib/a');item.size=1;t.addfile(item,io.BytesIO(b'a')) + item=tarfile.TarInfo('component/lib/b');item.type=tarfile.SYMTYPE;item.linkname='a';t.addfile(item) + with tarfile.open(archive,'r:xz') as source: + m.extract_members(source,dest,source.getmembers(),supports_filter=False) + self.assertEqual((dest/'component/lib/b').read_bytes(),b'a') + def test_python311_style_fallback_rejects_escaping_link(self): + with tempfile.TemporaryDirectory() as tmp: + root=Path(tmp);archive=root/'test.tar.xz';dest=root/'out' + with tarfile.open(archive,'w:xz') as t: + item=tarfile.TarInfo('component/b');item.type=tarfile.SYMTYPE;item.linkname='../../escape';t.addfile(item) + with tarfile.open(archive,'r:xz') as source: + with self.assertRaises(tarfile.FilterError): + m.extract_members(source,dest,source.getmembers(),supports_filter=False) + self.assertFalse((root/'escape').exists()) def test_bad_archive_paths_and_links(self): for name,link in [('../escape',None),('/escape',None),('other/file',None),('component/b','../../escape')]: with self.subTest(name=name),tempfile.TemporaryDirectory() as tmp: