T09 · Insecure Skill Coding Practices
- Location
scripts/check-quota.sh:7- Finding
Command Execution Through Unvalidated Arithmetic Threshold
- Content
View full analysis
"$STATE_FILE" ``` ### Technical Analysis `THRESHOLD` can be supplied through either the `QUOTA_THRESHOLD` environment variable or the `--threshold` argument. The script does not verify that the value is a decimal integer within the expected percentage range. The value is subsequently used as an operand in Bash's arithmetic comparison: ```bash [[ $quota_pct -lt $THRESHOLD ]] ``` Values evaluated in a Bash arithmetic context are treated as arithmetic expressions rather than inert strings. Bash can recursively resolve variable references and evaluate array-subscript expressions. Crafted expressions containing an array reference whose subscript includes command substitution can consequently cause a command to run when the comparison is evaluated. The value is also concatenated into the `jq` filter instead of being passed as a data argument. Although this second use will commonly produce a `jq` syntax error for malformed input, dynamically constructing the filter unnecessarily expands the injection surface. ### Atta ...[truncated 1413 chars]- Remediation
View remediation
&2 exit 2 fi ``` Convert the validated value explicitly using a decimal base before comparison: ```bash THRESHOLD_NUM=$((10#$THRESHOLD)) if (( 10#$quota_pct < THRESHOLD_NUM )); then LOW_QUOTAS+=("${provider} ${quota_name}: ${quota_pct}% left") fi ``` Do not concatenate the threshold into the `jq` filter. Pass it as typed data: ```bash jq -n \ --argjson warned "$(printf '%s\n' "${LOW_QUOTAS[@]}" | jq -R . | jq -s .)" \ --argjson current "$(printf '%s\n' "${CURRENT_QUOTAS[@]}" | jq -R . | jq -s .)" \ --arg timestamp "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \ --argjson threshold "$THRESHOLD_NUM" \ '{ warned: $warned, current: $current, lastCheck: $timestamp, threshold: $threshold }' > "$STATE_FILE" ``` Scheduled invocations should additionally use a controlled environment and fixed arguments rather than inheriting configuration from an untrusted caller. ]]>
