Sub topic 13
Protecting the CODEOWNERS File Itself
This chapter assumes you can write CODEOWNERS patterns, know that the last matching pattern wins, and have enabled "Require review from Code Owners" on a protected branch. It builds on two earlier points: GitHub reads CODEOWNERS from the pull request's base branch, and the file's own path is matched like any other path.
Why the file needs protection
The CODEOWNERS file is a normal file. If nobody owns it, a pull request can edit it like any other file. The fallback pattern * may cover it, which means the fallback owner approves changes to the rules. That is often too broad. An author could remove an owner, narrow a pattern, or add an empty pattern that clears ownership of a sensitive directory. A pull request that makes one of those edits would then need fewer approvals than it should.
The same applies to CI definitions. A workflow file decides which checks run and which secrets a job can reach. Treat it as part of the review rules.
Assign owners to the rules and the pipeline
Add entries for the CODEOWNERS file and the workflow directory. Put them at the end of the file, because the last matching pattern wins and a later broad pattern would otherwise override them.
# Make changes to ownership rules reviewed by the people who own them
/.github/CODEOWNERS @example-org/engineering-leads
/.github/workflows/ @example-org/platform @example-org/securityWith "Require review from Code Owners" enabled, a pull request that edits /.github/CODEOWNERS now needs approval from @example-org/engineering-leads. A pull request that touches /.github/workflows/ needs approval from a member of @example-org/platform or of @example-org/security, depending on how your approval rules are configured.
Why the base branch matters
GitHub evaluates ownership using the CODEOWNERS file on the base branch, not the version in the pull request. This is what makes the protection work. If a pull request deletes the entry for /.github/CODEOWNERS, the deletion is still reviewed under the old rules, so @example-org/engineering-leads must still approve it. The new rules take effect only after the merge.
Close the remaining gaps
Ownership entries alone do not enforce anything. Check these settings on the protected branch:
- Turn on required code owner review, using a branch protection rule or a ruleset.
- Turn on "Dismiss stale pull request approvals when new commits are pushed". Otherwise an author can get an approval and then push a different change to the CODEOWNERS file.
- Turn on "Do not allow bypassing the above settings", or
enforce_adminsin the API, so administrators follow the same rules. - Restrict who can push directly to the branch. A direct push skips pull request review.
Also confirm that the owners you name can approve. Owners need write access to the repository, and a team must be visible and have write access as well. An entry for a team without write access does not produce a usable approval.
Exercise
A pull request removes the line /.github/workflows/ @example-org/platform from CODEOWNERS and also edits a workflow file. Which version of CODEOWNERS decides who must review, and why does that matter?
Show answer
The version on the base branch decides. Because the workflow entry still exists there, the platform team must approve the workflow change. The removal only takes effect after the merge, so an author cannot use the same pull request to remove the review requirement. The CODEOWNERS file itself must also be approved by its owner entry.