T09 · Insecure Skill Coding Practices
- Location
dfmea.js:145- Finding
User-Controlled Path Traversal and Arbitrary File Overwrite
- Content
View full analysis
Vulnerability Details
File Location:
dfmea.js, lines 145–148
Vulnerability Type: Path traversal and unsafe file creation
Risk Level: MediumVulnerable Code
js const projectName = args[1] || 'New DFMEA Project'; const teamMembers = args.slice(2); const template = dfmea.generateTemplate(projectName, teamMembers); const outputFile = `${projectName.replace(/\s+/g, '_')}_dfmea.json`; dfmea.exportToJson(template, outputFile);The resulting path is passed to the following file-writing method at lines 126–128:
js exportToJson(dfmea, filename) { fs.writeFileSync(filename, JSON.stringify(dfmea, null, 2)); }Technical Analysis
The
createcommand constructs an output filename directly from the user-controlledprojectNameargument. The only transformation replaces whitespace with underscores. It does not reject:- Absolute paths
/or\path separators..traversal components- Paths outside an intended output directory
The constructed value is passed to
fs.writeFileSync, which follows the supplied path and overwrites an existing file by default. Consequently, a caller can direct generated JSON outside the current working directory.This issue requires permission to invoke the CLI and is limited by the operating-system permissions of the process. The fixed
_dfmea.jsonsuffix restricts which filenames can be targeted, but it does not prevent writing outside the intended directory or overwriting matching files.Attack Path
-
An attacker or untrusted automation invokes the CLI with a project name containing traversal components or an absolute path.
-
For example:
bash dfmea create "../../target" -
The application constructs this output path:
text ../../target_dfmea.json -
fs.writeFileSyncresolves that path relative to the process working directory. -
If the destination directory is writable, the application creates or overwrites
target_dfmea.jsonoutside the i ...[truncated 729 chars]
- Remediation
View remediation
Remediation Suggestions
- Write generated files into a fixed, application-controlled output directory.
- Treat the project name as a display value rather than a filesystem path.
- Convert it to a safe filename using a strict allowlist, such as ASCII letters, digits, hyphens, and underscores.
- Reject path separators, absolute paths,
.components, and..components. - Resolve the final destination and verify that it remains inside the approved output directory.
- Use exclusive creation, such as the
wxflag, if overwriting existing files is not explicitly intended. - Return a clear error when a name is invalid or the destination already exists.
Example hardening approach:
js const outputDir = path.resolve(process.cwd(), 'dfmea-output'); fs.mkdirSync(outputDir, { recursive: true }); const safeName = projectName .normalize('NFKC') .replace(/[^A-Za-z0-9_-]+/g, '_') .replace(/^_+|_+$/g, ''); if (!safeName) { throw new Error('Project name does not contain a valid filename'); } const outputFile = path.resolve(outputDir, `${safeName}_dfmea.json`); const relativePath = path.relative(outputDir, outputFile); if ( relativePath.startsWith(`..${path.sep}`) || relativePath === '..' || path.isAbsolute(relativePath) ) { throw new Error('Invalid output path'); } fs.writeFileSync(outputFile, JSON.stringify(template, null, 2), { encoding: 'utf8', flag: 'wx' });Add automated tests covering absolute paths, Windows and POSIX separators, nested traversal sequences, Unicode edge cases, empty sanitized names, and attempts to overwrite existing files.
