Securing a GitHub repository
A GitHub repository stores source code, the full git history of every file, the settings that decide who may change the default branch, and workflows that run on GitHub Actions with the repository's secrets and an access token. Settings paths match GitHub's documentation as of November 2026.
Branches
1. Protect the default branch
Open Settings, then Branches in the Code, planning, and automation section, and click Add classic branch protection rule. Enter the branch name, usually main, under Branch name pattern, then select Require a pull request before merging and Require approvals.1 A new rule already blocks force pushes and deletion of the branch.2
With required reviews on, a collaborator can change the branch only through a pull request that the set number of reviewers with write access has approved. Rules do not bind repository administrators by default; Do not allow bypassing the above settings extends them to administrators. A required status check accepts a result from anyone with write access unless the rule names the app expected to report it.2
Secrets
2. Turn on secret scanning
Secret scanning searches the whole Git history on every branch for credentials. It runs on public repositories automatically at no cost; organisation-owned private repositories need GitHub Secret Protection on a Team or Enterprise Cloud plan.3 A key from a provider in GitHub's partner program goes to that provider, which can revoke it. For any other alert, rotation the credential first. Exposed credentials in source code repositories explains why deleting the commit is not enough.
3. Turn on push protection
Push protection checks a push as it arrives and refuses it if it contains a recognised secret, so the key never enters the history. GitHub accounts have a personal form, on by default, that stops the account pushing secrets to public repositories. The repository form needs GitHub Secret Protection, is off by default, and is switched on by an administrator.4
Anyone with write access can still push past a block by choosing a reason: "It's used in tests", "It's a false positive" or "I'll fix it later". Each bypass creates an alert, an audit log entry and an email to the people watching the repository; "I'll fix it later" leaves the alert open. Delegated bypass limits who may push past a block.4
Dependencies and code
4. Turn on Dependabot alerts and security updates
Dependabot reads the default branch and raises an alert when a package in the dependency graph matches a new entry in the GitHub Advisory Database, or when a commit changes the packages. Only advisories reviewed by GitHub produce alerts.5 With security updates on, Dependabot also opens a pull request that moves the package to the lowest version containing the fix. It does this only for packages listed in a manifest or lock file.6
5. Set up code scanning
Open Settings, then Advanced Security in the Security and quality section. Under Code Security, next to CodeQL analysis, click Set up, then Default, then Enable CodeQL. It needs GitHub Actions turned on and is available on public repositories and on organisation repositories with GitHub Code Security.7 The same setup can also find unsafe Actions workflows.8
Workflows
6. Make the workflow token read-only
Workflows authenticate to GitHub with a GITHUB_TOKEN, and any action in a job can read it through the github.token context even when the workflow does not pass it.9 A safe default is read-only access to repository contents, raised per job where a step needs more.8 The permissions key sets this for the whole workflow or for one job:
permissions:
contents: read
jobs:
open-issue:
runs-on: ubuntu-latest
permissions:
contents: read
issues: writeRepository secrets need the same care: every user with write access can read all of them.8 See Principle of least privilege.
7. Pass untrusted text through an environment variable
A run step is written to a temporary shell script, and each ${{ }} expression is replaced by its value before the script runs. A pull request titled a"; ls $GITHUB_WORKSPACE" closes the quoted string in title="${{ github.event.pull_request.title }}" and runs ls on the runner, a case of shell command injection.10 Fields that end in title, body, head_ref, label, message, name or email can be written by outsiders, and so can branch names: zzz";echo${IFS}"hello";# is a valid one.10 Set the value as an environment variable and quote the variable in the script:8
- name: Check PR title
env:
TITLE: ${{ github.event.pull_request.title }}
run: |
if [[ "$TITLE" =~ ^octocat ]]; then
echo "PR title starts with octocat"
fi8. Keep pull request code out of privileged triggers
Workflows started by pull_request_target or workflow_run can hold write access and the repository's secrets, and share the default branch's cache. If such a workflow checks out code from a fork, the author of that code can take over the repository, so these workflows must never check out untrusted code.8 A self-hosted runner is not guaranteed to start clean for each job, so a public repository should not use one.8
9. Pin third-party actions to a commit
A tag such as @v4 can be moved or deleted by anyone who gains access to the action's repository. A full 40-character commit SHA cannot be redirected without a SHA-1 collision. Copy the SHA from the action's own repository, not a fork.8
- uses: example-org/example-action@<full 40-character commit SHA> # v4.2.0Notable incident
In March 2025, attackers moved the version tags of the tj-actions/changed-files action to a malicious commit that printed secrets from more than 23,000 repositories into their build logs.11 The full case is in Secret exposure in CI/CD workflows.
Prompt for a coding agent
Review every file in .github/workflows. Add a top-level permissions block with contents: read and give each job only the extra permissions its steps use. Move every ${{ github.event.* }} or ${{ github.head_ref }} expression that appears inside a run: script into an env: variable and quote that variable in the script. Replace each third-party action reference that uses a tag or branch with its full commit SHA, copied from the action's own repository, and keep the version as a comment. List any workflow that uses pull_request_target or workflow_run and checks out pull request code, without changing it. Report each file and line you changed.In SecFlash
The repository settings in steps 1 to 5 appear on the Cloud page: an unprotected default branch as SF-CLD-601, Dependabot alerts or security updates turned off as SF-CLD-602 and SF-CLD-603, secret scanning off on a public repository as SF-CLD-604, push protection off as SF-CLD-605, and missing code scanning as SF-CLD-606. A key in the code or history shows on the Security page as SF-SEC-702, and so do workflow problems: SF-SEC-801 (untrusted text run as code), SF-SEC-802 (a risky trigger), SF-SEC-804 (more access than needed), SF-SEC-805 (a credential that can leak) and SF-SEC-806 (an action not pinned).
See also
- Secret exposure in CI/CD workflows
- Exposed credentials in source code repositories
- Secret scanning
- Software supply chain compromise
- Vulnerable dependencies
- Multi-factor authentication
- Security checklist before launch
References
- back GitHub (2026). "Managing a branch protection rule". GitHub Docs.
- a b GitHub (2026). "About protected branches". GitHub Docs.
- back GitHub (2026). "About secret scanning". GitHub Docs.
- a b GitHub (2026). "Push protection". GitHub Docs.
- back GitHub (2026). "About Dependabot alerts". GitHub Docs.
- back GitHub (2026). "About Dependabot security updates". GitHub Docs.
- back GitHub (2026). "Configuring default setup for code scanning". GitHub Docs.
- a b c d e f g GitHub (2026). "Secure use reference". GitHub Docs.
- back GitHub (2026). "Use GITHUB_TOKEN for authentication in workflows". GitHub Docs.
- a b GitHub (2026). "Script injections". GitHub Docs.
- back GitHub (2025). "tj-actions changed-files through 45.0.7 allows remote attackers to discover secrets by reading actions logs" (GHSA-mrrh-fwg8-r2c3). GitHub Advisory Database.
External links
- Secure use reference, GitHub Docs
- Push protection, GitHub Docs