Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

main: filter out malicious files when extracting tar archives #609

Merged
merged 2 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ source = [
exclude_lines = [
'\#\s*pragma: no cover',
'^\s*raise NotImplementedError\b',
"if typing.TYPE_CHECKING:",
]

[tool.coverage.paths]
Expand Down
5 changes: 3 additions & 2 deletions src/build/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import shutil
import subprocess
import sys
import tarfile
import tempfile
import textwrap
import traceback
Expand Down Expand Up @@ -230,6 +229,8 @@ def build_package_via_sdist(
:param isolation: Isolate the build in a separate environment
:param skip_dependency_check: Do not perform the dependency check
"""
from ._util import TarFile

if 'sdist' in distributions:
raise ValueError('Only binary distributions are allowed but sdist was specified')

Expand All @@ -240,7 +241,7 @@ def build_package_via_sdist(
built: list[str] = []
if distributions:
# extract sdist
with tarfile.open(sdist) as t:
with TarFile.open(sdist) as t:
layday marked this conversation as resolved.
Show resolved Hide resolved
t.extractall(sdist_out)
try:
_ProjectBuilder.log(f'Building {_natural_language_list(distributions)} from sdist')
Expand Down
17 changes: 17 additions & 0 deletions src/build/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import re
import sys
import tarfile
import typing

from collections.abc import Iterator, Set

Expand Down Expand Up @@ -65,3 +67,18 @@

def parse_wheel_filename(filename: str) -> re.Match[str] | None:
return _WHEEL_FILENAME_REGEX.match(filename)


if typing.TYPE_CHECKING:
layday marked this conversation as resolved.
Show resolved Hide resolved
TarFile = tarfile.TarFile

else:
# Per https://peps.python.org/pep-0706/, the "data" filter will become
# the default in Python 3.14.
if sys.version_info < (3, 14) and hasattr(tarfile, 'data_filter'):

class TarFile(tarfile.TarFile):
extraction_filter = staticmethod(tarfile.data_filter)

else:
TarFile = tarfile.TarFile

Check warning on line 84 in src/build/_util.py

View check run for this annotation

Codecov / codecov/patch

src/build/_util.py#L84

Added line #L84 was not covered by tests