Course contentsShow
← Cloud and Infrastructure
Lesson 1722 of 2,98935. GitHub ActionsFree lesson

Workflow Security Hardening

Apply security best practices: pin action versions, restrict permissions, and prevent script injection attacks.

Workflow Security Hardening

What you'll learn: How to lock down GitHub Actions workflows to prevent common attack vectors and reduce your blast radius.

The Core Security Problem

GitHub Actions workflows execute code in response to events—some you control (like pushes), others you don't (like issue comments from strangers). Without hardening, a malicious actor could inject commands, escalate permissions, or exfiltrate secrets. Security hardening means building defensive layers that assume compromise attempts will happen.

Three Critical Hardening Practices

1. Pin Action Versions to Full SHA

Instead of using actions/checkout@v3, pin to the exact commit hash: actions/checkout@a81bbbf8298c0fa03ea29cdc473d45769f953675. This prevents supply-chain attacks where an action's tag gets moved to malicious code.

Analogy: It's like verifying a package's seal and its tamper-proof barcode, not just trusting the "organic" label.

2. Restrict Workflow Permissions

GitHub automatically grants workflows broad GITHUB_TOKEN permissions. Use the permissions key to grant only what's needed:

permissions:
  contents: read
  pull-requests: write

This follows least-privilege principles—if an attacker compromises a step, they can't delete code or modify releases.

3. Prevent Script Injection

Never directly interpolate untrusted input (issue titles, PR bodies, branch names) into shell commands:

# DANGEROUS
run: echo "Title: ${{ github.event.issue.title }}"

# SAFE - use environment variable
run: echo "Title: $ISSUE_TITLE"
env:
  ISSUE_TITLE: ${{ github.event.issue.title }}

Environment variables are treated as values, not executable code, preventing command injection.

Key Takeaway: Security hardening means pinning actions to SHAs for immutability, restricting token permissions to the minimum required, and isolating untrusted input through environment variables—together these practices dramatically shrink your attack surface.