Arbitrary file writes through archive extraction (Tar Slip)

Arbitrary file writes through archive extraction (Tar Slip)

Description

Extract tar archives into a new private directory under a trusted parent. A policy that leaves member paths and links unchecked may permit files outside it to be overwritten.

Potential impact

The service account may damage files it can write. Another process using those files may expose data, interrupt service, or give unintended access.

Remediation

  • Where available, specify extractall(..., filter="data") to restrict paths and links outside the destination, special files, and excessive permissions. Python 3.14 made data the default. Omitting the filter uses the runtime’s configured default.
  • On runtimes without filters, allow only regular files and directories, reject symbolic and hard links, and verify each resolved destination before creating a file.
  • Limit actual expanded bytes, entry count, and processing time separately.

Examples

Before

python
# Tar extraction using the runtime default filter
import tarfile

def unsafe_extract(tar_file_path, extract_path):
    with tarfile.open(tar_file_path) as tar:
        tar.extractall(path=extract_path)

After

python
# Safe tar extraction
import tarfile
import os
import shutil
from pathlib import Path

def safe_extract(tar_file_path, extract_path):
    destination = Path(extract_path)
    # Extract only into a new private directory without existing files or links
    destination.mkdir(mode=0o700, exist_ok=False)
    destination = destination.resolve()

    with tarfile.open(tar_file_path) as tar:
        if hasattr(tarfile, "data_filter"):
            # Restricted data extraction on runtimes that support extraction filters
            tar.extractall(path=destination, filter="data")
            return

        # Legacy fallback: reject links and special files, and check directory boundaries
        for member in tar.getmembers():
            if member.issym() or member.islnk():
                raise ValueError("Tar links are not allowed")
            if not (member.isfile() or member.isdir()):
                raise ValueError("Unsupported tar entry type")

            target = (destination / member.name).resolve()
            try:
                target.relative_to(destination)
            except ValueError as error:
                raise ValueError("Tar entry escapes extraction directory") from error

            if member.isdir():
                target.mkdir(parents=True, exist_ok=True, mode=0o700)
                continue

            target.parent.mkdir(parents=True, exist_ok=True, mode=0o700)
            source = tar.extractfile(member)
            if source is None:
                raise ValueError("Tar entry has no file data")
            fd = os.open(target, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
            with source, os.fdopen(fd, "wb") as output:
                shutil.copyfileobj(source, output)

The function checks for tarfile.data_filter to choose the extraction method. Its legacy fallback verifies each destination and uses os.O_EXCL to preserve existing files.

References