Extracting ZIP Files on Android: Keeping Entries Inside a Dedicated Directory
When importing a ZIP file into an Android app, define a precise write boundary: every extracted entry must remain inside a dedicated directory. An import success or error message alone is not enough to verify this requirement. This article covers extraction preconditions, canonical path comparisons and a fictional acceptance test set that verifies results against the contents of synthetic files you own.
Where the Trust Boundary Lies
The java.util.zip standard library does not check entry names for directory traversal characters. Simply joining the destination directory with a name from the archive therefore does not establish that the entry will remain inside it. Treat entry names as untrusted data and define the permitted write area separately.
The developer's task is to validate the target path before extracting each entry. For a product owner, this becomes an observable requirement: permitted files appear inside the destination with the expected contents, while an entry outside it is rejected before its contents are written. This check applies to a single ZIP import operation.
Prepare the Directory Before Importing
This protection assumes that only the app can write to the destination directory and that no external party controls it. This is an independent precondition: comparing paths does not establish exclusive access. If another party controls the directory, other risks remain, including attacks involving symbolic links.
Before extraction, ensure that the destination exists, is a directory and is empty. Reject a destination that is not a directory. Requiring an empty directory helps prevent accidental overwriting of existing files. Perform these checks before extraction, separately from validating archive entry names.
Compare Canonical Paths with a Separator
For each entry, construct the target path and obtain its canonical representation using File.getCanonicalPath(). Canonicalize the destination directory path as well. Then check that the entry's canonical path starts with the destination's canonical path followed by File.separator. In the Kotlin example, a prefix mismatch causes a ZipException to be thrown.
The separator is an essential part of the comparison. In a fictional example, the import-other directory sits alongside import. Matching the beginning of the strings does not establish that a path belongs inside import: the checked prefix must specifically identify its interior. Perform this check before extracting the corresponding entry; checking one entry does not replace checking the others.
You can also check whether the target file already exists to prevent accidental overwriting. This complements the boundary check but does not, by itself, establish that the path is inside the destination.
Run a Positive Control with Your Own Archive
The following are original proposed acceptance tests, not an official verification procedure. All names, archives and data are fictional; run the checks only within your app's own isolated space. Prepare a fresh, empty directory with the specified access conditions for each separate import.
Create your own ZIP containing note.txt and folder/item.txt, each with predetermined synthetic text. Before writing either entry, check that its canonical path falls under the permitted prefix. After importing, confirm that both entries were extracted successfully, are located inside the destination and contain exactly the expected contents. A success status alone is insufficient for acceptance.
Verify Rejection and an Unchanged External File
In your own test space, create a synthetic control.txt file alongside the empty import directory and save its original bytes. Prepare your own ZIP containing the entry ../control.txt. The acceptance criterion has two parts: the import is rejected during path validation before that entry's contents are written, and the control file's bytes after the attempt exactly match the originals.
Test the prefix boundary separately: create a neighboring import-other directory containing a control file, and your own archive containing the entry ../import-other/control.txt. Expect rejection and confirm that this file remains unchanged. This case helps detect comparisons that check only the start of a string without a directory separator.
Do not add a requirement to roll back permitted entries extracted before the rejected entry. The path check discussed here does not establish that guarantee; assess the state of files already extracted separately from rejection of the specific entry.
Prioritize Fixes and Verify Preconditions
First, establish the destination preconditions and require a canonical path check for every entry before extraction. If the implementation currently compares only the directory's textual prefix, replace it with the canonical path followed by File.separator. Use an additional target-file existence check to prevent accidental overwriting.
To verify the preconditions, separately supply your own nonempty directory containing a synthetic file and your own regular file in place of the destination directory. In both cases, expect rejection before extraction and confirm that the original data remains unchanged. Together with the positive control and the two boundary escape checks, these provide concrete acceptance criteria for the import operation.
What This Procedure Confirms
Passing the proposed tests confirms the expected behavior on the selected data you own; it does not guarantee protection against every archive-related risk. The procedure is limited to paths during extraction. It does not cover size or compression ratio limits, server uploads, format validation, FileProvider, permissions on objects owned by others, backups, temporary files and the WebView cache, deserialization or IPC configuration.
If your product has a website or web component, you can start with a free CheckMyApp website or web-product audit. The acceptance criteria described here apply to the Android ZIP import operation and require separate verification within the app.