Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(sdk): safely extract source distributions during verification

xp9e7b44ddc80b
diff --git a/sdk/python/README.md b/sdk/python/README.md
index de32c1b4..240da7d3 100644
--- a/sdk/python/README.md
+++ b/sdk/python/README.md
@@ -108,7 +108,7 @@ batch, local routing, model loading or Python prediction hooks.
## Development and verification
-From the repository root, in a build environment:
+From the repository root, in a build environment using a current patched Python:
```bash
python -m pip install build setuptools wheel
@@ -119,6 +119,10 @@ python sdk/python/tests/verify_package.py sdk/python/dist
The verifier builds an isolated client environment, installs the wheel with
normal dependency resolution and no package index, then runs the standard-library
tests against that installed package. It also checks the sdist-built wheel.
+Source archives are extracted with `tarfile`'s `data` filter and must contain only
+regular files and directories within the extraction directory. The verifier
+refuses to extract on older Python builds without `tarfile.data_filter`; this
+requirement applies to the verification tool, not the installed client's runtime.
For live integration, supply a Python with `laya[serve]` dependencies installed:
diff --git a/sdk/python/tests/test_package.py b/sdk/python/tests/test_package.py
new file mode 100644
index 00000000..5a82b115
--- /dev/null
+++ b/sdk/python/tests/test_package.py
@@ -0,0 +1,93 @@
+import io
+from pathlib import Path
+import tarfile
+import tempfile
+import unittest
+from unittest.mock import patch
+
+from verify_package import extract_sdist
+
+
[email protected](hasattr(tarfile, "data_filter"), "Requires patched tarfile")
+class ArchiveExtractionTests(unittest.TestCase):
+ def setUp(self):
+ directory = tempfile.TemporaryDirectory()
+ self.addCleanup(directory.cleanup)
+ self.root = Path(directory.name)
+ self.destination = self.root / "unpacked"
+ self.destination.mkdir()
+ self.source = self.root / "source.tar.gz"
+
+ def write_archive(self, *members):
+ with tarfile.open(self.source, "w:gz") as archive:
+ for name, kind, link in members:
+ entry = tarfile.TarInfo(name)
+ entry.type = kind
+ entry.linkname = link
+ if entry.isfile():
+ entry.size = len(b"package contents")
+ archive.addfile(entry, io.BytesIO(b"package contents"))
+ else:
+ archive.addfile(entry)
+
+ def test_regular_files_and_directories(self):
+ self.write_archive(("package", tarfile.DIRTYPE, ""),
+ ("package/README.md", tarfile.REGTYPE, ""))
+ extract_sdist(self.source, self.destination)
+ self.assertEqual((self.destination / "package/README.md").read_bytes(), b"package contents")
+
+ def test_parent_traversal_is_rejected_before_extracting_any_member(self):
+ self.write_archive(("safe.txt", tarfile.REGTYPE, ""), ("../outside.txt", tarfile.REGTYPE, ""))
+ with self.assertRaisesRegex(ValueError, "escapes"):
+ extract_sdist(self.source, self.destination)
+ self.assertFalse((self.root / "outside.txt").exists())
+ self.assertEqual(list(self.destination.iterdir()), [])
+
+ def test_absolute_path_is_rejected(self):
+ outside = self.root / "outside.txt"
+ self.write_archive((outside.as_posix(), tarfile.REGTYPE, ""))
+ with self.assertRaisesRegex(ValueError, "escapes"):
+ extract_sdist(self.source, self.destination)
+ self.assertFalse(outside.exists())
+
+ def test_links_are_rejected(self):
+ for kind in (tarfile.SYMTYPE, tarfile.LNKTYPE):
+ with self.subTest(kind=kind):
+ self.write_archive(("link", kind, "../outside.txt"))
+ with self.assertRaisesRegex(ValueError, "only regular files and directories"):
+ extract_sdist(self.source, self.destination)
+ self.assertEqual(list(self.destination.iterdir()), [])
+
+ def test_special_files_are_rejected(self):
+ for kind in (tarfile.FIFOTYPE, tarfile.CHRTYPE, tarfile.BLKTYPE):
+ with self.subTest(kind=kind):
+ self.write_archive(("special", kind, ""))
+ with self.assertRaisesRegex(ValueError, "only regular files and directories"):
+ extract_sdist(self.source, self.destination)
+ self.assertEqual(list(self.destination.iterdir()), [])
+
+ def test_existing_directory_symlink_cannot_escape(self):
+ outside = self.root / "outside"
+ outside.mkdir()
+ try:
+ (self.destination / "link").symlink_to(outside, target_is_directory=True)
+ except (OSError, NotImplementedError) as error:
+ self.skipTest(f"Directory symlinks unavailable: {error}")
+ self.write_archive(("link/file.txt", tarfile.REGTYPE, ""))
+ with self.assertRaisesRegex(ValueError, "escapes"):
+ extract_sdist(self.source, self.destination)
+ self.assertEqual(list(outside.iterdir()), [])
+
+
+class ExtractionFilterTests(unittest.TestCase):
+ def test_missing_filter_fails_before_opening_the_archive(self):
+ with patch.dict(vars(tarfile)):
+ vars(tarfile).pop("data_filter", None)
+ with patch.object(tarfile, "open") as open_archive:
+ with self.assertRaisesRegex(RuntimeError, "patched Python"):
+ extract_sdist(Path("unused.tar.gz"), Path("unused"))
+ open_archive.assert_not_called()
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/sdk/python/tests/verify_package.py b/sdk/python/tests/verify_package.py
index 0019c004..dabbbc22 100644
--- a/sdk/python/tests/verify_package.py
+++ b/sdk/python/tests/verify_package.py
@@ -28,6 +28,20 @@ def check_wheel(path):
assert all(n.startswith(("laya_client/", "laya_client-")) for n in names), names
+def extract_sdist(source, destination):
+ if not hasattr(tarfile, "data_filter"):
+ raise RuntimeError("Package verification requires a patched Python with tarfile.data_filter")
+ destination = destination.resolve()
+ with tarfile.open(source) as archive:
+ for entry in archive.getmembers():
+ target = (destination / entry.name).resolve()
+ if Path(entry.name).is_absolute() or not target.is_relative_to(destination):
+ raise ValueError("sdist member escapes the extraction directory")
+ if not (entry.isfile() or entry.isdir()):
+ raise ValueError("sdist must contain only regular files and directories")
+ archive.extractall(destination, filter="data")
+
+
def main():
parser = argparse.ArgumentParser()
parser.add_argument("dist", type=Path)
@@ -54,12 +68,7 @@ def main():
unpacked = root / "sdist"
unpacked.mkdir()
- with tarfile.open(sdists[0]) as archive:
- for entry in archive.getmembers():
- target = (unpacked / entry.name).resolve()
- assert target.is_relative_to(unpacked.resolve())
- assert entry.isfile() or entry.isdir(), "Unexpected link or device in sdist"
- archive.extractall(unpacked)
+ extract_sdist(sdists[0], unpacked)
projects = list(unpacked.iterdir())
assert len(projects) == 1 and (projects[0] / "pyproject.toml").is_file()
rebuilt = root / "rebuilt"