Sub topic 1
Triggers, tokens, secrets and who is trusted
Prerequisites
This chapter assumes you can read a workflow file and know these basics:
- YAML syntax.
- Workflow structure: events, jobs and steps.
- How forks and pull requests work on GitHub.
- Simple shell scripting.
The trust question
A GitHub Actions workflow is code that runs on a machine you pay for. It often holds credentials. Every security question about Actions reduces to one: which parts of this run did a trusted person write, and which did an outsider write?
Three things decide the answer.
- The trigger. The event that starts the run decides which code and which credentials are involved.
- The
GITHUB_TOKEN. This is a short-lived token that GitHub creates for each run. It lets the workflow call the GitHub API as the repository. - Secrets. These are values stored in repository, environment or organization settings, such as deploy keys and cloud credentials.
Triggers set the context
Some triggers run in the context of the contributor's change. Others run in the context of your base repository. The base context is more trusted because the workflow file comes from your default branch.
| Trigger | Workflow file comes from | Token | Secrets |
|---|---|---|---|
| `push` to your repository | The pushed branch | Per your defaults | Available |
| `pull_request` from a fork | The merge result of the PR | Read-only | Not available |
| `pull_request_target` | The base branch | Can have write access | Available |
| `issues`, `issue_comment` | The default branch | Per your defaults | Available |
The last two rows are the risky ones. They run with real privileges, yet they react to events that anyone can create. Anyone with a GitHub account can open an issue, post a comment, or open a pull request from a fork.
What the token can do
Every run gets a GITHUB_TOKEN. Its default permissions come from a repository or organization setting, which is either permissive (read and write) or restricted (read-only). Older repositories often still have the permissive default.
A workflow can narrow the token with a permissions block. Any permission you leave out is set to none once the block exists.
name: ci
on: pull_request
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "This job can only read repository contents"A compromised step can use whatever the token allows. A token with contents: write can push commits. A token with pull-requests: write can approve or comment on pull requests.
Secrets and forks
Secrets are masked in logs, but masking does not protect them from code that runs in the job. A script can read a secret from its environment and send it anywhere on the network.
GitHub's main protection is that workflows triggered by pull_request from a fork do not receive repository secrets. Dependabot pull requests are handled the same way. Workflows triggered by pull_request_target do receive secrets, because the trigger assumes the workflow code is yours.
That assumption holds only while the workflow never runs contributor-controlled code or text with those privileges. The next chapters show how two patterns break it:
- Script injection. Attacker text is expanded into a script.
- Pwn requests. Fork code is checked out and run under
pull_request_target.
Working rule
For each workflow, ask three questions:
- Who can trigger it?
- Which token permissions and secrets does it get?
- Which values in it did an outsider write?
If the first answer is "anyone" and the second includes write access or secrets, the third answer must be "nothing that gets executed".
Exercise
A workflow uses on: pull_request_target, has no permissions block, and your repository uses the permissive default token setting. A stranger opens a pull request from a fork. Name two privileges the run has that a pull_request run from the same fork would not have.
Show answer
The run can read the repository's secrets, and its GITHUB_TOKEN has write permissions because of the permissive default. A pull_request run from a fork gets a read-only token and no secrets.