T09 · Insecure Skill Coding Practices
- Location
index.js:21- Finding
Host Command Injection Through docker_exec Arguments
- Content
View full analysis
Vulnerability Details
File Location:
index.js:21-22
Vulnerability Type: OS command injection caused by unsafe shell interpolation
Risk Level: Criticaljs handler: async ({ command, workdir = '/workspace' }) => { try { const dockerCmd = `sudo docker exec -w ${workdir} astra-env bash -c "${command.replace(/"/g, '\\"')}"`; const { stdout, stderr } = await execAsync(dockerCmd); return { success: true, stdout: stdout || '(no output)', stderr }; } catch (error) { return { success: false, error: error.message }; } }Technical Analysis
The handler constructs a command string containing the user-controlled
commandandworkdirvalues and executes it usingchild_process.exec. This API invokes a host shell, so shell metacharacters and command substitutions are interpreted beforedocker execis launched.The
workdirvalue receives no validation or escaping. An attacker can inject shell separators or substitutions through it. The attempted protection forcommandonly escapes double quotes and does not prevent constructs such as$()or backticks. Becausecommandis placed inside host-shell double quotes, these substitutions can be evaluated by the host shell rather than exclusively inside the container.Attack Path
- The attacker obtains the ability to invoke the exposed
docker_exectool or influences its arguments. - The attacker places a shell payload in
workdir, such as a value containing a command separator, or places command substitution syntax such as$(HOST_COMMAND)incommand. - The handler concatenates the payload into
dockerCmd. execAsyncpasses the resulting string to the host shell.- The host shell evaluates the injected expression before or alongside
docker exec. - The injected command runs with the privileges of the skill process. The intended Docker operation also uses
sudo, increasing the potential impact where pas ...[truncated 628 chars]
- The attacker obtains the ability to invoke the exposed
- Remediation
View remediation
Remediation Suggestions
- Replace
execwithexecFileorspawnand pass each Docker argument as a separate array element, with shell processing disabled. - Do not construct a single shell command from user-controlled strings.
- Validate
workdiras a normalized absolute container path and restrict it to/workspaceor an explicitly approved subdirectory. - If arbitrary commands are a required feature, pass the command as an isolated argument to the container process without allowing the host shell to parse it.
- Remove
sudoand run the service with a narrowly scoped Docker authorization policy where possible. - Apply container hardening, including a non-root container user, dropped capabilities, read-only mounts where feasible, and resource limits.
- Add tests covering shell substitutions, backticks, separators, newlines, traversal, and malformed paths.
- Replace
