Securing coding agents: Claude Code, Cursor, Copilot, Gemini CLI and agents in CI
Coding agents read untrusted text and hold a shell, your source code and your tokens. The real incidents (Clinejection, the Nx s1ngularity attack, the Amazon Q extension, PromptPwnd), the attack paths (issues and PR comments, repo files, MCP servers, hallucinated packages) and a practical hardening checklist for laptops and CI.
By Sandip Maity, Applied AI Engineer
A coding agent is the most powerful agent most developers run. It reads text from anywhere (issues, pull requests, READMEs, web pages, package docs), and it can run shell commands, edit code, install packages and use whatever tokens are in its environment. That is the lethal combination by default: private data, untrusted content and outbound channels, all in one process.
This lesson covers Claude Code, Cursor, GitHub Copilot's agent mode and coding agent, Gemini CLI, Codex, Cline and similar tools. The principles apply to all of them; the setting names differ, so check your tool's current documentation.
#What has actually happened
These are documented incidents, not hypotheticals. They show the main attack paths.
| Incident | When | What happened | Lesson |
|---|---|---|---|
| Amazon Q Developer extension | July 2025 | A pull request slipped a prompt into a release of the VS Code extension telling the agent to wipe local files and cloud resources. AWS pulled the version; it reported the prompt did not run as intended. | Your agent's own instructions are part of your supply chain. |
| Nx "s1ngularity" | August 2025 | Malicious versions of the Nx build tool on npm ran a post-install script that called any AI coding CLI it found (Claude Code, Gemini CLI, Amazon Q) with permission checks turned off, asking it to search the disk for secrets and wallets, then published what it found to public GitHub repositories. | An installed agent CLI with a "skip permissions" flag is a tool for malware too. |
| PromptPwnd | Disclosed December 2025 | Researchers showed that GitHub Actions and GitLab pipelines running AI agents on issue and PR text could be steered by that text to edit issues, leak values or run commands, across several agents. | Text from strangers reaches your agent in CI. |
| Clinejection | Disclosed February 2026 | Cline's AI issue-triage workflow put the issue title straight into the agent's prompt. A crafted title started a chain that led to a stolen npm publish token and a malicious cline@2.3.0 that about 4,000 machines installed over about eight hours. Cline removed the workflow and moved to OIDC trusted publishing. |
One unsanitised field plus a privileged CI job is enough. |
Related research showed the same pattern through MCP: a malicious issue in a public repository made an agent with a GitHub MCP server read the user's private repositories and leak them in a public pull request (Invariant Labs, 2025). And "rules file" attacks hid instructions in invisible Unicode inside the configuration files that tell agents how to write code.
- 1Attackeropens an issue whose title contains instructions
- 2CI workflowpastes the title into the agent's prompt
- 3Agentfollows the instruction: installs a package, runs a script
- 4Runnerholds caches and tokens the agent can reach
- 5Token theftpublish token or cache poisoned
- 6Releasemalicious version published to thousands of users
Remove any one step (no splicing, no tools, no secrets in the job, trusted authors only) and the chain breaks.
#The attack paths
#1. Text the agent reads
Anything the agent reads can carry instructions (indirect prompt injection):
- Issue titles and bodies, PR descriptions, review comments, commit messages.
- README files, docs, code comments, test fixtures and error messages in a repository you cloned.
- Web pages and package documentation fetched during a task.
- Tool results: search results, CI logs, MCP server outputs.
A good model ignores most of these most of the time. Attackers need it to fail once.
#2. Files the agent trusts more than other text
Many agents read special files automatically and treat them as instructions: AGENTS.md, CLAUDE.md, .cursor/rules, .github/copilot-instructions.md, GEMINI.md, and project settings such as .claude/settings.json or .vscode/ tasks. Some project configuration can also register MCP servers or hooks that run commands.
A cloned repository can therefore bring its own instructions and sometimes its own code. Most tools now ask before trusting a new folder; treat that prompt as a real security decision, not a click-through. Several agents have had vulnerabilities where project configuration ran before the trust prompt appeared, which is one reason to keep them updated.
#3. Packages it installs
Agents install dependencies on their own. Three risks:
- Malicious packages that run code on install (the Nx case).
- Typosquats of popular names.
- Slopsquatting: models invent plausible package names that do not exist. A 2025 study of code-generating models found that roughly a fifth of recommended packages did not exist, and many invented names repeated across runs, so attackers can register them and wait.
#4. Tools and MCP servers it connects to
Every MCP server is code running with your credentials, plus descriptions that go straight into the model's context. Malicious or compromised servers have appeared on public registries (agent and MCP security, MCP lab).
#5. What it can reach
Once steered, the damage depends on what is within reach: .env files, ~/.aws, ~/.ssh, browser profiles, cloud CLIs that are already logged in, gh with a token, package-publish credentials, and the network.
#Hardening on your own machine
Work through these in order; the first three matter most.
- Run the agent in a sandbox. Use the tool's sandbox mode where it has one, or a dev container or VM. The goals: the agent sees only the project directory, cannot read your home directory's credentials, and has default-deny network egress with an allow-list (your package registry, your model API).
- Keep secrets out of reach. No long-lived tokens in environment variables of the agent's shell. Use short-lived, scoped credentials when a task needs them, and remove them afterwards. Do not let the agent read
.env. - Never use "skip all permissions" on a machine with anything to lose. Flags such as
--dangerously-skip-permissions,--yoloor "trust all tools" are for throwaway sandboxes only. - Use allow and deny lists for commands and files. For example, in Claude Code's project settings:
{
"permissions": {
"allow": ["Bash(npm run test:*)", "Bash(npm run lint)", "Bash(git diff:*)"],
"deny": ["Read(./.env)", "Read(./.env.*)", "Read(~/.ssh/**)", "Bash(curl:*)", "Bash(wget:*)", "Bash(npm publish:*)"]
}
}Other agents have equivalents (command allow-lists, auto-approve settings). Deny lists are a speed bump, not a wall: an agent can often reach the same goal by another command, which is why the sandbox comes first.
- Treat new repositories as untrusted. Read
AGENTS.md/CLAUDE.md, rules files and any project MCP or hook configuration before you grant trust. Look for invisible characters withgrep -nP '[\x{200B}-\x{200F}\x{202A}-\x{202E}\x{2060}-\x{2064}\x{E0000}-\x{E007F}]' -r . - Verify every new dependency. Check that the package exists, is the one you meant, has real history and maintainers, then pin it in a lockfile. Consider a registry proxy or a tool that blocks packages younger than a few days.
- Pin MCP servers to versions or hashes, install them from sources you trust, and review their tool descriptions.
- Review diffs like a stranger wrote them. Watch for changes to CI files, install scripts, lockfiles, network calls and anything touching credentials. A hijacked agent's most durable damage is a backdoor committed in plain sight.
- Update agents promptly. Coding-agent CVEs are frequent, and many fix ways that project files could run code before you approved them.
#Hardening agents in CI
CI agents are where the incidents happened. The rule: an agent that reads text from untrusted people must not hold anything worth stealing.
name: ai-issue-triage
on:
issues:
types: [opened]
permissions:
contents: read # nothing more
issues: write # only if it must label; otherwise remove
jobs:
triage:
# only run for people who already have write access, or require a maintainer label
if: contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.issue.author_association)
runs-on: ubuntu-latest
steps:
- name: Triage
env:
ISSUE_TITLE: ${{ github.event.issue.title }} # passed as data, never spliced into the prompt or a shell line
ISSUE_BODY: ${{ github.event.issue.body }}
run: ./scripts/triage.sh # reads $ISSUE_TITLE from the environment and gives the agent read-only toolsThe checklist:
- Never interpolate
${{ github.event.* }}text into a prompt or arun:line. Pass it through environment variables and mark it as data in the prompt. (Environment variables stop shell injection; they do not stop prompt injection, so the remaining items still matter.) - Least privilege token:
permissions:set explicitly, read-only by default. - No secrets in jobs that read untrusted input. Publishing, deploying and signing happen in separate jobs that untrusted input cannot reach.
- Restrict who can trigger agent jobs (author association, a maintainer-applied label, or manual dispatch). Be very careful with
pull_request_targetandissue_comment, which run with the base repository's permissions. - Separate caches for untrusted jobs, since cache poisoning was part of the Clinejection chain.
- Use trusted publishing (OIDC) for npm, PyPI and others instead of long-lived publish tokens, with provenance.
- Give the agent read-only tools for triage: comment drafts that a human posts, labels from a fixed list. No shell, no package installs.
- Log every tool call and alert on unexpected network destinations or installs.
#Organisation-level controls
- An approved list of agents and MCP servers, with versions.
- Managed settings that enforce sandboxing and deny rules for everyone (most enterprise agent plans support this).
- Separate machine identities for agents, so their actions are attributable and revocable.
- Secret scanning on every push, since agents sometimes commit what they read.
- Include coding agents in your threat model and red-team scope (red-team tooling).
#Common mistakes
- "It asks before running commands, so it's safe." Approval fatigue is real, and malicious steps look routine (
npm install,curlto a docs site). - Running the agent where you are logged in to everything. Your laptop session is the agent's session.
- One CI job that both reads issues and publishes releases.
- Trusting a repository because it is popular. Popular repositories receive malicious issues and pull requests too.
- Adding MCP servers from a list without reading them.
- Assuming the model will notice. Injected instructions are written to look like normal work.
#Exercise
Your team wants an agent that, for every new issue, reproduces the bug in CI and opens a PR with a fix. Design it safely.
Show answers
split it into two stages. Stage 1 runs on any new issue with a read-only token, no secrets, no network beyond the package registry, in a fresh container with its own cache; the agent can only write a proposed patch and a report as an artifact. Stage 2 runs only after a maintainer applies a label: it opens the PR from the artifact using a token that can push to a bot branch but cannot merge, publish or change workflows. Issue text is passed as data through environment variables, every tool call is logged, and changes to .github/, lockfiles or install scripts in the proposed patch require explicit human review.
#Going deeper
- The OWASP Top 10 for Agentic Applications maps these risks to the industry list.
- Injection-resistant agent designs shows architectures where untrusted text cannot pick the agent's actions.
- Supply chain and stack security covers dependency pinning, provenance and containers.
Quick quiz
+10 XP per correct answer, +25 XP for a perfect score
1. In the Clinejection incident, what was the attacker's way in?
2. Why is a coding agent in CI a higher-stakes target than the same agent on a laptop?
3. What is slopsquatting?
4. Which single change most reduces the damage if an agent on your laptop is hijacked by a poisoned repository file?
5. A GitHub Actions workflow runs an AI agent on new issues. Which setting is the safest default?
Up next · 22 min · Pro, first part free
Model extraction, abuse and denial of wallet
Threats to the service itself: model extraction and distillation theft, membership inference, resource-exhaustion and denial-of-wallet attacks, abuse of free tiers, and the controls: quotas, rate limits in tokens, anomaly detection, budgets, and legal and technical deterrents.
Continue →Found this useful?
Send it to someone who is learning the same thing.