T09 · Insecure Skill Coding Practices
Note
- Location
- scripts/convert_image.sh:16
- Finding
- Unvalidated Quality Argument Allows SIPS Option Injection<![CDATA[ ## Vulnerability Details **File Location**: `scripts/convert_image.sh`, lines 16, 30, and 60 **Vulnerability Type**: Unvalidated argument expansion and option injection **Risk Level**: Low ### Vulnerable Code ```bash QUALITY="${3:-85}" ``` ```bash FORMAT_OPTS="-s formatOptions $QUALITY" ``` ```bash sips -s format "$FORMAT" $FORMAT_OPTS "$INPUT" --out "$OUTPUT" >/dev/null 2>&1 ``` ### Technical Analysis The optional `QUALITY` argument is accepted without validating that it is an integer in an appropriate range. It is embedded into the `FORMAT_OPTS` string and subsequently expanded without quotation marks. Bash performs word splitting on the unquoted `$FORMAT_OPTS` expansion. Consequently, a value containing spaces can introduce additional command-line arguments to `sips`. This is an argument or option injection weakness. This is not direct shell-command injection because shell metacharacters contained in the variable are not parsed again as shell syntax during ordinary parameter expansion. Nevertheless, injected arguments may modify how `sips` processes files or options. ### Attack Path 1. An attacker or untrusted caller supplies a crafted third argument containing whitespace and additional `sips` options. 2. The script stores the entire value in `QUALITY` without numeric validation. 3. `FORMAT_OPTS` incorporates the attacker-controlled value. 4. The unquoted `$FORMAT_OPTS` expansion is split into multiple arguments. 5. `sips` interprets the additional words as command-line options or option values. 6. Depending on supported `sips` arguments and filesystem permissions, processing can be altered, unintended files may be accessed or overwritten, or the conversion can be forced to fail. ### Impact Assessment Exploitation is limited to the permissions of the user running the script and requires control over the quality argument. It does not independently provide privilege escalation or arbitrary shell-command execution. The practical scope in ...[truncated 207 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions Validate the quality argument before invoking `sips`. Accept only an integer in an explicitly supported range, such as 0 through 100. Avoid constructing command arguments in a scalar string. Pass every argument separately or use a Bash array: ```bash QUALITY="${3:-85}" if [[ ! "$QUALITY" =~ ^[0-9]+$ ]] || (( QUALITY < 0 || QUALITY > 100 )); then echo "Error: Quality must be an integer between 0 and 100" >&2 exit 1 fi sips -s format "$FORMAT" \ -s formatOptions "$QUALITY" \ "$INPUT" --out "$OUTPUT" >/dev/null 2>&1 ``` If optional arguments become more complex, construct them with an array: ```bash SIPS_ARGS=(-s format "$FORMAT") if [[ "$FORMAT" == "jpeg" ]]; then SIPS_ARGS+=(-s formatOptions "$QUALITY") fi sips "${SIPS_ARGS[@]}" "$INPUT" --out "$OUTPUT" ``` ]]>
