flawopen.com/Vulnerabilities/Path Traversal & Zip Slip
Imagine you are in a public museum where visitors are only allowed on Floor 1. In the elevator, the buttons for Floors 2 and 3 are removed. But an attacker discovers that pressing 'Up, Down, Down, Up' confuses the elevator computer, taking them into the restricted basement evidence vault. In Path Traversal, an attacker feeds dot-dot-slash (../../) sequences into a file downloader or unzipper, breaking out of the intended folder and rewriting critical system configuration files.
../, ..\), URL-encoded paths (%2e%2e%2f)/etc/passwd), remote code execution via file overwriteWhen an application concatenates user input directly into file paths (e.g. open('/data/' + filename)), path navigation tokens like ../ resolve relative to the root filesystem. In archive extraction (Zip Slip), malicious tar or zip archives contain relative filenames like ../../../../var/www/html/shell.php that overwrite webroots upon unzipping.
// VULNERABLE: Direct path concatenation and unvalidated zip extraction
const fs = require('fs');
const path = require('path');
app.get('/download', (req, res) => {
// Attacker passes: ../../../../etc/passwd
const filePath = path.join('/var/app/public/files', req.query.file);
res.sendFile(filePath); // Exfiltrates arbitrary system files!
});
// HARDENED: Canonicalize path and assert it remains strictly within base directory
const fs = require('fs');
const path = require('path');
app.get('/download', (req, res) => {
const BASE_DIR = path.resolve('/var/app/public/files');
const safePath = path.resolve(BASE_DIR, req.query.file);
// Ensure canonical path starts with base directory prefix
if (!safePath.startsWith(BASE_DIR + path.sep)) {
return res.status(403).send("Forbidden: Directory traversal attempt detected.");
}
res.sendFile(safePath);
});
file=report.pdf query parameter.:file=../../../../etc/shadow or ..%2f..%2f.env.:path.resolve() or os.path.realpath() before opening files to strip all relative traversal tokens.canonical_path.startswith(safe_base_directory) before reading or writing any file on disk.