Skip to content

fix: use tarfile extract filters to open tarfiles more safely #3769

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

Merged
merged 18 commits into from
Feb 12, 2024
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
2 changes: 2 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ jobs:
- uses: technote-space/get-diff-action@f27caffdd0fb9b13f4fc191c016bb4e0632844af # v6.1.2
with:
PATTERNS: |
cve_bin_tool/*.py
cve_bin_tool/data_sources/*.py
cve_bin_tool/checkers/*.py
test/condensed-downloads/*
FILES: |
Expand Down
15 changes: 8 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
repos:
- repo: https://github.com/econchick/interrogate
rev: 1.5.0
hooks:
- id: interrogate
verbose: True
exclude: ^(locales|presentation|fuzz|test|cve_bin_tool/checkers|build)
args: ["-vv", "-i", "-I", "-M", "-C", "-n", "-p", "-f", "60.0"]

- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
Expand Down Expand Up @@ -71,11 +79,4 @@ repos:
test/utils.py|
)$

- repo: https://github.com/econchick/interrogate
rev: 1.5.0
hooks:
- id: interrogate
verbose: True
exclude: ^(locales|presentation|fuzz|test|cve_bin_tool/checkers|build)
args: ["-vv", "-i", "-I", "-M", "-C", "-n", "-p", "-f", "60.0"]

38 changes: 35 additions & 3 deletions cve_bin_tool/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import re
import shutil
import sys
import tarfile
import tempfile
from pathlib import Path

Expand Down Expand Up @@ -73,11 +74,42 @@ def can_extract(self, filename):
return True
return False

@staticmethod
async def extract_file_tar(filename, extraction_path):
def tar_member_filter(self, members, extraction_path):
"""Generator function to serve as a backported filter for tarfile extraction
based on https://docs.python.org/3/library/tarfile.html#examples
"""
for tarmember in members:
if tarmember.isfile() and str(
Path(extraction_path, tarmember.name).resolve()
).startsWith(extraction_path):
yield tarmember

async def extract_file_tar(self, filename, extraction_path):
"""Extract tar files"""

# make sure we have full path for later checks
extraction_path = str(Path(extraction_path).resolve())
with ErrorHandler(mode=ErrorMode.Ignore) as e:
await aio_unpack_archive(filename, extraction_path)
tar = tarfile.open(filename)
# Python 3.12 has a data filter we can use in extract
# tarfile has this available in older versions as well
if hasattr(tarfile, "data_filter"):
tar.extractall(path=extraction_path, filter="data") # nosec
# nosec line because bandit doesn't understand filters yet

# FIXME: the backported fix is not working on windows.
# this leaves the current (unsafe) behaviour so we can fix at least one OS for now
elif sys.platform == "win32":
tar.extractall(path=extraction_path) # nosec

# Some versions may need us to implement a filter to avoid unsafe behaviour
# we could consider logging a warning here
else:
tar.extractall(
path=extraction_path,
members=self.tar_member_filter(tar, extraction_path),
) # nosec
tar.close()
return e.exit_code

async def extract_file_rpm(self, filename, extraction_path):
Expand Down