The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(pm): extract relative tar symlinks under the 3.10 bootstrap interpreter
ethernetf9ea87ed480b
diff --git a/pm/store.py b/pm/store.pyindex 007bf10a0e9..7b4a69211b5 100644--- a/pm/store.py+++ b/pm/store.py@@ -129,6 +129,26 @@ def hash_url(url: str) -> str:return retry_network(request)+def _tar_filter(member, dest: str):+ """The stdlib 'data' filter, with symlink targets resolved from the link's own+ directory. Bootstrap interpreters (Ubuntu 22.04 ships 3.10) resolve them from+ the archive root and reject python-build-standalone's terminfo links."""+ import tarfile++ if member.issym():+ if os.path.isabs(member.linkname):+ raise tarfile.AbsoluteLinkError(member)+ name = member.name.rstrip("/")+ placed = os.path.realpath(os.path.join(dest, name))+ link_dir = os.path.dirname(name)+ target = os.path.realpath(os.path.join(dest, link_dir, member.linkname))+ for path in (placed, target):+ if os.path.commonpath([path, dest]) != dest:+ raise tarfile.LinkOutsideDestinationError(member, path)+ return member.replace(deep=False, uid=None, gid=None, uname=None, gname=None, mode=None)+ return tarfile.data_filter(member, dest)++def extract(archive: Path, dest: Path) -> None:import tarfile@@ -136,8 +156,9 @@ def extract(archive: Path, dest: Path) -> None:dest.mkdir(parents=True, exist_ok=True)name = archive.name.lower()if name.endswith((".tar.gz", ".tgz", ".tar.xz", ".txz", ".tar.bz2")):+ real_dest = os.path.realpath(dest)with tarfile.open(archive) as tf:- tf.extractall(dest, filter="data")+ tf.extractall(dest, filter=lambda member, path: _tar_filter(member, real_dest))elif name.endswith(".zip"):_extract_zip(archive, dest)else:diff --git a/tests/pm/test_store_extract.py b/tests/pm/test_store_extract.pynew file mode 100644index 00000000000..da4832aeb63--- /dev/null+++ b/tests/pm/test_store_extract.py@@ -0,0 +1,43 @@+"""Regression for the Ubuntu 22.04 bootstrap interpreter: python-build-standalone+tarballs carry relative terminfo symlinks (``share/terminfo/1/1178 -> ../a/adm1178``)."""++import io+import os+import tarfile++import pytest++from pm.store import extract+++def _tar(tmp_path, members):+ archive = tmp_path / "pkg.tar.gz"+ with tarfile.open(archive, "w:gz") as tf:+ for name, linkname in members:+ info = tarfile.TarInfo(name)+ if linkname is None:+ data = b"x"+ info.size = len(data)+ tf.addfile(info, io.BytesIO(data))+ else:+ info.type = tarfile.SYMTYPE+ info.linkname = linkname+ tf.addfile(info)+ return archive+++def test_relative_symlinks_resolve_from_their_own_directory(tmp_path):+ archive = _tar(tmp_path, [("python/share/terminfo/a/adm1178", None), ("python/share/terminfo/1/1178", "../a/adm1178")])+ dest = tmp_path / "out"+ extract(archive, dest)+ link = dest / "python/share/terminfo/1/1178"+ assert os.readlink(link) == "../a/adm1178"+ assert link.resolve() == (dest / "python/share/terminfo/a/adm1178").resolve()++[email protected]("linkname", ["../../../etc/passwd", "/etc/passwd"])+def test_symlinks_escaping_the_destination_are_rejected(tmp_path, linkname):+ archive = _tar(tmp_path, [("python/bin/evil", linkname)])+ with pytest.raises(tarfile.FilterError):+ extract(archive, tmp_path / "out")+ assert not (tmp_path / "out" / "python/bin/evil").is_symlink()