Sub topic 15
Auditing and enforcing least privilege
This chapter assumes you can write a permissions block at workflow and job level. It also assumes you have the GitHub CLI (gh) installed and authenticated. It shows how to find workflows that grant too much and how to stop new ones from appearing.
Audit in a loop
Auditing is a repeating cycle. Settings drift, and new workflows are copied from old ones.
Step 1: Check the defaults
A repository or organization that defaults to read-write hands that access to every workflow that has no permissions key. List the setting for each repository in an organization:
gh repo list OWNER --limit 200 --json nameWithOwner --jq '.[].nameWithOwner' |
while read -r repo; do
echo "$repo $(gh api "repos/$repo/actions/permissions/workflow" --jq .default_workflow_permissions)"
doneAny repository that prints write needs attention. Set the organization default once with gh api --method PUT orgs/OWNER/actions/permissions/workflow -f default_workflow_permissions=read. Repositories can then only keep a broader default if the organization policy allows it.
Step 2: Scan the workflow files
Two quick searches find most problems. The first lists files with no top-level permissions key. The second lists every write grant, so you can ask whether each one is needed.
grep -L '^permissions:' .github/workflows/*.y*ml
grep -nE 'write-all|: *write' .github/workflows/*.y*mlThe first search matches only a permissions: line at column 0. A file listed there may still set permissions on every job, so open it before changing anything.
Grep cannot judge context. Dedicated scanners can:
- zizmor is a static analyzer for GitHub Actions. Its
excessive-permissionsaudit flags broad grants. - OpenSSF Scorecard has a
Token-Permissionscheck that scores each repository.
Run either in CI so that each pull request is checked.
Step 3: Reduce to the minimal set
For each flagged job, apply the method from earlier chapters:
- Start from
permissions: {}at workflow level. - Add
contents: readif the job checks out code. - Run the job and read any
Resource not accessible by integrationerror. - Add the single scope the error names, at the lowest level that works.
Step 4: Enforce
Cleaning up once does not last. Add controls that keep the result in place:
- Keep the organization default at read-only.
- Require review for workflow changes with a CODEOWNERS entry, together with a branch protection rule that requires code owner review:
/.github/workflows/ @OWNER/security-team- Restrict which actions may run, using the allowed-actions policy in the repository or organization settings.
- Fail pull requests when a scanner reports excessive permissions.
Review checklist
| Check | Good result |
|---|---|
| Organization default | `read` |
| Workflow-level block | Present, `{}` or `contents: read` |
| Write scopes | Only on jobs that use them |
| `id-token: write` | Only on jobs that authenticate to a cloud provider |
| Workflow edits | Need code owner approval |
Exercise
Your grep -L '^permissions:' search lists release.yml. Does that prove the workflow is over-privileged? What do you do next?
Show answer
No. The search only shows there is no top-level permissions key. Each job might still declare its own block. Open the file. If any job lacks a block, it falls back to the repository default, so check that default with gh api repos/OWNER/REPO/actions/permissions/workflow. Then add permissions: {} or contents: read at workflow level and elevate only the jobs that need more.