T09 · Insecure Skill Coding Practices
- Location
bin/genvr.js:92- Finding
Unrestricted Output Path Allows Arbitrary File Overwrite
- Content
View full analysis
{ https.get(url, (res) => { if (res.statusCode !== 200) { reject(new Error(`Failed to download: ${res.statusCode}`)); return; } const fileStream = fs.createWriteStream(dest); res.pipe(fileStream); fileStream.on('finish', () => { fileStream.close(); console.log(`Saved to ${path.resolve(dest)}`); resolve(); }); }).on('error', reject); }); } ``` ```js // Generate filename const cleanUrl = url.split('?')[0]; const ext = path.extname(cleanUrl) || '.png'; const timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19); const filename = args.filename || `${timestamp}-${args.category}${ext}`; await downloadFile(url, filename); ``` ### Technical Analysis The user-controlled `--filename` argument is passed directly to `fs.createWriteStream()` without validation or confinement to a designated output directory. Node.js permits absolute paths and relative traversal sequences such as `../`, so the destination can refer to any file writable by the current process. `fs.createWriteStream()` uses overwrite-compatible behavior by default. The implementation does not request exclusive creation, check whether the destination already exists, reject symbolic links, or verify that the resolved path remains inside an approved directory. Consequently, an existing file can be truncated and replaced. A symbolic link at the destination can also redirect the write to another file. The downloaded bytes originate fro ...[truncated 1314 chars]- Remediation
View remediation
