Movatterモバイル変換


[0]ホーム

URL:


Following system colour schemeSelected dark colour schemeSelected light colour scheme

Python Enhancement Proposals

PEP 721 – Using tarfile.data_filter for source distribution extraction

PEP 721 – Using tarfile.data_filter for source distribution extraction

Author:
Petr Viktorin <encukou at gmail.com>
PEP-Delegate:
Paul Moore <p.f.moore at gmail.com>
Status:
Final
Type:
Standards Track
Topic:
Packaging
Requires:
706
Created:
12-Jul-2023
Python-Version:
3.12
Post-History:
04-Jul-2023
Resolution:
02-Aug-2023

Table of Contents

Important

This PEP is a historical document. The up-to-date, canonical spec,Source distribution archive features, is maintained on thePyPA specs page.

×

See thePyPA specification update process for how to propose changes.

Abstract

Extracting a source distribution archive should normally use thedatafilter added inPEP 706.We clarify details, and specify the behaviour for tools that cannot use thefilter directly.

Motivation

Thesource distributionsdist is defined as a tar archive.

Thetar format is designed to capture all metadata of Unix-like files.Some of these are dangerous, unnecessary for source code, and/orplatform-dependent.As explained inPEP 706, when extracting a tarball, one should always eitherlimit the allowed features, or explicitly give the tarball total control.

Rationale

For source distributions, thedata filter introduced inPEP 706is enough. It allows slightly more features thangit andzip (bothcommonly used in packaging workflows).

However, not all tools can use thedata filter,so this PEP specifies an explicit set of expectations.The aim is that the current behaviour ofpipdownloadandsetuptools.archive_util.unpack_tarfile is valid,except cases deemed too dangerous to allow.Another consideration is ease of implementation for non-Python tools.

Unpatched versions of Python

Tools are allowed to ignore this PEP when running on Python without tarfilefilters.

The feature has been backported to all versions of Python supported bypython.org. Vendoring it in third-party libraries is tricky,and we should not force all tools to do so.This shifts the responsibility to keep up with security updates from the toolsto the users.

Permissions

Common tools (git,zip) don’t preserve Unix permissions (mode bits).Telling users to not rely on them insdists, and allowing tools to handlethem relatively freely, seems fair.

The only exception is theexecutable permission.We recommend, but not require, that tools preserve it.Given that scripts are generally platform-specific, it seems fitting tosay that keeping them executable is tool-specific behaviour.

Note that whilegit preserves executability,zip (and thuswheel)doesn’t do it natively. (It is possible to encode it in “external attributes”,but Python’sZipFile.extract does not honour that.)

Specification

The following will be added tothe PyPA source distribution format specunder a new heading, “Source distribution archive features”:

Because extracting tar files as-is is dangerous, and the results areplatform-specific, archive features of source distributions are limited.

Unpacking with the data filter

When extracting a source distribution, tools MUST either usetarfile.data_filter (e.g.TarFile.extractall(...,filter='data')), ORfollow theUnpacking without the data filter section below.

As an exception, on Python interpreters withouthasattr(tarfile,'data_filter')(PEP 706), tools that normally use that filter (directly on indirectly)MAY warn the user and ignore this specification.The trade-off between usability (e.g. fully trusting the archive) andsecurity (e.g. refusing to unpack) is left up to the tool in this case.

Unpacking without the data filter

Tools that do not use thedata filter directly (e.g. for backwardscompatibility, allowing additional features, or not using Python) MUST followthis section.(At the time of this writing, thedata filter also follows this section,but it may get out of sync in the future.)

The following files are invalid in ansdist archive.Upon encountering such an entry, tools SHOULD notify the user,MUST NOT unpack the entry, and MAY abort with a failure:

  • Files that would be placed outside the destination directory.
  • Links (symbolic or hard) pointing outside the destination directory.
  • Device files (including pipes).

The following are also invalid. Tools MAY treat them as above,but are NOT REQUIRED to do so:

  • Files with a.. component in the filename or link target.
  • Links pointing to a file that is not part of the archive.

Tools MAY unpack links (symbolic or hard) as regular files,using content from the archive.

When extractingsdist archives:

  • Leading slashes in file names MUST be dropped.(This is nowadays standard behaviour fortar unpacking.)
  • For eachmode (Unix permission) bit, tools MUST either:
    • use the platform’s default for a new file/directory (respectively),
    • set the bit according to the archive, or
    • use the bit fromrw-r--r-- (0o644) for non-executable files orrwxr-xr-x (0o755) for executable files and directories.
  • Highmode bits (setuid, setgid, sticky) MUST be cleared.
  • It is RECOMMENDED to preserve the userexecutable bit.

Further hints

Tool authors are encouraged to consider howhints for furtherverification intarfile documentation apply for their tool.

Backwards Compatibility

The existing behaviour is unspecified, and treated differently by differenttools.This PEP makes the expectations explicit.

There is no known case of backwards incompatibility, but some project out thereprobably does rely on details that aren’t guaranteed.This PEP bans the most dangerous of those features, and the rest ismade tool-specific.

Security Implications

The recommendeddata filter is believed safe against common exploits,and is a single place to amend if flaws are found in the future.

The explicit specification includes protections from thedata filter.

How to Teach This

The PEP is aimed at authors of packaging tools, who should be fine witha PEP and an updated packaging spec.

Reference Implementation

TBD

Rejected Ideas

None yet.

Open Issues

None yet.

Copyright

This document is placed in the public domain or under theCC0-1.0-Universal license, whichever is more permissive.


Source:https://github.com/python/peps/blob/main/peps/pep-0721.rst

Last modified:2025-02-01 08:55:40 GMT


[8]ページ先頭

©2009-2026 Movatter.jp