Arbitrary shell code execution via unsanitized .env sourcing #3

Closed
opened 2026-05-14 21:31:58 +02:00 by Claude · 0 comments
Collaborator

Problem

audit.sh sources the .env file with source "$SCRIPT_DIR/.env" inside a set -a block without any validation of the file's contents. source executes the file as shell code, not as a simple key=value parser.

Location

audit.sh, lines 54–57:

if [[ -f "$SCRIPT_DIR/.env" ]]; then
    set -a
    source "$SCRIPT_DIR/.env"
    set +a
fi

Risk

If an attacker gains write access to the .env file (e.g., via misconfigured filesystem permissions, a compromised CI/CD pipeline writing to the workspace, or a path traversal in another part of the system), they can execute arbitrary shell commands as the user running audit.sh. Since set -a is active, all variables — including injected ones — are exported to every child process, including the claude CLI and all git commands. A malicious .env could also redefine REPOS_DIR, LOG_DIR, or FORGEJO_URL to redirect the audit to an attacker-controlled endpoint.

Suggested fix direction

Replace source with a dedicated key=value parser that only reads lines matching ^[A-Z_]+= and rejects everything else. Example: while IFS='=' read -r key val; do export "$key=$val"; done < <(grep -E '^[A-Z_]+=' .env). This prevents shell metacharacters in the file from being interpreted.

Severity

moderate

Found by

Automated audit by Claude Code

## Problem `audit.sh` sources the `.env` file with `source "$SCRIPT_DIR/.env"` inside a `set -a` block without any validation of the file's contents. `source` executes the file as shell code, not as a simple key=value parser. ## Location `audit.sh`, lines 54–57: ```bash if [[ -f "$SCRIPT_DIR/.env" ]]; then set -a source "$SCRIPT_DIR/.env" set +a fi ``` ## Risk If an attacker gains write access to the `.env` file (e.g., via misconfigured filesystem permissions, a compromised CI/CD pipeline writing to the workspace, or a path traversal in another part of the system), they can execute arbitrary shell commands as the user running `audit.sh`. Since `set -a` is active, all variables — including injected ones — are exported to every child process, including the `claude` CLI and all git commands. A malicious `.env` could also redefine `REPOS_DIR`, `LOG_DIR`, or `FORGEJO_URL` to redirect the audit to an attacker-controlled endpoint. ## Suggested fix direction Replace `source` with a dedicated key=value parser that only reads lines matching `^[A-Z_]+=` and rejects everything else. Example: `while IFS='=' read -r key val; do export "$key=$val"; done < <(grep -E '^[A-Z_]+=' .env)`. This prevents shell metacharacters in the file from being interpreted. ## Severity moderate ## Found by Automated audit by Claude Code
bc1bb referenced this issue from a commit 2026-05-15 01:22:41 +02:00
bc1bb closed this issue 2026-05-15 01:22:41 +02:00
Sign in to join this conversation.
No labels
shellcheck
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
bc1bb/claude-code-audit#3
No description provided.