All guides

    Guide

    How do I protect AI provider API keys and tokens from being stolen?

    Updated

    Short answer

    Keep AI keys out of anything a coding agent or CI job can read. Make the keys you can't remove short-lived, scoped and revocable. Use a secrets manager, per-developer keys with spend caps, OIDC in CI and a default-deny egress list. Then check every command and network call a coding agent makes before it runs, because if an agent can read a key, it can send it out.

    Where AI keys actually get stolen#

    AI provider keys leak the same way other secrets do. They sit in a .env file, a shell profile, a CI variable or a settings file, and something with read access picks them up.

    The number of leaks is growing. CSO Online reports more than 1.27 million AI-related secrets exposed in 2025, up 81% year over year. AI services made up 12 of the 15 fastest-growing types of leaked secret.

    Coding agents add a new reader. The agent holds the developer's credentials, reads untrusted content and runs shell commands in the same session. It can reach anything the developer can reach.

    What the s1ngularity attack showed#

    In the Nx supply-chain attack, the malware went looking for AI tools on the machine. Wiz's write-up says it checked for popular AI CLIs (Claude, Gemini and Amazon Q), and that over 2,000 unique, verified secrets were leaked.

    For a security leader, the point is this: once a laptop has an agent installed, it holds a tool that can hunt for secrets when told to. The instructions can come from malware, or from injected content the agent reads during a task.

    Controls, and what each one stops#

    Keep keys out of the filesystem. Pull keys from a secrets manager at run time instead of leaving them in .env files and dotfiles. That takes away the easiest target. But any key the process needs still ends up in memory or in an environment variable the agent's shell can see.

    Give every developer their own key, with a spend cap. If the whole org shares one key, one theft burns everyone. With per-developer keys, you can revoke one person's access in a single step and trace misuse to a person. A spend cap limits the bill. It doesn't limit what data gets exposed.

    Use short-lived, scoped credentials in CI. GitHub's hardening guide says OpenID Connect can "create short-lived, well-scoped access tokens for your workflow runs." It also warns that any user with write access to a repository has read access to all secrets configured there. Cut GITHUB_TOKEN permissions to the minimum, and limit who can change workflows.

    Set the agent's read deny rules, then test them. Denying reads of .env files keeps them away from the agent's file tools. Check whether a shell command can still read the same file on your version. Text-matching rules and the shell don't always agree. See permission rules vs hooks vs sandboxing.

    Turn on the sandbox and restrict egress. Anthropic's sandbox docs say sandboxed commands have no direct route out. Connections go through a local proxy that checks each host against your allowed domains, and that list starts empty. The same docs say the default read access covers most of the machine, including ~/.ssh and ~/.aws/credentials, so tighten that read list yourself.

    Rotate on exposure, not on a schedule. If a key was ever in a repo, a CI log or a developer's settings file, treat it as stolen. Rotate it, then check the provider's usage logs for the whole time it was live.

    A decision rule#

    Ask 4 questions about each key:

    • Can an agent session read it without a human deciding to allow it?
    • If it leaks, can I revoke it within minutes, for just one person?
    • Is it limited by scope, spend or expiry?
    • Could a network call from the laptop or CI runner carry it out?

    A key that gets a yes on the first question and a no on the other three is your next incident.

    Checklist for this week#

    • Search repos, CI variables and agent settings files for AI provider keys.
    • Replace shared keys with per-developer keys and spend caps.
    • Move CI cloud access to OIDC and cut token permissions to the minimum.
    • Turn on the agent sandbox, keep the allowed-domain list short and tighten default reads.
    • Make sure someone can revoke a key and pull its usage log when asked.

    Where Harden fits#

    Deny rules and sandboxes limit what an agent can see and reach. They don't judge what the agent does with a key it's allowed to hold.

    Harden Agentic Integrity Foundation (AIF) runs on the developer laptop, outside the agent, at the agent's pre-execution hook. It checks every command, file edit and push against the session's intent before it runs. It parses the command and follows the data. If a secret reaches a network call, AIF treats that as a dangerous flow and acts on it.

    It judges secrets by what a file contains, not by its name. A .pem file holding a public certificate passes, but one holding a private key is stopped. AIF decides in milliseconds, on the machine, and nothing leaves the laptop. It starts in watch-only mode, so you can see what agents touch before you turn on enforcement. The full mechanism is in stopping coding agents leaking secrets.

    It's one layer of protection, not a perimeter. It doesn't replace scoped credentials, per-developer keys or egress policy. A leak split across several separate calls is still the hard case.

    For the CI side, see the wfx-loot campaign write-up.

    FAQ

    Related questions

    Is a .env file safe if it's in .gitignore?

    No. .gitignore keeps the file out of commits, but any process running as the developer can still read it, including a coding agent's shell. Use a secrets manager and scoped keys instead.

    Should every developer have their own AI API key?

    Yes. With per-developer keys you can revoke one person's access without breaking everyone else's, trace misuse to a person and set a spend cap for each user. With one shared key, a single theft means rotating the key for everyone.

    What should I do when an AI key leaks?

    Revoke and rotate it right away, then check the provider's usage logs for the whole time it was live. Find where it leaked from (a repo, a CI log or a settings file) so the next key doesn't end up there too.

    Does the Claude Code sandbox stop key theft?

    It helps. Per Anthropic's docs, sandboxed commands can only reach the domains you allow, and that list starts empty. Default read access still covers files like ~/.ssh and ~/.aws/credentials, so tighten what it can read. Pair it with scoped credentials and a pre-execution check.

    How do I protect keys used in CI?

    Use OIDC to get short-lived cloud tokens and set the GitHub token to the minimum permissions. Remember that anyone with write access to the repo can read its secrets, and limit who can change workflows.

    Sources

    1. s1ngularity's aftermath | Wiz Blog
    2. AI coding is fueling a secrets sprawl crisis few CISOs are containing | CSO Online
    3. Security hardening for GitHub Actions | GitHub Docs
    4. Sandboxing | Claude Code Docs

    Start free at harden.run

    Full Enterprise access, free for 60 days: unlimited developers, custom policies, centralized visibility, no commitment.