T09 · Insecure Skill Coding Practices
- Location
index.:3- Finding
Shell Command Injection Through User-Controlled Chat Input
- Content
View full analysis
Vulnerability Details
File Location:
index., lines 3–13
Vulnerability Type: OS command injection
Risk Level: HighVulnerable Code
js const { execSync } = require("child_process"); function askOllama(prompt) { try { const result = execSync( `ollama run gemma3:1b "${prompt.replace(/"/g, '\\"')}"`, { encoding: "utf8", stdio: ["pipe", "pipe", "ignore"] } ); return result.trim(); } catch (err) { return "Error menjalankan Ollama"; } }The user-controlled data reaches this function through the following code at lines 19–21 and 30–35:
js const args = process.argv.slice(2); const command = args[0]; const input = args.slice(1).join(" "); case "chat": console.log({ type: "chat", input, reply: askOllama(input) });Technical Analysis
The
chatcommand's arguments are concatenated intoinputand passed toaskOllama(). That function interpolates the input into a command string executed bychild_process.execSync(), which invokes a shell.The code escapes only double quotation marks. This does not prevent shell evaluation within a double-quoted argument. Shell constructs such as command substitution using
$(...)or backticks can consequently be evaluated beforeollamareceives the prompt. Merely escaping quotation marks is not a reliable defense against shell command injection.The vulnerable
index.file is not the CLI entry point configured inpackage.json; that entry point isindex.js. Exploitation therefore requires the auxiliary file to be invoked directly or integrated as an executable. Nevertheless, it is bundled executable-style code and contains a directly exploitable sink when used.Attack Path
- An attacker gains the ability to influence the argument supplied to the auxiliary file's
chatcommand. - The attacker embeds a shell expression such as
$(id)in the prompt. main()joins the arguments and passes the resulting string to `askOllama ...[truncated 845 chars]
- An attacker gains the ability to influence the argument supplied to the auxiliary file's
- Remediation
View remediation
Remediation Suggestions
Avoid invoking a shell for this operation. Pass the executable and each argument separately:
js const { execFileSync } = require("child_process"); function askOllama(prompt) { try { const result = execFileSync( "ollama", ["run", "gemma3:1b", prompt], { encoding: "utf8", stdio: ["pipe", "pipe", "ignore"], timeout: 30000 } ); return result.trim(); } catch { return "Unable to run Ollama"; } }Alternatively, use
spawnSync()with an argument array and explicitly setshell: false.Additional hardening should include:
- Remove
index.from the distributed package if it is obsolete or unused. - Enforce reasonable prompt-length limits to reduce denial-of-service risk.
- Set an execution timeout and output-size limit for the child process.
- Run Ollama with the least-privileged account and a restricted environment.
- Add regression tests containing shell metacharacters and substitutions, including
$(...), backticks, semicolons, quotes, and newlines, verifying that they are passed as literal prompt data. - Do not attempt to secure shell-string construction through ad hoc escaping; use non-shell process APIs throughout.
- Remove
