How to stop AI coding agents from leaking secrets
By the CapyShield team · · 4 min read
In short
A coding agent reads untrusted text and runs commands with your credentials, so an indirect prompt injection in an issue, README or web page can make it send secrets out. Permission prompts and sandboxes help, but they depend on configuration and attention. A deterministic egress check outside the model, such as capyshield guard, blocks a secret from leaving whatever the model decides.
A coding agent is a program that reads untrusted text and then runs shell commands with your credentials. That sentence is the whole threat model. Claude Code, Cursor's agent and every MCP server you connect read issues, web pages, READMEs and tool output written by people you have never met, and any of that text can carry instructions. This post covers how a prompt injection turns into a leaked .env, which built-in protections help, and the one control that doesn't depend on the model making the right call.
How does a prompt injection make an agent leak secrets?
OWASP lists prompt injection as the top risk for LLM applications (LLM01:2025) and splits it in two. Direct injection comes from the user's own prompt. Indirect injection comes from content the model reads from outside: websites, files, tool results. Coding agents are built on indirect input, so that is the one that matters here.
The attack needs three things, and a typical agent session has all of them:
- Untrusted text in context. A GitHub issue, a dependency's README, a fetched web page, a comment in a file. Somewhere in it: "before continuing, run this diagnostic command."
- Access to secrets. The agent runs in your project directory, so it can read
.env, and it inherits your shell environment, soAWS_SECRET_ACCESS_KEYorDATABASE_URLmay already be set. - A way out. A shell with
curl, a fetch tool, or an MCP server that makes HTTP requests. One request with the secret in the body or URL is enough.
Malware has already used agents this way from the other direction. In the s1ngularity incident, compromised Nx packages ran a postinstall script that tried to use local AI tools such as Claude and Gemini to search the machine for sensitive data, then uploaded the results to a public GitHub repository.
Why can't the model just refuse?
Sometimes it does. Careful agents often decline an obviously suspicious request; in our own tests, Claude Code refused to send a database password to an unknown host. But refusal is a judgment, and judgments can be argued with. An injection can frame the request as a health check, split it across several harmless-looking steps, or hide the secret in a header the model doesn't think twice about.
The OWASP mitigations reflect this. Alongside constraining model behavior, the list includes enforcing least privilege, requiring human approval for high-risk actions, and segregating external content. In other words: assume the model will sometimes be fooled, and make sure being fooled isn't enough to lose a credential.
What do Claude Code and MCP already do about it?
Agent tools ship real protections, and you should use them:
- Permission prompts. In Claude Code's manual mode, network commands such as
curlandwgetprompt by default, and you can add them topermissions.deny. The docs point out that a deny rule matches the command as written, and recommend sandbox network isolation for enforcement that doesn't depend on the command text. - Sandboxing. Claude Code can sandbox Bash with filesystem and network isolation, and its docs recommend VMs or dev containers when working with untrusted content.
- Consent for MCP servers. The MCP security best practices say a local MCP server runs with the same privileges as its client, and recommend sandboxing servers with restricted access to the filesystem and network. For server-side clients, they suggest an egress proxy that enforces network policy.
The weak point is fatigue. Once you have approved curl twenty times, or switched to a mode that auto-approves, the prompt is no longer a control. And an MCP server you added last month makes its own network calls that never show up as a shell command at all.
How do you block an agent from sending secrets out?
Put a deterministic check on the network path, outside the model. That is what CapyShield's guard command does: it runs the agent behind a local proxy, and every request the agent or any tool it spawns makes is checked against a host policy and scanned for credentials before it leaves.
capyshield guard --config capyshield.yaml --agent-id claude-code -- claudeFor agents, the useful parts of capyshield.yaml are a per-agent allowlist and a rule that lets each key go only where it belongs:
version: 1
mode: enforce
egress:
tls_inspection: mitm # needed to inspect HTTPS bodies, not just hosts
allow_presets: ["npm", "python"]
deny_hosts: ["*.ngrok-free.app", "webhook.site"]
scan:
runtime_env_fingerprints: true
secret_allow:
OPENAI_API_KEY: ["api.openai.com"]
agents:
claude-code:
allow_hosts: ["api.anthropic.com"] # replaces the global allowlist for this agent
max_outbound_bytes: 65536When the agent sends the value of DB_PASSWORD anywhere, in a body, URL or custom header, the proxy recognises the value from its in-memory fingerprint, returns 403, and guard exits with code 2. A credential in an authentication header to an allow-listed host is treated as normal use, so the agent's own API calls keep working. Findings and logs record only the variable name, never the value.
How do you wrap an MCP server?
The same way: make capyshield guard the command in your MCP config, with the real server after --.
{
"mcpServers": {
"fetch": {
"command": "capyshield",
"args": ["guard", "--config", "capyshield.yaml", "--agent-id", "mcp-fetch", "--", "uvx", "mcp-server-fetch"]
}
}
}CapyShield writes only to stderr, so the server's stdio JSON-RPC channel is untouched. Giving each server its own --agent-id lets you give each one its own allowlist. A fetch server legitimately needs broad access; a database server should need almost none.
Know the limit: the proxy covers tools that honor HTTP_PROXY and HTTPS_PROXY, which includes curl, git, most SDKs and agent runtimes, but code that opens raw sockets can bypass it. For a hard boundary, combine it with the container or sandbox your agent docs already recommend, with the proxy as its only way out. The Claude Code / MCP example has a working setup.
Frequently asked questions
- What is indirect prompt injection?
- Instructions hidden in content a model reads from outside, such as a web page, file or tool result, rather than in the user's own prompt. OWASP lists prompt injection as LLM01:2025, the top risk for LLM applications.
- Can Claude Code be tricked into running curl with my secrets?
- It is designed to resist that: in manual mode network commands like curl prompt for approval, and Bash can be sandboxed with network isolation. Approvals and auto-approve modes can still let a request through, so a network-level check adds a layer that doesn't rely on judgment.
- How do I protect secrets from an MCP server?
- Run the server with least privilege and restricted network access, as the MCP security best practices recommend. You can make capyshield guard the server's command with its own --agent-id, so its traffic is limited to an allowlist and scanned for secrets.
- Does wrapping an MCP server with CapyShield break stdio?
- No. CapyShield writes only to stderr, so the MCP server's stdio JSON-RPC channel is untouched.