T09 · Insecure Skill Coding Practices
- Location
bin/elite-memory.js:262- Finding
Arbitrary Command Execution Through Unsanitized Restore Filename
- Content
View full analysis
Vulnerability Details
File Location:
bin/elite-memory.js, lines 262-284
Vulnerability Type: OS command injection
Risk Level: HighVulnerable code:
js restore: () => { const file = process.argv[3]; if (!file) { console.log('Usage: npx liu-longterm-memory restore <backup-file>'); console.log('\nExample:'); console.log(' npx liu-longterm-memory restore memory-backup-20260404.zip'); console.log(' npx liu-longterm-memory restore memory-backup-20260404.tar.gz'); return; } if (!fs.existsSync(file)) { console.log(`✗ File not found: ${file}`); return; } console.log(`🧠 Restoring memory from ${file}...\n`); try { if (file.endsWith('.tar.gz') || file.endsWith('.tgz')) { execSync(`tar -xzf "${file}"`, { stdio: 'pipe' }); } else if (file.endsWith('.zip')) { execSync(`unzip -o "${file}"`, { stdio: 'pipe' }); }Technical Analysis
The restore filename comes directly from
process.argv[3]and is interpolated into a command executed throughexecSync. Because string-formexecSyncinvokes a shell, the shell interprets metacharacters and command substitutions embedded in the filename.Enclosing the filename in double quotes does not neutralize shell constructs such as
$(command)or backtick substitution. Thefs.existsSync(file)check only verifies that a file with the supplied literal name exists; it does not make subsequent shell interpolation safe.Attack Path
- An attacker supplies, uploads, or causes the creation of an archive whose filename contains a shell substitution while retaining a supported suffix.
- The victim invokes
elite-memory restorewith that filename. fs.existsSyncconfirms that the specially named file exists.- The filename is interpolated into the
tarorunzipshell command. - The shell evaluates the embedded substitution before start ...[truncated 502 chars]
- Remediation
View remediation
Remediation Suggestions
- Replace string-form
execSyncwithexecFileSyncorspawnSyncand pass arguments as an array with shell processing disabled:js const { execFileSync } = require('child_process'); if (file.endsWith('.tar.gz') || file.endsWith('.tgz')) { execFileSync('tar', ['-xzf', file], { stdio: 'pipe' }); } else if (file.endsWith('.zip')) { execFileSync('unzip', ['-o', file], { stdio: 'pipe' }); } - Resolve the filename to an absolute path and validate that it refers to a regular file.
- Do not attempt to solve this solely through shell escaping; avoiding a shell is the safer design.
- Add regression tests using filenames containing spaces, quotes, dollar signs, backticks, semicolons, and command-substitution syntax.
- Replace string-form
