How to secure AI coding agents
Short version: treat anything the agent reads as possibly hostile, keep it in a mode that asks before shell and network actions, deny reads of secret files, allowlist MCP tools one by one, and commit before every agent run. Each setting below links to the vendor's doc, checked Nov 4, 2026. We read the docs; we did not test these settings hands-on.
Last updated
Why it matters
OWASP's Top 10 for LLM applications lists prompt injection first (LLM01). Indirect prompt injection happens when a model accepts input from external sources such as websites or files, and that content changes the model's behavior in unintended ways [1]. A coding agent reads exactly that kind of content all day: READMEs, issues, docs pages, dependency code and MCP tool results. OWASP's mitigations include least privilege ("restrict the model's access privileges to the minimum necessary") and human approval for high-risk actions [1]. The checklist below applies both.
New Nov 9, 2026: secrets in Claude Code's settings.local.json
Security firm Apiiro reported this month that it found more than 100,000 copies of Claude Code's .claude/settings.local.json in public GitHub repositories, and that about 1,100 of them, from 63 organizations, contained secrets such as API tokens, database passwords or private keys [9]. Those are Apiiro's figures as reported by Ars Technica; we did not repeat the scan.
Updated Nov 11, 2026: GitGuardian has published a larger count, and the gap between the two numbers is easy to explain [11]. Apiiro looked at what is exposed in current files today, while GitGuardian counted every secret ever pushed to public GitHub from January 2025 to November 7, 2026. By that measure it found 31,088 distinct secrets in settings.local.json across 23,276 repositories, and 5,343 of them still worked when it checked. It also found 613 secrets, 99 of them valid, inside public Docker Hub images. Those images were built with COPY . ., which copies the working folder rather than what git tracks, so a file that git ignores can still ship in a container.
The mechanism is in Claude Code's own docs. When you choose "Yes, and don't ask again" for a Bash command, Claude Code saves an allow rule to .claude/settings.local.json at the repository root [2]. Sometimes that rule is a pattern like npm test *, but when it is one exact command, any password typed into that command is saved with it. Claude Code adds **/.claude/settings.local.json to your global git excludes the first time it writes the file [10]. That only covers your own git setup on one machine, so it does not help a clone on another computer or a container, a folder that becomes a repository later, or a file that is already tracked [9].
What to do today:
- Search your repositories, including git history, for
.claude/settings.local.json. If a copy with a password or token was ever pushed, rotate that credential first. Deleting the file does not remove it from history or from forks. - Stop tracking the file with
git rm --cached .claude/settings.local.jsonand add.claude/settings.local.jsonto the repository's own.gitignore, so the rule travels with the project. - Open the file and delete any allow rule that contains a literal secret. Approve patterns instead of one-off commands that carry passwords, and keep credentials in environment variables.
- If you build Docker images from the project folder, add
.claude/to.dockerignoreas well, because.gitignoredoes not stopCOPY . .from packing the file into the image [11].
New Nov 12, 2026: Copilot for JetBrains security fix (CVE-2026-83953)
Microsoft published CVE-2026-83953 on November 10, 2026, for the GitHub Copilot plugin in JetBrains IDEs [12]. It is a command injection flaw, and Microsoft says an attacker who already has local access to the machine could use it to read files they should not see. Microsoft rates it Important, with a CVSS score of 5.5, and lists it as neither publicly disclosed nor exploited when it was published. The NVD record shows plugin versions from 1.0.0 up to, but not including, 1.17.0-251 as affected [13].
If you use Copilot in IntelliJ, PyCharm or another JetBrains IDE, open the plugin settings and make sure Copilot is on 1.17.0-251 or later. Microsoft marks this one as needing customer action, so don't assume it updated itself. VS Code and other editors are not named in either record.
New Nov 19, 2026: API keys shipped in your frontend code
One story about this mistake spread widely this week. On November 18, the X account @fromcodetocloud shared screenshots of a Reddit post in which a non-developer says a friend found their OpenAI key, their live Stripe secret key and their Resend key in the JavaScript files of a booking app they had built with Cursor and Claude [14]. By the evening of November 19, that X post showed about 2.5 million views and nearly 15,000 bookmarks. The author writes that the keys carried Vite's VITE_ prefix so the chat feature could call OpenAI from the browser, and that the server logs showed 212 requests for /.env in the two weeks the app had been online. We could not check the original Reddit post, so read it as one person's account rather than a verified incident.
The mechanism behind it, however, is written down in the framework docs. Vite says variables prefixed with VITE_ "will be exposed in client-side source code after Vite bundling" and should not contain sensitive information such as API keys [15]. Next.js does the same thing with NEXT_PUBLIC_, whose values are inlined "into any JavaScript sent to the browser" [16]. A gitignored .env file does not protect you here, because the build copies the value straight into the files every visitor downloads. OpenAI tells developers never to deploy a key in browsers or mobile apps and to route requests through their own backend [17], while Google's Gemini docs put it in four words: "Never expose keys client-side in production" [18].
How often does this happen? Three small studies published this month give a rough picture, although none of them is a census of the web. ShipSniffer says it confirmed live secret keys on 157 public sites between October 21 and November 13, 2026; 154 of them were OpenAI keys, 3 were Anthropic keys, and 135 of the sites were hosted on GitHub Pages, where a static site has no server to keep a key on [19]. Keybrum read the public JavaScript of 2,802 apps built with Lovable and found only four real secrets, three of which were AI keys called straight from the browser [20]. A DEV Community post that checked 573 apps shared on Hacker News found three that served a secret key to every visitor, or about one app in 200 [21]. These are each author's own numbers, and we did not repeat their scans. Read together, they suggest the leak is uncommon, yet it keeps turning up wherever an app calls an AI provider directly from the page.
What to do today, in this order:
- Build the app, then search the output folder (for example
dist,buildor.next/static) for live key prefixes. One way isgrep -rnoE '(sk-proj-|sk-ant-|sk_live_|rk_live_)[0-9A-Za-z_-]{20,}' dist, with your own folder name in place ofdist. Older OpenAI keys start with a plainsk-, so if you used one of those, search for its first few characters as well. Our free quick scan below reads your source files rather than the built bundle, and it flags only variable names that look secret, so a name likeVITE_OPENAI_API_KEYcan pass it. Run this check as well. - If a secret key turns up, rotate it in the provider's dashboard before anything else. Removing it from the code does not help if someone already copied it, and OpenAI's own advice for a leaked key is to rotate it immediately [17].
- Set a monthly spend limit with hard enforcement wherever the provider offers one. OpenAI points out that spend alerts on their own don't stop API traffic [17].
- Move every call that needs a secret key into a server route, a serverless function or an edge function, so the browser only ever talks to your backend. Stripe's publishable key (
pk_live_) is meant to sit in client code, but Stripe treats secret and restricted keys as server-side keys and recommends restricted keys to limit the damage if one leaks [22]. - Once the call runs on the server, rename the variable without the public prefix, so a later build cannot quietly put it back into the page.
If you ask an AI agent to add an AI feature, tell it plainly that provider keys must stay on the server. That instruction is our own advice rather than a vendor rule, and it costs a single sentence in your prompt or rules file.
New Nov 20, 2026: claude -p runs a repository's hooks with no trust prompt
Claude Code hooks are shell commands that run at set points in a session, and Anthropic's hooks reference says they "execute shell commands with your full user permissions" [23]. In an interactive session, Claude Code holds hooks back from every settings file until you accept the workspace trust dialog for the folder. A headless run follows a different rule. In a -p or SDK session, Anthropic says, "Claude Code never shows the dialog and treats the folder as trusted, so hooks committed in a repository’s .claude/settings.json run in a folder you’ve never trusted" [23]. The headless guide agrees: "Without --bare, a -p session runs the hooks in a project’s .claude/settings.json and connects the servers in its .mcp.json, even in a folder you’ve never trusted" [24].
Attackers have already used that file. Google Threat Intelligence Group's AI Threat Tracker, published November 16, 2026, describes a suspected cluster it calls MIDNIGHT NEPTUNE in its section on North Korea-linked activity [27]. After the actor got into an organization's internal code repositories, Google says it modified .claude/settings.json "configuration files so that when users started up Claude Code, it would automatically trigger the download of the SOMBERMEME backdoor" [27]. Google does not say whether those developers started Claude Code interactively or with -p. Unconfirmed: any role for headless mode in that incident is our inference, drawn only from the fact that both cases involve hooks in a repository's settings file.
Claude Code 2.1.329, published on November 19, 2026, adds a related warning. Its changelog lists: "Added a trust dialog warning that says where a folder’s hooks are declared" [26] [28]. That change is to the dialog, and Anthropic's docs say -p sessions never show it [25]. Neither the release notes nor the docs we read on November 20 say that -p now asks first, so we treat headless runs as still skipping the prompt. We did not run version 2.1.329 to test it.
What to do before you run claude -p on code you did not write, such as a CI job that reviews outside pull requests or a script that loops over cloned repositories:
- Read the repository's
.claude/settings.jsonand.mcp.jsonfirst. Anthropic's table of what runs before you trust a folder lists hooks, theenvblock and helper commands such asapiKeyHelperas used in a-prun, and says servers in.mcp.jsonare connected without asking [25]. - Start with
--bare, or switch hooks off for that run. Anthropic's advice is to "review its.claude/settings files, start with--bare, or turn hooks off for that run with--settings '{"disableAllHooks": true}'" [23]. That flag takes precedence over project and local settings, but hooks that an administrator sets through managed policy can only be disabled at the managed level [23]. Bare mode does not use your subscription login, so setANTHROPIC_API_KEYbefore you use it [24]. - Run the job in a throwaway container or VM that holds only the tokens it needs, because hooks run with your user's full permissions. This is our own advice rather than a vendor rule.
- In your own repositories, review changes to
.claude/settings.jsonthe way you review code, for example with a code-owners rule on the.claude/folder. That is also our advice, prompted by Google's account of an actor editing the file. - In an interactive session, read the trust dialog before you accept a folder you did not write. Since 2.1.329 it also names where the folder's hooks are declared [26].
The checklist
- Pick a permission mode that asks. Leave shell commands, network access and edits outside the project behind an approval prompt. Use read-only or plan modes when you only want analysis.
- Write allow and deny rules. Allow the few commands you run constantly (tests, build). Deny the dangerous ones, such as cloud CLIs or
rmpatterns you never want. - Turn on the sandbox. OS-level sandboxes limit what shell commands can touch on disk and the network, even when a command slips past a prompt.
- Keep secrets out of reach. Deny file reads of
.envand secret folders, and give the agent only the scoped tokens it needs instead of your own credentials. Never type a password into a command you approve with "don't ask again", and gitignore.claude/settings.local.jsonin every repository (see above). - Review every MCP server. Install only servers you trust, enable specific (ideally read-only) tools instead of all of them, and pass keys through environment variables, not pasted into config.
- Checkpoint with git. Commit (or work on a branch or worktree) before handing a task to an agent, then review the diff before you merge. This is our own advice, not a vendor requirement; it makes any bad run cheap to undo.
- Keep skip-all modes in throwaway environments. If you want an agent to run unattended, do it in a container or VM.
Free pre-ship quick scan
AI agents write code fast, and one easy slip is an API key that ends up somewhere public. Before you show a vibe-coded app to real users, this free script runs five checks in your project folder. It is read-only, makes no network calls, and needs only bash and git.
- Real
.envfiles committed to git (.env.exampleis fine). - Whether
.gitignoreactually ignores.env. - Secret-looking names behind browser-exposed prefixes (
NEXT_PUBLIC_,VITE_,EXPO_PUBLIC_,REACT_APP_), which frameworks such as Next.js and Vite bundle into the code every visitor downloads. - Server-only key names, such as Supabase's
service_role, in client folders. - Common live key patterns (Stripe, OpenAI, Anthropic, AWS, GitHub, Slack) in tracked files.
Download pre-ship-quick-scan.sh (free)
bash pre-ship-quick-scan.sh
Each check prints PASS, WARN or FAIL, and it names the files behind any problem, with line numbers where it can. A clean result is a good sign, but it covers secrets only and is not a security audit. We tested it on two small sample repositories, one with planted mistakes and one clean.
Secrets are only the first part of shipping safely. Auth, row-level security, payment webhooks, cost caps and backups are in the pre-launch checklist inside the Vibe-Coder Ship-It Kit, along with rules files that tell your agent to follow it. Buy: $12 with code FIRST25 (first 25), then $19
The Ship-It Kit is our own product, and buying it supports WhichAICoder.
Where each tool exposes these settings
| Tool | Permission modes and approvals | Allow / deny rules and secrets | Sandbox and network | MCP |
|---|---|---|---|---|
| Claude Code | Modes include default (prompts on first use of each tool), acceptEdits, plan and bypassPermissions; set defaultMode in settings files. Admins can set permissions.disableBypassPermissionsMode to "disable" [2] | Rules are evaluated deny, then ask, then allow; first match wins. A Read(./.env) or Read(./secrets/**) deny rule blocks Claude's file tools from reading them; a .claudeignore file has no effect [2] | Docs describe sandboxing as OS-level enforcement for shell commands' filesystem and network access, complementary to permissions [2] | Permission rules apply to MCP tools too [2] |
| OpenAI Codex | approval_policy, e.g. on-request; switch to read-only with /permissions. The old untrusted policy is retired; per-project trust_level = "untrusted" makes commands require approval [3] | Execution-policy rules can allow specific commands [3] | sandbox_mode (read-only, workspace-write). Locally the default keeps network off and writes limited to the workspace; enable with [sandbox_workspace_write] network_access = true [3] | Codex can ask for approval on connector tool calls that advertise side effects [3] |
| Cursor | Terminal commands need approval by default; Run Modes range from an allowlist to the Auto-review classifier, which Cursor calls best-effort guardrails, not a hard security boundary [4] | Command allowlist in Run Modes [4] | A sandbox runs terminal commands in a restricted environment that blocks unauthorized file access and network activity [5] | MCP connections need approval, and each tool call still needs approval unless pre-approved with an MCP allowlist [4] |
| GitHub Copilot cloud agent | Secrets for MCP servers go in Agents secrets or variables prefixed COPILOT_MCP_; only those are available to the MCP config [6] | Copilot's docs have a separate "Customize the firewall" page for the agent's environment [6] | A tools array per server. GitHub "strongly recommend[s]" allowlisting specific read-only tools, because the agent uses them autonomously without asking [6] | |
Setting names change between versions; follow the linked doc for your version.
Tools that help
The built-in settings above are the base layer. If you want one policy across several agents, there are add-on tools. One is HOL Guard. HOL says it is "a local-first runtime control for AI agents and the tools they use", that on supported integrations it "evaluates shell commands, secret access, MCP changes, and plugin or skill installs before they run", and that it is free on one machine and works without an account [7]. HOL's docs list setup guides for Codex, Claude Code, Cursor, OpenCode and others [8]. HOL also says it is "not a cloud MCP gateway and not a complete prompt-injection preventer", and that coverage depends on the tool and event [7]. We have not tested it.
HOL Guard (hol.org/guard) affiliate link
Code GUARD25-15187857: 25% off the first payment on a paid HOL Guard plan. It is applied automatically through the link above.
Affiliate link: we may earn a commission from HOL Guard if you sign up through it. It costs you nothing extra and did not change this checklist. Disclosure.
What we could not verify
- HOL Guard's paid plan prices; we don't list them here.
- How well any of these controls stop a real prompt-injection attempt. We read the docs and did not run attacks.
- The Reddit post shown in the Nov 18 X screenshots (we saw only the screenshots), and the scan counts from ShipSniffer, Keybrum and the DEV Community post, which we report as each author published them.
- Whether the developers in Google's MIDNIGHT NEPTUNE case ran Claude Code with
-por interactively, when Anthropic's-ptrust behavior began (the docs we read do not say), and how Claude Code 2.1.329 behaves in a-prun, since we read the docs and release notes and did not run it.
FAQ
What is prompt injection in an AI coding agent?
OWASP lists prompt injection as LLM01. Its indirect form happens when the model takes in external content, such as a website or file, that changes its behavior in unintended ways. For a coding agent that means a README, issue, web page or tool result can carry instructions.
Which permission mode should I use day to day?
One that still asks before shell commands and network access: Claude Code default or plan mode, Codex workspace-write or read-only with on-request approvals, and Cursor with terminal approval on. Keep skip-all-prompts modes for containers or VMs; Anthropic says bypassPermissions should only be used in isolated environments like that.
Do deny rules beat allow rules in Claude Code?
Yes. Claude Code docs say rules are evaluated deny, then ask, then allow, and the first match wins regardless of how specific the rule is.
Can Claude Code leak secrets through settings.local.json?
Yes, if the file gets committed. Choosing "Yes, and don't ask again" on a Bash prompt saves an allow rule to .claude/settings.local.json, and when the approved command had a password or token typed into it, that text can land in the file. Claude Code adds the file to your own global git excludes, but that does not protect a clone on another machine, a copied folder or a file already tracked. Add it to the repository's own .gitignore and rotate anything that was pushed. If you build Docker images with COPY . ., list .claude/ in .dockerignore too, since GitGuardian found working keys from this file inside public Docker Hub images.
Is the GitHub Copilot plugin for JetBrains affected by a security flaw?
Yes. CVE-2026-83953, published by Microsoft on November 10, 2026, is a command injection flaw that lets an attacker with local access read files. The NVD record lists plugin versions 1.0.0 up to, but not including, 1.17.0-251 as affected, so update the Copilot plugin in your JetBrains IDE to 1.17.0-251 or later. Microsoft rates it Important (CVSS 5.5) and says it was not exploited when published.
Is it safe to put an API key in a VITE_ or NEXT_PUBLIC_ variable?
Not a secret key. Vite says VITE_ variables are exposed in client-side source code after bundling and should not hold API keys, and Next.js inlines NEXT_PUBLIC_ values into the JavaScript it sends to the browser. Keep secret keys in server-only variables and call the provider from a server route or function. Keys that are designed for the browser, such as Stripe's publishable key, are the exception. If a secret key already shipped, rotate it first and then move the call to the server.
Does claude -p ask for workspace trust before running a repository's hooks?
No, according to Anthropic's docs. In a -p or SDK session Claude Code never shows the trust dialog and treats the folder as trusted, so hooks committed in a repository's .claude/settings.json run in a folder you have never trusted. Before you script claude -p over a repository you did not write, review its .claude/ settings files, start with --bare, or turn hooks off for that run with --settings '{"disableAllHooks": true}'. Claude Code 2.1.329 (Nov 19, 2026) added a trust dialog warning that says where a folder's hooks are declared, but its release notes do not mention -p, and we have not tested it.
Is HOL Guard required?
No. Every step on this checklist uses settings built into the tools. HOL Guard is an optional extra layer; HOL itself says it is not a complete prompt-injection preventer.
Sources (checked Nov 4, 2026 unless noted)
- OWASP GenAI: LLM01 Prompt Injection checked Nov 4, 2026
- Claude Code docs: Configure permissions checked Nov 4, 2026
- OpenAI Codex: agent approvals and security checked Nov 4, 2026
- Cursor docs: Agent security checked Nov 4, 2026
- Cursor docs: Terminal (sandbox) checked Nov 4, 2026
- GitHub Docs: Configure MCP servers for Copilot cloud agent checked Nov 4, 2026
- HOL Guard overview and FAQ checked Nov 4, 2026
- HOL Guard documentation checked Nov 4, 2026
- Ars Technica (Nov 9, 2026): Claude Code settings files expose thousands of secrets on GitHub, reporting Apiiro's research checked Nov 9, 2026
- Claude Code docs: Settings (project local settings and git excludes) checked Nov 9, 2026
- GitGuardian: Claude Code settings.local.json secrets on GitHub and Docker Hub checked Nov 11, 2026
- Microsoft Security Response Center: CVE-2026-83953, GitHub Copilot for JetBrains Information Disclosure Vulnerability checked Nov 12, 2026
- NIST NVD: CVE-2026-83953 (affected versions) checked Nov 12, 2026
- @fromcodetocloud on X (Nov 18, 2026): screenshots of a Reddit post about keys found in a vibe-coded app's frontend (about 2.5M views on Nov 19) checked Nov 19, 2026
- Vite docs: Env variables and modes (VITE_ prefix, protecting secrets) checked Nov 19, 2026
- Next.js docs: Environment variables (NEXT_PUBLIC_ values inlined into browser JavaScript) checked Nov 19, 2026
- OpenAI Help Center: Best practices for API key safety checked Nov 19, 2026
- Google AI for Developers: Using Gemini API keys (security rules) checked Nov 19, 2026
- ShipSniffer (Nov 13, 2026): What we found scanning vibe-coded apps for leaked API keys checked Nov 19, 2026
- Keybrum (Nov 18, 2026): Vibe coding security statistics, 2,802 apps read checked Nov 19, 2026
- DEV Community: We checked 573 vibe-coded apps for leaked API keys checked Nov 19, 2026
- Stripe docs: API keys (publishable vs secret and restricted keys) checked Nov 19, 2026
- Claude Code docs: Hooks reference (workspace trust, -p sessions, disableAllHooks) checked Nov 20, 2026
- Claude Code docs: Run Claude Code programmatically (bare mode, -p trust behavior) checked Nov 20, 2026
- Claude Code docs: Permissions, what runs before you trust a folder checked Nov 20, 2026
- Claude Code docs: Changelog, 2.1.329 (November 19, 2026) checked Nov 20, 2026
- Google Cloud: GTIG AI Threat Tracker, Agentic Threat Activity Crosses a Significant Threshold (November 16, 2026) checked Nov 20, 2026
- GitHub: anthropics/claude-code release v2.1.329 (published November 19, 2026) checked Nov 20, 2026
Related: One rules file for every AI coding agent · Ship-It Kit (Edition 2 packs this checklist with rules files; $12 with code FIRST25).
The Ship-It Kit is our own product. Buying it supports WhichAICoder; it never affects rankings or scores. Disclosure.
Free PDF: the AI Coding Cost Cheat Sheet. Enter your email to download it, plus one email a week when picks or prices move.
Spotted an error or a changed doc? Send a correction.