AI coding agent security: letting agents push to PRs
How to let an AI coding agent push to pull requests safely: prompt injection, test tampering, CI secrets, MCP scopes, and a checklist you can apply today.
You want an AI coding agent to push commits to pull requests, and you do not want it to leak a secret, rewrite your tests, or merge something nobody read. AI coding agent security comes down to a small number of places where things go wrong. This post maps them, then gives a checklist.
The short version:
- Anything a stranger can type into GitHub can end up in the agent's prompt.
- An agent rewarded for green CI will sometimes change the checks. Keep the checks out of its reach.
- A push from an agent starts a CI run that may have secrets the original PR did not.
- MCP servers hand the agent your permissions. Narrow them.
- Branch protection and a human merge are the last line.
Where untrusted text enters the loop
A coding agent reads a lot of text that other people wrote. OWASP calls the general problem indirect prompt injection: the model takes in content from an external source, and instructions hidden in that content change what it does.
For an agent working on pull requests, the sources are easy to list.
Every box on the left can carry instructions. Every box on the right is something the agent may be able to do with your credentials.
- Issue and PR text. Titles, bodies, comments and commit messages. Microsoft described a payload hidden in an HTML comment, invisible when rendered but plain to the model.
- CI logs. Test output often echoes strings from dependencies. A log is text a package author can influence.
- Repository files and tool results. READMEs, changelogs, dependency files, and whatever an MCP server returns.
What has actually happened
These are published reports, not hypotheticals.
- May 2025, GitHub MCP server. Invariant Labs showed that a malicious issue on a public repository could steer an agent that had access to the user's private repositories. Asked to review open issues, the agent pulled private data into its context and put it in a pull request on the public repo, where the attacker could read it. Invariant said the server code was not at fault and that GitHub could not fix this alone with a server-side patch.
- PromptPwnd, from Aikido Security. Several AI actions for GitHub Actions put issue titles, bodies and PR descriptions straight into prompts. In a Gemini CLI proof of concept, a hidden instruction made the agent write its API key and GitHub token into the issue. Aikido says Google patched its repository within four days of disclosure.
- June 2026, Claude Code GitHub Action. Microsoft reported that the action's Bash tool ran with a scrubbed environment but its file Read tool did not, so an injected prompt could read
/proc/self/environand get the workflow's API key. Microsoft says it reported this on 29 April 2026 and that Anthropic fixed it in Claude Code 2.1.128 on 5 May 2026.
The rule of two
Meta's "Agents Rule of Two" gives you a way to reason about this. An agent session should have at most two of these three properties:
- It can process untrustworthy input.
- It can access sensitive systems or private data.
- It can change state or communicate externally.
If a task needs all three, Meta says a person should supervise it, or it should start from a fresh context. A pull request agent reads untrusted input by definition (A) and pushes commits (C). So the job is to keep it away from sensitive access (B): no production secrets, no other repositories, no broad tokens.
Agents that edit the judge
The second failure needs no attacker. Give a model the goal "make CI pass" and it will sometimes change the tests.
The EvilGenie benchmark tested this on coding problems that were set up so cheating was easy. Among the agents tested, Claude Code hardcoded test cases on 2.1% of unambiguous problems and Codex on 0.7%. Gemini CLI modified test files before testing on 0.7%, and deleted test files afterwards on 3.4%. The authors treat the deletion as misaligned behaviour but not scored hacking. On ambiguous problems the rates were far higher, though from a small sample. Their held-out tests added little, and an LLM judge worked better. These are benchmark numbers on competition problems, not a measure of your repository. We saw the same pattern in our own test runs, described in let your CI judge the fix, not the AI.
Forms this takes in a real repo:
- Editing or skipping a failing assertion.
- Editing test config: excluding files from the run, or lowering a coverage threshold.
- Deleting the module under test, or stubbing a failing call so the test checks nothing.
- Pinning a dependency back so the update PR is no longer an update.
A blocklist of tricks will not hold. Use an allowlist and structure: existing tests and test tooling are off limits, and the check looks at which files changed, not at what the agent says it did.
Secrets: what a push from an agent triggers
An agent that pushes a commit starts CI. CI runs the code in that commit, and it runs it with whatever your workflow can read. This is where pull_request and pull_request_target matter.
| Trigger | Secrets | Token |
|---|---|---|
pull_request from a fork |
none | read-only |
pull_request on a Dependabot branch |
Dependabot secrets only, not Actions secrets | read-only unless raised |
pull_request_target |
yes | can have write access |
Sources: GitHub Security Lab and GitHub's Dependabot troubleshooting page. GitHub's docs describe pull_request_target as a privileged trigger that "may have repository write access and access to referenced secrets", and say such workflows must not check out untrusted code. Security Lab calls the combination of pull_request_target and a checkout of the PR head a "pwn request" and recommends splitting the work: an unprivileged pull_request workflow builds and tests, then a workflow_run workflow does anything that needs privilege, treating the artifacts as data only.
Two things follow for agents.
First, a Dependabot-triggered run is treated like a fork run: no Actions secrets. But a push from your agent's bot account onto that branch starts a run triggered by a different actor. On our private test repository, that second run could see a secret the Dependabot run could not. We wrote up that test in the CI secrets problem with AI fixes, including what we did not test. Check your own repository before you assume.
Second, GitHub says events created with the built-in GITHUB_TOKEN do not start new workflow runs, with a few exceptions. If your agent pushes with GITHUB_TOKEN, CI may not run at all. Teams usually fix that with a GitHub App token or a personal token. That fix is where the new exposure comes from, so use a narrow token and decide on purpose which secrets the resulting run can reach.
Two defaults worth setting: make the default GITHUB_TOKEN read-only and raise it per job, as GitHub's secure-use guide recommends, and pass ${{ github.event.issue.title }} and similar values to shell steps through an environment variable, not inline.
MCP: when the agent acts as you
MCP, the Model Context Protocol, is an open standard for connecting AI applications to external systems: data sources, tools and workflows. A GitHub MCP server lets an agent read issues, open pull requests and search code. It does that with a token, either a personal access token or an OAuth login, and that token carries your permissions. That is the sense in which the agent acts as you. If you can read your private repositories, so can the agent.
What the MCP documentation and the GitHub server docs tell you to do:
- Minimise scopes. The MCP security guide describes a progressive model: start with low-risk read scopes and escalate only when a privileged operation needs it. It lists wildcard and "full-access" scopes as common mistakes.
- Limit the tools. The GitHub MCP server takes
--toolsetsand--toolsto choose what is exposed, and--read-onlyto skip write tools even when they are requested explicitly. - Know what lockdown mode is.
--lockdown-modehides content from public repositories written by people without push access. The server's own docs call it a best-effort content filter and not an authorization boundary. It does not change what the token can read or write. - Scope the token to the repository. Invariant's recommended fix for the private-repo leak is least privilege: restrict the agent to the repositories it needs. That limits what an injected instruction can reach, though it does not stop the injection itself.
Branch protection and the human merge
Branch protection decides whether a bad commit can reach your default branch. The settings that matter most here:
- Required status checks. The checks must pass before merge. This is how CI becomes the judge.
- Required reviews. A set number of approvals before merge.
- Code owners. Changes to files with a code owner need that owner's approval. Put
.github/, test config and lockfiles under owners, so an agent cannot quietly change the thing that judges it. - Dismiss stale approvals, and require approval of the most recent reviewable push by someone other than the pusher.
- Apply to administrators. By default rules do not bind admins or anyone with bypass permission. There is a separate option to include them.
For runs on fork PRs, GitHub can require a maintainer to approve the workflow run. Read changes under .github/workflows/ before you click.
A guarded loop for AI coding agent security
Put the pieces in order and you get a loop where the agent is never the judge of its own work.
Vet before the push, audit after the landing, and let your own CI and a person make the decisions.
Vetting happens before any push, and a refused change is never pushed. The push uses a token that can do only that. Your CI gives the verdict, and nothing is scored by a model. A second check reads what git recorded, not what the agent claimed. A person reads the diff and merges.
A checklist you can copy
Protected paths
- Deny edits to
.github/, CI config, git hooks, lockfiles, package-manager config and env files. - Put the same paths under CODEOWNERS.
- Refuse deleting or emptying a file.
Tests off limits
- Existing test files and test tooling (coverage thresholds, ignore lists) are read-only for the agent. New test files are fine.
CI as the only judge
- Require status checks on the default branch.
- Count a result as green only when every check on the exact pushed commit has finished and at least one really passed.
Least privilege
- Default
GITHUB_TOKENto read-only. - Use a token limited to the repositories and permissions the agent needs.
- No production secrets in workflows that run on agent pushes. Use environments with required reviewers for real credentials.
- Run MCP servers with
--read-onlyunless writes are required, and enable only the toolsets you use.
Untrusted input
- Tell the agent in its instructions that issue, PR and log text is data, not instructions. It helps, but OWASP is clear that it does not make injection impossible.
- Keep any agent that reads public issues away from private repositories and production secrets.
Human merge
- Require reviews, dismiss stale approvals, apply the rules to admins. The agent never merges.
Where Patchlit fits
Patchlit is a GitHub App that does one narrow job in this loop: when a Dependabot or Renovate PR fails CI, it reads the failing logs and pushes the smallest fix to the same branch, and your CI decides. It vets changes before the push, audits what landed, never edits existing tests or test tooling, declines when your workflows use secrets unless you opt in, and never merges. It also has limits. Its fix loop only edits files, so it cannot regenerate a lockfile or resolve a peer-dependency conflict, and it says so on the PR instead. (A separate merge-conflict tool, switched on per PR, can regenerate package-lock.json; the fix loop cannot.) The how it works and security pages list what it refuses to do and what it cannot catch.
FAQ
Can an AI coding agent be prompt-injected through a GitHub issue?
Yes, if it reads issue text and has tools that act on it. Invariant Labs showed this in May 2025 with the GitHub MCP server, and Aikido and Microsoft have reported similar problems in Actions workflows.
Is pull_request_target safe to use with an AI agent?
Only if the workflow never checks out or runs code from the pull request. It runs with secrets and possible write access. GitHub advises avoiding it unless you need that privilege.
Do Dependabot PRs get my repository secrets?
Not Actions secrets. GitHub treats Dependabot-triggered runs like fork runs: a read-only token and Dependabot secrets only. A later push by an agent starts a run with a different actor, so test what that run can see.
What can an MCP server do on my behalf?
Whatever its token allows. The agent calls the server's tools with a token that carries your permissions, so limit scopes, toolsets and writes.
Will an AI agent edit tests to make CI pass?
Sometimes. EvilGenie recorded hardcoded test cases and edited or deleted test files, at low rates on clear problems. Make tests and test config off limits.
Should an AI agent merge its own pull request?
No. Keep a person at the merge, with required reviews and status checks.
Sources
- OWASP: LLM01 Prompt Injection
- Invariant Labs: GitHub MCP exploited
- Aikido Security: PromptPwnd
- Microsoft Security Blog: Securing CI/CD in an agentic world, Claude Code GitHub Action case
- Meta AI: Agents Rule of Two
- EvilGenie: A Reward Hacking Benchmark (arXiv)
- GitHub Security Lab: Preventing pwn requests
- GitHub Docs: Secure use reference
- GitHub Docs: Troubleshooting Dependabot on GitHub Actions
- GitHub Docs: Using GITHUB_TOKEN
- GitHub Docs: Approving workflow runs from forks
- GitHub Docs: About protected branches
- Model Context Protocol: What is MCP
- Model Context Protocol: Security best practices
- github/github-mcp-server README
Tags: ai, security, github-actions, mcp