T09 · Insecure Skill Coding Practices
Error
- Location
- references/advanced.md:40
- Finding
- Shell Command Injection Through Attacker-Controlled Filenames<![CDATA[ ## Vulnerability Details **File Location**: `references/advanced.md:40-41` **Vulnerability Type**: OS command injection **Risk Level**: High ### Vulnerable Code ```bash find . -name "*.jpg" -print0 | xargs -0 -P 4 -I FILE \ sh -c 'wrangler r2 object put "$R2_BUCKET/agent/$(date +%Y%m%d)/$(basename FILE)" --file FILE --remote' ``` ### Technical Analysis The command uses `xargs` to substitute each discovered filename directly into the command string evaluated by `sh -c`. Although null-delimited filenames are used between `find` and `xargs`, the filename is not safely passed as a positional argument to the shell. A filename containing shell metacharacters or single quotes can alter the command passed to `sh -c`. Because filenames are under the control of anyone able to create files in the upload directory, a crafted `.jpg` filename can break out of the intended command and inject arbitrary shell operations. The injected command executes with the same operating-system privileges and environment as the agent running this Skill. ### Attack Path 1. An attacker creates or causes the creation of a `.jpg` file with a filename containing shell syntax. 2. The user or agent invokes the documented concurrent-upload workflow in the directory containing that file. 3. `find` returns the crafted filename and `xargs` substitutes it directly into the `sh -c` script. 4. The shell interprets the substituted metacharacters as executable syntax. 5. The attacker's command executes with the privileges of the agent process. ### Impact Assessment Successful exploitation permits arbitrary command execution as the local account running the Skill. An attacker could read or modify accessible files, steal environment variables and Cloudflare credentials, upload sensitive data, tamper with R2 objects, or execute additional local and network operations. The scope is limited by the permissions of the executing account, but may include all resources available through that ac ...[truncated 36 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions Pass the filename as a positional argument instead of interpolating it into shell source: ```bash find . -name '*.jpg' -print0 | xargs -0 -P 4 -n 1 sh -c ' file=$1 wrangler r2 object put \ "$R2_BUCKET/agent/$(date +%Y%m%d)/$(basename "$file")" \ --file "$file" \ --remote ' sh ``` Additionally: - Quote every filename expansion. - Avoid `sh -c` where a direct command invocation can perform the operation. - Treat filenames as untrusted data, even when they originate from the local filesystem. - Test the workflow with filenames containing spaces, quotes, newlines, semicolons, and command-substitution characters. ]]>
