Description
When extracting a ZIP, confine each entryName to the destination directory. Link entries or directories that another user can change can redirect writes.
Potential impact
The service account may overwrite configuration, logs, or data. If another process loads or runs a changed file, the service may fail or execute unintended code.
Remediation
- Create a new private directory under a trusted parent. Allow only necessary extensions and directories, using library entry filters where available. Skip entries marked as UNIX symlinks in their ZIP external attributes.
- Normalize the name, then reject absolute paths, Windows drive prefixes, and remaining parent components. Check the
path.resolveresult against the destination withpath.relative. - Restrict directory permissions and create new files with
wx.
Examples
Before
javascript
// Before: extract using entry paths directly (Zip Slip).
const fs = require("fs");
const path = require("path");
const AdmZip = require("adm-zip");
function extractUnsafe(zipPath, destDir) {
const zip = new AdmZip(zipPath);
zip.getEntries().forEach((entry) => {
const outPath = path.join(destDir, entry.entryName); // No validation
const data = entry.getData();
// An entry.entryName of "../../../../var/www/html/shell.js" can escape the destination.
fs.mkdirSync(path.dirname(outPath), { recursive: true });
fs.writeFileSync(outPath, data); // Create or overwrite a file at an uncontrolled path.
});
}
After
javascript
// After: validate entry types and final paths in a new private directory.
const fs = require("fs");
const path = require("path");
const AdmZip = require("adm-zip");
function isUnixSymlink(entry) {
// The high 16 bits of the ZIP external attributes contain the UNIX file mode.
const unixMode = entry.header.attr >>> 16;
return (unixMode & 0o170000) === 0o120000;
}
function extractSafe(zipPath, destParent) {
const zip = new AdmZip(zipPath);
const parentReal = fs.realpathSync(destParent);
const destReal = fs.mkdtempSync(path.join(parentReal, "extract-"));
fs.chmodSync(destReal, 0o700);
for (const entry of zip.getEntries()) {
// 1) Reject UNIX symbolic-link entries.
if (isUnixSymlink(entry)) continue;
// 2) Normalize the path.
const normalized = path.normalize(entry.entryName);
// 3) Reject empty/absolute paths, drive prefixes, and parent components.
if (normalized === "" || normalized === ".") continue;
if (path.isAbsolute(normalized)) continue;
if (normalized.split(path.sep).includes("..")) continue;
if (/^[A-Za-z]:/.test(normalized)) continue;
// 4) Resolve the final path and check containment.
const targetPath = path.resolve(destReal, normalized);
if (
path.relative(destReal, targetPath) !== ".." &&
!path.relative(destReal, targetPath).startsWith(".." + path.sep) &&
!path.isAbsolute(path.relative(destReal, targetPath))
) {
if (entry.isDirectory) {
fs.mkdirSync(targetPath, { recursive: true, mode: 0o700 });
continue;
}
// 5) Create only within the new private directory, without overwriting files.
fs.mkdirSync(path.dirname(targetPath), { recursive: true, mode: 0o700 });
const data = entry.getData();
fs.writeFileSync(targetPath, data, { flag: "wx", mode: 0o600 });
}
}
return destReal;
}
The example skips UNIX symlink entries and writes only accepted paths. entry.getData() expands one entry in memory, so enforce a separate limit on actual expanded bytes.