T09 · Insecure Skill Coding Practices
- Location
scripts/humanize-de.js:1257- Finding
Predictable Output Path Allows Symbolic-Link File Overwrite
- Content
View full analysis
Vulnerability Details
File Location:
scripts/humanize-de.js:1257-1258
Vulnerability Type: Symbolic-link following and unsafe predictable output file
Risk Level: MediumComplete Code Snippet
js const outPath = path.join(dir, `${base}.fixed${ext}`); fs.writeFileSync(outPath, fixed, 'utf-8');Technical Analysis
The
fixcommand derives a predictable output path from the input filename and writes to it unconditionally. Node.jsfs.writeFileSync()follows an existing symbolic link, while this implementation does not inspect the destination withlstatSync(), reject symbolic links, request exclusive creation, or obtain confirmation before replacing an existing file.An attacker who can create files in the input directory can therefore prepare the expected output path as a symbolic link to another file writable by the user running the command. When the user invokes
fix, the linked target is truncated and replaced with the transformed document.The backup logic does not prevent this issue because the vulnerable write affects the separate
.fixeddestination.Attack Path
-
A victim has an input file named
document.mdin a shared or attacker-controlled directory. -
The attacker predicts that the command will write to
document.fixed.md. -
The attacker creates
document.fixed.mdas a symbolic link to a target file that the victim can modify. -
The victim runs:
bash node scripts/humanize-de.js fix document.md -
fs.writeFileSync()follows the symbolic link and truncates the linked target. -
The target is replaced with the transformed contents of
document.md.
Impact Assessment
Exploitation permits overwrite of any file writable by the invoking user and reachable through the prepared symbolic link. Potential consequences include data loss, corruption of user configuration, or modification of scripts and other files later consumed by trusted ap ...[truncated 348 chars]
-
- Remediation
View remediation
Remediation Suggestions
-
Refuse to use an existing output destination by opening it with exclusive creation:
js const fd = fs.openSync(outPath, 'wx', 0o600); try { fs.writeFileSync(fd, fixed, 'utf8'); } finally { fs.closeSync(fd); } -
Before writing, use
fs.lstatSync()when the path exists and explicitly reject symbolic links and non-regular files. Treat this as defense in depth rather than relying on a separate check alone, because check-then-write sequences can be subject to race conditions. -
Use no-follow filesystem semantics where the platform and Node.js version support them. Combine no-follow behavior with exclusive creation to prevent both symbolic-link traversal and unintended replacement.
-
Avoid predictable destinations in shared directories. Create output through a securely generated temporary filename in a trusted directory, then atomically rename it only after validating the final destination.
-
Require explicit user confirmation before replacing any existing
.fixedfile, or fail safely and ask the user to choose another path. -
Apply equivalent safeguards to the
.bakdestination so future changes cannot introduce the same symbolic-link issue in backup creation. -
Document that input and output operations should not be performed in untrusted writable directories until secure destination handling is implemented.
-
