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.

Lesson 60 of 69 · 15 min read · practice only with authorization

Diagram

From an entry name to a path outside the extract root

Follow the path. One example at a time.

Step 1 of 4

Archive

A caller uploads an archive. The product means to unpack it into one folder.

Illustration: an archive is a list of names
# An upload contains entry names.
# Each name is data. It is not a path you already approved.
What changes the outcome
Name

The archive says where the file goes.

Join

The string is appended to the destination.

Disk

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.

  1. 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.
  2. 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.
  3. 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.

1 / 3

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.

  1. MapPath traversal: when a filename parameter walks out of its folderHow file download and upload features turn user-supplied names into filesystem paths, how traversal sequences and encodings escape the intended folder, what content-discovery tools such as DirBuster and gobuster actually find, and how to fix it with identifiers and canonical path checks.
  1. 01Normalize the path before you open itFilters that delete a few character sequences lose to encodings, extra separators, and links.

Practice

  1. LabControl benchA failure is described. Choose the control that changes the outcome.

Glossary

  1. CardPath traversalA file path built from user input escapes the intended directory and reads or writes elsewhere on the host.