AI coding agents now open pull requests on their own. That changes CI/CD security in one important way: your pipeline is now running code that no human wrote and few humans have read, triggered by an account that can be steered by text. The fix is not a new tool. It is a pipeline where GitHub Actions permissions are minimal by default, secrets never sit next to untrusted code, every change passes automated security gates, and a person approves anything that reaches production.
This guide gives you a reference DevSecOps pipeline for agent-authored pull requests, the GitHub Actions permissions and secrets settings that matter most, a complete workflow you can adapt, and what to do when something goes wrong.

Why agent-authored pull requests change CI/CD security
In a human workflow, three things are true that we rarely say out loud: the author understood the change, the author was a known team member, and the author was not taking instructions from the pull request itself. With coding agents, none of these is guaranteed.
- Volume: agents can open many pull requests quickly, so anything that runs on every pull request runs far more often.
- Provenance: the code was generated, not written, and may include patterns no one on the team would have chosen.
- Steerability: agents read issues, comments and documentation, so their output can be influenced by anyone who can write those. Our guide to prompt injection in GitHub covers that risk in depth.
CI/CD security for AI coding agents therefore comes down to one principle: the pipeline must be safe even when the pull request is hostile.
A reference DevSecOps pipeline for AI coding agents
A DevSecOps pipeline for agent-authored changes has five stages, and the most important design decision is the line between them.
- Branch: the agent pushes a branch and opens a pull request. It never pushes to the default branch.
- Unprivileged CI: tests, linters and security scanners run on the
pull_requesttrigger with a read-only token and no deployment secrets. - Independent review: an automated code review and, for anything risky, a human reviewer look at the diff.
- Human approval: branch protection requires approvals and passing checks before merge, and CODEOWNERS force the right people to look at sensitive paths.
- Trusted deploy: a separate workflow, triggered only by a merge to the default branch, deploys using short-lived credentials and a protected environment.
Untrusted work happens in stages 1 to 3. Privileged work happens only in stage 5. Nothing crosses that line without a person approving it in stage 4.
GitHub Actions permissions: start from zero
Every workflow gets a GITHUB_TOKEN. GitHub's own security hardening guide recommends setting the default permission for that token to read-only and raising it only where a job needs more.
Do this in two places:
- Repository or organisation settings: set "Workflow permissions" to read repository contents only, and keep "Allow GitHub Actions to create and approve pull requests" switched off unless you have a specific need.
- Every workflow file: declare
permissions: {}at the top, then grant each job exactly what it needs.

Typical minimums:
- Run tests and linters:
contents: read. - Post review comments:
contents: read,pull-requests: write. - Upload code scanning results:
contents: read,security-events: write. - Deploy to a cloud with OIDC:
contents: read,id-token: write. - Create a release:
contents: write, only in the release job.
GitHub Actions secrets: keep them away from untrusted code
Secrets are the prize in almost every CI/CD attack. Four rules keep them out of reach.
1. No deployment secrets in pull request jobs
Jobs triggered by pull_request run code from the pull request. Give them nothing beyond what a test run needs. GitHub already withholds repository secrets from pull requests opened from forks; apply the same rule to your own agents' branches by keeping deploy credentials in protected environments that pull request jobs cannot use.
2. Use environments with required reviewers
Put production credentials in a GitHub environment, such as production, with required reviewers and a rule that only the default branch can deploy to it. A job can only read those secrets after a named person approves the deployment.
3. Prefer OIDC over long-lived cloud keys
GitHub's guidance is to authenticate to your cloud provider with OpenID Connect so you can "stop storing these credentials as long-lived secrets." The workflow requests a short-lived token for one run, scoped to one role. There is no static key to steal, rotate or leak in a log.
4. Treat every derived value as a secret
If a step turns a secret into something else, such as a signed URL or a session token, register that value as a secret too so it is masked in logs.

Supply chain: pin what your pipeline runs
Your pipeline also runs other people's code: every third-party action. In March 2025 the popular tj-actions/changed-files action was compromised. Attackers rewrote its existing version tags to point at malicious code that printed CI secrets into workflow logs, and over 23,000 repositories used the action. Repositories that pinned the action to a full commit SHA were not affected by the moved tags.
- Pin every third-party action to a full-length commit SHA, with the version in a comment for readability.
- Let Dependabot propose updates to those pins, so pinning does not mean never updating.
- Prefer fewer actions. A short shell step you can read is often safer than a dependency you cannot.
Automated security and quality gates
No single scanner catches everything, so a DevSecOps pipeline layers them. Each of these runs on the unprivileged pull_request trigger.
- Secret scanning with push protection: blocks commits that contain known credential formats before they ever reach the repository.
- Dependency review: flags new dependencies with known vulnerabilities or unexpected licences in the pull request.
- Static analysis (SAST): CodeQL or a similar engine finds injection and data-flow problems.
- Tests: unit and integration tests, including failure paths. See our code review checklist for AI-generated code for what good AI-era tests look like.
- Contextual code review: an AI reviewer reads the diff with repository context and flags problems scanners are not built to see, such as missing authorization checks, broken contracts between modules, and risky changes to workflow files themselves.
Human approval and deployment boundaries
Automation decides what is obviously wrong. People decide what is acceptable.
- Branch protection or rulesets on the default branch: require pull requests, at least one approval from someone other than the author, passing status checks, and up-to-date branches.
- CODEOWNERS for
.github/workflows/, infrastructure code, authentication, payments and agent instruction files, so changes there always reach a named owner. - Agents never approve. An approval must come from a person who is not the account that opened the pull request.
- Production deploys need an environment approval, separate from the code review, so shipping is a deliberate decision.
Example GitHub Actions workflows
Here is a minimal, security-conscious pair of workflows. Replace the placeholders, marked with angle brackets, with your own values and real commit SHAs.

