T09 · Insecure Skill Coding Practices
- Location
index.js:163- Finding
Path Traversal Allows Unauthorized JSON File Reading, Modification, and Deletion
- Content
View full analysis
Vulnerability Details
File Location:
index.js:163-169,index.js:178-181,index.js:469-482, andindex.js:508-523
Vulnerability Type: Path traversal through an unvalidated story identifier
Risk Level: High
Classification: T09: Insecure Skill Coding PracticesVulnerable Code
The story identifier is inserted directly into a filesystem path when reading a story:
js function getStory(storyId) { const storyFile = path.join(STORIES_DIR, `${storyId}.json`); if (!fs.existsSync(storyFile)) { return null; } try { return JSON.parse(fs.readFileSync(storyFile, 'utf-8')); } catch (e) { return null; } }A story object's
idis similarly trusted when saving it:js function saveStory(story) { const storyFile = path.join(STORIES_DIR, `${story.id}.json`); story.updatedAt = Date.now(); fs.writeFileSync(storyFile, JSON.stringify(story, null, 2)); }The
switchcommand accepts the identifier directly from command-line input and passes it togetStory:js case 'switch': case '切换': { const storyId = args[1]; if (!storyId) { console.log('❌ Please provide a story ID'); console.log('Usage: node index.js switch <story ID>'); return; } const story = getStory(storyId); if (!story) { console.log('❌ Story does not exist'); return; } setCurrentStoryId(storyId); console.log(`\n✅ Switched to story: ${story.title}`); console.log(`📖 ID: ${storyId}`); break; }The
deletecommand also constructs a path directly from the untrusted identifier:js case 'delete': case '删除': { const storyId = args[1]; if (!storyId) { console.log('❌ 请提供故事ID'); console.log('用法: node index.js delete <故事ID>'); return; } const storyFile = path.join(STORIES_DIR, `${storyId}.json`); if (!fs.existsSync(storyFile)) { console.log('❌ 故 ...[truncated 4006 chars]- Remediation
View remediation
Remediation Suggestions
-
Strictly validate story identifiers
Require the canonical generated format before performing any filesystem operation:
js function validateStoryId(storyId) { if (typeof storyId !== 'string' || !/^STORY-[A-Z0-9]{4}$/.test(storyId)) { throw new Error('Invalid story ID'); } return storyId; } -
Resolve and contain every story path
Use
path.resolve()and verify that the final path remains inside the intended directory:js function getStoryPath(storyId) { validateStoryId(storyId); const base = path.resolve(STORIES_DIR); const candidate = path.resolve(base, `${storyId}.json`); if (!candidate.startsWith(`${base}${path.sep}`)) { throw new Error('Story path escapes the story directory'); } return candidate; }Use this centralized function for all story reads, writes, switches, and deletions.
-
Reject dangerous path syntax explicitly
Reject absolute paths,
.., path separators, null bytes, and platform-specific separators before path construction. Strict allowlist validation should remain the primary control. -
Do not trust identifiers loaded from JSON
Before calling
saveStory(story), validatestory.id. Prefer passing the already validated active story ID separately rather than deriving the destination path from file contents. -
Validate persisted state
Validate
currentStoryIdafter readingcurrent.json. If it is malformed, reject it and clear the active-story state rather than passing it to filesystem functions. -
Apply least privilege
Avoid a hard-coded root-owned workspace and do not run the application as root. Store data in a configurable, user-owned directory with restrictive permissions.
-
Use safer deletion controls
Resolve and validate the target before deletion, verify that it is a regula ...[truncated 335 chars]
-
