CCyberwiki

Zip Slip

Zip Slip
ClassInjection, Access control
CWECWE-22: Improper Limitation of a Pathname to a Restricted Directory
OWASP Top 10A01:2025 Broken Access Control
Typical severityCritical when the server extracts archives from users
AffectsCode that extracts zip or tar files from users or plugins; hand-written extraction loops; outdated archive libraries

A zip file holds a list of entries, and each entry carries the path it should be written to. The zip specification allows that path to be ../../../../../../../../tmp/evil.sh. Ordinary archiving tools do not create such names, so the archive is built by hand or with a purpose-made tool.1 Zip Slip is the name under which Snyk disclosed, on 5 June 2018, a flaw in extraction code that joins each entry name to a destination folder and writes the file there without checking the result. Snyk found it in thousands of projects, including ones from HP, Amazon and Apache. The entry then lands outside the folder and can replace a file that already exists there.1 It is Path traversal delivered inside an archive, and it applies equally to tar, jar, war, cpio, apk, rar and 7z files.1

How an entry escapes

The disclosure used this Java loop as its example:

Enumeration<ZipEntry> entries = zip.getEntries();
while (entries.hasMoreElements()) {
  ZipEntry e = entries.nextElement();
  File f = new File(destinationDir, e.getName());
  InputStream input = zip.getInputStream(e);
  IOUtils.copy(input, write(f));
}

The fourth line builds the output path from the entry name and nothing checks it. The long run of ../ in the malicious name is deliberate: moving to the parent of the root folder leaves you in the root folder, so extra steps cost the attacker nothing and make it likely the path reaches / before descending to the chosen file.1 An absolute name such as /usr/local/bin/x achieves the same result without any dots.2

Overwriting a program, then running it remotely or waiting for the system to run it, gives the attacker command execution; overwriting a configuration file changes how the application behaves. The same code is vulnerable on a user's computer as on a server.1

Tar archives can also hold a symbolic link, and a link can change where later entries go. For example, one entry creates a link named assets that points to /etc, and a later entry named assets/cron.d/job passes a check on its name but is written through the link into /etc.3 In 2021 the npm package node-tar had checks of this kind bypassed: an archive with a folder and then a link of the same name, written with backslashes on Linux or in different letter case on a case-insensitive disk, let later files land anywhere. It was fixed in versions 4.4.16, 5.0.8 and 6.1.7, and the advisory tells users to upgrade instead of filtering tar input themselves.4

Where it has appeared

Snyk began private disclosure on 15 April 2018. The npm package unzipper released a fix two days later, in version 0.8.13 (CVE-2018-1002203), and adm-zip followed in 0.4.9 (CVE-2018-1002204).15 The flaw was most common in Java. Neither java.util.zip nor Apache commons-compress offers a call that extracts a whole archive, so developers wrote their own loops and shared them on Stack Overflow; those two libraries received documentation fixes only.15

Python's tarfile module carried the flaw as CVE-2007-4559 for fifteen years. The mailing list post that first described it, on 24 August 2007, said that a member named ../../../../../etc/passwd overwrites /etc/passwd when an administrator extracts the archive. In September 2022 Trellix estimated that the flaw was present in more than 350,000 open-source projects, and prepared patches for 11,005 repositories.6

VMware vCenter Server accepted a tar upload without authentication, and .. sequences in its entries reached files outside the intended folder (CVE-2021-21972).2 Anyone who could reach port 443 could run commands on the host with unrestricted privileges. The flaw has a CVSS score of 9.8 out of 10 and was added to the CISA catalogue of known exploited vulnerabilities on 3 November 2021.7

Safe extraction

The check belongs after the path is resolved: build the full output path, then confirm it is still inside the destination folder. In Node.js, path.isAbsolute() alone is documented as unsafe for this purpose; path.resolve() followed by path.relative() works:8

const path = require("node:path");
const dest = path.resolve("data/imports");

function outputPath(entryName) {
  const target = path.resolve(dest, entryName);
  const rel = path.relative(dest, target);
  if (rel === "" || rel.startsWith("..") || path.isAbsolute(rel)) {
    throw new Error("Entry outside the destination: " + entryName);
  }
  return target;
}

Python's zipfile extract() and extractall() strip drive letters and leading slashes and remove every .. component, so ../../foo../../ba..r becomes foo../ba..r. Code that writes entries itself, including through zipfile.Path, should resolve each name with os.path.abspath() and compare it with the folder using os.path.commonpath().9 For tarfile, Python 3.12 added a filter argument, and 3.14 made 'data' the default; before that, extraction trusted every member.310

import tarfile

with tarfile.open(upload_path) as tar:
    tar.extractall("data/imports", filter="data")

The data filter refuses absolute names, names that resolve outside the folder after following links, links that point outside it, and device files. The documentation still warns that no filter blocks every dangerous archive feature.3

Go 1.20 added filepath.IsLocal. It looks only at the text of a name: when it returns true, joining the name to a base folder gives a path inside that folder.11 Setting the environment variable GODEBUG=zipinsecurepath=0 (or tarinsecurepath=0) makes the standard readers return ErrInsecurePath for an archive holding such a name.12

The fixed versions named above are minimums; archive libraries belong on their current releases. The rest of an upload feature is covered under Unrestricted file upload.

Prompt for a coding agent

Find every place in this project that extracts a zip, tar, jar or other archive, including library calls such as adm-zip, unzipper, tar, zipfile, tarfile and archive/zip, and any loop that writes archive entries to disk. For each entry, resolve the full output path and refuse the entry if the path is not inside the destination folder; refuse symbolic links that point outside it. In Python use tarfile extractall with filter="data". Upgrade archive libraries to their current versions. List each file and line you changed.

In SecFlash

SecFlash reports code that writes archive entries where their names point as SF-SEC-741 on the Security page. Archive extraction without a path check also appears among the patterns grouped under SF-SEC-711 on the same page.

See also

References

  1. a b c d e f g Snyk Security (5 June 2018). "Zip Slip Vulnerability". Snyk.
  2. a b MITRE. "CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')". Common Weakness Enumeration.
  3. a b c Python Software Foundation. "tarfile: Read and write tar archive files". Python documentation.
  4. back GitHub Advisory Database (August 2021). "Arbitrary File Creation/Overwrite via insufficient symlink protection due to directory cache poisoning using symbolic links" (CVE-2021-37701). GitHub.
  5. a b Snyk. "Zip Slip" (affected libraries list). GitHub.
  6. back Claburn, Thomas (22 September 2022). "Python tarfile bug found in 350,000 projects". The Register.
  7. back NIST (published 24 February 2021). "CVE-2021-21972". National Vulnerability Database.
  8. back OpenJS Foundation. "Path". Node.js documentation.
  9. back Python Software Foundation. "zipfile: Work with ZIP archives". Python documentation.
  10. back Viktorin, Petr (2023). "PEP 706: Filter for tarfile.extractall". Python Enhancement Proposals.
  11. back The Go project. "filepath.IsLocal". Go package documentation.
  12. back The Go project (February 2023). "Go 1.20 Release Notes". go.dev.