Files
leleka/scripts/test-cuda-bootstrap.py
T
Pavlo Ratushnyi 65a8dbd469
Linux / verify (pull_request) Failing after 28s
Linux / keyless-cuda (pull_request) Skipped
Linux / static-cuda-package (pull_request) Skipped
Linux / hosted-cuda-package (pull_request) Skipped
Linux / linux-static-cli (pull_request) Skipped
Linux / linux-cli-container (pull_request) Skipped
Linux / release-cli (pull_request) Skipped
fix: safely extract CUDA bundles with Python 3.11
2026-10-05 21:25:52 +02:00

88 lines
5.4 KiB
Python

#!/usr/bin/env python3
import importlib.util
import io
import json
from pathlib import Path
import tarfile
import tempfile
import unittest
from unittest.mock import patch
spec=importlib.util.spec_from_file_location('bootstrap',Path(__file__).with_name('bootstrap-cuda-toolkit.py'))
m=importlib.util.module_from_spec(spec);spec.loader.exec_module(m)
class Tests(unittest.TestCase):
def test_safe_tree_and_internal_link(self):
with tempfile.TemporaryDirectory() as tmp:
root=Path(tmp);archive=root/'test.tar.xz'
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)
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(ValueError):
m.extract_members(source,dest,source.getmembers(),supports_filter=False)
self.assertFalse((root/'escape').exists())
def test_rejects_member_nested_under_archive_symlink(self):
with tempfile.TemporaryDirectory() as tmp:
root=Path(tmp);archive=root/'test.tar.xz'
with tarfile.open(archive,'w:xz') as t:
item=tarfile.TarInfo('component/a');item.type=tarfile.SYMTYPE;item.linkname='..';t.addfile(item)
# Lexically this link is still inside the root, but after the
# parent symlink is followed it would escape the extraction tree.
item=tarfile.TarInfo('component/a/x');item.type=tarfile.SYMTYPE;item.linkname='../..';t.addfile(item)
with self.assertRaises(ValueError):m.extract(archive,root/'out','component')
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:
root=Path(tmp);archive=root/'test.tar.xz'
with tarfile.open(archive,'w:xz') as t:
item=tarfile.TarInfo(name)
if link:item.type=tarfile.SYMTYPE;item.linkname=link
t.addfile(item)
with self.assertRaises((ValueError,tarfile.TarError)):m.extract(archive,root/'out','component')
self.assertFalse((root/'escape').exists())
def test_component_merge_rejects_conflict(self):
with tempfile.TemporaryDirectory() as tmp:
root=Path(tmp);a=root/'a';b=root/'b';a.mkdir();b.mkdir()
(a/'x').write_text('a');(b/'x').write_text('b')
with self.assertRaises(ValueError):m.merge(a,b)
self.assertEqual((b/'x').read_text(),'b')
def test_identical_merge_and_moving(self):
with tempfile.TemporaryDirectory() as tmp:
root=Path(tmp);a=root/'a';b=root/'b';a.mkdir();b.mkdir()
(a/'x').write_text('a');(b/'x').write_text('a');(a/'y').write_text('y')
m.merge(a,b);self.assertEqual((b/'y').read_text(),'y');self.assertFalse((a/'y').exists())
def test_download_mismatch_size_and_redirect(self):
for data,url,sha,limit in [(b'bad',m.BASE+'x','0'*64,4),(b'large',m.BASE+'x','0'*64,2),(b'x','https://other.invalid/x','0'*64,2)]:
with tempfile.TemporaryDirectory() as tmp:
response=io.BytesIO(data);response.url=url
with patch.object(m.urllib.request,'urlopen',return_value=response),self.assertRaises(ValueError):
m.download(m.BASE+'x',Path(tmp)/'file',sha,limit)
def test_lock_pins_match_reference(self):
lock=json.loads((Path(__file__).resolve().parents[1]/'deploy/cuda-toolkit.lock.json').read_text())
manifest={x['component']:{'version':x['version'],'linux-x86_64':x.copy()} for x in lock['components']}
m.validate_lock(lock,manifest)
lock['components'][0]['sha256']='0'*64
with self.assertRaises(ValueError):m.validate_lock(lock,manifest)
def test_resource_gate_before_download(self):
with patch.object(m.Path,'read_text',return_value='MemTotal: 1024 kB\n'),self.assertRaises(ValueError):m.resource_gate(Path('/tmp'))
if __name__=='__main__':unittest.main()