Understanding How GitHub Actions Expressions (${{ }}) Behave
Contents
Note
This is a learning post about how script injection works in GitHub Actions, so I can protect my own workflows. Always run these tests in a repository you own.
I often write GitHub Actions workflows that do something with the title when a PR comes in. For example, one that posts the PR title to Slack.
on: pull_request
jobs:
notify:
runs-on: ubuntu-latest
steps:
- name: Notify Slack
run: |
curl -s -X POST "$SLACK_WEBHOOK" \
-d "text=New PR: ${{ github.event.pull_request.title }}"
It looks normal, but whoever opens the PR can set the title to anything. If the title contains an injection like x"; touch PWNED; echo ", the ${{ }} part is embedded as is, and on the runner the command becomes this.
curl -s -X POST "$SLACK_WEBHOOK" -d "text=New PR: x"; touch PWNED; echo ""
Swap touch for another command, and just opening a PR lets someone run whatever they want on the runner.
This vulnerability is called script injection. My understanding was that you prevent it by not writing ${{ }} directly in run:, and instead passing the value through env and reading it as "$VAR".
But ${{ }} can also go in places other than run:: the script: of github-script, with: passed to another action, and the inputs of a composite action or a reusable workflow. I didn't understand which of these are dangerous, or whether moving the value to env works for all of them. So I tried several patterns for real.
The official docs say this.
Before the shell script is run, the expressions inside
${{ }}are evaluated and then substituted with the resulting values, which can make it vulnerable to shell command injection.
${{ }} embeds the evaluated result string into the source before the shell or JS interprets it as code. It is different from a shell $VAR, which reads the value at runtime. The key point is that evaluation happens "before execution". That is why a " or ; in the embedded string acts as a metacharacter of the language at that spot.
Going through env is safe for the opposite reason. The value is not embedded in the source. It is read as a plain variable at runtime: "$TITLE" in run:, process.env.TITLE in github-script. The value stays data the whole way and never becomes part of the code.
If you trace "which language's source does the result of this ${{ }} get embedded into", you can tell whether it is dangerous.
I prepared workflows that put ${{ github.event.pull_request.title }} in six places (trigger: on: pull_request).
The attacker only has to open a PR from a branch in the same repository with a payload in the PR title.
These are the six patterns I tried.
# C1 Expanded directly in run: → embedded in the shell command string
# A " or ; in the title acts as a bash metacharacter, so you can append commands
run: echo "PR: ${{ github.event.pull_request.title }}"
# C2 Expanded directly in github-script's script: → embedded in JS source
# The content of script: runs as JS, so a JS breakout works, not a shell one
script: console.log("PR: ${{ github.event.pull_request.title }}")
# C3 Just passing a value to another action with with: → not embedded in code by itself
# Whether it is dangerous depends on how the receiver uses it. Judge by the receiving side (C5/C6)
with: { text: "${{ github.event.pull_request.title }}" }
# C4 Put it in env, then reference it again with ${{ env.TITLE }} → back to the shell
# ${{ env.X }} is also template substitution, so the string still ends up embedded in the run: source
run: echo "PR: ${{ env.TITLE }}"
# C5 A composite action expands its inputs directly in an internal run: (receiving side)
# The caller only passes it with with:. The embedding and execution happen inside the action
run: echo "got ${{ inputs.text }}"
# C6 A reusable workflow expands its inputs directly in an internal run: (receiving side)
# Same as C5: embedded in a run: on the other side of the uses: boundary
run: echo "got ${{ inputs.text }}"
Only C3's with: is passing a value along. All the others connect the result of ${{ }} directly to the shell in run: or the JS in script:.
I judged whether the attack worked by whether an empty file named PWNED appeared in the workspace. I put this step at the end of each workflow.
- name: observe
if: always()
run: test -f PWNED && echo "C1_run_direct=executed" || echo "C1_run_direct=literal"
(Note) From a fork, the real damage is limited even if the injection works
In a normal pull_request from a fork, GITHUB_TOKEN is read-only and no secrets are passed, so even if this injection works the real damage is limited. The scary cases are pull_request_target, self-hosted runners, tokens with write permission, and jobs that reference secrets. I'll also say up front that PWNED appearing does not immediately mean secrets leaked.
I prepared two PR titles. One assumes shell execution and the other assumes JS execution, so they have different shapes.
# For the shell
x"; touch PWNED; echo "
# For JS
"); require("child_process").execSync("touch PWNED"); ("
I tried two PRs with the attack strings (shell and JS). Each workflow prints a marker: executed if the planted command ran, literal if it didn't run and stayed a plain string.
These were the results.
| # | Where it can be written (embedded into) | Shell PR | JS PR | Why |
|---|---|---|---|---|
| 1 | Directly in run: (shell) |
executed | literal | Embedded in the shell |
| 2 | script: of github-script (JS) |
literal | executed | Embedded in JS source |
| 3 | Passed to another action with with: |
— | — | Not decided on its own (resolved in #5/#6) |
| 4 | env: referenced again as ${{ env.TITLE }} (shell) |
executed | literal | Even with env, re-expansion brings it back |
| 5 | Composite inputs into an internal run: |
executed | literal | The receiving side re-expands it |
| 6 | Reusable inputs into an internal run: |
executed | literal | The receiving side re-expands it |
Logs from the actual run (the output of each observe step, combined)
===== PR#1 attack-shell (shell payload) =====
C1_run_direct=executed C1_run_env=literal
C2_script=literal C2_script_env=literal
C4_env_quoted=literal C4_env_retemplate=executed
C5_composite_bad=executed C5_composite_safe=literal
C6_reusable_bad=executed C6_reusable_safe=literal
===== PR#2 attack-js (JS payload) =====
C1_run_direct=literal C1_run_env=literal
C2_script=executed C2_script_env=literal
C4_env_quoted=literal C4_env_retemplate=literal
C5_composite_bad=literal C5_composite_safe=literal
C6_reusable_bad=literal C6_reusable_safe=literal
Only github-script (#2) has the shell PR and JS PR results reversed, because script: runs as JS. The JS payload is valid JS and gives executed. The shell payload breaks the JS syntax and gives literal (for how it works, see async-function.ts).
Two other things caught my attention. I'll go through them in order.
- Moving the value to env, and it still comes back (#4)
- Crossing a boundary with composite and reusable (#5, #6)
Until now I thought "moving it to env makes it safe", but what actually mattered was not putting it in env. It was how you reference it.
env-quoted: # ✅ Safe
steps:
- env: { TITLE: "${{ github.event.pull_request.title }}" }
run: |
echo "PR: $TITLE" # The shell reads the variable at runtime. It stays data
env-retemplate: # ❌ Dangerous
steps:
- env: { TITLE: "${{ github.event.pull_request.title }}" }
run: |
echo "PR: ${{ env.TITLE }}" # Re-expanded with ${{ }}. Back in the source again
If you reference it as "$TITLE", the shell just reads the value at runtime, so it is safe.
If you reference the same value as ${{ env.TITLE }}, the string is embedded into run: again at the template substitution stage, comes back to the shell, and the command runs.
My understanding is that what worked was not the fact that it was in env, but not putting the value back into the source with ${{ }}.
When you pass a value somewhere else, it is also decided by how the receiver uses that value.
I passed the same input to a composite that expands it into run: internally and to a composite that receives it through env, and compared them.
# ✅ Safe: receive it through env
runs:
using: composite
steps:
- shell: bash
env: { TEXT: "${{ inputs.text }}" }
run: echo "got $TEXT"
# ❌ Dangerous: expand inputs into run:
runs:
using: composite
steps:
- shell: bash
run: echo "got ${{ inputs.text }}"
A reusable workflow (workflow_call) has the same structure. It depends on how the workflow in the separate file you call with uses: uses the inputs it receives.
# reusable-safe.yml (✅ Safe: receive it through env)
on:
workflow_call:
inputs: { text: { type: string } }
jobs:
use:
runs-on: ubuntu-latest
steps:
- env: { TEXT: "${{ inputs.text }}" }
run: echo "got $TEXT"
# reusable-bad.yml (❌ Dangerous: expand inputs into run:)
on:
workflow_call:
inputs: { text: { type: string } }
jobs:
use:
runs-on: ubuntu-latest
steps:
- run: echo "got ${{ inputs.text }}"
Here too, the command ran when the inside expanded it into run:, and it stayed a string when received through env.
I passed the same string through ${{ }} in six places and confirmed that the danger is decided not by the injection point but by the language and usage it gets embedded into.
Through hands-on work I also came to understand that env comes back if you reference it again with ${{ env.X }}, and that composite and reusable depend on the receiving side.
Translated from the Japanese original.
Thanks. I'll look into it.
Sorry, that didn't go through. Please try again later.