T09 · Insecure Skill Coding Practices
- Location
scripts/write.mjs:153- Finding
OS Command Injection Through the Article Topic Argument
- Content
View full analysis
Vulnerability Details
File Location:
scripts/write.mjs, lines 153-166
Vulnerability Type: OS command injection caused by unsafe shell command construction
Risk Level: Highjs async function searchHotTopics(topic) { if (!TAVILY_API_KEY) return null; const skillDir = path.dirname(path.dirname(new URL(import.meta.url).pathname)); const tavilyScript = path.join(process.env.HOME, '.openclaw', 'skills', 'tavily-search', 'scripts', 'search.mjs'); try { const cmd = `node "${tavilyScript}" "${topic} 热点 趋势 2024" -n 3 --topic news`; const result = execSync(cmd, { encoding: 'utf-8', env: { ...process.env, TAVILY_API_KEY } }); return result; ``` ### Technical Analysis The `topic` value originates from the user-controlled `--topic` or `-t` command-line argument. It is interpolated directly into a command string passed to `execSync()`. Node.js executes a string supplied to `execSync()` through a shell by default. Placing the value inside double quotes does not make it safe: shell command substitution constructs such as `$(...)` and backticks remain active within double-quoted strings, while embedded quotes can alter the intended command structure. The vulnerable path is reached when `TAVILY_API_KEY` is configured. No validation or shell escaping is applied before the topic is inserted into the command. Moreover, the spawned shell inherits the complete process environment through `{ ...process.env, TAVILY_API_KEY }`, potentially exposing credentials and other sensitive environment variables to an injected command. ### Attack Path 1. The application is run in its documented configuration with `TAVILY_API_KEY` set. 2. An attacker supplies a crafted value through `--topic`, including shell syntax such as command substitution. 3. `generateArticle()` passes the attacker-controlled topic to `searchHotTopics()`. 4. `searchHotTopics()` interpolates the topic in ...[truncated 1220 chars]- Remediation
View remediation
Remediation Suggestions
Eliminate shell interpretation by replacing
execSync()with an API that accepts the executable and arguments separately, such asexecFileSync()orspawnSync()withshell: false.js import { execFileSync } from 'child_process'; const result = execFileSync( process.execPath, [ tavilyScript, `${topic} 热点 趋势 2024`, '-n', '3', '--topic', 'news' ], { encoding: 'utf-8', shell: false, env: { PATH: process.env.PATH, HOME: process.env.HOME, TAVILY_API_KEY } } );Additional hardening should include:
- Validate that
topicis a string and enforce a reasonable maximum length. - Do not attempt to solve the issue solely through shell escaping; avoiding the shell is more reliable.
- Pass only environment variables required by the child process instead of copying all of
process.env. - Resolve and validate
tavilyScript, confirming that it points to the expected regular file before execution. - Apply a child-process timeout and output-size limit to reduce denial-of-service risks.
- Add regression tests using topics containing quotes, backticks, dollar signs, semicolons, newlines, and command-substitution syntax, verifying that they are passed as literal argument content.
- Validate that