name: ci
on:
pull_request:
permissions: {}
jobs:
test:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@<full-commit-sha> # v4
with:
persist-credentials: false
- uses: actions/setup-node@<full-commit-sha> # v4
with:
node-version: 22
- run: npm ci
- run: npm test
dependency-review:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/dependency-review-action@<full-commit-sha> # v4name: deploy
on:
push:
branches: [main]
permissions: {}
jobs:
deploy:
runs-on: ubuntu-latest
environment: production # required reviewers + main-only rule
permissions:
contents: read
id-token: write # OIDC, no stored cloud keys
steps:
- uses: actions/checkout@<full-commit-sha> # v4
- uses: aws-actions/configure-aws-credentials@<full-commit-sha> # v4
with:
role-to-assume: arn:aws:iam::<account-id>:role/<deploy-role>
aws-region: <region>
- run: ./scripts/deploy.shTwo details matter most. The CI workflow can read code but cannot write to the repository or reach any deploy credential. The deploy workflow only runs after a merge to main, only after a person approves the production environment, and authenticates with a short-lived OIDC token instead of a stored key.
Failure scenarios and rollback
Plan for these before they happen.
- A check fails on an agent's pull request: the pull request cannot merge. Have the agent or its owner fix it; never add a bypass for agent accounts.
- A compromised dependency or action: remove it, check workflow logs for printed secrets, rotate anything that may have been exposed, and review recent runs that used it.
- An unexpected change merged: revert first and investigate second. Keep deploys small enough that a revert is cheap.
- A bad deploy: roll back to the previous release artifact, not by re-running the pipeline on a hurried fix. Then add the missing check so it cannot happen the same way twice.
Integrating Mesrai into the pipeline
Mesrai sits in stage 3 of the pipeline, the independent review, and works the same for agent-authored and human pull requests.
- Review comments on every pull request. Mesrai installs as a GitHub App and posts its review as comments on the pull request. It does not need your deployment secrets, so it fits on the untrusted side of the pipeline.
- Rules for pipeline risks. With Mesrai Rules you can describe your own pipeline policies in plain English, such as "flag workflows without an explicit permissions block", "flag third-party actions not pinned to a commit SHA", or "flag any use of pull_request_target", and every pull request is checked against them.
- Review in CI or before the push. The Mesrai CLI runs the same review on a developer machine or as a CI step, so agent output can be checked before a branch is even pushed.
- Self-hosted if you need it. Teams that cannot send code to a hosted service can run Mesrai on their own infrastructure. See Self-Hosting Mesrai.
CI/CD security checklist for AI coding agents
- Default
GITHUB_TOKENpermission is read-only, and every workflow declarespermissions: {}. - Jobs that run pull request code have no write permissions and no deployment secrets.
- Production credentials live in an environment with required reviewers and a main-only deploy rule.
- Cloud access uses OIDC, not stored long-lived keys.
- Every third-party action is pinned to a full commit SHA, with Dependabot proposing updates.
- Secret scanning with push protection, dependency review, static analysis and tests run on every pull request.
- An independent review runs on every pull request, including agent-authored ones.
- Branch protection requires a human approval from someone other than the author.
- CODEOWNERS cover workflows, infrastructure, auth and agent instruction files.
- A written rollback plan exists, and secrets are rotated after any suspected exposure.
Frequently asked questions
What are the most important GitHub Actions permissions to set?
Set the default GITHUB_TOKEN permission to read-only for the repository or organisation, add permissions: {} at the top of every workflow, and grant each job only the scopes it needs, such as pull-requests: write for review comments or id-token: write for OIDC.
How should I store GitHub Actions secrets?
Store them as encrypted secrets, never in workflow files. Put production secrets in a protected environment with required reviewers, keep them out of any job triggered by pull requests, and replace long-lived cloud keys with OIDC wherever your provider supports it.
What is a DevSecOps pipeline?
A delivery pipeline where security checks are built into every stage rather than added at the end: secret scanning and dependency checks on every change, static analysis and tests in CI, review before merge, and protected, approved deployments.
Should AI coding agents be allowed to merge their own pull requests?
No. An agent can open and update pull requests, but approval and merge should require a person who is not the account that authored the change. Keep "Allow GitHub Actions to create and approve pull requests" off unless you have a specific, reviewed need.
Why pin GitHub Actions to a commit SHA?
Version tags can be moved to point at different code, as the 2025 tj-actions incident showed. A full commit SHA cannot be changed, so the code you reviewed is the code that runs.
Sources
- Security hardening for GitHub Actions, GitHub Docs
- GitHub Action tj-actions/changed-files supply chain attack (CVE-2025-30066), Wiz
Want an independent review stage on every agent-authored pull request? Try Mesrai on your repositories.