# Pipeline Security

Course: [CI/CD](https://www.devtutorials.ai/learn/cicd) · Section: Continuous Integration
Source: https://www.devtutorials.ai/learn/cicd/continuous-integration/pipeline-security

> Close four attack paths in GitHub Actions: pin actions to commit SHAs, use a read-only token, keep fork code away from secrets, and stop checkout leaving the token on disk. With the Azure Pipelines equivalents.

## Pipeline Security

Your pipeline can read your code, use your secrets and deploy to production.

That makes it a target.

In this lesson you close four doors an attacker can use:

- **tag swap:** a borrowed action is replaced with bad code.
- **token abuse:** the pipeline's token can do too much.
- **fork pull request:** a stranger's code runs with your secrets.
- **token left on disk:** a later step steals the token.

**Simple lesson:** give the pipeline only what it needs, and trust nothing that can change under you.

### A Real-World Example

Think about the keys to a school.

- The **watchman** needs a key to the gate. Not to the principal's safe.
- A **visitor** gets a pass for one room, for one hour.
- A **lock you can open with any key** is no lock.

Now map it:

- **Watchman's limited key** = a read-only token.
- **One-hour visitor pass** = a short-lived token.
- **Any-key lock** = an action pinned by a tag that anyone with access can move.

**Simple lesson:** small keys, short passes, and locks that cannot be swapped.

## A Real Attack: tj-actions/changed-files

In March 2025, a popular GitHub action was attacked.

- **What:** `tj-actions/changed-files`, used by thousands of repositories.
- **How:** the attacker moved its version tags to their own commit.
- **Result:** workflows that used `@v45` ran the bad code. It printed secrets into the build logs.
- **Who was safe:** workflows pinned to an older commit SHA.

It was tracked as CVE-2025-30066.

**Simple lesson:** a version tag is a sticky note. Anyone who controls the repo can move it.

## Door 1: Pin Actions to a Commit SHA

- A tag like `@v45` can be moved.
- A **commit SHA** is 40 characters and cannot be moved.
- Write the version as a comment, so people can still read it.

```yaml
# Movable: avoid for other people's actions
- uses: tj-actions/changed-files@v45

# Pinned: safe
- uses: tj-actions/changed-files@a284dc1814e3fd07f2e34267fc8f81227ed29fb8 # v45.0.9
```

- Pins go out of date. Let Dependabot or Renovate open pull requests to update them.
- Each update is a normal pull request, so it gets reviewed and tested.

**Simple lesson:** pin to a SHA, and let a bot keep the pins fresh.

## Door 2: A Read-Only Token

Every GitHub run gets a token called `GITHUB_TOKEN`.

- Older repositories gave it write access by default.
- A bad package that runs during install can use that token.
- With write access, it can push code to main.

Fix: set the smallest permissions at the top of the file.

- Most CI only needs `contents: read`.
- Give more only to the one job that needs it, like `id-token: write` for a deploy.

**Simple lesson:** start with read-only. Add a permission only where a job proves it needs one.

## Door 3: Fork Pull Requests

**Fork** = a copy of your repository owned by someone else.

- Anyone can open a pull request from a fork.
- `pull_request` runs fork code **without** your secrets. Safe.
- `pull_request_target` runs **with** your secrets.
- If it also checks out the fork's code, a stranger's code gets your secrets.

Use `pull_request_target` only if you never run the fork's code in that job. If you are not sure, use `pull_request`.

**Simple lesson:** stranger's code never meets your secrets.

## Door 4: Do Not Leave the Token Behind

- `actions/checkout` saves the token in `.git/config` by default.
- Every later step can read it, including steps from other people's actions.
- Most jobs never need it after checkout.

**Simple lesson:** if a job does not push, set `persist-credentials: false`.

## Close Every Door

Now fix all four at once. The attacker report in the lab shows which doors are still open.

**Real tools vs this lab:** the attacker in this lab is simulated. It tries the same four paths real attacks have used. The fixes are exactly what you write in real workflow files.

### Azure Pipelines Too

The same ideas, with Azure's settings:

| Door | Azure Pipelines |
|---|---|
| Moving versions | Pin templates from other repos with `ref:` to a commit or tag you control |
| Token power | Turn on "Limit job authorization scope to current project" |
| Fork pull requests | Keep "Make secrets available to builds of forks" turned off (the default) |
| Token left on disk | `persistCredentials` is off by default. Leave it off |

Look for files under `.github/workflows/` and `azure-pipelines.yml` in every repo you own. Add them to CODEOWNERS, so changes need a review from your team.

**Simple lesson:** the same four ideas work on every platform. Only the setting names change.

1. In your starter repo, open **Settings → Actions → General**.
2. Set **Workflow permissions** to read-only.
3. Check that every `uses:` line in your workflow ends in a 40-letter SHA.

**What you should see:** runs still green, because each job asks only for the permissions it needs.

## Quick Check

## Cheat Sheet

A hardened GitHub Actions workflow:

```yaml
name: CI
on:
  push:
    branches: [main]
  pull_request:

permissions:
  contents: read

jobs:
  build-and-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          persist-credentials: false
      - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
        with:
          node-version: 22
      - run: npm ci
      - run: npm test
```

| Door | Unsafe | Safe |
|---|---|---|
| Tag swap | `@v45` | `@<40-char SHA> # v45.0.6` |
| Token abuse | no `permissions`, or `write-all` | `permissions: contents: read` |
| Fork pull request | `pull_request_target` + checkout of the fork | `pull_request` |
| Token on disk | checkout defaults | `persist-credentials: false` |

## Common mistakes

| Mistake | Wrong idea | Correct idea | What goes wrong |
|---|---|---|---|
| Pinning by tag | A version tag never changes | Tags can be moved | Bad code runs with your secrets |
| `permissions: write-all` | It fixes the error | Give one job one permission | Any step can push to main |
| `pull_request_target` for tests | It needs secrets to test | Tests should not need secrets | Strangers read your secrets |
| Pins never updated | Pinned means done | A bot updates them | Known bugs stay forever |
| No review on workflow files | They are just config | They hold the keys | Anyone can weaken the gates |

## What to Remember

- Pin actions to a full commit SHA. Let a bot update the pins.
- Start with `contents: read`. Add one permission where needed.
- `pull_request`, not `pull_request_target`, for fork code.
- `persist-credentials: false` unless the job pushes.
- Pipeline files need a reviewer, like any code.

## Exercises

Next: **Environments and Secrets**
