Secret Leakage Risks When LLMs Execute Shell Commands
Running autonomous AI coding agents on local workstations or dev containers grants large language models execution capabilities over shell environments. When agents scan files, execute tests, or interact with external APIs, unredacted credentials in environment variables, .env files, or git logs risk being sent to remote model endpoints. Securing these environments requires strict sandboxing, secret masking, and restrictive tool permissions before granting an agent local execution rights.
An AI agent with arbitrary execution access is functionally equivalent to running unvetted curl-to-bash scripts in an infinite loop. Security controls must exist outside the model’s control boundary.
Checklist
- Isolate agent execution inside unprivileged containers: Running an agent directly on host hardware exposes standard environment variables, SSH agent sockets, and personal dotfiles. Wrap agent processes in ephemeral Docker or Podman containers running under non-root users. Map only the specific repository directory required for the immediate task, dropping capabilities like CAP_SYS_ADMIN and mounting host paths read-only whenever write privileges are unnecessary.
- Strip environment variables before launching agent processes: Subprocesses spawned by coding agents inherit host environment variables by default. Exported AWS keys, API tokens, and database connection strings sitting in shell memory can leak via prompt context or agent telemetry. Explicitly prune environment variables using
env -ior clean environment options in execution wrappers before initializing the agent binary. - Scrub stdout and stderr for secret strings: Agent tools capture command outputs to evaluate build and test status. If a command prints raw tokens or keys, that output enters the LLM context window. Implement regular-expression masking proxies or pre-execution hooks to scrub high-entropy strings, JWTs, and private key headers from stdout and stderr prior to model submission.
- Restrict network egress with localized firewall rules: Autonomous agents do not need unrestricted internet access if their core function is local file editing or offline unit testing. Use iptables, nftables, or container network namespaces to restrict egress traffic to approved API endpoints and local test services. Block direct outbound traffic to unknown IP blocks to prevent data exfiltration via rogue model instructions.
- Disable git credential helper inheritance: When agents execute git operations, standard credential helpers can automatically inject system HTTPS tokens or SSH keys without explicit prompt authorization. Configure repository-level git configs (
git config credential.helper "") to clear inherited credential helpers, ensuring the agent cannot pull or push to remote repositories without explicit user credential passing. - Block access to private key stores and sensitive file paths: Agents equipped with directory traversal tools can inspect standard paths like
~/.ssh,~/.aws, or/etc/shadow. Ensure file access policies or filesystem permissions strictly deny access to dotfiles and user configurations outside the project root. Apply mandatory access control rules (AppArmor or SELinux) to enforce path isolation at the kernel level. - Audit tool definition schemas and dynamic arguments: Custom agent tools that accept arbitrary shell strings invite command injection vulnerabilities. Avoid passing raw strings to shell execution functions inside tool definitions. Structure tool inputs with strict parameter schemas (using Pydantic or JSON Schema) to enforce typing and sanitize arguments before calling underlying system binaries.
- Enforce interactive confirmation on destructive system calls: Non-reversible operations like disk formatting, package removal, or force-pushing git branches should never execute silently. Configure agent runtimes to require explicit human approval for commands matching high-risk patterns. Set clear policy boundaries so low-risk reads execute automatically while state-altering system changes pause for confirmation.
- Inject mock credentials into local testing environments: Unit tests running inside agent sessions should never rely on production secrets or live staging keys. Provide synthetic mock credentials (such as dummy tokens or local KMS emulators like LocalStack) within the test harness. This isolates testing workflows and ensures that even if an agent logs test failures, no valid credentials are exposed.
- Enable structured audit logging for all agent tool invocations: Debugging security incidents requires an accurate record of what an agent executed, which files were touched, and what payload was transmitted. Maintain append-only, structured JSON audit logs recording timestamps, exact tool calls, sanitations applied, and process exit codes. Store these logs outside the container filesystem to prevent tampered trace deletion.
#!/usr/bin/env bash
# Wrapper script to launch AI agent with purged environment and network isolation
set -euo pipefail
TARGET_DIR="${1:-$(pwd)}"
exec docker run --rm -it \
--network isolated_agent_net \
--read-only \
--tmpfs /tmp:rw,noexec,nosuid \
--user 1000:1000 \
--volume "${TARGET_DIR}:/workspace:rw" \
--workdir /workspace \
--env PATH=/usr/local/bin:/usr/bin:/bin \
agent-runner-image:latest