# Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal') (CWE-22) The product uses external input to construct a pathname that is intended to identify a file or directory that is located underneath a restricted parent directory, but the product does not properly neutralize special elements within the pathname that can cause the pathname to resolve to a location that is outside of the restricted directory. **Stack:** JavaScript - Prevalence: 高 频繁被利用 - Impact: 关键 1 条严重级别为关键的规则 - Prevention: 已记录 6 个修复示例 **OWASP:** Broken Access Control (A01:2021-Broken Access Control) - #1 ## Description Many file operations are intended to take place within a restricted directory. By using special elements such as '..' and '/' separators, attackers can escape outside of the restricted location to access files or directories that are elsewhere on the system. ## Prevention 基于 2 条 Shoulder 检测规则的 Path Traversal 预防策略。 ### JavaScript Use path.basename() to strip directory components or validate resolved paths stay within allowed directories Validate that extracted archive entry paths resolve within the target directory before writing ## Warning Signs - [HIGH] unsafe extraction of zip/tar archives without path validation, which can lead to arbitrary file writ - [CRITICAL] untrusted user input used in file system operations without proper validation ## Consequences - 读取文件或目录 - 修改文件或目录 - 执行未授权代码 ## Mitigations - 使用经过审查的库或框架,以防止此弱点出现 - 使用严格符合规范的可接受输入的允许列表 - 对于文件名,使用限制字符集的严格允许列表 ## Detection - Total rules: 6 - Critical: 1 - Languages: go, javascript, typescript, python ## Rules by Language ### Javascript (2 rules) - **Path Traversal in File Operations** [CRITICAL]: Detects untrusted user input used in file system operations without proper validation. This can allow attackers to read or write arbitrary files on the server. - Remediation: Use path.basename() to extract filenames or validate resolved paths stay within allowed directories. ```javascript const safeName = path.basename(userInput); const filePath = path.join(__dirname, 'uploads', safeName); fs.readFile(filePath, callback); ``` Learn more: https://shoulder.dev/learn/javascript/cwe-22/path-traversal - **Zip Slip Path Traversal** [HIGH]: Detects unsafe extraction of zip/tar archives without path validation, which can lead to arbitrary file writes via path traversal (Zip Slip). Zip Slip is a form of path traversal attack where a malicious archive contains entries with paths like "../../../etc/passwd" that escape the intended extraction directory and overwrite arbitrary files on the system. Vulnerable patterns: 1. Extracting zip entries without validating the extracted path 2. Not checking if extracted path is inside target dire - Remediation: Validate extracted paths are inside the target directory: ```javascript const path = require('path'); function isPathSafe(baseDir, targetPath) { const resolvedBase = path.resolve(baseDir); const resolvedTarget = path.resolve(baseDir, targetPath); return resolvedTarget.startsWith(resolvedBase + path.sep); } for (const entry of zip.getEntries()) { if (!isPathSafe(targetDir, entry.entryName)) { throw new Error('Path traversal attempt'); } zip.extractEntryTo(entry, targetDir, false, true); } ``` Learn more: https://shoulder.dev/learn/javascript/cwe-22/zip-slip ### Typescript (2 rules) - **Path Traversal in File Operations** [CRITICAL]: Detects untrusted user input used in file system operations without proper validation. This can allow attackers to read or write arbitrary files on the server. - Remediation: Use path.basename() to extract filenames or validate resolved paths stay within allowed directories. ```javascript const safeName = path.basename(userInput); const filePath = path.join(__dirname, 'uploads', safeName); fs.readFile(filePath, callback); ``` Learn more: https://shoulder.dev/learn/javascript/cwe-22/path-traversal - **Zip Slip Path Traversal** [HIGH]: Detects unsafe extraction of zip/tar archives without path validation, which can lead to arbitrary file writes via path traversal (Zip Slip). Zip Slip is a form of path traversal attack where a malicious archive contains entries with paths like "../../../etc/passwd" that escape the intended extraction directory and overwrite arbitrary files on the system. Vulnerable patterns: 1. Extracting zip entries without validating the extracted path 2. Not checking if extracted path is inside target dire - Remediation: Validate extracted paths are inside the target directory: ```javascript const path = require('path'); function isPathSafe(baseDir, targetPath) { const resolvedBase = path.resolve(baseDir); const resolvedTarget = path.resolve(baseDir, targetPath); return resolvedTarget.startsWith(resolvedBase + path.sep); } for (const entry of zip.getEntries()) { if (!isPathSafe(targetDir, entry.entryName)) { throw new Error('Path traversal attempt'); } zip.extractEntryTo(entry, targetDir, false, true); } ``` Learn more: https://shoulder.dev/learn/javascript/cwe-22/zip-slip