Skip to main content
coreydaley.dev

Zip slip is more than ../: Unicode dots, NTFS streams, and symlinks

Zip slip is the bug where an archive entry named ../../.bashrc gets written outside the folder you extracted into. The textbook fix is to reject any path with a .. segment, and most extractors stop there. Purrchiver’s path check started there too, and over May and June it grew into something longer. Here is what it does now, and why each step is there.

The baseline

Every entry path an archive reports is untrusted. libarchive, UnRAR, and Apple’s Archive framework all hand back whatever string the archive contains, so the check runs on the raw value before anything touches the disk. An entry is skipped, not aborted on, when its path:

  • is empty
  • is absolute (starts with /)
  • contains a NUL byte anywhere (safe.txt\0/../../etc/passwd)
  • starts with a Windows drive letter (C:)
  • has a .. segment anywhere, not just at the start

One bad entry doesn’t kill the extraction. It gets logged and skipped, and the rest of the archive extracts normally.

Split on both slashes

Archives made on Windows can use \ as the separator. If you only split on /, then ..\..\evil.dll is a single harmless-looking segment. The check splits on both, so .. is caught whichever separator the archive used.

Normalize first

Unicode lets the same visible text be encoded more than one way. é can be one scalar (NFC) or e plus a combining accent (NFD). Swift’s String == compares canonically, so ".." comparisons are safe either way. But any check that walks unicodeScalars, like the dot test below, sees the raw encoding. So the path is converted to NFC once, up front, and every later check, plus the name that eventually lands on disk, works on that one form:

let path = raw.precomposedStringWithCanonicalMapping

Dot lookalikes, as defense in depth

There are characters that render like dots and aren’t: the full-width period (U+FF0E), the one-dot leader (U+2024), the two-dot leader (U+2025, a single character that looks like ..), and the horizontal ellipsis (U+2026). NFC leaves them alone. They only fold to ASCII periods under compatibility normalization (NFKC), which would also rewrite legitimate names, such as full-width letters in Japanese filenames.

I don’t know of a macOS file API that treats U+2025 as a parent-directory reference, and I’m not claiming one does. The reason to reject these anyway is that a path check can be undone by any later step that normalizes differently: a URL API, a filesystem, or a tool someone runs on the extracted tree. A segment made entirely of dot-like characters has no legitimate use. So the rule is that the only all-dot segment allowed is a single ASCII .:

let dotLike: (Unicode.Scalar) -> Bool = {
    $0 == "." || $0 == "\u{FF0E}" || $0 == "\u{2024}"
        || $0 == "\u{2025}" || $0 == "\u{2026}"
}
if segment.unicodeScalars.allSatisfy(dotLike), segment != "." {
    return nil
}

NTFS streams and colons

name:stream is NTFS alternate-data-stream syntax, and archives made on Windows can carry it. HFS+ historically mapped : to /; APFS treats it as a literal character. Rather than depend on which filesystem the destination is on, any segment containing : is rejected.

That has a real cost. A Mac file that legitimately has a colon in its name (Finder shows it as a slash) won’t extract. I took that trade over having the same archive behave differently depending on the destination volume.

Check where the write actually lands

A sanitized relative path still has to be joined to a destination, and the destination may already contain symlinks, put there by the user or by an earlier entry. So after joining, the extractor resolves the deepest existing ancestor of the target, through any symlinks, appends the part that doesn’t exist yet, and confirms the result is still inside the destination root. Resolving only the leaf isn’t enough, because a file you’re about to create doesn’t exist yet and resolves to nothing.

The same check runs again on paths the extractor makes up itself. When you choose “Keep Both” on a name conflict, the name 2.ext path is new, and it gets confined too.

A crafted archive can ship a symlink docs -> /Users/you and then a regular file docs/.zshrc. Each entry passes a .. check on its own. Together they write through the link to anywhere the process can reach.

So symlink targets go through the same check as entry paths. If the target is absolute or contains .., the link isn’t created. That’s true whatever “Symbolic links on extract” is set to, including “Recreate”. In June I marked this rejection in the code as load-bearing, so a future refactor doesn’t quietly weaken it. The cost is that legitimate relative links that climb, such as ../lib/libfoo.dylib, are skipped too.

Update, October 23: That cost turned out to be too high. Apps ship links like Contents/MacOS/helper -> ../Helpers/helper, and dropping them broke the app’s signature. In 1.1, a link whose leading .. stays inside its own top-level folder is kept, and the stored target is written as-is rather than made absolute. A link that would climb to the extraction folder itself, or has .. after a name, is still refused, and a climbing link is also checked on disk, so an earlier link in its path can’t change where the .. leads. A test signs an app with such links, zips it the way Finder does, extracts it, and runs codesign --verify --deep --strict on the result.

Apple Archive (.aar) goes through Apple’s framework instead of libarchive, and I don’t rely on that framework to confine symlinks. It extracts into a private staging folder, every symlink in that folder is deleted before anything is read, and the read path refuses symlinks again in case one appears later.

The rest of the boundary

A few smaller things sit next to the path check:

  • The file can’t change under you. Purrchiver sniffs magic bytes to pick a backend, then opens the file. It records the file’s device, inode, size, and modification time at the sniff and refuses to open if any of them changed. That narrows the swap-the-file window to almost nothing. It doesn’t close it, because the comparison is still by path.
  • Permission bits are masked. Setuid, setgid, sticky, and group- and world-write bits from the archive are stripped before they’re applied.
  • Decrypted data starts private. The in-progress file for an entry is created at mode 0600, not the umask default. The entry’s own permissions are applied only once it’s finished, so on a shared Mac a half-written decrypted file is never readable by other users.

Test it with a real archive

Unit tests on the path function were already in place. In June I added the test I should have written first: build a real ZIP with entries named ../escape.txt, ../../escape2.txt, and nested/../../escape3.txt, plus one benign file. Then extract it with the real extractor and check the disk. Nothing may appear above the destination, and the benign file must still extract. That test runs the real extraction pipeline from end to end, which a unit test of one function can’t do.

The takeaway: rejecting .. is the floor. Normalize before checking, split on both separators, confine the resolved write location as well as the string, treat symlink targets as paths, and prove it with an end-to-end test using a hostile archive.

Related posts