Three Mechanisms to Protect Your Git Repositories
Published: 2024-05-06 19:25:11


Your version control system, like Git, is a primary vector for [secret sprawl](https://blog.gitguardian.com/secret-sprawl/), unintentional source poisoning, and [intentional source poisoning](https://blog.gitguardian.com/poisoning-the-source-how-and-why-attackers-are-targeting-developer-accounts/). In a [shift left](https://blog.gitguardian.com/shift-left-moving-security-to-the-development-phase-the-case-of-secrets-detection-in-code-repositories/) model, there are degrees of *leftness*. The most left you can get is to test all the code before the developer tries to commit anything and train them thoroughly in the best practices. But when we rely on people to remember to do things consistently and correctly, we're cutting holes in the safety net. We need mechanisms.

At Amazon they have a saying: "Good intentions don't work. Mechanisms do." Humans can feel fatigued, rushed, distracted, or otherwise encumbered, and despite all intentions to follow best practices, they don't. When you automate enforcement of best practices, you can ensure those practices are followed in a much more consistent and correct fashion.

## Force code reviews by "Code Owners"

Git exists independently of GitHub, GitLab, and BitBucket. All of these businesses are providing Git as a service. Rather than categorizing them as "GaaS" (Git as a Service), let's call them GSPs (Git Service Providers). Pretty much all GSPs add functions and features you won't get from setting up your own basic Git server. But while they each add some unique value propositions, they often provide some of the same added value features as well. One of these is called "Code Owners."

Just like you would use .gitignore to specify which files should be ignored and never committed, you can add a file called CODEOWNERS to the root of a branch to specify who owns files, directories, naming patterns, etc. Those owners must review pull requests that impact the files they own before they can be merged. 

This is supported by default in [GitHub](https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners), [GitLab](https://docs.gitlab.com/ee/user/project/codeowners/), and [BitBucket Data Center](https://confluence.atlassian.com/bitbucketserver/code-owners-1296171116.html). For [BitBucket Cloud, there's a free plugin](https://marketplace.atlassian.com/apps/1218598/code-owners-for-bitbucket?tab=overview&hosting=cloud). Generally they're pretty similar. Owners must have write permission so they can merge the pull requests. Depending on how the Git service identifies the owners, you might use their handle in the system, their email address, or a team identifier. In general, all three services use a similar syntax for their files, but consult the documentation for each.

For example, to fully protect a GitHub repository end to end, using CODEOWNERS, they recommend adding a CODEOWNERS file to the .github directory of the main branch, using it to set ownership of all the other CODEOWNERS files in the main repo or branches. No one will be able to modify a CODEOWNERS file in a subdirectory or branch without a review except the owner of the CODEOWNERS files.

**CAVEAT**: 

This can get a little fiddly as you deal with various combinations of permissions, settings, and rules. A couple of the fiddly bits from GitHub…

- Someone on a team that is set as a code owner, but who doesn't have write permissions, will be able to approve a pull request, but not merge it.
- You'll need to set "Require review by Code Owners" in the branch protection rules (see next section) to limit merges to the designated code owners. Without it, the designated code owners will get notified, but anyone with write permissions will be able to merge. 

## Set branch protection rules / permissions

This goes by different names at the different services. We've got [GitHub branch protection](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/about-protected-branches), [Gitlab protected branches](https://docs.gitlab.com/ee/user/project/protected_branches.html'), and [Bitbucket branch permissions](https://support.atlassian.com/bitbucket-cloud/docs/use-branch-permissions/).

These give you a bit more expansive and varied permissions than a CODEOWNERS file does and can even impact how CODEOWNERS is enforced, but require a bit more of a learning curve, and in some cases, navigating configuration menus in the website instead of just adding files.

For example, [GitHub branch protection](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/about-protected-branches) rules are set up by going to **Settings - Code & automation - Branches - Branch protection rules**, then adding a rule.

The first step is to define the branches to which the rule will apply using a pattern or a name. Then, in the docs, 21 sentences following that begin with "optionally" before you get to finalizing and applying the rule.

If I've counted correctly, the fourth "optionally" covers setting "Require review by Code Owners" as mentioned in the previous section.

Three other options to keep in mind:

- **Require status checks to pass before merging:** I don't just like this one because it's how you [enable a GitGuardian scan on the pull request](https://docs.gitguardian.com/secrets-detection/secrets-detection-in-sdlc/detect-secrets-in-real-time-in-github) and block the merge if it fails. You can test for adherence to coding style, software content analysis (SCA) on any dependency changes, etc. And if any of them fail… the merge is blocked.
- **Allow specified actors to bypass required pull requests:** Maybe you're being pressured by a hands-on CTO to use this to let them bypass a Code Owners review on their commits, because they're the CTO and they won't push bad code. Don't. Ask your manager for backup, but resist this with all your might. Even the CTO's good intentions don't work. Mechanisms do.
- **Do not allow bypassing the above settings:** Even with some of the protections you can set, another person with admin privileges (or even you in a moment of weakness) can bypass them. This makes EVERYONE, no matter how good their intentions, subject to the mechanisms.

It's probably obvious to you by now that I like mechanisms.

## Get fine-grained control with rulesets

[Gitlab's "rulesets"](https://docs.gitlab.com/ee/user/application_security/sast/customize_rulesets.html) are used to control a number of scans, such as SAST, SCA, and secrets scanning.

In GitHub, [branch and tag behaviors can be governed with rulesets](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-rulesets/about-rulesets). Rulesets, however, are not limited to a single repository. They can be set at an organizational level. And all rules in effect for a branch or tag, at whatever level, are evaluated in a process called [rule layering](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-rulesets/about-rulesets#about-rule-layering).

This is a very powerful feature, and to help you out, GitHub has its own [ruleset recipes repository](https://github.com/github/ruleset-recipes).

One caveat on rulesets can be found in GitHub's [Enterprise Migration Guide](https://docs.github.com/en/migrations/using-github-enterprise-importer/migrating-from-bitbucket-server-to-github-enterprise-cloud/about-migrations-from-bitbucket-server-to-github-enterprise-cloud). If you're importing commits from another GSP and some or all of those commits don't meet the requirements of rules in your organization, the commit imports will be blocked.

## Next steps

Now that you've learned about these three mechanisms, start using them. Start with Code Owners and then move on to enabling it with a branch protection rule. If you're feeling adventurous, [enable a GitGuardian scan on pull requests](https://docs.gitguardian.com/secrets-detection/secrets-detection-in-sdlc/detect-secrets-in-real-time-in-github) to block secrets from getting merged (and let you [remediate them](https://docs.gitguardian.com/secrets-detection/remediate/remediate-incidents)). And once you're a bonafide branch protecting talent, level up into rulesets.

Remember, messing up and not following best practices isn't evil. It's human. Use your skills to build mechanisms that aren't your overlords, but a second set of eyes to help you and everyone writing code for your repository catch mistakes before they become problems.