Your AI Copilot Is Leaking Your Cloud Keys: Stop Pasting Secrets Into Prompts
作者:Seaworthy · 2026年9月6日
I keep seeing the same accident in code reviews: a developer pastes an AWS access key or a database password right into a config file, and then asks an AI assistant to "improve" the code. The assistant doesn't blink. It takes that secret, weaves it into a helper function, and produces a neat little constant like const DB_PW = 'P@ssw0rd123'. Congratulations, you've just handed your production credentials to every fork, log aggregator, and future intern who clones the repo.
AI coding tools are amazing at writing boilerplate, but they are also relentless pattern-matchers. If you show them a secret, they will treat it as a normal value and copy it into every related snippet they generate. They won't stop to say, "Hey, that looks like a private key, should we use an environment variable instead?" Because they're trained on code that often contains hardcoded secrets from public repos, yes, even the infamous ones that got scraped. So they think it's normal.
Let me be blunt: you are the last line of defense. The assistant doesn't know your cloud account is billable to your personal card. It doesn't know that a leaked GitHub token lets someone push malware to your production repo. It only knows text. You have to enforce the boundary.
Here's the dangerous pattern I see over and over in AI-assisted code:
# Generated by a copilot after you pasted your real credentials
import psycopg2
DB_NAME = "production"
DB_USER = "admin"
DB_PASSWORD = "Tr0ub4dor&3" # hardcoded - this leaks
DB_HOST = "db.example.com"
conn = psycopg2.connect(dbname=DB_NAME, user=DB_USER, password=DB_PASSWORD, host=DB_HOST)
That DB_PASSWORD line is a landmine. Every time you commit that file, the secret is in your git history forever, even if you delete it later, it lives in past commits. And AI assistants that read your project for context will happily reference that file again in future suggestions, keeping the secret alive in every new session.
The fix is simple and should be non-negotiable. Never write a secret in a code file. Use environment variables or a secret manager. Tell your AI assistant that explicitly in your prompt, or even in a AGENTS.md file that it respects. Here's the corrected version, which I'd rather see in any pull request:
import os
import psycopg2
# Load from environment - never commit actual values
DB_NAME = os.environ["DB_NAME"]
DB_USER = os.environ["DB_USER"]
DB_PASSWORD = os.environ["DB_PASSWORD"]
DB_HOST = os.environ.get("DB_HOST", "localhost")
conn = psycopg2.connect(dbname=DB_NAME, user=DB_USER, password=DB_PASSWORD, host=DB_HOST)
Now, the test is: can you paste that code into an AI chat with a clear conscience? Yes, because it contains no secret. You've unblocked the assistant from helping you with connection logic while keeping your credentials out of the codebase.
But I'm not naive. You're probably thinking, "I'll just put the default in a .env file and commit that, it's not public, right?" Wrong. .env files get committed all the time by mistake, and AI tools often index whole directories. If your .env is in the repo, you've re-created the same problem. Add .env to .gitignore. Use a local .env.local that is never committed.
There's a dangerous twist too: ask an assistant to review code for security issues, and it may miss a hardcoded secret entirely, or worse, suggest wrapping it in base64 for "safety." That's not security, that's performance art, and a determined attacker decodes it in seconds.
What should you do if you already committed hardcoded keys? Don't just delete them, rotate them. Kill the old key, generate a new one, and update your environment variables. Then scrub git history using tools like git filter-repo or BFG. But rotation is the emergency brake. If that credential ever touched a public repo or an AI service's training data, assume it's compromised. Yes, assume that. Because many AI chat logs are stored and reviewed for quality, and some tools share prompts with third parties. Your secret is no longer secret once it leaves your machine.
Another habit I've seen: developers paste temporary tokens into chat to ask for help with authentication flows. Stop doing that. Generate a dummy token with the same format (like sk-test-1234) to keep the logic intact, and never show the real value. Your AI assistant doesn't need the real token to write a retry loop or an API client.
I'm not anti-AI, I use it daily. But I treat it like a junior developer who has no sense of shame or secrecy. I never paste a credential into a prompt, and I review anything it writes for accidental secrets. I also add a pre-commit hook that scans for common patterns (like AKIA for AWS keys or password= in variables). That's a cheap physical boundary that works even when you're stressed.
Here's your new rule: if you wouldn't commit it, don't paste it. That simple test catches most leaks. When you ask for code examples, replace real values with placeholders. When you copy code from an AI result, scan it for lines that look like api_key = "..." and change them to environment variable reads.
Believe me, the cost of a secret leak is not a fun scramble to revoke keys and explain to your CTO why a spam bot is using your email service. It's far better to spend 30 seconds now writing a proper config loader.
If you're building a tool that helps teams check for these leaks automatically, you know the gap is real. Seaworthy builds developer tools that flag suspicious patterns in code and pull requests before they reach production, so you don't have to rely on your AI assistant's memory.
Stop treating secrets like comments. They are not metadata, they are live keys to your kingdom. Treat them with the same paranoia you'd give an admin password written on a sticky note under your keyboard. Because that's exactly what a hardcoded credential is: a sticky note left in plain sight for every passing bot, and every future AI model that trains on your code, to read.
本文由 AI 生成,总结了公开可用的来源。
Seaworthy 会扫描你的代码仓库,发现类似本文提到的各种问题。
安全漏洞、泄露的密钥和配置错误——在部署前捕获。免费运行,无需账户。