T09 · Insecure Skill Coding Practices
- Location
scripts/set_key.sh:8- Finding
Arbitrary Shell Execution Through a Sourced Credential File
- Content
View full analysis
Vulnerability Details
File Location:
scripts/set_key.sh:8-15,scripts/generate.sh:91-94
Vulnerability Type: Executable credential configuration / command injection
Risk Level: HighVulnerable Code
scripts/set_key.sh:8-15:bash KEY="$1" CONF_DIR="$HOME/.config/z-image" CONF_FILE="$CONF_DIR/.env" mkdir -p "$CONF_DIR" cat > "$CONF_FILE" <<EOF X_API_KEY=$KEY EOF chmod 600 "$CONF_FILE"scripts/generate.sh:91-94:bash if [[ -z "${X_API_KEY:-}" && -f "$HOME/.config/z-image/.env" ]]; then set -a . "$HOME/.config/z-image/.env" set +a fiTechnical Analysis
set_key.shaccepts an arbitrary command-line string and writes it into a file formatted as shell source code.generate.shsubsequently loads that file using the shell.command.The key value is not validated or encoded before storage. A value containing a newline followed by shell commands, or a shell expression such as command substitution, can therefore become executable shell syntax when the file is sourced. File mode
600limits access by other local users but does not prevent execution of malicious content supplied by the current user, another invoking component, or a compromised configuration file.Persisting a key is relevant to the declared functionality, but interpreting the credential file as executable code exceeds the minimum privilege and behavior necessary to read an API credential.
Attack Path
- An attacker persuades a user or an automated caller to configure a crafted value as the API key.
- The attacker-controlled value includes a newline or other content that forms a valid shell command in
.env. scripts/set_key.shstores the content under~/.config/z-image/.env.- The user later invokes
scripts/generate.shwithout an existingX_API_KEYenvironment variable. generate.shsources the credential file.- The injected shell statemen ...[truncated 481 chars]
- Remediation
View remediation
Remediation Suggestions
-
Never source a file containing credential data.
-
Store only the raw key, without an assignment or other shell syntax, and read it as data:
bash IFS= read -r X_API_KEY < "$HOME/.config/z-image/api-key" export X_API_KEY -
Prefer an operating-system credential manager instead of a plaintext file.
-
If an environment-file format must be retained, parse exactly one expected assignment without evaluating it as shell code.
-
Validate the key against the provider's documented key syntax and reject newlines, carriage returns, null bytes, and unexpected characters.
-
Create the destination file with restrictive permissions from the outset rather than applying permissions only after writing.
-
