Skip to content
mentoor

Topic 3 of 5 · 5 min read

CI workflow changes and pipeline security

Goal: Spot the insecure trigger, permission or unpinned action in an AI-written CI workflow and correct it.

A CI workflow runs code with your secrets, so every line that widens its permissions or trusts outside input is a security change.

Check yourself · 1 of 3

Why is pull_request_target risky when combined with checking out the pull request's code?

Choose one answer

Why it matters

CI is a machine that can push images, deploy and read secrets. An agent asked to "make CI faster" or "run tests on forks" will often copy a snippet from the web. A single risky trigger can let a stranger's pull request read your secrets. The GitHub documentation on secure use of Actions covers these cases; the examples here use GitHub Actions, but the ideas apply to GitLab CI and others.

What to check

  1. Triggers. pull_request_target runs with secrets and a write token in the context of the base repository. If the workflow also checks out and runs the pull request's code, untrusted code gets your secrets. Plain pull_request from a fork gets no secrets, which is safer.
  2. Token permissions. Set permissions: explicitly and as low as possible, for example contents: read. A workflow with no block may get broad defaults.
  3. Unpinned actions. uses: some-org/action@main or @v1 can change under you. Pin third-party actions to a full commit hash.
  4. Secrets in the wrong places. Secrets echoed to logs, passed to steps that do not need them, or exposed to a step that runs code from a pull request.
  5. Script injection. Putting ${{ github.event.pull_request.title }} straight into a run: step lets the title execute as shell. Pass it through an environment variable instead.
  6. Deploy gates. Did the agent remove a required approval, a protected environment, or a failing test step "to make CI green"?

Before and after

on: pull_request_target
jobs:
  test:
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha }}
      - run: npm test

This checks out the fork's code in a context that holds secrets, then runs it. The fix is to use pull_request, add permissions: contents: read, and keep any secret-using job separate from code you did not write.

What goes wrong

  • The agent makes a red pipeline green by skipping or deleting the failing step.
  • A new action from an unknown publisher is added with a moving tag.
  • A workflow gets permissions: write-all because "it was failing on permissions".

Examples

Green by deleting

The pipeline fails on a flaky integration test. The agent's diff removes the npm run test:integration step and the PR says "CI now passes". The check is green because the check was deleted. Ask for the fix of the test or a clearly tracked skip.

Title injection

A step runs echo "New PR: ${{ github.event.pull_request.title }}". A pull request titled with a shell command in quotes runs that command on the runner. Pass the title as an environment variable and quote it in the script.

Key points

  • Treat edits to workflow files as security changes, even when they look like speed-ups.
  • pull_request_target plus checkout of the pull request's code puts secrets within reach of untrusted code.
  • Declare minimal permissions for the token in every workflow.
  • Pin third-party actions to a full commit hash, not a moving tag or branch.
  • Never put untrusted text such as a PR title directly into a run step.

Common mistakes

  • Reviewing the application diff and waving through the .github folder.
  • Accepting write-all permissions to silence a permission error.
  • Pinning to a version tag and assuming it never moves.
  • Counting a green pipeline as proof when the agent edited the pipeline.

Check yourself · 2 of 3

Which is the safest way to reference a third-party action?

Choose one answer

Check yourself · 3 of 3

An agent fixes a failing CI job by deleting the failing test step. What is the right response?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic