Dependabot, CodeQL & workflow permissions
Lock down GITHUB_TOKEN and scan code and dependencies.
By Invicly Tech, , in Git, GitHub & GitHub Actions. About 10 minutes to read.
In short
Workflows run with a GITHUB_TOKEN and can pull in third-party code, so they need guarding like production. Least-privilege permissions, Dependabot and CodeQL cover the main risks.
Explained simply
A workflow is code that runs with real power: it can push to your repo, publish packages and deploy to production. If an attacker can change what a workflow runs, they get that power. So workflows deserve the same care as production servers.
Every job gets an automatic key called GITHUB_TOKEN. It is created for the job and stops working when the job ends. What it can do depends on its permissions. The safest default is read-only, with extra rights added only to the jobs that need them.
Most of your code is other people's code: libraries and third-party actions. Dependabot watches them. It warns you when a dependency has a known security hole and opens pull requests to update it. It can also keep your actions up to date.
CodeQL looks at your own code. It understands how data flows through your program and finds patterns like user input reaching an SQL query without escaping. Results show up as alerts in the Security tab and as comments on pull requests.
Press Play on a diagram to walk through it one step at a time.
- 1Least-privilege token
Default permissions: contents: read; add more per job.
- 2Pinned actions
Third-party actions pinned to a full commit SHA.
- 3Dependabot
Alerts and update PRs for vulnerable dependencies and actions.
- 4CodeQL scanning
Finds bugs like injection in your own code.
- 5Branch protection
Workflow changes need review before they run on main.
- permissions: write-all
- uses: some/action@main
- run: echo "${{ github.event.issue.title }}"
- pull_request_target plus checkout of PR head
- permissions: contents: read, widen per job
- uses: some/action@FULL_SHA # v2.1.0
- env: TITLE, then echo "$TITLE"
- pull_request for untrusted code
In depth
GITHUB_TOKEN permissions in practice
The token is created when a job starts and expires when the job finishes (at most after 24 hours). Its default permissions come from the repo or organization setting: new repos and organizations default to read-only for contents and packages. Override it with a permissions: block at workflow level and, where needed, at job level.
Two rules matter. As soon as you list any permission, every permission you did not list becomes none. And permissions: {} removes all of them. Common scopes are contents (code and releases), pull-requests, issues, packages, id-token (OIDC), security-events (upload scan results), checks, statuses and actions. Give write only to the one job that comments, releases or pushes.
- Top of every workflow: permissions: contents: read
- Widen per job, for example pull-requests: write for a commenting job
- Forked PRs always get a read-only token
Script injection and dangerous triggers
Expressions in ${{ }} are pasted into the script text before the shell runs. If a step contains run: echo "${{ github.event.pull_request.title }}", a PR titled with a quote followed by a command runs that command on your runner, with your token. Treat titles, branch names, issue bodies and commit messages as untrusted. The fix is simple: pass them through env: and use the shell variable, so the value stays data.
pull_request_target and workflow_run run in the context of the base repo, with secrets and a write token, even for PRs from forks. That is fine for labelling or commenting. It is dangerous if the workflow checks out and runs the PR's code, a pattern often called a pwn request. Never build or test untrusted PR code in those triggers.
- Attacker to GitHub: Open PR with a crafted title1Open PR with a crafted title
- GitHub to Runner: Workflow pastes title into run: script2Workflow pastes title into run: script
- 3Shell runs the attacker's commandRunner to Runner: Shell runs the attacker's command
- Runner to Attacker: Token or secrets sent out4Token or secrets sent out
- GitHub to Runner: Fix: title passed via env, used as "$TITLE"5Fix: title passed via env, used as "$TITLE"
Third-party actions and the supply chain
uses: someone/action@v3 runs whatever commit the tag v3 points to today, and tags can be moved. In March 2025 the popular tj-actions/changed-files action was compromised this way: its tags were repointed to code that dumped secrets into build logs. Pinning to a full 40-character commit SHA protects you, because a SHA cannot be changed. Keep the version in a comment, and let Dependabot propose SHA updates.
Organizations can also restrict which actions are allowed (GitHub-made, verified creators, or an allow list). Static checkers help: actionlint catches syntax and expression mistakes, and zizmor flags security problems like injection and over-broad permissions. CodeQL can scan workflow files too.
Dependabot: alerts, security updates and version updates
Dependabot alerts use the dependency graph and the GitHub Advisory Database to tell you which dependencies have known vulnerabilities. Dependabot security updates go one step further and open PRs that upgrade just enough to fix them. Version updates, configured in .github/dependabot.yml, keep everything current on a schedule, including the github-actions ecosystem.
Useful keys: groups to bundle related updates into one PR, ignore to skip packages or major versions, open-pull-requests-limit (5 by default) and labels. Workflows triggered by Dependabot PRs run with a read-only token and only Dependabot secrets, so CI that needs a secret must use one stored under Dependabot secrets. Patch updates can auto-merge once CI passes.
CodeQL code scanning
CodeQL builds a database of your code and runs queries over it to find bugs like SQL injection, cross-site scripting, path traversal and unsafe deserialization. It supports languages including C and C++, C#, Go, Java and Kotlin, JavaScript and TypeScript, Python, Ruby, Swift, and GitHub Actions workflows.
Default setup is one click in Settings, Code security: GitHub picks languages and runs on pushes, PRs and a weekly schedule. Advanced setup is a workflow you control, using github/codeql-action init and analyze with security-events: write; you can add the security-extended query suite or custom queries. Alerts appear in the Security tab and on PRs, and you can require code scanning results in a ruleset. It is free for public repos; private repos need GitHub Code Security.
Learn it in 3 short steps
About 40m each. Track them with a timer in your plan.
- 1Set least-privilege GITHUB_TOKEN permissions~40m
- 2Enable Dependabot updates and alerts~40m
- 3Run CodeQL code scanning~40m
Key points
- Set permissions at the top of each workflow and widen only for jobs that need it.
- Pin third-party actions to a commit SHA; tags can be moved by their owner.
- Dependabot can update the github-actions ecosystem as well as package managers.
- Never interpolate untrusted input like PR titles directly into run scripts.
Key terms
Tap a card to flip it. 0/5 known
Examples
# .github/dependabot.yml
version: 2
updates:
- package-ecosystem: pip
directory: "/"
schedule: { interval: weekly }
- package-ecosystem: github-actions
directory: "/"
schedule: { interval: weekly }
# top of every workflow
permissions:
contents: readWeekly update PRs plus a read-only default token.
Fixing a script injection in a triage workflow
A workflow greets new issues by echoing their title. Step 1: notice that the title is pasted into the script, so a title containing $(curl ...) would run. Step 2: move the value into env: so GitHub sets it as an environment variable. Step 3: quote the shell variable in the script. Step 4: limit the token to issues: write for this job only.
# before (vulnerable):
# - run: echo "New issue: ${{ github.event.issue.title }}"
on: { issues: { types: [opened] } }
permissions: {}
jobs:
greet:
runs-on: ubuntu-latest
permissions: { issues: write }
steps:
- run: gh issue comment "$NUMBER" --body "Thanks! We got: $TITLE"
env:
TITLE: ${{ github.event.issue.title }}
NUMBER: ${{ github.event.issue.number }}
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}CodeQL advanced setup for a two-language repo
You want extra security queries and a scan of your workflow files. Step 1: run on pushes to main, on PRs and weekly. Step 2: grant security-events: write so results can be uploaded (private repos also need actions: read). Step 3: use a matrix of languages; for interpreted languages no build step is needed. Step 4: init with the security-extended suite, then analyze.
name: codeql
on:
push: { branches: [main] }
pull_request:
schedule: [{ cron: "27 3 * * 1" }]
permissions: { contents: read, security-events: write }
jobs:
analyze:
runs-on: ubuntu-latest
strategy: { matrix: { language: [javascript-typescript, python, actions] } }
steps:
- uses: actions/checkout@v4
- uses: github/codeql-action/init@v3
with: { languages: "${{ matrix.language }}", queries: security-extended }
- uses: github/codeql-action/analyze@v3Quick reference
- permissions: contents: read at the top; add write scopes only on the jobs that need them
- Listing any permission sets all unlisted ones to none; permissions: {} removes all
- Pin third-party actions: uses: owner/action@FULL_40_CHAR_SHA # v2.1.0
- Pass untrusted event text through env:, never straight into run: with ${{ }}
- Never check out and run PR code in pull_request_target or workflow_run
- Dependabot github-actions ecosystem keeps actions and SHA pins current
- CodeQL upload needs security-events: write; free on public repos
- actionlint and zizmor catch workflow mistakes and security issues before merge
Common mistakes
- Using run: echo "${{ github.event.pull_request.title }}", which allows injection.
- Granting write-all permissions because one step needed to comment on a PR.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Add a read-only default token to a workflow that has none, and give only its comment job permission to write PR comments.
- Exercise 2medium
Configure Dependabot to update npm weekly with minor and patch updates grouped into one PR, and GitHub Actions monthly.
- Exercise 3hard
Auto-merge Dependabot patch updates once required checks pass, and nothing else.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- What is the difference between Dependabot alerts and Dependabot security updates?
- Alerts tell you a dependency has a known vulnerability. Security updates also open a pull request that upgrades it to a fixed version.
- Is CodeQL free?
- Yes for public repositories. For private repositories it requires GitHub Code Security, a paid add-on.
- Should I pin GitHub's own actions like actions/checkout to a SHA too?
- Pinning everything is the strictest option and some organizations require it. Many teams pin third-party actions to SHAs and accept major tags for actions from GitHub itself.
- When is pull_request_target safe to use?
- When the workflow never checks out or runs the pull request's code, for example to add labels or post a comment based on metadata.