Sub topic 1
Triggers, Tokens, Secrets, and Who Is Trusted
Prerequisites
Assume you know basic YAML, GitHub repositories, pull requests, forks, and GitHub Actions jobs and steps. You should also know basic shell quoting.
Triggers, Tokens, Secrets, and Who Is Trusted
A workflow trigger is an event that starts a workflow. The trigger also affects the security context in which the workflow runs.
GITHUB_TOKEN is a temporary token that lets a workflow call GitHub’s API. Its permissions may include reading code, writing issues, or creating pull requests. Secrets are sensitive values, such as deployment keys, exposed to selected workflow jobs.
A pull request from a fork is controlled partly by an outside contributor. Treat its title, body, branch name, commit message, and changed files as untrusted. With pull_request, GitHub normally uses the pull request’s merge result and gives fork workflows a restricted token without repository secrets. With pull_request_target, the workflow runs in the base repository context. It may receive secrets and a write-capable token, so it must not execute code from the pull request.
Which Event Data an Attacker Controls
An outside contributor can choose values such as:
- An issue or pull request title and body
- A source branch name
- A commit message
- Labels or other fields they can influence through the event
These values are data, but a workflow can accidentally turn them into code.
How Expressions Are Expanded Before the Shell Runs
GitHub expands ${{ }} expressions while preparing a step. The resulting text becomes part of the command script. The shell parses that script afterward. Shell quotes written around the expression do not protect against syntax inserted during expansion.
Script Injection in Run Steps
This step is unsafe because the title is inserted into shell source:
- run: echo "New issue: ${{ github.event.issue.title }}"A title containing shell syntax can close the quoted string and add another command. Pass untrusted data through an environment variable instead. The shell then expands the variable as a value:
- env:
ISSUE_TITLE: ${{ github.event.issue.title }}
run: echo "New issue: $ISSUE_TITLE"Use least privilege even after fixing injection:
permissions:
contents: readExercise
Why can quotes around ${{ github.event.issue.title }} fail to prevent shell injection?
Show answer
GitHub expands the expression before the shell parses the command, so attacker-controlled text can become part of the shell source.