Sub topic 1
Triggers, tokens, secrets and who is trusted
Every GitHub Actions security question comes down to two things. One is what the workflow can do. The other is who controls the data it reads. This chapter covers the first: the triggers that start a workflow, the GITHUB_TOKEN and secrets a job receives, and how trust differs by trigger. Later chapters build on this to explain script injection and pull_request_target mistakes.
Prerequisites
This chapter assumes you:
- can read basic YAML (mappings, lists, multi-line strings),
- know what a repository, fork and pull request are on GitHub,
- have seen a workflow file with
on:,jobs:andsteps:, - can read a short shell command such as
echo "hello $NAME".
You do not need any security background.
The three things a job receives
When a workflow job starts, it gets three kinds of power. Each is set by the trigger and the repository settings.
- Code. The workflow file itself, and whatever the job checks out.
- A token.
GITHUB_TOKENis a short-lived credential that GitHub creates for each job. It lets the job call the GitHub API as thegithub-actionsapp, for example to comment on an issue or push a commit. - Secrets. Values stored in repository, environment or organization settings, such as a deploy key or a package registry password.
The token expires when the job ends (and never lasts longer than 24 hours). Secrets last until someone rotates them. A stolen secret is therefore usually worse than a stolen token, though a write-capable token can still do real damage while the job runs.
Triggers decide the privilege level
The on: key picks which event starts the workflow. GitHub chooses the workflow file version, the token permissions and the secret access based on that event. The table below covers the triggers you will meet most often in this tutorial.
| Trigger | Which workflow file runs | Token (default) | Secrets available | Who can cause it |
|---|---|---|---|---|
| `push` | The file on the pushed branch | Repository default | Yes | Someone with write access |
| `pull_request` (same repository) | The merge result of the PR | Repository default | Yes | Someone with write access |
| `pull_request` (from a fork) | The merge result of the PR | Read-only | No (except the token) | Anyone who can open a PR |
| `pull_request_target` | The file on the PR's base branch | Repository default, can be write | Yes | Anyone who can open a PR |
| `issues`, `issue_comment` | The file on the default branch | Repository default | Yes | Anyone who can open an issue or comment |
| `workflow_run` | The file on the default branch | Repository default | Yes | Indirectly, by any run it watches |
Two rows deserve attention.
The fork row for pull_request is the safe baseline. GitHub assumes a fork's code is untrusted, so it removes secrets and makes the token read-only. A malicious PR can still run code on a runner (a virtual machine that executes the job), but it holds almost nothing worth stealing.
The pull_request_target row is the opposite. The name suggests a variant of pull_request, and in one respect it is: it also fires when a PR is opened. But it runs in the context of the base repository, with secrets and a possibly writable token. GitHub designed it so maintainers can do trusted work for outside contributions, such as adding labels or posting comments. It is safe only if the workflow never runs code the contributor controls. Chapters later in this tutorial cover how that goes wrong.
The issues and issue_comment triggers matter for the same reason. They run with base repository privileges, yet anyone with a GitHub account can create the event that starts them.
Who is trusted
The useful question for each workflow is not "is this workflow secure?" but "which parts of this run does an outsider control, and what privileges does the run have?" You can sort the inputs into two groups.
- Trusted: the workflow file on the default branch, secrets you configured, and code that has been reviewed and merged.
- Untrusted: anything an outside user can write. This includes issue titles and bodies, PR titles and descriptions, comment text, branch names, commit messages, and the files in a fork's PR.
Danger appears when untrusted data meets a privileged context. A fork PR running under pull_request has no privileges, so untrusted data is mostly harmless. An issue title read by an issues workflow with a write token is untrusted data in a privileged context, and that is where the later chapters concentrate.
Controlling the token with `permissions`
The token's rights come from two places: the repository (or organization) default setting, and the permissions: key in the workflow.
Repositories created before February 2023 may default to read and write access for every scope. Newer repositories default to read-only access to contents and packages. Check yours under Settings, Actions, General, "Workflow permissions". Do not assume which default applies.
The permissions: key overrides the default. If you set it, every scope you do not list becomes none. This makes it a good tool for least privilege: grant only what the job needs.
name: label-new-issues
on:
issues:
types: [opened]
permissions:
issues: write
jobs:
label:
runs-on: ubuntu-latest
steps:
- name: Add a label
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ISSUE_URL: ${{ github.event.issue.html_url }}
run: gh issue edit "$ISSUE_URL" --add-label "needs-triage"This workflow can edit issues and nothing else. It cannot push code, because contents is none. If an attacker managed to run commands in this job, they could only change issues, not rewrite the repository.
Notice how the issue URL reaches the shell: through env: and $ISSUE_URL, not by writing ${{ ... }} inside the script. The next chapters explain why that matters. For now, treat it as the pattern to copy.
You can also set permissions: at the top of the file (applies to all jobs) or inside a single job. A job-level block replaces the workflow-level one for that job.
permissions: {} # no access for any job by default
jobs:
report:
runs-on: ubuntu-latest
permissions:
pull-requests: write # only this job gets this scope
steps:
- run: echo "Job-level permissions override the workflow level"Starting from permissions: {} and adding scopes per job is a strict, easy-to-audit style.
How secrets reach a job
Secrets are not loaded into every job automatically. A step receives a secret only when you reference it with ${{ secrets.NAME }}, usually in env:. GitHub masks secret values in logs, but masking is a convenience, not a guarantee. A script can print a secret in a transformed form (for example, base64-encoded) that the masker does not recognize.
Three rules limit exposure:
- Give a secret only to the step that needs it, rather than to the whole job.
- Store deployment secrets in an environment with required reviewers, so a job cannot read them until a human approves.
- Never expose secrets to workflows that run code from a fork.
Fork pull requests and approval
For pull_request workflows from forks, GitHub can require a maintainer to approve the run first. This setting is under Settings, Actions, General, "Fork pull request workflows". It reduces abuse such as using your runners for cryptocurrency mining. It is not a substitute for the privilege rules above. A maintainer approving a run is deciding that the code may run, so they must read the diff first. A later chapter returns to this setting along with protected environments.
Putting it together
Use this checklist when you open any workflow:
- Find the trigger. Is it one an outsider can cause?
- Find the token scopes. Does the workflow set
permissions:, or does it inherit the default? - Find the secrets. Which steps reference them?
- Find the untrusted inputs. Which
github.event.*values reach a script? - Check whether the job checks out or runs code from a pull request.
If the answers to 1 and 4 are "yes" and the token or secrets are powerful, you have found something to fix. The rest of the tutorial shows how.
Exercise
A repository has a workflow triggered by pull_request_target that adds a label to each new PR. The workflow has no permissions: block, and the repository was created in 2021. A fork PR arrives. What token rights and secrets should you assume the job has, and what is the first change you would make?
Show answer
Assume the job runs in the base repository context with all repository secrets available and a token that may have read and write access to every scope, because the repository predates the read-only default and the workflow does not restrict it. Check Settings, Actions, General to confirm. The first change is to add a permissions: block granting only pull-requests: write (and contents: read only if the job truly needs it), so the token cannot do more than label PRs.
Exercise
Why is the same fork PR much less dangerous under pull_request than under pull_request_target?
Show answer
Under pull_request, GitHub gives fork runs a read-only token and withholds repository secrets, so even if the PR's code is malicious, there is little to steal or change. Under pull_request_target, the job runs in the base repository context with secrets and a possibly writable token, so any attacker-controlled code or data that reaches a script gains those privileges.
Exercise
Sort these into trusted or untrusted inputs for a workflow on the default branch: the workflow file on main, an issue title, a branch name in a fork PR, a repository secret.
Show answer
Trusted: the workflow file on main and the repository secret. Untrusted: the issue title and the fork's branch name, because outside users can set both.