Extract Tar Archives Without Path Traversal
Extract gzipped tar archives after rejecting absolute paths, parent traversal, and link members that escape the destination.
Code
from __future__ import annotations
import io
import tarfile
import tempfile
from pathlib import Path, PurePosixPath
def safe_members(archive: tarfile.TarFile, destination: Path) -> list[tarfile.TarInfo]:
root = destination.resolve()
accepted: list[tarfile.TarInfo] = []
for member in archive.getmembers():
path = PurePosixPath(member.name)
if path.is_absolute() or ".." in path.parts:
raise ValueError(f"unsafe archive path: {member.name}")
if member.issym() or member.islnk():
raise ValueError(f"links are not allowed: {member.name}")
if not (member.isfile() or member.isdir()):
raise ValueError(f"unsupported member type: {member.name}")
target = (root / path).resolve()
if root not in target.parents and target != root:
raise ValueError(f"member escapes destination: {member.name}")
accepted.append(member)
return accepted
def extract_archive(data: bytes, destination: Path) -> list[str]:
destination.mkdir(parents=True, exist_ok=True)
with tarfile.open(fileobj=io.BytesIO(data), mode="r:gz") as archive:
members = safe_members(archive, destination)
archive.extractall(destination, members=members)
return sorted(
path.relative_to(destination).as_posix()
for path in destination.rglob("*")
if path.is_file()
)
def build_archive(entries: list[tuple[str, bytes]]) -> bytes:
buffer = io.BytesIO()
with tarfile.open(fileobj=buffer, mode="w:gz") as archive:
for name, payload in entries:
info = tarfile.TarInfo(name)
info.size = len(payload)
archive.addfile(info, io.BytesIO(payload))
return buffer.getvalue()
def main() -> None:
with tempfile.TemporaryDirectory() as raw_root:
root = Path(raw_root)
destination = root / "extract"
archive = build_archive([("a.txt", b"alpha"), ("nested/b.txt", b"beta")])
files = extract_archive(archive, destination)
print(files)
assert files == ["a.txt", "nested/b.txt"]
assert (destination / "nested" / "b.txt").read_bytes() == b"beta"
malicious = build_archive([("../evil.txt", b"owned")])
try:
extract_archive(malicious, root / "unsafe")
except ValueError as exc:
print(f"rejected: {exc}")
else:
raise AssertionError("traversal archive was accepted")
assert not (root / "evil.txt").exists()
if __name__ == "__main__":
main()
How the snippet works
Extract Tar Archives Without Path Traversal keeps everything in the functions safe_members, extract_archive, build_archive and main.
A single loop drives the file — for member in archive.getmembers(). From __future__ it pulls annotations, Path, PurePosixPath — the imports name exactly which library surface the snippet touches. A ValueError and AssertionError is raised inside an except block, so it is conditional on that failure being handled in the first place.
Points of care
__future__,tarfileandtempfilemust be installed first — the snippet imports them directly.- The guard
path.is_absolute() or ".." in path.partsshort-circuits the flow with an earlyraisewhen its condition holds.
Using it
Signature: def safe_members(archive: tarfile.TarFile, destination: Path) -> list[tarfile.TarInfo]
Running the file prints labeled results such as rejected: — each operation’s effect is visible in the output.
There are 2 calls inside main — build_archive and extract_archive — executed top to bottom.
Key statements
with tarfile.open(fileobj=io.BytesIO(data), mode="r:gz") as archive:
members = safe_members(archive, destination)
archive.extractall(destination, members=members)
with tarfile.open(fileobj=buffer, mode="w:gz") as archive:
for name, payload in entries:
info = tarfile.TarInfo(name)
info.size = len(payload)
archive.addfile(info, io.BytesIO(payload))
except ValueError as exc:
print(f"rejected: {exc}")
Interactive Tools for This Topic
FAQ
- When is the exception raised?
- Inside an `except` handler — the `ValueError` and `AssertionError` fires only while that failure type is already being handled, not on its own trigger.
- Do I need to install anything before running this?
- Yes: `__future__` must be present in the project or the import fails at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.