Sub topic 2
Which event data an attacker controls
This chapter assumes you can read a workflow file (events, jobs, steps) and write basic shell. It also assumes you know what a fork and a pull request are. The previous chapter covered triggers, GITHUB_TOKEN and secrets. This chapter answers a narrower question: which values in the workflow context can a person without write access set? Later chapters show how those values become code execution. Here you learn to spot them.
The core question: who can write this string?
Every workflow run starts with an event. GitHub builds a JSON payload for it and exposes it as github.event. It also exposes derived values such as github.head_ref. Some fields are produced by GitHub, such as IDs, timestamps and SHA hashes. Others are copied from text a person typed.
For each value you use in a workflow, ask one question: could someone who is not a maintainer have chosen the exact characters in this value? If so, treat the value as attacker-controlled. The rest of the tutorial assumes this rule.
Two points make the rule more useful than it first looks:
- The trigger decides who can create the event. On a public repository, anyone can open an issue, comment on one, or open a pull request from a fork. Pushing a branch needs write access.
- Free text is the danger, not the field's category. An issue number is a GitHub-assigned integer. An issue title is a string of up to 256 characters, and every character is the author's choice.
Fields an attacker controls
The table lists the values that most often appear in real workflows. It is not exhaustive. Any field that holds free text from a user belongs in the same group.
| Context value | Set by | Notes |
|---|---|---|
| `github.event.issue.title`, `.body` | Issue author | Any GitHub user on a public repo. |
| `github.event.pull_request.title`, `.body` | PR author | Includes fork authors. |
| `github.event.pull_request.head.ref` | PR author | The branch name in the fork. |
| `github.event.pull_request.head.label` | PR author | Looks like `owner:branch`. |
| `github.head_ref` | PR author | Same branch name, as a shortcut. |
| `github.event.comment.body` | Commenter | Issue, PR and commit comments. |
| `github.event.review.body` | Reviewer | Any user can review on a public repo. |
| `github.event.review_comment.body` | Reviewer | Inline review comments. |
| `github.event.discussion.title`, `.body` | Discussion author | Applies to the `discussion` events. |
| `github.event.head_commit.message` | Commit author | Also `.author.name` and `.author.email`. |
| `github.event.commits[*].message` | Commit author | Also each commit's author name and email. |
| `github.event.pages[*].page_name` | Wiki editor | For the `gollum` event. |
| `github.event.workflow_run.head_branch` | Author of the triggering run | Also `.head_commit.message` and author fields. |
Some entries need explanation.
Branch names are free text
Git allows many shell-significant characters in branch names. Dollar signs, parentheses, backticks, semicolons, pipes and quotes are all legal. Spaces are not, but shell syntax such as ${IFS} can stand in for them. A branch named feature/$(whoami) is a valid branch. A workflow that reads github.head_ref sees that exact string. Many people think of branch names as tidy identifiers, so this field gets overlooked more than any other.
Commit metadata comes from the committer's Git config
Commit messages are plainly free text. The author name and email are also free text, because Git takes them from local configuration. GitHub does not verify them on a normal push. Treat author.name and author.email as attacker-controlled whenever a stranger's commit can reach the event.
`workflow_run` carries attacker data forward
A workflow_run workflow starts after another workflow finishes. Its payload copies fields from the first run, including the branch name and head commit message. If the first run was triggered by a fork pull request, those fields still hold the fork author's text. Moving to a second workflow does not make the data trustworthy.
Files and artifacts are inputs too
Everything above is in the event payload. Data can also arrive in other ways:
- Files from a checkout of a pull request, such as
package.json, test scripts and the pull request's own workflow files. - Artifacts uploaded by a workflow that processed fork code.
- Outputs of steps that read any of the above. Reading a PR title into a step output does not clean it.
Fields that are not attacker-controlled
These values are safe from the standpoint of free-text injection:
github.sha,github.run_id,github.run_number,github.event_name. GitHub generates them.github.repositoryandgithub.repository_owner. The base repository owner chooses them. In a fork's run they hold the fork owner's names, but your workflow in your repository sees your own.github.event.issue.numberandgithub.event.pull_request.number. They are integers assigned by GitHub.github.actor. Usernames are limited to letters, digits and single hyphens, so they cannot carry shell syntax. The name still identifies an outside user, so do not use it as a trust check.
Some values are conditional:
github.ref_nameandgithub.refon a push are branch names, so they are attacker-controlled if untrusted people can push branches. On apull_requestevent they look like123/merge, which is generated by GitHub.- On a private repository the same fields come from collaborators, not the public. The risk falls but does not vanish. A compromised or malicious collaborator account can still set them.
See what an event really contains
Do not guess which fields exist. Print the payload from a throwaway workflow and read it. The workflow below passes the JSON through an environment variable. That pattern is the main mitigation later in the tutorial, so it is good to use it from the start.
name: inspect-context
on:
issues:
types: [opened]
permissions:
contents: read
jobs:
dump:
runs-on: ubuntu-latest
steps:
- env:
EVENT_JSON: ${{ toJSON(github.event) }}
run: printf '%s\n' "$EVENT_JSON"Open an issue with a title such as test $(id) "quotes". In the log, find issue.title and confirm the characters arrive unchanged. Try the same with a pull request from a fork, using a branch named try/$(id), and look at pull_request.head.ref. Use a test repository, not a production one.
Find these fields in your own workflows
A script can list every ${{ }} expression that mentions a risky field. This one uses only the Python standard library.
import re
import sys
from pathlib import Path
RISKY = re.compile(
"|".join(
[
r"github\.event\.(issue|pull_request|discussion)\.(title|body)",
r"github\.event\.(comment|review|review_comment)\.body",
r"github\.event\.pull_request\.head\.(ref|label)",
r"github\.event\.head_commit\.(message|author\.(name|email))",
r"github\.event\.commits.*\.(message|name|email)",
r"github\.event\.pages.*\.page_name",
r"github\.head_ref",
r"github\.event\.workflow_run\.(head_branch|head_commit\.message"
r"|head_commit\.author\.(name|email))",
]
)
)
EXPRESSION = re.compile(r"\$\{\{(.*?)\}\}")
def scan(root: Path) -> int:
hits = 0
files = sorted(list(root.glob("*.yml")) + list(root.glob("*.yaml")))
for path in files:
for number, line in enumerate(path.read_text().splitlines(), start=1):
for match in EXPRESSION.finditer(line):
if RISKY.search(match.group(1)):
print(f"{path}:{number}: {match.group(0)}")
hits += 1
return hits
if __name__ == "__main__":
folder = Path(sys.argv[1] if len(sys.argv) > 1 else ".github/workflows")
count = scan(folder)
print(f"{count} risky expression(s) found")
sys.exit(1 if count else 0)Run it from the repository root with python find_untrusted.py. Each hit is a place where attacker text enters the workflow. The script also flags the safe env: assignments, so check each hit by where it sits. An expression inside a run: value, or inside the script: input of actions/github-script, is the dangerous case. An expression on the right side of an env: entry is the fix you will learn in a later chapter.
Common mistakes
- Assuming titles are short and harmless. Length limits do not stop a short shell payload.
- Trusting
github.actorchecks alone. The actor name says who triggered the run. It does not say the text they submitted is clean. - Forgetting derived data. A value copied into
GITHUB_ENV, a step output or an artifact stays attacker-controlled. - Ignoring non-obvious events.
gollum,discussionandworkflow_runcarry free text too.
Exercise
A workflow triggered by pull_request from forks uses these four values: github.event.pull_request.number, github.event.pull_request.head.ref, github.sha and github.event.pull_request.title. Which two are attacker-controlled, and why is the other pair safe from free-text injection?
Show answer
head.ref (the fork's branch name) and pull_request.title are attacker-controlled, because the PR author chooses every character. pull_request.number is an integer assigned by GitHub, and github.sha is a hash computed by Git, so neither can carry shell syntax.
Exercise
A workflow_run workflow runs after a "build" workflow that was triggered by fork pull requests. It reads github.event.workflow_run.head_commit.message. Is that value trustworthy because it comes from a different workflow? Explain.
Show answer
No. The workflow_run payload copies fields from the triggering run, so the commit message is still text written by the fork author. Moving to a second workflow does not clean the data.