Using proxies to hide secrets from Claude Code
Recorded: Jan. 19, 2026, 10:03 a.m.
| Original | Summarized |
Using Proxies to Hide Secrets from Claude Code - FormalBlogJan 13, 2026Using Proxies to Hide Secrets from Claude CodeDrew GregorySoftware EngineerTable of contentsSandboxing agentic coding tools is a networking problemWhat is the worst a sandbox can do?Unpacking the devcontainer firewallUsing network proxies to prevent secrets exfiltration: hiding the ANTHROPIC_API_KEY from Claude CodeTying a developer’s permissions to their Claude Code permissions: proxying Claude Code to the Formal ConnectorWhen hostnames and headers are hard to edit: mitmproy add-onsApplying fine-grained least privilege policiesSandboxing agentic coding tools is a networking problem Sandboxes help us reason about their relation to the lethal trifecta: What untrusted content is the sandbox exposed to? Claude Code’s Sandbox Bash tool, which uses sandbox-exec under the hood for OS X users. This is the same technique Chromium uses What is the worst a sandbox can do? What network access am I allowing Claude Code to have? For example, almost all Claude Code instances have access to Anthropic API keys to be able to interact with the Anthropic API. What data am I providing to Claude Code? Unpacking the devcontainer firewall registry.npmjs.org: allow installing npm packages Use this firewall Connect to a random IP over port 22 Using network proxies to prevent secrets exfiltration: hiding the ANTHROPIC_API_KEY from Claude Code A HTTP_PROXY environment variable that applies to all Claude Code HTTP traffic. This proxy environment variable intercepts HTTP traffic that the parent Claude Code process makes. mitmproxy is a great tool to run these HTTP proxies. Note: provide the mitmproxy-generated TLS certificate to intercept TLS traffic to Claude for node processes via export NODE_EXTRA_CA_CERTS=~/mitmproxy/mitmproxy-ca-cert.pem. You could then run mitmweb with the right API key: Afterwards, run claude with the ANTHROPIC_API_KEY environment variable set to an invalid API key: From the perspective of Claude Code, all API responses from api.anthropic.com will appear to Claude Code as if the API key of sk-ant-dummy is a valid API key! Tying a developer’s permissions to their Claude Code permissions: proxying Claude Code to the Formal Connector When hostnames and headers are hard to edit: mitmproy add-ons You can then pass this add-on via mitmproxy -s reroute_hosts.py. Applying fine-grained least privilege policies If we change the path param to “/v1/messages,” we can confirm that this policy is able block requests to the Anthropic API even outside of the Claude Code sandbox: We also get visibility into every request being made to the Anthropic API across our organization! Of course, this technique was not specific to safeguarding Anthropic API Keys: one can use HTTP proxies and Claude Code sandboxes to enforce least privilege for your API Keys even if you want to enable Claude Code to use some of these APIs! Proxies can limit the ability to externally communicate by allowing or blocking traffic. Speak to an EngineerLearn the platform in less than an hour. Secure your data stack in less than a day. |
The article explores the challenges of securing agentic coding tools like Claude Code by addressing the critical issue of secret exfiltration through network proxies. It begins by framing sandboxing as a networking problem, emphasizing three core concerns: the exposure of untrusted content to the sandbox, its external communication capabilities, and the sensitive data it accesses. The authors reference Simon Willison’s work on this topic, highlighting that while sandboxes aim to isolate processes, they often remain vulnerable to data leaks. For example, Claude Code instances typically have access to environment variables and files in the directory from which they are run, including secrets like API keys. This creates a risk if developers inadvertently expose sensitive data through configuration files or environment variables, such as `.env` files that may not be properly excluded from version control. The article then delves into the specific tools provided by Anthropic for sandboxing, such as the Sandbox Bash tool and devcontainers template. These tools apply firewalls that restrict network access to predefined hosts, including `api.anthropic.com` and GitHub servers. However, the firewall operates at the IP layer, using `iptables` to allow connections based on resolved IP addresses. This approach has limitations: for instance, a load balancer on an allowlisted IP could route traffic to unintended hosts via SNI or Host headers, bypassing the firewall’s intent. Additionally, the firewall permits SSH traffic on port 22, which could be exploited by malicious code to exfiltrate data. The authors caution that even with these protections, a compromised devcontainer running `claude –dangerouslyUnsafePermissions` could still leak secrets through methods like creating malicious npm packages or GitHub gists. To mitigate these risks, the article advocates for using network proxies to intercept and control Claude Code’s traffic. It outlines two primary methods: configuring the `HTTP_PROXY` environment variable for general HTTP traffic and setting a sandbox-specific `httpProxyPort` to intercept bash command traffic. The authors recommend tools like mitmproxy, which can act as a transparent proxy to monitor and manipulate HTTP/HTTPS requests. By deploying mitmproxy with custom add-ons, developers can rewrite headers (e.g., replacing the `X-API-Key` field) to inject real API keys only after requests leave the sandbox. This technique allows developers to pass dummy API keys to Claude Code, ensuring that the actual credentials are never exposed within the sandboxed environment. However, they note a caveat: Claude Code requires OAuth authentication before validating the `ANTHROPIC_API_KEY`, so users must first sign in, retrieve the key, and then reset it to an invalid value before running the tool. The article also discusses extending this approach to enforce least privilege policies at the application layer. By integrating formal identity management systems like Formal, organizations can decouple human and machine identities, ensuring that even if a developer’s credentials are compromised, the scope of potential damage is limited. For instance, Formal Connectors can act as intermediaries between Claude Code and external APIs, injecting real secrets only when necessary. This approach prevents direct exposure of API keys to the sandbox while maintaining accountability through logging. The authors emphasize that this strategy aligns with broader organizational security practices, where fine-grained access controls are applied to both human and automated workflows. A specific challenge addressed is the difficulty of modifying hostnames and headers in certain scenarios, such as when dealing with third-party APIs. The article suggests using mitmproxy add-ons to reroute HTTP requests dynamically, bypassing the need for manual configuration of hostnames and ports. For example, an add-on script can intercept traffic to `api.anthropic.com` and redirect it to a local proxy, ensuring that Claude Code remains unaware of the redirection. This method preserves the appearance of normal network behavior while enabling centralized monitoring and policy enforcement. The authors also highlight that such proxies can be used to apply granular access controls, blocking requests based on specific paths (e.g., `/v1/messages`) or headers, thereby preventing unauthorized API calls. The discussion extends to the broader implications of proxies in addressing the “lethal trifecta” of security: external communication, data access, and privilege escalation. By limiting network traffic to predefined endpoints and injecting secrets only when necessary, proxies reduce the risk of data leaks. Additionally, they restrict agents from accessing sensitive information not required for their tasks, such as environment variables or source code. The article underscores that this dual-layer approach—combining technical safeguards (proxies, firewalls) with organizational policies (least privilege)—is critical for securing agentic tools. Finally, the authors conclude by emphasizing that while sandboxes and proxies are powerful tools, they must be complemented by robust internal practices. This includes auditing environment variables, ensuring `.env` files are excluded from version control, and training developers to recognize the risks of exposing secrets. The article positions these strategies as essential for maintaining security in an era where agentic tools increasingly interact with sensitive systems and data. By combining technical solutions like mitmproxy with organizational frameworks such as Formal, developers can create a more secure ecosystem for using tools like Claude Code. |