AI Hints
Home›CI/CD with GitHub Actions Tutorial›Privilege, Runners, and Monorepos
CI/CD with GitHub Actions · Lesson 109 of 138

Least Privilege

Only what you need.

⏱ 2 min read▶ Video at 2:07:13🎓 Free
Watch this lesson in the CI/CD with GitHub Actions courseWatch on YouTube ↗Full CI/CD with GitHub Actions course

Every job gets a GitHub token. If a job is ever tricked into running bad code, that token is what the attacker gets. So give each job only the permissions it really needs. That's the principle of least privilege.

Least Privilege
From the video: Least Privilege
  • Every job gets a token
  • Attackers get it too
  • Only what's needed

Permissions per job

At the top, permissions with empty braces turns everything off by default. The test job only reads the code. The comment job gets write access to pull requests, and nothing else. If the test job is ever attacked, it can't push code, or change anything.

.github/workflows/ci.ymlyaml
permissions: {}          # nothing by default

jobs:
  test:
    runs-on: ubuntu-latest
    permissions:
      contents: read     # just read the code
    steps:
      - uses: actions/checkout@v4
      - run: pytest -q
  comment:
    needs: test
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write   # only this job can comment
    steps:
      - run: echo "post a comment"

More least privilege

Set the repository's default token to read only, in the settings. Keep secrets in environments, so only approved jobs see them. Use OIDC instead of long lived cloud keys. And give cloud roles the smallest set of rights that works.

keyvalue
Default tokenread-only
permissions:per job
Secretsin environments
Cloud rolessmallest rights

Summary

Turn permissions off by default, and grant each job only what it needs. Next, running jobs on your own machines.

Key takeaways
  • permissions: {}
  • contents: read
  • Grant per job
Next up: Self-Hosted Runners — your own machines