Definition
Archive path traversal is a vulnerability in programs that unpack archives: a crafted archive makes the unpacking program, the extractor, create or change a file outside the destination directory it was given. An archive, such as a tar or zip file, packs many files, called members, into one file. Each member stores a name, a type (regular file, directory, symbolic link §13.10, hard link) and, for a link, a target. The escape comes from names that start at the root or climb with .., or from links that redirect later members.
The idea
An update tool unpacks a tar file into /srv/app/new. For each member it joins the destination and the stored name, and whoever built the archive chose that name. The name ../../../etc/cron.d/job climbs three levels, from new up to app, srv and the root, so the tool writes /etc/cron.d/job, a file whose commands a scheduler runs. Refusing every name with a .. component stops this case.
It does not stop a subtler case. A symbolic link is a small file whose content is another path, and the operating system follows it whenever a path passes through it. The first member is a symbolic link lib that points to /etc; the second is a regular file named lib/passwd. That name has no .. and no leading /, yet writing /srv/app/new/lib/passwd passes through the link and replaces /etc/passwd.
The core idea: a stored name is not a location. It is a list of steps that the operating system walks from the destination at the moment of the write. What each step finds depends on the directory tree at that moment, and earlier members built part of that tree. Containment is therefore a property of each actual write, not of the name as text. The security company Snyk named this class of flaw Zip Slip in 2018 [387]; the name covers every archive format, not only zip.
How it works
A naive extractor takes the members in stored order. For each one it joins the destination and the stored name, then creates what the member’s type asks for: it opens the path and writes the bytes of a regular file, creates a directory, creates a symbolic link holding the stored target, or creates a hard link, a second name for an existing file, to the stored target. Finally it applies the stored permission bits, owner and times.
Routes out of the destination.
Parent steps. The name
a/../../xclimbs above the destination.Absolute names. The name
/etc/xstarts at the root. Many path-joining functions, Python’s §12.23os.path.joinamong them, return/etc/xunchanged when the second part is absolute. On Windows, drive prefixes such asC:and backslash separators add more forms.A path through a link, as in the
lib/passwdcase.A link overwritten by name. A link
run.shpoints outside, and a later regular file also namedrun.shis written by opening that name, which follows the link.A hard link. A hard-link member whose target is
/etc/passwdgives that file a second name inside the destination, where the operating system permits the link. A later write to that name changes/etc/passwd.
A safe extractor enforces a written policy for every member at the moment of that member’s effect:
Allow only the member types the application needs, often only regular files and directories.
Refuse empty names, absolute names, drive prefixes and any name with a
..component, after splitting at every separator the platform accepts.If links are allowed, resolve each target from the directory that will hold the link, and refuse targets that lead outside. Judge every later member against the tree as the earlier members left it.
Let the operating system refuse escapes. On Linux, open the destination once and create each file relative to it with the
openat2call and its flagRESOLVE_BENEATH, which makes the call fail if any step of the walk leaves the destination.Refuse to overwrite existing entries unless the policy allows it. The open flags
O_CREATandO_EXCLtogether fail if the name already exists, even as a symbolic link.Do not restore owners or the set-user-ID bit, a permission bit that makes a program run with its owner’s rights. Bound the expanded size and the member count.
Python’s tarfile module §15.30 has offered extraction filters since version 3.12, and since version 3.14 its default filter is the one named data. That filter refuses absolute names, members whose location after following links lies outside the destination, links that point outside or to an absolute path, and device files; it also drops stored owners and the set-user-ID bit. The documentation still warns against extracting an untrusted archive without prior inspection, because the filter does not block every dangerous feature.
Worked example
The destination is the empty directory /dest, and /other is a directory elsewhere. The archive holds two members, in this order: a symbolic link named link with target /other, then a regular file named link/x that holds the 4 bytes ABCD.
Naive extraction. Member 1 creates the link /dest/link. Member 2’s name has the components link and x. Neither is .., and the name is not absolute, so a name check passes. The joined path /dest/link/x starts with /dest/, so a prefix check passes too. To open it, the operating system walks from the root to dest, then to link, which is a symbolic link, so the walk continues at /other and creates /other/x. The 4 bytes land outside /dest.
With a policy. A policy that allows no links refuses member 1; if extraction continues, the extractor creates /dest/link as an ordinary directory and member 2 lands in /dest/link/x. A policy that checks link targets also refuses member 1, because /other is absolute. It would refuse the relative target ../other too, since /dest/../other is /other, and allow sub/data, which gives /dest/sub/data. With openat2 and RESOLVE_BENEATH, creating link/x relative to a file descriptor §15.21 for /dest (a number that refers to the opened directory) fails with the error EXDEV at the step link, even if the extractor’s own checks let member 1 through.
The prefix trap. The name ../dest2/x joins and normalizes to /dest2/x. That text starts with /dest, so a check for that prefix accepts a file outside the destination. Compare whole components, or test the prefix /dest/ including its final separator.
Stated precisely
Let be the destination directory. The subtree of is together with every file and directory reachable from by going down through directory entries without following a symbolic link. Extraction is contained when every effect it performs (creating, writing, linking, or changing permissions, owner or times) applies, at the moment of the effect, to an object in the subtree of , and no hard link it creates gives an outside object a name inside the subtree. A link inside the subtree that points outside is not itself an escape, but a later write through it is. A policy that allows such links must ensure that nothing follows them, later members included.
Checking stored names as text is sufficient only if all four of these assumptions hold: (1) the archive holds only regular files and directories; (2) the destination starts empty; (3) no other process changes the destination during extraction; (4) the platform is a POSIX (Portable Operating System Interface) system, where / is the only separator. Then refusing empty names, absolute names and names with a .. component keeps every effect inside, because each walk starts at and descends only through directories that the extraction itself created, none of them a link. If any assumption fails, containment must be enforced at each open: with RESOLVE_BENEATH, or by opening each component relative to its parent with symbolic links refused.
Limits and common mistakes
Containment does not limit quantity. A decompression bomb, a small archive that expands to an enormous size, fills the disk from inside the destination. Bound the expanded size, the member count and the expansion ratio.
Searching names for the substring .. is both too strict and too weak: it refuses the harmless name notes..txt and misses absolute names, Windows backslash forms and links. Test whole components.
Normalizing a path as text, as Python’s os.path.normpath does, deletes the component before each .. without asking whether that component is a symbolic link. When it is one, the text and the real walk disagree.
Resolving links before the write, for example with Python’s os.path.realpath, judges the tree as it was at the check. A later member or another process can change the tree before the write. That gap is a time-of-check to time-of-use §9.26 (TOCTOU) race.
Extract as an unprivileged user, following least privilege §1.15. With administrator rights every escape does more damage, and restored owners or set-user-ID bits let an archive plant a program that runs with another user’s rights.
The Common Weakness Enumeration (CWE), a public catalog of software weaknesses, lists the two halves of this flaw as CWE-22, path traversal, and CWE-59, link following.
Questions answered
Can a symlink member make a later relative member escape the destination?
Yes. A symbolic link member, an archive member that creates a link to another path, can make a later member with a harmless relative name land outside the destination directory. Member 1 is a link named link that points to /other; member 2 is a regular file named link/x. The second name has no .. and is not absolute, so a check of the names alone passes. Yet writing /dest/link/x follows the link and creates /other/x. A later member with exactly the link’s name escapes the same way, because opening that name for writing follows the link.
The defense judges each write against the directory tree as it stands at that moment. Refuse link members the application does not need, and refuse links whose target leads outside. Create every file with a call that cannot leave the destination, such as Linux openat2 with RESOLVE_BENEATH, or by opening each path component relative to its parent with links refused. Checking stored names as text before extraction cannot see what earlier members did to the tree.
Related entries: File descriptor versus pathname (§15.21), O_NOFOLLOW and descriptor-relative access (§15.23), tarfile module (§15.30), zlib module (§15.32), Namespace and symlink resolution (§13.9), Hard link versus symbolic link (§13.10), Time-of-check to time-of-use (TOCTOU) (§9.26). Further reading: [52, 141, 387].