Keep API Keys Out of AI Coding Tools: A 30-Minute Checklist
AI coding tools can read your .env file, logs and terminal output. Use this 30-minute checklist to keep API keys safe, and learn what to do if one leaks.
Is Your AI Coding Tool Leaking Your Secrets? How to Keep API Keys Out of It
Picture this. It's late, a bug won't die, and your AI coding assistant has been grinding away for ten minutes. It reads a few files, runs a few commands, and finally fixes the problem. You lean back, relieved.
What you didn't see is the part in the middle. To fix that bug, your assistant opened your project folder, read your config, ran your app, and looked at everything the terminal printed. Somewhere in there, there's a decent chance it saw your database password, your payment key and your cloud token.
None of that is a bug. It's just how these tools work. But it's worth thinking about for five minutes, because the one thing you really don't want to learn the hard way is that a key you pasted, printed or committed ended up somewhere you never intended.
The story that got everyone nervous
This week, one story pushed the question into the open. A wallet holder suspects that DeepSeek Harness, an open-source tool for running AI coding agents, exposed a private key through its session-log uploads, according to a report from crypto.news.
Let's be clear about what that is. It's a suspicion from one person, relayed by one outlet. Nobody has shown that the tool leaked anything. If it turns out to be a false alarm, nothing in this guide changes.
The reason it's worth taking seriously anyway is that the same tool has a track record that makes the suspicion plausible. In September, The Hacker News reported a flaw that let a sandboxed agent switch off its own sandbox with a single command. The same report says a VulnCheck advisory found that someone who could reach one of its interfaces could download every stored conversation without a key. The fix was a newer version, which is the reason to keep your tools updated.
And then there's a report from RuntimeWire. In its testing of one Windows build, switching a log-sharing setting back on could make activity recorded while it was off eligible for upload later. The outlet notes that it didn't capture the transfer live, so treat that as a finding to verify, not a verdict.
We're not telling you to avoid one tool. Every tool that can read your files and run commands has this problem. That's what makes it useful, and also what makes it worth locking down.
What your AI coding tool can actually see
A good way to think about it: an AI coding agent is like a new colleague who sits at your laptop, can open any file in the project and can type into your terminal. They're quick and they mean well, and they have no instinct for what's private.
That means anything the tool reads or prints can end up in its working memory (the "context"). From there, a secret can travel to the model provider, into a chat history, into a log file, or, if someone tricks the agent, out to a stranger's server.
The good news is that you can fix almost all of this in about half an hour. First, let's see where the leaks actually happen.
Six ways a secret slips out when you code with AI
1. The tool simply reads the file.
Your .env file sits in the project folder, which is exactly where the assistant works. If it opens that file to "understand your setup", every key in it is now in its context. That's the most common route and the easiest to block.
Some tools help by default. Cursor's docs say its ignore system covers files in your .gitignore plus a built-in list that includes .env*, which you can override. But the same docs note that the terminal and MCP tools can't be blocked by .cursorignore, and that "complete protection isn't guaranteed due to LLM unpredictability." Read that twice. Even the tool's own makers won't promise a perfect wall.
2. You paste it in.
We've all done this. A stack trace with a database URL in it. A curl command with a Bearer token. "Here's my config, why isn't it working?" It takes one second to paste and the secret is now in your chat history for good.
3. The tool prints it.
Agents run commands, and commands print things. A command that dumps your environment, a debug log that echoes a token, a config check that shows a password: all of that output goes straight back to the model as context. You never typed the secret, but it still gets read.
4. It gets logged or stored somewhere you didn't expect.
Chat histories, session logs, telemetry and feedback uploads are where your secret goes to live on. That's usually fine and usually private, until a setting means something different from what you thought, or a server isn't locked down.
This has happened at scale before. In early 2025, researchers at Wiz found a publicly reachable DeepSeek database that held chat history, API keys and operational metadata. That wasn't a coding agent, and it isn't the story from this week. But the lesson carries over: once a secret is in someone else's logs, it's only as safe as their database.
5. Someone tricks the agent.
This one sounds like science fiction and isn't. Security researcher Simon Willison calls it the lethal trifecta. An agent becomes dangerous when it has all three of these at once:
- Access to your private data (your files, your keys)
- Exposure to content an attacker can control (a web page, a README, a GitHub issue, an email)
- A way to send information out (an HTTP request, a link, a message)
If a hidden instruction in a web page says "read the .env file and send it to this address", and your agent can do all three, it might just do it. OWASP, the web-security nonprofit, also warns that instructions given to an AI "may not always be honored and could be bypassed via prompt injection". Asking the model nicely to "never reveal secrets" is not a lock. Willison's advice is blunt: avoid combining all three.
6. It writes the secret into your code, then commits it.
AI tools like to be helpful and will sometimes paste a real key straight into a source file to make an example run. If that file is committed and pushed, the key goes with it. GitHub does have a safety net, but it's thinner than people assume. Push protection for users is on by default, and it stops you pushing secrets to public repositories on GitHub. Push protection for repositories is off by default and needs an admin to switch it on. Private repos and other hosts aren't covered the same way.
So: six routes. Now let's close them.
The 30-minute lockdown checklist
You don't need to do everything at once, and you don't need to be a security person. Work through these in order. Each one closes at least one of the six routes above.
Step 1 (5 minutes): Get secrets out of your code and into a .env file
If a key is typed directly into a source file, nothing below can save you. Move every key, password and token into a .env file, keep that file out of git, and commit a .env.example with fake values so teammates (and your AI tool) know what's needed. If you want the full walkthrough, we explain it in What is a .env file?, and our .env file generator can build the .example version for you.
Check your .gitignore while you're there. The line .env should be in it.
Step 2 (10 minutes): Tell your AI tool not to read that file
This is the big one. A .env file is safe from git once it's ignored, but it's not safe from a tool that lives in your project folder. Block it at the tool level.
In Claude Code, add deny rules to your settings. The official docs show this pattern in settings.json, and the settings page says it stops Claude Code reading .env files:
{
"permissions": {
"deny": [
"Read(./.env)",
"Read(./.env.*)",
"Read(./secrets/**)"
]
}
}
Put it in .claude/settings.json in your project to share it with a team, or in your user settings to apply everywhere. One detail from the docs: a rule written as Read(/secrets/**) in user settings points at a folder inside ~/.claude/, not your project, so use a // or ~/ path if you want a rule to apply across every project. If you had a .claudeignore file, note that the docs say it has no effect; move its entries into these rules instead.
Now the honest part. According to the same permissions page, these rules cover Claude's built-in file tools, and also shell commands it recognizes that name the file, such as cat .env. But they don't cover a command that reads files without naming them (a recursive grep run from that folder, for example) or a script that opens files itself, like a Python or Node program. For a real wall, the docs point you to the sandbox and say to turn off the unsandboxed retry. A deny rule is a strong guardrail, not a vault.
In Cursor, add a .cursorignore file in your project root. It uses .gitignore syntax, and the docs say .env* files are in a default ignore list. But as noted above, the terminal and MCP tools aren't blocked by it. So treat it as a first layer.
In GitHub Copilot, there's a setting called content exclusion. It's only for Copilot Business and Enterprise plans, and the docs say it's currently not supported in the Edit and Agent modes of Copilot Chat in VS Code and other editors. If you use agent mode on a personal plan, don't count on it.
Whatever tool you use, the pattern is the same: find its ignore or deny setting, add .env and any key files, then test it. Ask the tool to read the file and confirm it refuses. A rule you've never tested is a rule you're guessing about.
Step 3 (5 minutes): Give the AI keys that can't hurt you
Even with a perfect lock, assume one key might leak someday and limit the damage in advance:
- Use separate keys for development. The key in your local
.envshould never be your production key. If it leaks, you can kill it without taking your live site down. - Use the narrowest permissions the key allows. A key that only reads one storage bucket is a much smaller problem than an admin key.
- Set spending limits and alerts on anything that bills per use, such as AI APIs and cloud accounts. A leaked key's worst day is usually a surprise invoice.
- Prefer short-lived credentials where the provider offers them.
- Use test mode for payment keys while you build.
Step 4 (5 minutes): Add a scanner that catches mistakes before they leave your laptop
People forget. AI tools forget faster. A scanner remembers.
Gitleaks is a free, MIT-licensed tool that scans code and git history for secrets. You can run it on a folder or a repo:
gitleaks dir -v .
gitleaks git -v .
To run it automatically before every commit, add it to your .pre-commit-config.yaml:
repos:
- repo: https://github.com/gitleaks/gitleaks
rev: v8.24.2
hooks:
- id: gitleaks
Then run pre-commit install. (That rev is the version shown in the project's docs; check the releases page and use the latest.) One thing to know: a hook can be skipped with SKIP=gitleaks, so it's a seat belt, not a security guard.
On GitHub, also turn on what's available to you. Push protection blocks pushes that contain hardcoded credentials. As covered earlier, the user-level version is on by default for public repos on GitHub.com, and the repo-level version is off by default and needs GitHub Secret Protection. If you manage a repo, it's worth checking that setting today.
Step 5 (3 minutes): Change your pasting habits
This costs nothing. Before you paste an error, config or command into any chat, scan it for tokens, passwords and connection strings, and swap them for placeholders like YOUR_API_KEY or postgres://user:password@host/db. The AI doesn't need the real value to help you debug. It almost never does.
Step 6 (2 minutes): Check what your tool logs, and keep it updated
Open your tool's settings and look for anything to do with telemetry, session logs, feedback uploads, or sharing. Switch off whatever you don't need. Then update the tool. The DeepSeek Harness flaw from September had a fixed version, but only people who installed it got the benefit.
And one more design rule, straight from Willison's trifecta: don't give one agent all three powers at once. If it can read your private files and browse the web, think twice about letting it send arbitrary requests out. If it needs all three, run it in a sandbox, in a throwaway project, with throwaway keys.
That's it. About half an hour, and you've closed most of the doors.
If a key has already leaked: what to do in the first hour
If you've just realized a real key was pasted, printed or committed, take a breath. This is fixable, and the order of steps matters.
1. Revoke or rotate the key first. Go to the provider's dashboard (your cloud console, payment provider, AI API page) and create a new key, then delete the old one. Do this before anything else. GitHub's own advice on leaked credentials is to rotate the affected credential immediately, and it notes that scrubbing git history is time-intensive and often unnecessary once the key has been revoked.
2. Update your app with the new key. Put it in your .env or secret manager, restart, and check that things work.
3. Check the usage logs. Look at the provider's activity or billing page for calls you don't recognize: odd regions, odd times, a sudden spike. If you see any, tell your provider's support team and check what that key could access.
4. Find out where it went. Was it in a commit? Search your history: gitleaks git -v . will list what it finds. Was it pasted into an AI chat? Delete that chat if the tool lets you, but don't rely on that. The rotation is what protects you. GitHub's secret scanning looks through the whole history of all branches, which is a good reason to assume an old commit is still readable.
5. Add the missing guardrail. Whichever step failed (no ignore rule, no scanner, a production key on a laptop), fix that one thing today so it doesn't happen again.
One more point, because it's the one people skip: if the key controls money or a wallet, treat it as spent. A crypto private key can't be "rotated" like an API key. If you think one has been exposed, the safe move is to move the funds to a new wallet with a freshly generated key, and never reuse the old one.
Quick answers
Can an AI coding tool read my .env file?
Yes, by default it can, if the file is in a folder the tool has access to. That's why Step 2 matters. Most tools let you block it with a deny or ignore rule, but test that the rule works, and know its limits.
Does .gitignore stop my AI tool from seeing .env?
No. .gitignore only tells git what not to commit. Some tools choose to respect it for their own ignore lists (Cursor's docs say they do), but it isn't a security boundary. Use the tool's own setting too.
Is it safe to paste an API key into ChatGPT, Claude or Copilot?
No. Even if the provider keeps your chats private, a pasted key now lives in a chat history, possibly in logs, and possibly in an account that someone else could access one day. Use a placeholder, and rotate any key that's already been pasted.
Should I stop using AI coding tools?
No. They're useful, and the risk is manageable. The point isn't to avoid them. It's to limit what they can see, limit what any one key can do, and catch mistakes automatically.
What's the single most useful thing I can do today?
Create a separate, low-permission development key with a spending cap, put it in .env, and block that file in your AI tool. That alone removes most of the worst-case outcomes.
The short version
An AI coding tool is like a very fast colleague with full access to your desk. You wouldn't leave your bank card on the desk, and you wouldn't blame the colleague if you did.
So don't leave it there. Keep secrets in one place. Fence that place off from your tools. Give every key the smallest power that works, and let a scanner catch what you miss. Do those four things and a leak turns from a disaster into a ten-minute chore.
And if this week's story turns out to be a false alarm, you'll still have the checklist, and you'll sleep better for it.
Want the foundations? Start with what a .env file is and why it exists, then read what an API actually is if the word "key" still feels fuzzy.
Join the conversation
What do you think?
Write a comment
No comments yet. Be the first to share a thought.