T09 · Insecure Skill Coding Practices
Warning
- Location
- dist/core/memory.js:36
- Finding
- Path Traversal Through Unsanitized Memory and Synchronization Identifiers<![CDATA[ ## Vulnerability Details **File Location**: `dist/core/memory.js`, lines 36-64 and 269-275 **Vulnerability Type**: Path traversal leading to filesystem access outside the intended memory directory **Risk Level**: Medium ### Vulnerable Code ```javascript constructor(skillName = 'ai_system', baseDir = 'memory', config) { this.L0Variables = new Map(); this.L0Context = ''; this.L1Content = ''; this.L2Entries = []; this.L3Data = { worldviews: [], methodologies: [], capabilityModels: [], personalProfile: [] }; this.L4Data = { insights: [], coreValues: [], longTermPredictions: [], inheritableAssets: [] }; this.skillName = skillName; this.baseDir = baseDir; this.config = { L0_MAX_ITEMS: 10, L1_MAX_LINES: 50, L2_MAX_ENTRIES: 200, L3_MAX_ENTRIES: 1000, AUTO_ARCHIVE_THRESHOLD: 0.8, ...config }; this.initialize(); } initialize() { this.createDirectories(); this.loadMemories(); } createDirectories() { const dirs = ['L0_flash', 'L1_working', 'L2_experience', 'L3_knowledge', 'L4_wisdom', 'shared', 'logs']; for (const dir of dirs) { const fullPath = path.join(this.baseDir, this.skillName, dir); if (!fs.existsSync(fullPath)) { fs.mkdirSync(fullPath, { recursive: true }); } } } ``` Additional affected filesystem operations include: ```javascript syncToSystem(targetSystem, entries) { fs.writeFileSync( path.join( this.baseDir, this.skillName, 'shared', `${targetSystem}_sync.json` ), JSON.stringify({ timestamp: new Date().toISOString(), entries }, null, 2), 'utf-8' ); } syncFromSystem(sourceSystem) { const filePath = path.join( this.baseDir, this.skillName, 'shared', `${sourceSystem}_sync.json` ); if (fs.existsSync(filePath)) return JSON.parse(fs.readF ...[truncated 2901 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. **Validate all identifiers at runtime** Restrict `skillName`, `targetSystem`, and `sourceSystem` to simple identifiers: ```javascript function validateIdentifier(value, fieldName) { if (typeof value !== 'string' || !/^[A-Za-z0-9_-]+$/.test(value)) { throw new Error(`${fieldName} contains invalid characters`); } return value; } ``` 2. **Establish a fixed trusted storage root** Resolve the configured memory root once and do not permit untrusted callers to supply arbitrary base directories: ```javascript this.baseDir = path.resolve(TRUSTED_MEMORY_ROOT); this.skillName = validateIdentifier(skillName, 'skillName'); ``` 3. **Enforce path containment** Resolve every destination and verify that it remains under the trusted root: ```javascript function resolveWithinRoot(root, ...segments) { const normalizedRoot = path.resolve(root); const candidate = path.resolve(normalizedRoot, ...segments); const relative = path.relative(normalizedRoot, candidate); if ( relative === '..' || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative) ) { throw new Error('Resolved path escapes the storage root'); } return candidate; } ``` 4. **Apply containment checks to every filesystem sink** Use the safe resolver for directory creation, L1 through L4 loading and saving, health checks, and synchronization operations. Do not rely on validation at only one call site. 5. **Reject absolute paths and traversal components** Explicitly reject identifiers containing path separators, `.` or `..` path components, null bytes, and platform-specific alternate separators. 6. **Use tenant isolation and least privilege** Run the service under a dedicated account with access only to its memory root. For multi-tenant deployments, use separate roots or operating-system i ...[truncated 341 chars]
