T09 · Insecure Skill Coding Practices
- Location
src/focus-timer.js:15- Finding
User-Controlled Arbitrary File Overwrite Through Unrestricted Data Path
- Content
View full analysis
Vulnerability Details
File Location:
src/focus-timer.js, lines 15 and 88–90
Vulnerability Type: Unrestricted file write path
Risk Level: MediumVulnerable Code
javascript this.dataFile = options.dataFile || './focus-data.json';javascript _save() { try { fs.writeFileSync(this.dataFile, JSON.stringify(this.sessions, null, 2)); } catch {} }Technical Analysis
The constructor accepts
options.dataFilewithout validating, normalizing, or restricting the supplied path. The_save()method subsequently passes that path directly tofs.writeFileSync(), which creates or replaces the selected file with serialized session data.If an untrusted caller can control the
FocusTimerconstructor options, it can provide an absolute path or a traversal-based relative path targeting any file writable by the Node.js process. Callingcomplete()invokes_save()and overwrites the target.The empty
catchblock suppresses all filesystem errors. This makes unintended writes and attempted exploitation difficult to detect and prevents the caller from responding safely to persistence failures.Attack Path
- An attacker gains influence over the
options.dataFilevalue passed toFocusTimer. - The attacker supplies the path of a sensitive file writable by the application process, for example:
javascript const timer = new FocusTimer({ dataFile: '/path/to/writable/application-config.json' }); - The attacker or application calls
timer.start()to create an active session. - The attacker or application calls
timer.complete(). complete()adds a session and invokes_save()._save()replaces the selected file with attacker-influenced session JSON.
Impact Assessment
Exploitation permits corruption or replacement of arbitrary files that are writable by the hosting Node.js process. Potential targets include application configurat ...[truncated 392 chars]
- An attacker gains influence over the
- Remediation
View remediation
Remediation Suggestions
- Store session data under a fixed, application-owned directory rather than accepting unrestricted paths.
- Resolve and normalize the requested path with
path.resolve(), then verify that it remains within the approved storage directory. - Reject absolute paths, traversal sequences, symlinks, directories, and unexpected filename extensions.
- Open files with restrictive permissions appropriate to the platform, such as mode
0o600for user-private data. - Use atomic persistence: write to a securely created temporary file in the same directory, flush it, and rename it over the destination.
- Do not suppress filesystem exceptions. Return or log sanitized errors so failed and suspicious writes can be detected.
- Run the hosting process with least-privilege filesystem permissions.
- If caller-selected storage is required, accept a logical storage identifier and map it to a server-controlled path rather than accepting a raw filesystem path.
