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

Add tarfile member sanitization to extractall() #1805

Merged
merged 2 commits into from
Jan 11, 2023
Merged
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
30 changes: 28 additions & 2 deletions maintenance/scripts/combine_restore.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import sys
import tarfile
import tempfile
from typing import List, Tuple
from typing import List, Optional, Tuple

from aws_backup import AwsBackup
from combine_app import CombineApp
Expand Down Expand Up @@ -123,7 +123,33 @@ def main() -> None:
step.print("Unpack the backup.")
os.chdir(restore_dir)
with tarfile.open(restore_file, "r:gz") as tar:
tar.extractall()
tar_dir = Path(".")

def is_within_directory(directory: Path, target: Path) -> bool:

abs_directory = Path(directory).absolute()
abs_target = Path(target).absolute()

prefix = Path(os.path.commonprefix([abs_directory, abs_target]))

return prefix == abs_directory

def safe_extract(
tar: tarfile.TarFile,
path: Path = tar_dir,
members: Optional[List[tarfile.TarInfo]] = None,
*,
numeric_owner: bool = False,
) -> None:

for member in tar.getmembers():
member_path = path / member.name
if not is_within_directory(path, member_path):
raise Exception("Attempted Path Traversal in Tar File")

tar.extractall(path, members, numeric_owner=numeric_owner)

safe_extract(tar)

step.print("Restore the database.")
db_pod = combine.get_pod_id(CombineApp.Component.Database)
Expand Down