Skip to content
mentoor

Topic 4 of 5 · 5 min read

Permissions and guardrails for CI agents

Goal: Review a CI agent workflow and find the permission, trigger or secret exposure that lets untrusted input reach a privileged action.

Give a CI agent the smallest token it needs, never let untrusted text reach a privileged step, and keep a human between the agent and main. An agent that can write code, read secrets and push is as powerful as a developer, and it can be steered by anyone who can put text in front of it.

Check yourself · 1 of 3

Which workflow setting best limits the damage if a review bot is tricked by a crafted comment?

Choose one answer

Why it matters

The agent reads pull requests, issues and comments. Some of that text comes from strangers. If the workflow also holds a write token or a secret, a crafted comment becomes an attack. CI pipelines are a known target: OWASP lists poisoned pipeline execution and weak access controls among its top CI/CD risks.

What to check in a workflow

  1. Token permissions. Set permissions: explicitly and narrowly, for example contents: read and pull-requests: write. Do not leave the default broad grant.
  2. The trigger. pull_request from a fork runs without secrets. pull_request_target and issue_comment run with the base repo's secrets and token, so they must never check out and run the fork's code or feed its text into a shell.
  3. Secret exposure. Pass a secret only to the step that needs it. Never echo it, and keep it out of the agent's prompt and environment where possible.
  4. Script injection. Do not put ${{ github.event.pull_request.title }} directly in a run: line. Pass it through an environment variable.
  5. Protected branches. Require a human review and passing checks, and prevent the bot's token from bypassing them.
  6. Who can trigger it. Limit agent runs to collaborators.

Before and after

on: issue_comment
permissions: write-all
steps:
  - run: echo "${{ github.event.comment.body }}"

Anyone can comment, the token can write everything, and the comment is pasted into a shell. Fixed:

on: issue_comment
permissions:
  contents: read
  issues: write
jobs:
  triage:
    if: github.event.comment.author_association == 'MEMBER'
    steps:
      - env:
          BODY: ${{ github.event.comment.body }}
        run: ./triage.sh

What goes wrong

  • Pwn requests. pull_request_target plus a checkout of PR code runs attacker code with secrets.
  • Over-wide tokens. A review bot with contents: write can push to branches.
  • Auto-merge by the agent. No human sees the change before it ships.

Examples

The pwn request

A workflow runs on pull_request_target, checks out the PR's head commit and runs npm test. A stranger's PR edits package.json so the test script prints the repository token. The fix is to run untrusted code under pull_request, with no secrets, and to keep privileged steps separate.

Narrow token, narrow damage

A review bot only needs to read code and post comments. Setting contents: read and pull-requests: write means that even a fooled bot cannot push a commit.

Key points

  • Set permissions explicitly and narrowly instead of using a broad default token.
  • Triggers such as pull_request_target and issue_comment carry secrets, so they must not run untrusted code or text.
  • Pass untrusted values through environment variables, never straight into a shell command.
  • Branch protection with required human review stops an agent from merging its own work.
  • Restrict who can start an agent run, for example to repository members.

Common mistakes

  • Leaving permissions unset and trusting the default.
  • Using pull_request_target and then checking out the fork's code.
  • Interpolating ${{ ... }} event text directly into run: steps.
  • Letting a bot approve or merge its own pull request.

Check yourself · 2 of 3

Why is checking out a fork's code under pull_request_target dangerous?

Choose one answer

Check yourself · 3 of 3

A step runs: echo "${{ github.event.pull_request.title }}". What is the risk?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic