DEV Community

Cover image for GitHub Actions: From Zero to Production(EP8)πŸš€
Vishwark
Vishwark

Posted on

GitHub Actions: From Zero to Production(EP8)πŸš€

Episode 8 – Security, Permissions & the Fork PR Model

In the previous episodes, we learned how to:

  • build clean CI/CD pipelines
  • reuse workflows
  • speed things up with cache and artifacts

Now we’ll cover the topic that most broken pipelines get wrong:

Security.

This episode explains:

  • how GitHub Actions protects your secrets
  • what GITHUB_TOKEN really is
  • why forked pull requests are treated differently
  • how to design secure pipelines without fear

Why Security Matters in CI/CD

A GitHub Actions workflow can:

  • access your source code
  • deploy to production
  • publish packages
  • modify repositories

That means:

A vulnerable workflow can compromise your entire system.

GitHub Actions is secure by default β€” but only if you understand how its model works.


1️⃣ What is GITHUB_TOKEN?

GITHUB_TOKEN is a short-lived, auto-generated token created for every workflow run.

You don’t create it.
You don’t store it.
GitHub injects it automatically.

It can be used to:

  • read repository contents
  • comment on PRs
  • create releases
  • push commits (if allowed)

Example:

env:
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Enter fullscreen mode Exit fullscreen mode

2️⃣ Permissions & Least Privilege

By default, workflows run with restricted permissions.

You should explicitly define what your workflow can access.

❌ Bad (implicit permissions)

jobs:
  build:
    runs-on: ubuntu-latest
Enter fullscreen mode Exit fullscreen mode

βœ… Good (least privilege)

permissions:
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest
Enter fullscreen mode Exit fullscreen mode

Common permissions you’ll see

Permission What it allows
contents Read/write repo
pull-requests Comment on PRs
packages Publish packages
pages Deploy GitHub Pages
id-token Cloud auth (OIDC)

3️⃣ Job-Level Permission Overrides

You can restrict permissions per job.

jobs:
  deploy:
    permissions:
      contents: read
      id-token: write
Enter fullscreen mode Exit fullscreen mode

This ensures:

  • CI jobs stay minimal
  • deploy jobs get only what they need

4️⃣ Forked Pull Requests (Critical Concept)

Pull requests from forks are untrusted code.

GitHub protects you by:

  • ❌ not passing secrets
  • πŸ”’ making GITHUB_TOKEN read-only
  • 🚫 blocking write access

This prevents:

β€œMalicious PR steals secrets”


5️⃣ The Danger of pull_request_target

on: pull_request_target
Enter fullscreen mode Exit fullscreen mode

This event:

  • runs in the base repository context
  • has access to secrets
  • can be dangerous if misused

Use it ONLY for:

  • labeling PRs
  • commenting
  • metadata tasks

❌ Never run untrusted code with it.


6️⃣ Third-Party Actions: Trust Carefully

Using an action is equivalent to:

Running someone else’s code in your pipeline

Best practices:

  • use official or well-maintained actions
  • pin versions (@v4, not @main)
  • avoid unknown or inactive repos
  • consider SHA pinning for high-security pipelines

Example:

uses: actions/checkout@v4
Enter fullscreen mode Exit fullscreen mode

7️⃣ Secure Deployment Pattern (Recommended)

permissions:
  contents: read

jobs:
  test:
    runs-on: ubuntu-latest

  deploy:
    needs: test
    if: github.ref == 'refs/heads/main'
    environment: production
    permissions:
      contents: read
      id-token: write
Enter fullscreen mode Exit fullscreen mode

Why this is safe:

  • tests run first
  • deploy only from main
  • environment approvals apply
  • permissions are minimal

Common Security Mistakes 🚨

❌ Running deploys on PRs
❌ Over-permissive tokens
❌ Printing secrets in logs
❌ Trusting all marketplace actions
❌ Skipping environment protection

Secure pipelines are:

  • boring
  • predictable
  • explicit

Final Mental Model (Lock This In)

Untrusted code β†’ no secrets
Trusted branch β†’ controlled access
Least privilege β†’ always
Enter fullscreen mode Exit fullscreen mode

If you follow this model, most security issues disappear.


What’s Next?

πŸ‘‰ Episode 9:
Debugging & Troubleshooting GitHub Actions

We’ll learn how to:

  • read logs effectively
  • debug secrets and permissions
  • understand silent failures
  • fix flaky pipelines

This is where theory turns into real-world confidence πŸš€


Thanks for reading!
Build safely πŸ‘‹

Top comments (0)