#0406
A file server validates user-supplied paths by checking whether the combined path string starts with the base directory prefix.
An attacker submits /safe/../../../etc/passwd. The naive prefix check passes (/basedir/safe starts with /basedir), but after resolving .. segments the path escapes the base directory.
This class of bug defeats simple string-prefix checks and is the root cause of many real-world path traversal vulnerabilities.
Prefix-only checks are a deceptively easy mistake. The fix requires understanding that path resolution must happen before validation — a subtle ordering bug with critical security consequences.
validateFilePath so it properly resolves all '..' segments before checking the prefix.'Path traversal detected' when the resolved path escapes baseDir.validateFilePath('/var/app', 'safe/../../../etc/passwd')// naive: '/var/app/safe' starts with '/var/app' → passes// resolved: '/etc/passwd' — does NOT start with '/var/app'// → throws Error('Path traversal detected')
validateFilePath('/var/app', 'data/reports/q1.pdf')// → returns '/var/app/data/reports/q1.pdf'