T09 · Insecure Skill Coding Practices
- Location
scripts/generate-rate-table.js:121- Finding
Unrestricted Output Path Traversal and File Overwrite
- Content
View full analysis
Vulnerability Details
File Location:
scripts/generate-rate-table.js:38-39, 121-124
Vulnerability Type: Path traversal and unrestricted file overwrite
Risk Level: MediumVulnerable Code
javascript function parseArgs() { const args = process.argv.slice(2); const productName = args[0] || '费率表'; const params = { ...DEFAULT_PARAMS }; // ... return { productName, params }; } // ... const fileName = `${productName}费率表.xlsx`; const filePath = path.join(process.cwd(), fileName); XLSX.writeFile(wb, filePath);Technical Analysis
The product name is read directly from the command line and incorporated into the output filename without validation or sanitization. An attacker can include absolute or relative path components, including
../, in this value.path.join()normalizes path traversal segments but does not verify that the resulting path remains under the intended working directory. The resulting path is passed toXLSX.writeFile(), which creates the file or replaces an existing file at that location.The
.xlsxsuffix restricts the final filename but does not prevent directory traversal or replacement of writable files whose names end with that suffix.Attack Path
-
An attacker or untrusted caller controls the product-name argument passed to the generator.
-
The attacker invokes the script with a traversal value, for example:
bash node scripts/generate-rate-table.js "../../shared/report" -
The script constructs a filename equivalent to:
text ../../shared/report费率表.xlsx -
path.join(process.cwd(), fileName)normalizes the path outside the current working directory. -
If the destination directory exists and is writable by the process,
XLSX.writeFile()creates or overwrites the target file there.
The same input can pass through
scripts/generate-rate-table.shbecause the wrapper correctly quot ...[truncated 620 chars]-
- Remediation
View remediation
Remediation Suggestions
- Treat the product name as a display label rather than a path. Reject path separators, traversal segments, control characters, and platform-specific reserved names.
- Generate files only inside a dedicated output directory.
- Resolve both the output directory and candidate path to absolute paths, then verify that the candidate remains inside the approved directory.
- Avoid replacing existing files unless overwrite behavior is explicitly requested and authorized.
- Apply a reasonable filename-length limit and reject empty names after sanitization.
- Return an error rather than silently transforming clearly malicious input.
Example hardening approach:
javascript const fs = require('fs'); const path = require('path'); function validateProductName(value) { if ( typeof value !== 'string' || value.length === 0 || value.length > 100 || value === '.' || value === '..' || /[\/\\\0-\x1F\x7F]/.test(value) ) { throw new Error('Invalid product name'); } return value; } const safeProductName = validateProductName(productName); const outputDir = path.resolve(process.cwd(), 'workspace'); fs.mkdirSync(outputDir, { recursive: true }); const fileName = `${safeProductName}费率表.xlsx`; const filePath = path.resolve(outputDir, fileName); const relativePath = path.relative(outputDir, filePath); if ( relativePath === '' || relativePath.startsWith(`..${path.sep}`) || path.isAbsolute(relativePath) ) { throw new Error('Output path escapes the workspace'); } if (fs.existsSync(filePath)) { throw new Error('Output file already exists'); } XLSX.writeFile(wb, filePath);
