Section 15.30

tarfile module

Full name: tarfile: tar, short for tape archive, the Unix archive format, joined to file.

Definition

The tarfile module is the Python §12.23 standard-library module that reads and writes tar archives. A tar archive (tar is short for tape archive) is one file that holds a sequence of members. Each member is a header, which records a name, a type, permission bits, an owner and times, followed by the content if the member is a regular file. Member types include regular files, directories, symbolic links §13.10, hard links, device files and named pipes. Extracting an archive recreates its members as files below a destination directory.

Where it fits

Main entry: Archive path traversal (§15.18).

The Archive path traversal entry explains the attack: the name of a member, or the target of a link member, leads outside the destination, so extraction writes where the archive’s author should not be able to write. What is specific to tarfile is the extraction filter, a function that receives each member’s header just before extraction and may pass it unchanged, change it, skip the member or refuse it by raising an exception. Filters arrived in Python 3.12 and were backported as security fixes to some older releases; hasattr(tarfile, "data_filter") tells whether the running interpreter §2.13 has them.

extractall and extract take the filter as the argument filter. Three filters have names:

A call without a filter uses TarFile.extraction_filter if the program has set it. Otherwise the default is equivalent to "fully_trusted" up to Python 3.13 and is "data" from Python 3.14, a secure default §1.47. The same line of code is therefore protected on one interpreter and unprotected on another.

A two-member archive shows the difference. Member 1 is a symbolic link named cfg whose target is /etc; member 2 is a regular file named cfg/cron.d/job. With "fully_trusted", the link is created and member 2 is written through it to /etc/cron.d/job, if the process may write there. With "tar", member 1 is extracted, but member 2 is refused, because its path leads through the new link to outside the destination. With "data", member 1 is already refused, because its target is absolute.

The named filters judge one member at a time. Python’s documentation states that they do not prevent denial of service §1.28: they limit neither the number of members nor file sizes, and a compressed archive can expand far beyond its size on disk, a bound the zlib module §15.32 entry treats.

Questions answered

What filter and destination policy governs each extracted member?

Each extracted member should be governed by a filter that the program names explicitly, normally filter="data" or a function that first calls tarfile.data_filter and then applies stricter rules, together with a destination policy that the program enforces itself. A program that leaves the filter out hands the decision to the interpreter: up to Python 3.13 the default honors everything the archive says, including links that lead outside the destination; from 3.14 it is the "data" filter. A complete policy has six parts:

  1. Filter. Pass it explicitly, and refuse to run where tarfile.data_filter does not exist.

  2. Member types. Accept only the types the application needs, often regular files and directories.

  3. Names. Allow only expected characters, extensions and lengths, and refuse a repeated name, which would overwrite the earlier member.

  4. Destination. Extract into a new, empty directory that the program has just created and no other user can write, so that no existing link can redirect a write.

  5. Sizes. Before writing, check the member count, each member’s size and the running total against fixed limits; for a compressed archive, also limit the decompressed bytes.

  6. Failure. A refused member stops extractall but leaves earlier members on disk, so treat any refusal or error as failure of the whole archive: delete the new directory, and move the result into place only after every member has passed.

Related entries: Archive path traversal (§15.18), File descriptor versus pathname (§15.21), zlib module (§15.32), Hard link versus symbolic link (§13.10), stat module (§15.29). Further reading: [52].