T09 · Insecure Skill Coding Practices
- Location
SKILL.md:99- Finding
Shell Command Injection Through Unsafely Generated guardian.conf
- Content
View full analysis
"$SKILL_DIR/guardian.conf" << GUARDIANCONF # Auto-generated by gateway-guardian installer. Do not upload to GitHub. # Fallback notification target (used when dynamic session detection fails) FALLBACK_CHANNEL={FALLBACK_CHANNEL} FALLBACK_TARGET={FALLBACK_TARGET} # Notification language: zh (Chinese) | en (English) LOCALE={LOCALE} # Bot display name used in staff/team notifications BOT_NAME={BOT_NAME} # Team group/channel notification (optional) # Leave empty to disable. Supported formats: # Feishu: oc_xxx # Telegram: -100xxxxxxxxxx (supergroup/channel numeric id) # Discord: 123456789012345678 (channel id, digits only) # Only effective if the channel is configured and running in OpenClaw. STAFF_GROUP_CHAT_ID= GUARDIANCONF ``` The generated file is subsequently loaded as executable shell code: ```bash _GUARDIAN_LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" [ -f "$_GUARDIAN_LIB_DIR/guardian.conf" ] && source "$_GUARDIAN_LIB_DIR/guardian.conf" ``` ### Technical Analysis The installer instructs the Agent to insert user-controlled `BOT_NAME` and conversation-derived notification values directly into an unquoted heredoc. An unquoted heredoc performs command substitution, backtick substitution, and parameter expansion while it is being processed. For example, if a value is inserted as: ```bash BOT_NAME=$(touch /tmp/gateway-guardian-injection) ``` the command substitution can execute immediate ...[truncated 1744 chars]- Remediation
View remediation
"$SKILL_DIR/guardian.conf" <<'GUARDIANCONF' ... GUARDIANCONF ``` 3. Pass values separately rather than replacing placeholders inside executable shell text. 4. If shell configuration is unavoidable, serialize each value with `printf '%q'` before writing it. 5. Enforce strict allowlists: - `FALLBACK_CHANNEL`: one of explicitly supported channel names. - `LOCALE`: exactly `zh` or `en`. - Notification IDs: channel-specific anchored regular expressions. - `BOT_NAME`: a conservative length and character allowlist. 6. Reject newlines, control characters, backticks, dollar signs, semicolons, shell redirection characters, and command-substitution syntax. 7. Create the file with restrictive permissions, such as mode `0600`. 8. Add tests using values containing `$(...)`, backticks, quotes, newlines, semicolons, backslashes, and redirection operators. ]]>
