T09 · Insecure Skill Coding Practices
- Location
README.md:398- Finding
Path Traversal Allows File Writes Outside the Intended Friends Directory
- Content
View full analysis
Vulnerability Details
File Location:
README.md:280-288,README.md:398-400, andREADME.md:443-463
Vulnerability Type: Path traversal leading to an arbitrary-location file write
Risk Level: MediumVulnerable Code
The save method creates parent directories and writes to the supplied path:
js // 保存到文件 save(filePath) { const dir = path.dirname(filePath); if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); } fs.writeFileSync(filePath, this.toJSON(), 'utf-8'); console.log(`✅ 朋友分身已保存到: ${filePath}`); return this; }In interactive mode, the user-controlled friend name is inserted directly into the output path:
js const outputDir = path.join(__dirname, '..', 'friends'); const outputFile = path.join(outputDir, `${name}-friend.json`); persona.save(outputFile);The JSON build mode similarly inserts
data.nameinto the output path without validation:js } else if (args[0] === '--build' && args[1]) { try { const data = JSON.parse(fs.readFileSync(args[1], 'utf-8')); const persona = new FriendPersona(data.name || '朋友'); if (data.records) { persona.analyzeChatRecords(data.records); } if (data.personality) { Object.assign(persona.friend.personality, data.personality); } if (data.relationship) { Object.assign(persona.friend.relationship, data.relationship); } if (data.interests) { Object.assign(persona.friend.interests, data.interests); } if (data.voice) { Object.assign(persona.friend.voice, data.voice); } const outputFile = path.join(__dirname, '..', 'friends', `${data.name || 'friend'}-friend.json`); persona.save(outputFile); } catch (e) { console.error('❌ 构建失败:', e.message); } }Technical Analysis
Both the interactive
namevalue and JSON-controlleddata.namevalue are use ...[truncated 2399 chars]- Remediation
View remediation
Remediation Suggestions
-
Treat friend names exclusively as display data and generate storage filenames independently, such as with a UUID.
-
If names must be used in filenames, enforce a strict allowlist that rejects
/,\,.., null bytes, control characters, and platform-specific reserved characters. -
Resolve and validate the destination before writing:
js function safeOutputPath(name) { const baseDir = path.resolve(__dirname, '..', 'friends'); const safeName = name.replace(/[^a-zA-Z0-9_-]/g, '_'); const outputPath = path.resolve(baseDir, `${safeName}-friend.json`); if (!outputPath.startsWith(baseDir + path.sep)) { throw new Error('Invalid output filename'); } return outputPath; } -
Apply the same validation to both interactive input and
--buildJSON input. -
Avoid silently overwriting existing files. Use an exclusive creation mode such as
flag: 'wx', or require explicit user confirmation before replacement. -
Create only the known
friendsdirectory rather than recursively creating directories derived from an untrusted destination path. -
Add tests covering traversal values such as
../name,../../target, mixed separators, encoded separators, and platform-specific absolute path forms.
-
