{"id":8074,"date":"2022-01-18T15:46:42","date_gmt":"2022-01-18T15:46:42","guid":{"rendered":"https:\/\/emtunc.org\/blog\/?p=8074"},"modified":"2022-01-18T15:46:44","modified_gmt":"2022-01-18T15:46:44","slug":"github-security-2022-branch-protection-edition","status":"publish","type":"post","link":"https:\/\/emtunc.org\/blog\/01\/2022\/github-security-2022-branch-protection-edition\/","title":{"rendered":"GitHub Security 2022: Branch Protection Edition"},"content":{"rendered":"\n<p>In this post, I will be going through some of the <em>essential<\/em> Branch Protection Rules you should have on all* of your GitHub repositories. GitHub is constantly releasing new updates <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/orgs\/github\/projects\/4247\" data-type=\"URL\" data-id=\"https:\/\/github.com\/orgs\/github\/projects\/4247\" target=\"_blank\">all the time<\/a> but my recommendations stand as of  January 2022.<\/p>\n\n\n\n<p>I&#8217;ve also added some <em>gotchas<\/em> I&#8217;ve <s>tripped over<\/s> discovered whilst deploying such rules across nearly a thousand repositories.<\/p>\n\n\n\n<p style=\"font-size:15px\">*Assuming we&#8217;re talking about corporate GitHub organisations&#8230; and by all, I mean ALL. Branch protection Rules should be the rule, not the exception. Have exceptions if you need them (you will!) but otherwise apply it everywhere.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Which branches to protect?<\/h2>\n\n\n\n<p>At the very least I&#8217;d suggest protecting your main branch; it&#8217;s probably called <em>master<\/em> or <em>main<\/em>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Protection Rules<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"100\" src=\"https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-1024x100.png\" alt=\"\" class=\"wp-image-8076\" srcset=\"https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-1024x100.png 1024w, https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-300x29.png 300w, https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-768x75.png 768w, https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-1200x117.png 1200w, https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image.png 1396w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>You almost certainly don&#8217;t want anyone merging directly to your master branch. It&#8217;s worth mentioning that this isn&#8217;t strictly <em>only<\/em> a security control; pull requests (PR&#8217;s) encourage healthy, secure and reliable codebases by pulling in appropriate team members to review changes to potentially critical services. Talking of reviews&#8230;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"160\" src=\"https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-1-1024x160.png\" alt=\"\" class=\"wp-image-8077\" srcset=\"https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-1-1024x160.png 1024w, https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-1-300x47.png 300w, https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-1-768x120.png 768w, https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-1-1200x187.png 1200w, https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-1.png 1296w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>You should check this box to enforce reviews for proposed code changes to the repository. The number of <em>approvals<\/em> required is largely dependent on SDLC maturity and criticality of the code in the repository.<\/p>\n\n\n\n<p class=\"has-background\" style=\"background-color:#a8002c\">Note: If your GitHub Actions Workflow permissions are set to read and write <em>and<\/em> you only require one approval, an adversary can trivially bypass this rule no matter how strict your other protection rules might be.<br>GitHub approvals are designed such that an author cannot approve their own PR&#8217;s, however, a compromised GitHub account can trigger a GitHub Action to create a PR (the PR will be created under a bot user) then approve it with the compromised account.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"83\" src=\"https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-3-1024x83.png\" alt=\"\" class=\"wp-image-8081\" srcset=\"https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-3-1024x83.png 1024w, https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-3-300x24.png 300w, https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-3-768x62.png 768w, https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-3.png 1092w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>Closely linked to the above; we want at least one reviewer but we don&#8217;t want just <em>anyone<\/em> reviewing and approving the code. We may want a specific team within the GitHub organisation to review and approve the code changes.<br>Even if you don&#8217;t mind <em>anyone<\/em> viewing and approving code changes, I would still recommend having a <code>CODEOWNERS<\/code> file with a team that everyone is a member of, bar service and machine accounts.<\/p>\n\n\n\n<p class=\"has-background\" style=\"background-color:#a8002c\">Note: <code>CODEOWNERS<\/code> failures are silent so I&#8217;m going to shamelessly plug one of <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/emtunc\/CODEOWNERS-Health\" data-type=\"URL\" data-id=\"https:\/\/github.com\/emtunc\/CODEOWNERS-Health\" target=\"_blank\">my GitHub repositories<\/a> to help discover such silent failures<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-4.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"85\" src=\"https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-4-1024x85.png\" alt=\"\" class=\"wp-image-8082\" srcset=\"https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-4-1024x85.png 1024w, https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-4-300x25.png 300w, https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-4-768x64.png 768w, https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-4.png 1198w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>It&#8217;s usually a good idea to dismiss an approval if additional commits are made after the fact. This helps protect against seemingly harmless but reliability affecting code changes to more malicious ones caused by compromised accounts.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-5.png\"><img loading=\"lazy\" decoding=\"async\" width=\"970\" height=\"90\" src=\"https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-5.png\" alt=\"\" class=\"wp-image-8084\" srcset=\"https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-5.png 970w, https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-5-300x28.png 300w, https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-5-768x71.png 768w\" sizes=\"auto, (max-width: 970px) 100vw, 970px\" \/><\/a><\/figure>\n\n\n\n<p>This is a new option that only got released a few months ago but is a big win for security. Previously, the only way to get any form of automation to work on a repository was to uncheck the &#8220;Include Administrators&#8221; option which meant that <em>any<\/em> repository admin <em>or<\/em> organisation owner could bypass branch protection rules for that repository.<\/p>\n\n\n\n<p>With this new option, we can apply all branch protection rules to everyone (including admins and owners!) <em>and<\/em> explicitly an automation user which is permitted to bypass the branch protection rules.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-6.png\"><img loading=\"lazy\" decoding=\"async\" width=\"748\" height=\"114\" src=\"https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-6.png\" alt=\"\" class=\"wp-image-8085\" srcset=\"https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-6.png 748w, https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-6-300x46.png 300w\" sizes=\"auto, (max-width: 748px) 100vw, 748px\" \/><\/a><\/figure>\n\n\n\n<p>Now that the option of <em>allowing specified actors to bypass PR requirements<\/em> exists, this option <em>must<\/em> be checked. I can&#8217;t think of any valid reasons not to at this point. Leave a comment if there&#8217;s an edge case I haven&#8217;t considered!<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-7.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"239\" src=\"https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-7-1024x239.png\" alt=\"\" class=\"wp-image-8086\" srcset=\"https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-7-1024x239.png 1024w, https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-7-300x70.png 300w, https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-7-768x179.png 768w, https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-7-1200x280.png 1200w, https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-7.png 1492w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>It&#8217;s a good idea to enable status checks that help enable your engineering team. Status checks can help maintain and improve the security and reliability of your services.<\/p>\n\n\n\n<p>Requiring branches to be up to date is more of a hygiene thing and is more likely to cause reliability issues than anything else if not checked.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><a href=\"https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-8.png\"><img loading=\"lazy\" decoding=\"async\" width=\"868\" height=\"108\" src=\"https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-8.png\" alt=\"\" class=\"wp-image-8088\" srcset=\"https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-8.png 868w, https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-8-300x37.png 300w, https:\/\/emtunc.org\/blog\/wp-content\/uploads\/2022\/01\/image-8-768x96.png 768w\" sizes=\"auto, (max-width: 868px) 100vw, 868px\" \/><\/a><\/figure>\n\n\n\n<p>This one might be a little controversial (feel free to leave a comment with your thoughts!) but I actually don&#8217;t recommend enabling this <em>unless<\/em> your organisation is already heavily invested and embedded in the GPG world (my condolences). <\/p>\n\n\n\n<p>This control adds very little (it can help mitigate impersonation attacks within the GitHub organisation) but comes at the heavy cost of creating, storing, distributing and managing GPG keys.<\/p>\n\n\n\n<p>When SSH signing becomes a thing then commit signing starts to sound a lot more attractive. Given SSH signing was released to Git late last year, I am hopeful that we will see it supported at some point in 2022!<\/p>\n\n\n\n<p>If you&#8217;re interested in reading more about commit signing and what attacks it can (and can&#8217;t!) defend again, check out <a href=\"https:\/\/dlorenc.medium.com\/should-you-sign-git-commits-f068b07e1b1f\" data-type=\"URL\" data-id=\"https:\/\/dlorenc.medium.com\/should-you-sign-git-commits-f068b07e1b1f\" target=\"_blank\" rel=\"noreferrer noopener\">Dan Lorenc&#8217;s blog where he explains it a lot more eloquently.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, I will be going through some of the essential Branch Protection Rules you should have on all* of your GitHub repositories. GitHub is constantly releasing new updates all the time but my recommendations stand as of January 2022. I&#8217;ve also added some gotchas I&#8217;ve tripped over discovered whilst deploying such rules across [&hellip;]<\/p>\n","protected":false},"author":32,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"templates\/template-full-width.php","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8074","post","type-post","status-publish","format-standard","hentry","category-tech"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1trTO-26e","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/emtunc.org\/blog\/wp-json\/wp\/v2\/posts\/8074","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/emtunc.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/emtunc.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/emtunc.org\/blog\/wp-json\/wp\/v2\/users\/32"}],"replies":[{"embeddable":true,"href":"https:\/\/emtunc.org\/blog\/wp-json\/wp\/v2\/comments?post=8074"}],"version-history":[{"count":12,"href":"https:\/\/emtunc.org\/blog\/wp-json\/wp\/v2\/posts\/8074\/revisions"}],"predecessor-version":[{"id":8099,"href":"https:\/\/emtunc.org\/blog\/wp-json\/wp\/v2\/posts\/8074\/revisions\/8099"}],"wp:attachment":[{"href":"https:\/\/emtunc.org\/blog\/wp-json\/wp\/v2\/media?parent=8074"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/emtunc.org\/blog\/wp-json\/wp\/v2\/categories?post=8074"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/emtunc.org\/blog\/wp-json\/wp\/v2\/tags?post=8074"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}