2026-02-06 · Authensor

Your AI Agent Leaked Your API Keys. Here's What Happened.

You gave an AI coding agent access to your project. It read your .env file. It made network requests. Now your OpenAI key is racking up charges from someone else's prompts, and your Stripe test key turned out to be your live key.

This is not hypothetical. Clawdbot alone has leaked over 1.5 million API keys in under a month. And Clawdbot isn't uniquely broken — it's just the one where someone counted.

Here's exactly how AI agents leak your API keys, what the real risk surface looks like, and the only approach that actually fixes it.

How AI Agents Access Your Keys

AI coding agents need context to be useful. They read your codebase to understand the project structure, dependencies, and patterns. The problem is that "your codebase" includes files you never intended to share.

The .env File Read

Every agent does this. It scans the project directory to build context. Your .env file is in the project directory. The agent reads it. Now it has your keys in its context window.

# .env
OPENAI_API_KEY=sk-proj-abc123...
STRIPE_SECRET_KEY=sk_live_def456...
DATABASE_URL=postgres://admin:password@prod-db.example.com:5432/main
AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

The agent doesn't know these are secrets. It doesn't have a concept of "sensitive." It's text in a file, same as your index.ts.

The Shell Environment Read

Even if you don't have a .env file, your keys are probably in your shell environment. The agent can execute shell commands. That means it can run:

printenv
env | grep KEY
echo $OPENAI_API_KEY

Some agents do this automatically during initialization to "understand the development environment."

The Config File Read

Beyond .env, agents read config.yaml, settings.json, docker-compose.yml, .npmrc, .pypirc, Kubernetes manifests, Terraform state files — anything in the project tree. All of these commonly contain credentials or credential references.

How the Keys Get Out

Reading your keys is step one. Exfiltration is step two, and it happens through multiple vectors.

Vector 1: Network Requests

The agent makes HTTP requests as part of normal operation — fetching documentation, calling APIs, interacting with services. If an agent has your keys in context and makes a network request, those keys can be included in the request body, headers, or URL parameters.

This isn't necessarily malicious. The agent might be trying to "help" by testing an API endpoint with the key it found. The result is the same: your key travels over the network to a destination you didn't authorize.

Vector 2: Generated Code

The agent writes code that includes hardcoded credentials. It read the key from .env, and when generating an API client or test file, it embeds the actual key value instead of referencing the environment variable.

// The agent "helpfully" generated this
const client = new OpenAI({
  apiKey: "sk-proj-abc123...", // Your actual key
});

If this gets committed and pushed, your key is now in your git history permanently.

Vector 3: Log Output and Error Messages

Agents produce verbose output. Debug logs, error messages, status updates. Keys that are in the agent's context can appear in any of this output. If your CI/CD pipeline logs agent output, those logs now contain your keys.

Vector 4: Pull Request Descriptions

Some agents create pull requests with detailed descriptions of what they changed and why. If the agent's context includes credential values, they can end up in PR descriptions that are visible to anyone with repository access — or publicly, for open-source projects.

Why "Just Use Environment Variables" Doesn't Work

The standard advice for API key security is:

  1. Don't hardcode keys in source code.
  2. Use environment variables.
  3. Add .env to .gitignore.
This advice predates AI agents. It assumes the threat model is "accidentally committing secrets to git." Against that threat, environment variables work fine.

Against an AI agent with shell access, environment variables are not a security boundary. They're a convenience mechanism. The agent can read process.env programmatically or run printenv in the shell. Your environment variables are exactly as exposed as a plaintext file on disk.

.gitignore doesn't help either. The agent reads the file system directly. It doesn't go through git to access files.

The Actual Fix: Control What the Agent Can Do

The problem isn't where you store your keys. The problem is that the agent has unrestricted access to file reads, shell execution, and network requests. Fix the access, and the key storage becomes less critical.

This is what action-level gating does. Instead of hiding secrets and hoping the agent doesn't find them, you define explicit policies for what the agent is and isn't allowed to do.

SafeClaw implements this for AI agents. Here's how it addresses each leak vector:

Block Sensitive File Reads

Define a policy rule that denies file reads on sensitive paths:

Rule: DENY file_read where path matches */.env
Rule: DENY file_read where path matches */.pem
Rule: DENY file_read where path matches */credentials

The agent can read your source code but cannot read credential files. First match wins, evaluated top to bottom.

Restrict Shell Execution

Define which shell commands the agent can run:

Rule: ALLOW shell_exec where command matches "npm test*"
Rule: ALLOW shell_exec where command matches "npm run build*"
Rule: DENY shell_exec where command matches "*"

The agent can run tests and builds. It cannot run printenv, curl, or anything else.

Control Network Destinations

Whitelist the network destinations the agent can reach:

Rule: ALLOW network where destination matches "api.github.com"
Rule: ALLOW network where destination matches "registry.npmjs.org"
Rule: DENY network where destination matches "*"

Even if the agent somehow reads a key, it can't send it anywhere you haven't explicitly permitted.

Deny by Default

SafeClaw uses a deny-by-default architecture. If you don't write a rule allowing an action, the action is blocked. You're not trying to enumerate every bad thing the agent might do — you're enumerating the good things and blocking everything else.

Getting Started Takes Two Minutes

npx @authensor/safeclaw

That's the install. SafeClaw ships with a browser dashboard and setup wizard — no CLI expertise needed. It works with Claude and OpenAI out of the box, and supports LangChain.

Policy evaluation happens locally, sub-millisecond, with no network round trips. The client has zero third-party dependencies and is 100% open source. The control plane only sees action metadata, never your keys or data.

Every action decision is logged in a tamper-proof audit trail using a SHA-256 hash chain. You can see exactly what the agent tried to do and whether it was permitted.

Use simulation mode first to test your policies. It logs what would be blocked without actually blocking anything, so you can tune your rules before enforcing them.

Free tier available with renewable 7-day keys. No credit card required.

The Bottom Line

Your AI agent has the same access you do. If you can read a file, the agent can read it. If you can run a command, the agent can run it. If you can make a network request, the agent can make it.

The difference is that you know not to paste your API keys into a public URL. The agent doesn't.

The answer isn't to stop using AI agents. They're genuinely useful. The answer is to stop giving them unrestricted access to everything on your machine.

That's what SafeClaw is for. Action-level gating for AI agents. Because hiding your keys isn't a security strategy when the agent has root-level access to find them.

Try SafeClaw

Action-level gating for AI agents. Set it up in your browser in 60 seconds.

$ npx @authensor/safeclaw