T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/memory-backup.sh:29
- Finding
- Shell Command Injection Through GIT_SSH_COMMAND<![CDATA[ ## Vulnerability Details **File Location**: `scripts/memory-backup.sh:29-40` **Vulnerability Type**: Shell command injection through environment-controlled command construction **Risk Level**: High ### Vulnerable Code ```bash # TODO: Replace with your SSH private key path MEMORY_BACKUP_KEY="${MEMORY_BACKUP_KEY:-~/.ssh/id_rsa}" # ---- SSH configuration (secure mode) ---- KNOWN_HOSTS="${HOME}/.ssh/known_hosts" export GIT_SSH_COMMAND="ssh -i ${MEMORY_BACKUP_KEY} -o IdentitiesOnly=yes -o UserKnownHostsFile=${KNOWN_HOSTS}" ``` ### Technical Analysis `MEMORY_BACKUP_KEY` and the path derived from `HOME` are interpolated into `GIT_SSH_COMMAND` without validation or shell-safe quoting. Git invokes the value of `GIT_SSH_COMMAND` through shell command parsing when it starts SSH. Consequently, a value containing shell syntax can alter the intended command. For example, if an attacker can control the backup process environment, a malicious `MEMORY_BACKUP_KEY` value containing a command separator can cause an additional command to execute when `git push` invokes SSH. Placing quotes inside the string is not a sufficient fix because embedded quotes and other shell constructs could still manipulate parsing. The underlying problem is treating untrusted data as part of an executable command string. ### Attack Path 1. An attacker gains the ability to set or influence `MEMORY_BACKUP_KEY` or `HOME` for the backup process, such as through an unsafe service configuration, CI variable, Agent-controlled environment, or wrapper script. 2. The attacker supplies shell metacharacters and a command in the affected value. 3. The user, Agent, or scheduled task runs `scripts/memory-backup.sh`. 4. The script exports the constructed `GIT_SSH_COMMAND`. 5. A staged change causes the script to execute `git push origin master`. 6. Git invokes the SSH command through shell parsing. 7. The injected command executes with the operating-system privileges of the backup process. ### Impac ...[truncated 449 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions - Do not construct an executable SSH command from unvalidated environment values. - Require `MEMORY_BACKUP_KEY` to be an explicitly configured absolute path. - Resolve the key and known-hosts paths canonically and verify that each points to an expected regular file. - Reject control characters, whitespace, quotes, command separators, substitutions, and option-like values. - Prefer a fixed SSH wrapper whose executable content is not derived from environment variables. Pass validated paths as positional parameters or through a tightly controlled configuration file. - Clear or strictly allowlist the environment when backups are started by an Agent, service, or scheduled task. - Run the backup under a dedicated unprivileged operating-system account. ]]>
