FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

test(project): cover runtime_assets checksum and extraction helpers by Adebowale-Morakinyo · Pull Request #75 · pythonnative/pythonnative · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
33 changes: 25 additions & 8 deletions src/pythonnative/project/runtime_assets.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,37 @@ def _sha256(path: Path) -> str:


def _safe_extract(tar_path: Path, dest: Path) -> None:
"""Extract a tarball, refusing entries that escape ``dest``."""
"""Extract a tarball, refusing entries that escape ``dest``.

Preflight checks reject unsafe paths, link targets, and special files.
The data filter also checks each member during extraction, accounting
for links created by earlier members and sanitizing file permissions.
"""
dest = dest.resolve()
with tarfile.open(tar_path, "r:gz") as tar:
members = tar.getmembers()
for member in members:
target = (dest / member.name).resolve()
if not str(target).startswith(str(dest)):
# ``is_relative_to`` compares path components. A string prefix
# test would accept a sibling whose name merely starts with
# ``dest``, such as ``../out-evil/x.txt`` beside ``out/``.
if not target.is_relative_to(dest):
raise RuntimeError(f"Refusing to extract unsafe path: {member.name}")
# ``filter='data'`` (3.12+) blocks unsafe members; older Pythons
# fall back to the manual check above.
try:
tar.extractall(dest, filter="data")
except TypeError:
tar.extractall(dest)
if member.issym() or member.islnk():
# A link's name can be innocuous while its target escapes.
# Symlink targets resolve against the link's own directory;
# hardlink targets are relative to the archive root.
base = target.parent if member.issym() else dest
link_target = (base / member.linkname).resolve()
if not link_target.is_relative_to(dest):
raise RuntimeError(f"Refusing to extract unsafe link: {member.name} -> {member.linkname}")
if member.isdev():
# The pinned archives only need files, directories, and
# links, so refuse FIFOs and device nodes before extraction.
raise RuntimeError(f"Refusing to extract special file: {member.name}")
# Every supported interpreter provides the data filter. Keep it
# explicit because Python 3.13 defaults to unfiltered extraction.
tar.extractall(dest, filter="data")


def _locate_runtime(extract_root: Path, python_version: str) -> IOSRuntime:
Expand Down
Loading
Loading

Back | FazBrowse Home | New Git URL