Hardening Workflows
A CI system holds the keys to production: it can push images, change infrastructure and deploy. Attacks on pipelines (compromised third-party actions, script injection through PR titles, poisoned pull_request_target workflows, stolen long-lived tokens) target exactly that. Permissions, OIDC & Pinning by SHA explains least-privilege tokens, OIDC and SHA pinning, and reproduces a script injection step by step. This lesson turns those rules into a checklist and into automated checks that run on your own workflow files.
The checklist
| # | Rule | Why |
|---|---|---|
| 1 | permissions: contents: read at the top; grant more per job |
a compromised step can't push code or releases |
| 2 | no ${{ github.event.* }} / ${{ inputs.* }} inside run:; pass through env: |
expressions are pasted into the script as code |
| 3 | third-party actions pinned to a full commit SHA, with a version comment | tags can be moved to malicious commits |
| 4 | no pull_request_target (or workflow_run) that checks out and runs PR code |
those triggers run with secrets and a write token |
| 5 | deployment secrets only in environments; cloud access through OIDC | PR and test jobs never hold production credentials |
| 6 | persist-credentials: false on checkout unless the job pushes |
the token isn't left in .git/config for later steps |
| 7 | self-hosted runners only for private repositories, ephemeral | PR code would run inside your network |
| 8 | Dependabot (or Renovate) for github-actions updates |
pinned SHAs still need updating |
| 9 | CODEOWNERS entry for .github/workflows/ |
workflow changes get a review from the platform team |
The dangerous pattern
# DON'T: a "thank the contributor" workflow that runs fork code with secrets
on: pull_request_target
jobs:
greet:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v7
with:
ref: ${{ github.event.pull_request.head.sha }} # the attacker's code...
- run: npm install && npm test # ...runs here, with secrets and a write token
- run: echo "Thanks for ${{ github.event.pull_request.title }}" # and the title is code tooBoth the checkout of the PR head and the title in run: hand control to whoever opens the pull request. Untrusted code belongs in a pull_request workflow (no secrets, read-only token); a pull_request_target workflow should only label, comment, or read metadata through env: variables.
Lint your workflows with Semgrep
Rules 2 and 3 are mechanical, so a scanner can enforce them on every pull request. Semgrep understands YAML; the two rules below are written for this lesson (dedicated tools such as actionlint and zizmor ship many more checks, see the Toolkit):
mkdir -p .github/workflows
cat > .github/workflows/greet.yml <<'YML'
name: greet
on: pull_request
jobs:
greet:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- run: echo "Thanks for ${{ github.event.pull_request.title }}"
- env:
TITLE: ${{ github.event.pull_request.title }}
run: echo "Thanks for $TITLE"
- uses: some-org/label-action@v3
YML
cat > workflow-rules.yml <<'YML'
rules:
- id: run-step-injection
languages: [yaml]
severity: ERROR
message: Untrusted github.event data is pasted into a run script; pass it through env and quote it
patterns:
- pattern-inside: "run: ..."
- pattern-regex: '\$\{\{\s*github\.event\.[^}]*\}\}'
- id: action-not-pinned
languages: [yaml]
severity: WARNING
message: Third-party action is not pinned to a full commit SHA
patterns:
- pattern: "uses: $USES"
- metavariable-regex:
metavariable: $USES
regex: '^(?!actions/)[^@]+@(?![0-9a-f]{40}$).+'
YML
semgrep scan --config workflow-rules.yml --metrics=off --quiet --error .github
echo "exit $?"
┌─────────────────┐
│ 2 Code Findings │
└─────────────────┘
.github/workflows/greet.yml
❯❯❱ run-step-injection
❰❰ Blocking ❱❱
Untrusted github.event data is pasted into a run script;
pass it through env and quote it
8┆ - run: echo "Thanks for ${{
github.event.pull_request.title }}"
❯❱ action-not-pinned
❰❰ Blocking ❱❱
Third-party action is not pinned to a full commit SHA
12┆ - uses: some-org/label-action@v3
exit 1
Two findings, and the safe env: step was not flagged: the rule looks for the expression inside a run: value only. --error makes Semgrep exit non-zero when anything is found, which turns it into a CI gate. Semgrep itself is the subject of SAST with Semgrep; this is the same tool pointed at pipeline code instead of application code.
Policy at the organization level
Repository rules are only as good as the repositories that adopt them. Organization settings make some rules global:
- Allowed actions: only GitHub-authored actions, verified creators, or an explicit allow-list (
aquasecurity/trivy-action@*,docker/*); optionally require every action to be pinned to a full SHA. - Default workflow permissions: read-only
GITHUB_TOKENfor all repositories. - Fork PR approvals: require approval for all outside collaborators, not just first-time ones.
- Rulesets requiring the security workflow to pass before merging, across all repositories.
Test yourself
Why is checking out github.event.pull_request.head.sha in a pull_request_target workflow dangerous?
- It's slow
- That trigger runs with secrets and a write token, and the checked-out code is controlled by the PR author
- The SHA may not exist
- It disables caching
In the session, why wasn't the step with env: TITLE: ${{ github.event.pull_request.title }} flagged?
- Semgrep missed it
- The expression is in env, where it becomes data; the run script only references $TITLE
- env is never evaluated
- The rule only checks the first step
What does --error add to a Semgrep scan in CI?
- Colour
- A non-zero exit code when findings exist, so the job fails
- More rules
- JSON output