This week on X, a post about leaked GitHub secrets was going around: 28.6 million API keys and passwords showed up in public repositories last year, and code written with AI leaks them about twice as often. The source is GitGuardian's State of Secrets Sprawl 2026: 28.65 million new hardcoded secrets in public GitHub commits in 2025, a 34% jump from 2024. Commits co-authored by Claude Code leaked a secret 3.2% of the time, against a 1.5% baseline for all public commits they scanned.
That is not only an app-backend problem. It shows up on marketing sites too.
The coding agent needs a contact form to email the owner, so it pastes a Resend key into src/contact.js. It adds an "AI gift ideas" box, so an OpenAI key goes in a browser script. It wires a "buy a print" button and uses the Stripe secret key because that is the snippet in the docs sidebar. Then it writes a .env for "best practice," runs git init && git add ., and pushes the tree to a public repo because that is how the preview deploy works. The homepage looks finished. The keys are in the zip.
It does not matter whether the site came from Cursor, Claude Code, Lovable, or an AI website builder. If a secret that can send mail, charge a card, or call a model is in the files you are about to deploy or push, this applies.
So I wrote the kind of site a coding agent produces for "a screen-print shop in Providence with hours, a contact form that emails the owner, a map, and an AI gift-idea helper." The HTML is prerendered on purpose. There is a real title, a real H1, hours, and an address — this is not an empty #root. Then I wrote a short scanner, secret_scan.py, that reads the local tree and reports leftover secrets. I ran it on the finished-looking repo, on the same repo after I only deleted .env, and on a version where the keys stay off the client and .gitignore actually excludes .env. All the numbers below come from those three runs.
The fixture
The business is fictional. It is called Harbor Line Prints, and it sells screen prints and short-run posters on Ballast Street in Providence. The "before" tree is a finished-looking marketing site: a title, an H1, hours, a contact form, and JSON-LD. The leftovers are a committed .env and three browser scripts.
Here is the contact form that actually got deployed, trimmed to the part that matters. The values are fake, labeled as a fixture, and will not authenticate anywhere.
// Contact form: emails the shop from the browser. // Fixture only — this value is fake and will not authenticate anywhere. const RESEND_API_KEY = "re_harborLinePrintsFixtureAAA111"; const TO = "owner@harborline.example"; export async function sendContact(form) { return fetch("https://api.resend.com/emails", { method: "POST", headers: { Authorization: "Bearer " + RESEND_API_KEY, "Content-Type": "application/json", }, body: JSON.stringify(/* ... */), }); } If you open the site, the form looks ordinary. If you open src/contact.js, src/gift.js, or src/checkout.js, the mail key, the model key, and the Stripe secret key are in the files the browser will download. .env has the same three values, and .gitignore only lists node_modules, dist, and .DS_Store.
The scanner
secret_scan.py reads a local directory: env files, HTML, and JS/TS/Vue/Svelte sources. It does not call Resend, Stripe, OpenAI, or GitHub. It does not validate a key, and it does not try to use one. Values in the output are redacted.
It reports:
- a
.env(or.env.local,.env.production, and similar) that assigns a secret-looking name or value, and is not ignored - a hardcoded assignment such as
RESEND_API_KEY,OPENAI_API_KEY,STRIPE_SECRET_KEY, or a service-role key in a client source file - known key prefixes in those files:
re_,sk-proj-,sk_live_,sk_test_,SG.,ghp_,AKIA, Slackxox* - a PEM private key block
- a
VITE_orNEXT_PUBLIC_prefix on a secret-named variable (that prefix puts the value in the browser bundle) - a missing
.gitignorerule for.env(warning if the file is not present; a fail if it is)
It does not flag a Stripe publishable key, and it does not treat re_your_api_key_here or an empty value in .env.example as a secret. It also does not scan node_modules, dist, or .git. A clean run is not a promise that git history is clean, or that a host-level scan would be. It is a check of the files you are about to ship.
#!/usr/bin/env python3 """secret_scan.py — local leftover-secret scanner for a marketing-site repo. Reads files already on disk. It does not call any vendor API, does not validate keys, does not fetch URLs, and does not try to use a value it finds. Printouts redact secret-looking values. Run it on a tree you own. """ from __future__ import annotations import argparse import os import re import sys import unittest from dataclasses import dataclass, field from pathlib import Path SKIP_DIRS = { ".git", "node_modules", "dist", "build", "out", "coverage", "target", ".next", ".cache", ".venv", "venv", "__pycache__", "vendor", } ENV_NAMES = { ".env", ".env.local", ".env.development", ".env.production", ".env.development.local", ".env.production.local", ".env.test", ".env.test.local", } ENV_EXAMPLE_NAMES = { ".env.example", ".env.sample", ".env.template", ".env.placeholder", } CLIENT_SUFFIXES = { ".js", ".jsx", ".ts", ".tsx", ".mjs", ".cjs", ".html", ".htm", ".vue", ".svelte", } # Names that should never appear as assigned values in a client bundle # or in a committed env file, unless the value is an obvious placeholder. SECRET_NAMES = { "RESEND_API_KEY", "SENDGRID_API_KEY", "MAILGUN_API_KEY", "POSTMARK_API_TOKEN", "POSTMARK_SERVER_TOKEN", "OPENAI_API_KEY", "ANTHROPIC_API_KEY", "STRIPE_SECRET_KEY", "STRIPE_SECRET", "AWS_SECRET_ACCESS_KEY", "AWS_SECRET_KEY", "GITHUB_TOKEN", "GH_TOKEN", "GH_PAT", "NPM_TOKEN", "DATABASE_URL", "POSTGRES_PASSWORD", "SUPABASE_SERVICE_ROLE_KEY", "SERVICE_ROLE_KEY", } PUBLIC_OK_NAMES = { "STRIPE_PUBLISHABLE_KEY", "NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY", "VITE_STRIPE_PUBLISHABLE_KEY", } # Prefixes that usually mean a live credential, not a documentation stub. PREFIX_RULES = ( ("openai-key", re.compile(r"\bsk-proj-[A-Za-z0-9_-]{12,}\b")), ("openai-key", re.compile(r"\bsk-svcacct-[A-Za-z0-9_-]{12,}\b")), ("openai-key", re.compile(r"\bsk-(?!test_|live_|proj-|svcacct-)[A-Za-z0-9_-]{20,}\b")), ("stripe-secret", re.compile(r"\bsk_live_[A-Za-z0-9]{8,}\b")), ("stripe-secret", re.compile(r"\bsk_test_[A-Za-z0-9]{8,}\b")), ("resend-key", re.compile(r"\bre_[A-Za-z0-9]{8,}\b")), ("sendgrid-key", re.compile(r"\bSG\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\b")), ("github-pat", re.compile(r"\bghp_[A-Za-z0-9]{20,}\b")), ("github-pat", re.compile(r"\bgithub_pat_[A-Za-z0-9_]{20,}\b")), ("aws-access-key-id", re.compile(r"\bAKIA[0-9A-Z]{16}\b")), ("slack-token", re.compile(r"\bxox[baprs]-[A-Za-z0-9-]{10,}\b")), ) PEM_RE = re.compile( r"-----BEGIN (?:RSA |OPENSSH |EC |DSA |ENCRYPTED )?PRIVATE KEY-----" ) ASSIGN_RE = re.compile( r"(?:(?:const|let|var|export\s+const)\s+)?(?P<name>[A-Z][A-Z0-9_]{2,})" r"\s*(?:=|:)\s*(?P<quote>['\"])(?P<value>[^'\"]{4,})(?P=quote)", re.MULTILINE, ) ENV_ASSIGN_RE = re.compile( r"^(?:export\s+)?(?P<name>[A-Z][A-Z0-9_]{2,})[ \t]*=[ \t]*(?P<value>[^\n]*)$", re.MULTILINE, ) PLACEHOLDER_RE = re.compile( r"(?i)^(your[_-]?|changeme|change_me|placeholder|example|xxx+|\*+|<.*>|" r"re_your|sk_test_your|pk_test_your|pk_live_your|insert[_-]?|todo|" r"replace[_-]?me|dummy|not_a_real|n/a)?$", ) PLACEHOLDER_SUB = re.compile( r"(?i)(your[_-]?api[_-]?key|your[_-]?key(?:[_-]?here)?|replace[_-]?me|" r"changeme|placeholder|example[_-]?key|xxx{3,}|<.*>|not_a_real_key_here|" r"sk_test_your|sk_live_your|re_your_|sk-proj-your|pk_test_your|pk_live_your)" ) def is_placeholder(value: str) -> bool: v = value.strip().strip("'\"").strip() if not v: return True if PLACEHOLDER_RE.match(v): return True if PLACEHOLDER_SUB.search(v) and len(v) < 80: return True # Common env-example shapes: re_xxxxxxxxxxxx, sk_test_xxxx if re.fullmatch(r"(re_|sk_test_|sk_live_|sk-proj-|pk_test_|pk_live_|ghp_)x{6,}", v): return True return False def redact(value: str) -> str: v = value.strip().strip("'\"") if len(v) <= 6: return "…" return v[:4] + "…" + v[-2:] def iter_files(root: Path): for dirpath, dirnames, filenames in os.walk(root): dirnames[:] = [d for d in dirnames if d not in SKIP_DIRS and not d.startswith(".git")] for name in filenames: yield Path(dirpath) / name def rel(root: Path, path: Path) -> str: try: return path.relative_to(root).as_posix() except ValueError: return str(path) def gitignore_excludes_env(root: Path) -> bool | None: gi = root / ".gitignore" if not gi.is_file(): return False text = gi.read_text(errors="replace") for raw in text.splitlines(): line = raw.strip() if not line or line.startswith("#"): continue if line in {".env", ".env.*", "*.env", "**/.env", "/.env"}: return True if line.startswith("!.env"): continue if line in {".env.local", ".env*.local"}: # partial; still look for a general .env rule continue # ".env*" covers .env and .env.local, but also .env.example — still a yes for raw in text.splitlines(): line = raw.strip() if line in {".env*", ".env.local", ".env*.local"}: return True return False @dataclass class Finding: code: str severity: str # "fail" or "warn" path: str detail: str @dataclass class Report: root: Path title: str = "" files: list[str] = field(default_factory=list) findings: list[Finding] = field(default_factory=list) env_files: list[str] = field(default_factory=list) gitignore_has_env: bool | None = None def add(self, code: str, severity: str, path: str, detail: str) -> None: self.findings.append(Finding(code, severity, path, detail)) @property def fails(self) -> list[Finding]: return [f for f in self.findings if f.severity == "fail"] @property def warns(self) -> list[Finding]: return [f for f in self.findings if f.severity == "warn"] def looks_like_secret_name(name: str) -> bool: n = name.upper() if n in PUBLIC_OK_NAMES: return False if n in SECRET_NAMES: return True if n.startswith("VITE_") or n.startswith("NEXT_PUBLIC_"): bare = n.split("_", 1)[1] if n.startswith("VITE_") else n[len("NEXT_PUBLIC_"):] if bare in SECRET_NAMES or bare.endswith("SECRET_KEY") or bare.endswith("API_KEY") and "PUBLISHABLE" not in bare: # public prefix on a secret-named variable is itself a finding return True return bare in SECRET_NAMES if n.endswith("SECRET_KEY") or n.endswith("_SECRET") or n.endswith("SERVICE_ROLE_KEY"): return True if n.endswith("API_KEY") and "PUBLISHABLE" not in n and "PUBLIC" not in n: return True return False def public_prefix_on_secret(name: str) -> bool: n = name.upper() if n.startswith("VITE_") or n.startswith("NEXT_PUBLIC_"): return looks_like_secret_name(n) return False def scan_text(report: Report, path: Path, text: str, *, in_env: bool, in_client: bool, example: bool) -> None: relpath = rel(report.root, path) if PEM_RE.search(text): report.add("private-key", "fail", relpath, "PEM private key block in " + relpath) assigns = [] if in_env or path.suffix in {".env"} or path.name in ENV_NAMES | ENV_EXAMPLE_NAMES: for m in ENV_ASSIGN_RE.finditer(text): assigns.append((m.group("name"), m.group("value").strip().strip("'\""))) for m in ASSIGN_RE.finditer(text): assigns.append((m.group("name"), m.group("value"))) seen = set() for name, value in assigns: key = (name, value, relpath) if key in seen: continue seen.add(key) if is_placeholder(value): continue if looks_like_secret_name(name): sev = "warn" if example else "fail" if public_prefix_on_secret(name): report.add( "public-prefix-secret", sev, relpath, f"{name} is a public-bundle prefix on a secret-looking name, value {redact(value)} in {relpath}", ) else: kind = "env-secret" if in_env else "hardcoded-secret" report.add( kind, sev, relpath, f"{name}={redact(value)} in {relpath}", ) # Prefix scan for values that were not caught by name (inline strings). # Skip obvious placeholders. for code, rx in PREFIX_RULES: for m in rx.finditer(text): val = m.group(0) if is_placeholder(val): continue # Avoid double-counting the same value already reported by name. if any(val in f.detail or redact(val) in f.detail for f in report.findings if f.path == relpath): continue # If this exact raw value was already an assign we reported, skip already = False for n, v in assigns: if v == val and looks_like_secret_name(n): already = True break if already: continue sev = "warn" if example else "fail" where = "client bundle" if in_client else ("env file" if in_env else "file") report.add( code, sev, relpath, f"{code} {redact(val)} in {where} {relpath}", ) def scan(root: Path) -> Report: root = root.resolve() report = Report(root=root) report.gitignore_has_env = gi...