How to Secure AI Coding Agents: Lessons From the Nx and Amazon Q Incidents, With Permission Rules and Sandboxing
Malware has started asking the AI agents on developer laptops to find secrets for it. What the Nx and Amazon Q incidents teach, and the settings that limit what an agent can read and reach.

An AI coding agent on your laptop can read your files, run shell commands and reach the internet, with your user account’s permissions. That is what makes it useful, and it is what you limit when you secure AI coding agents. It is also why attackers have started writing malware that doesn’t bring its own tools, and simply asks the agent you already installed to do the work.
This guide shows how to secure AI coding agents on developer machines. It starts with two real incidents from 2025, then turns them into settings you can apply today: what to keep away from the agent, how to limit what its shell can touch, and which flags to never use outside a throwaway environment. The configuration examples use Claude Code, because its documentation spells out exactly what each control covers, but the same ideas apply to Gemini CLI, Amazon Q, Cursor and the rest.
Incident 1: malware that asked AI agents to find the secrets
On August 26, 2025, malicious versions of the Nx build system were published to npm. The Nx team’s post-mortem says they “were active for 4 hours before being completely removed from the registry,” and that the attackers got the npm token through a GitHub Actions workflow that echoed pull request titles without sanitizing them. The packages ran a post-install script that “scanned user systems for sensitive data, attempted to use local AI tools (like Claude and Gemini), and uploaded the results to a public GitHub repo via the GitHub CLI.”
Snyk’s analysis of the script shows how. It checked whether the claude, gemini and q command-line tools were installed, then started each one with the flag that turns its safety prompts off:
claude --dangerously-skip-permissions -p PROMPT
gemini --yolo -p PROMPT
q chat --trust-all-tools --no-interactive PROMPT
The prompt asked the agent to search the home directory, /etc, /tmp and wallet folders for files such as .env, id_rsa, keystore.json and secrets.json, and to write the paths to /tmp/inventory.txt. The script then collected environment variables, the GitHub token from gh auth token and the npm config, encoded everything and pushed it to a new public repository named s1ngularity-repository under the victim’s own GitHub account.
The lesson isn’t that the agents were hacked. They did exactly what an agent with skipped permissions does: follow instructions. The attack worked because the agents could read every secret on the machine, and because a flag existed to make them do it without asking.
Incident 2: a wiper prompt shipped inside an AI extension
In July 2025, version 1.84.0 of the Amazon Q Developer extension for Visual Studio Code shipped with a malicious prompt. AWS security bulletin AWS-2025-015 says the extension “had an inappropriately scoped GitHub token in their CodeBuild configuration,” which let the attacker commit code to the open-source repository “that was automatically included in a release.” Press reports quoted the injected prompt as telling the agent its goal was “to clean a system to a near-factory state and delete file-system and cloud resources.” AWS found it “was unsuccessful in executing due to a syntax error,” fixed it in version 1.85.0 and tracked it as CVE-2025-8217.
Here the lesson is about the supply chain of the agent itself. The tool you trust to edit your code is software from a repository with build tokens, and those tokens deserve the same least-privilege care as your production keys. We covered the wider pattern in software supply chain security.
What an agent can reach by default
Before changing settings, it helps to know the starting point. Claude Code’s security documentation says that in manual mode it “starts with read-only permissions” and asks before editing files or running commands, while read-only commands such as ls, cat and git status run without asking. Its sandboxing page adds the part people miss: the OS-level Bash sandbox “is off by default,” and even when it is on, reads are allowed for “most of the machine, including credential files such as ~/.ssh and ~/.aws/credentials,” and environment variables are “inherited from Claude Code, including any secrets in its environment.”
In other words, “read-only” still means the agent can read your secrets. Every step below narrows that.
Step 1: Never use the skip-permissions flags on a real machine
All three flags from the Nx script exist for a reason: running an agent unattended in a container or CI job with nothing worth stealing. On a laptop with SSH keys, cloud credentials and a logged-in GitHub CLI, they hand the machine to whatever text reaches the agent. Treat these as red flags in scripts, README files and setup guides:
--dangerously-skip-permissions(Claude Code)--yolo(Gemini CLI)--trust-all-tools(Amazon Q CLI)
If you need unattended runs, run them in a disposable container or VM that has only the repository and a short-lived, narrowly scoped token.
Step 2: Deny reads of secret files
Claude Code permission rules have three lists: allow, ask and deny. The documentation says rules “are evaluated in order: deny, then ask, then allow,” and “an allow rule can’t carve an exception out of a deny rule.” A deny rule on a secrets file therefore can’t be undone by a broad allow rule elsewhere. A project’s .claude/settings.json can look like this:
{
"permissions": {
"deny": [
"Read(./.env)",
"Read(./.env.*)",
"Read(./secrets/**)",
"Bash(git push *)"
]
},
"sandbox": {
"enabled": true
}
}
Know the limits of these rules. The documentation says Read deny rules also apply to shell commands Claude Code can analyze, such as cat, head, tail, sed and tee. They don’t stop a command that reads a file without naming it, such as grep -r pattern . run in the folder that holds it, or a Python or Node script that opens files itself. That is why step 3 exists. Also note that a .claudeignore file has no effect: move its entries into Read deny rules.
Step 3: Turn on the sandbox for shell commands
Permission rules decide what the agent may ask for. A sandbox decides what a command can actually do once it runs. Claude Code’s Bash sandbox is “a boundary that the operating system enforces around the shell commands Claude runs,” and you turn it on with /sandbox or sandbox.enabled: true. According to its documentation:
- Writes are limited to the working directory, a per-user temp directory and directories you add.
- Network access has “no direct route out.” Connections go through a local proxy that checks each host against your allowed domains, “which start empty.”
- Reads still default to most of the machine, so add your credential paths to the sandbox’s credentials setting in deny mode. Its examples deny
~/.aws/credentials,~/.ssh,GITHUB_TOKENandNPM_TOKEN. - The sandbox runs on macOS, Linux and WSL2. “On native Windows, Claude Code runs commands unsandboxed.”
- Commands you exclude from the sandbox run “with your full access,” so keep that list short.
Two details are easy to get wrong. A sandbox denyRead entry doesn’t stop the built-in Read tool, and the sandbox’s allowed domains don’t limit WebFetch, because those tools run outside the sandbox and follow permission rules instead. You need both layers: permission rules for the built-in tools, and the sandbox for the shell.
Apply that to the Nx prompt: an agent whose shell is sandboxed with denied credential paths can’t list or read ~/.ssh. The malicious script itself ran outside the agent, though, and published the results with the GitHub CLI on its own. The sandbox protects what the agent does, and steps 4 and 7 cover the rest.
Step 4: Keep secrets out of reach in the first place
- Don’t keep production secrets in the project folder. Use a secrets manager or your platform’s environment settings, and keep only development values in a local
.env. - Don’t export long-lived tokens in your shell profile. Agents inherit the environment they start in.
- Use short-lived, scoped credentials. A token that can only read one repository for one hour is a much smaller loss.
- Log out tools you don’t need. The Nx script used the GitHub CLI’s saved login to publish the stolen data under the victim’s own account.
Step 5: Treat repository content as untrusted input
Agents read README files, issues, pull request descriptions, code comments, dependency source and web pages, and any of them can contain instructions aimed at the agent. This is prompt injection, explained in our prompt injection guide. Claude Code’s documentation lists several protections against it, including command approval and injection detection, and it is frank that “no system is completely immune to all attacks.” Its own advice includes “avoid piping untrusted content directly to Claude.”
In practice:
- Be most careful when you point an agent at code you didn’t write: a new dependency, a contributor’s branch, a cloned repository.
- Read what an agent wants to run before you approve it, especially commands that touch the network, credentials or files outside the project.
- Review agent-written pull requests like any other contributor’s. Watch for new dependencies, changed CI workflows and added install scripts.
Step 6: Check the tools you connect to the agent
MCP servers and plugins extend what an agent can do, and their tool descriptions go straight into the model’s context. A poisoned description can steer the agent the same way a poisoned README can. We showed how that works, and how to pin tool descriptions so changes are noticed, in MCP tool poisoning. For servers you run yourself, see securing MCP servers.
Step 7: Harden the CI around the agent
Both incidents started in CI, not on a laptop. Nx’s root cause was a pull_request_target workflow that ran text from a pull request title with a token that could write. AWS’s was a build token with more access than it needed. If agents open pull requests in your repository, check:
- Workflows triggered by
pull_request_targetnever run code or text from the pull request. - Workflow tokens are read-only by default and get write access only where needed.
- Publishing workflows don’t accept manual or API triggers unless you need them. The Nx attackers started the publish workflow through the API.
- Release tokens are scoped to one repository and one job.
A checklist to secure AI coding agents
| Control | Stops |
|---|---|
| No skip-permissions flags outside disposable environments | Malware or injected text running commands without a prompt |
Read deny rules for .env and secrets folders | The agent’s file tools and simple shell reads of secrets |
| OS sandbox with an empty domain list and denied credential paths | Shell commands and scripts reading keys or sending data out |
| Short-lived, scoped tokens and no secrets in the shell profile | Long-term damage when something leaks anyway |
| Reviewing commands and agent pull requests | Prompt injection that gets past the tools |
Least-privilege CI tokens and safe pull_request_target use | Attacks on the agent’s own supply chain |
None of these controls is enough alone, and none of them makes an agent safe to point at hostile input with full access. Together they turn “the agent can do anything I can do” into “the agent can change this project and talk to the hosts I chose,” which is the right amount of trust for a tool that reads text from strangers. For the design side of the same problem, see how to secure AI agents by designing for a compromised model.
Sources: the Nx s1ngularity post-mortem on nx.dev, Snyk’s analysis of the malicious Nx package, AWS security bulletin AWS-2025-015, and Claude Code’s security, sandboxing and permissions documentation, all read on December 2, 2026.







