Archive extraction that leaves the directory
A name inside a zip or a similar archive is still a path. Joining it to a destination directory can write outside that directory.
Diagram
From an entry name to a path outside the extract root
Step 1 of 4
Archive
A caller uploads an archive. The product means to unpack it into one folder.
# An upload contains entry names. # Each name is data. It is not a path you already approved.
The archive says where the file goes.
The string is appended to the destination.
A file can land outside the folder.
The example on the open step is one illustration, or the fix you can paste into an application you own.
What this page is made of
4 pieces. Each one says what fails, who is affected, what you would notice, and what you change.
An entry name is a path
- What fails
- Archive formats store a name for each member. That name is chosen by whoever built the archive, not by your directory layout.
- Who is affected
- Whoever owns files the process can overwrite outside the extract folder.
- What you would notice
- An unpack call that joins the entry name to a destination with ordinary string or path joins and no containment check.
- What you change
- Resolve and compare to the root before any write. Reject the whole archive on a bad name.
Links inside the archive
- What fails
- A member can be a link that points outside the root even when the member’s own name looks safe.
- Who is affected
- The same file owners, because the write follows the link.
- What you would notice
- An extractor that does not refuse links, or that checks the name and then copies through a link.
- What you change
- Reject link entries, or extract with a library mode that does not follow them. Check the real path after creation.
The process’s rights
- What fails
- Containment is the first control. The account that unpacks should still be unable to write the web root, the home directory, or configuration.
- Who is affected
- Everyone who depends on files that account can change.
- What you would notice
- An application user that owns the deployment directory and also unpacks uploads.
- What you change
- Run extraction as a user that can write only the scratch directory. Move a vetted file into place afterward with a name you generated.
What you notice in review
- What fails
- The bug is visible without a hostile sample: the unpack loop uses the entry name as a path segment.
- Who is affected
- The operator who has to restore a file that was overwritten.
- What you would notice
- No test where an entry name escapes the root and the extract is rejected.
- What you change
- Add that test with a name you constructed in the test, and assert zero files outside the root.
Archives carry paths, not just bytes
People think of a zip file as a bag of documents. The format also stores a name for each entry, and that name can include directories. If you unpack by joining that name to a folder, you are trusting the archive to stay inside the folder. It does not have to. This failure is often called zip slip. The same shape exists in other archive formats. The format is not the lesson. The join is.
The caller who uploaded the archive chose those names, or forwarded an archive from someone else. Either way the names are input. Extraction is a write, which makes a bad name worse than a bad read: you can overwrite a file the process can reach, not only open one.
Resolve, then compare to the root
For each entry, resolve the destination against the extract root and require the result to be the root or a path inside it. Do this before you create directories or write bytes. Rejecting one punctuation sequence and then joining anyway is how encodings and extra separators survive. The resolved path is the one the operating system will use. That is the string to check.
Refuse entries that are links. A name that stays inside the root can still point at a file outside it if you create a link and then write through it. If the library you use has a containment mode, turn it on and add a test that proves it. A dependency is not a control until you have seen it reject an escaping name.
The account is the second control
A correct check fails closed. A missed check fails as whatever user ran the unpack. If that user owns the application directory, a bad entry can replace code or configuration. Give the unpacker a scratch directory and nothing else. After the names check out, copy the files you expected using names you generated, or serve them from the scratch space directly.
You would notice an unpack loop with no resolve-and-prefix check, a process user that can write the deploy directory, and no test for an escaping entry. The people affected are the owners of whatever that user can overwrite. Do not attach a sample archive to the ticket. Describe the entry name class and the root.
What you change
Centralize extraction in one function that enforces the root, rejects links, caps the uncompressed size, and caps the number of entries. Size caps are the resource control. They are not the path control. Call the function from every upload path, including the one an admin tool added later.
Test with an entry whose resolved path leaves the root, and assert that the function throws and the destination has no new file outside the root. Test a normal nested name and assert it lands inside. Those two cases are the whole contract.
Check yourself
Recognition and the fix. Nothing here is a procedure.
Upload unpacking joins each zip entry name onto the destination directory and writes the file. One name climbs out of that directory. What failed?
Sibling techniques
Pages at the same depth, plus a lab and a glossary card for this one.