T09 · Insecure Skill Coding Practices
- Location
index.js:14- Finding
Path Traversal in Daily Memory File Reader
- Content
View full analysis
Vulnerability Details
File Location:
index.js:14-17, used byindex.js:64-69
Vulnerability Type: Path traversal / arbitrary Markdown file read
Risk Level: MediumVulnerable Code
js function dateFilePath(date, memoryDir) { const dir = memoryDir || DEFAULT_MEMORY_DIR; return path.join(dir, `${date}.md`); }The resulting path is used directly by the public
readfunction:js function read(date, memoryDir) { const d = date || todayStr(); const filePath = dateFilePath(d, memoryDir); if (!fs.existsSync(filePath)) return null; return fs.readFileSync(filePath, 'utf8'); }Technical Analysis
The documented
dateformat isYYYY-MM-DD, but the implementation does not validate that the supplied value conforms to this format. An attacker who can influence thedateargument can include../path traversal components.path.join()normalizes these components, allowing the resulting path to escape the intended memory directory.The implementation appends
.mdto the supplied value, so exploitation is generally limited to process-readable files whose names end in.md. Nevertheless, this can expose Markdown files outside the memory directory, including documentation, notes, configuration material, or other agent memory stored elsewhere.The optional
memoryDirargument further affects the traversal base. Although directory overriding is documented functionality, callers that expose this API to untrusted input must not treat it as a security boundary.Attack Path
-
An application or agent exposes
read(date, memoryDir)directly or indirectly to an untrusted user. -
The attacker supplies traversal components instead of a valid date, such as:
js read('../../other-directory/secrets', trustedMemoryDir); -
The function constructs:
text trustedMemoryDir/../../other-directory/secrets.md -
`path.jo ...[truncated 598 chars]
-
- Remediation
View remediation
Remediation Suggestions
Enforce the documented date format before constructing a path:
js function validateDate(date) { if (!/^\d{4}-\d{2}-\d{2}$/.test(date)) { throw new TypeError('Date must use YYYY-MM-DD format'); } const parsed = new Date(`${date}T00:00:00`); if ( Number.isNaN(parsed.getTime()) || todayParts(parsed) !== date ) { throw new RangeError('Invalid calendar date'); } }Independently enforce path containment using resolved paths:
js function dateFilePath(date, memoryDir) { validateDate(date); const base = path.resolve(memoryDir || DEFAULT_MEMORY_DIR); const target = path.resolve(base, `${date}.md`); if (path.dirname(target) !== base) { throw new Error('Resolved path escapes the memory directory'); } return target; }Additional hardening measures:
- Do not permit untrusted callers to choose
memoryDir. - Apply the same validation centrally so both
read()and any future date-based operations remain protected. - Consider rejecting symbolic links or verifying the canonical path with
fs.realpathSync()when the memory directory may be writable by another user. - Add tests for
../, absolute-path-like input, encoded traversal passed through upstream layers, invalid calendar dates, and legitimateYYYY-MM-DDvalues.
- Do not permit untrusted callers to choose
