CHAPTER 09
Security and Best Practices
Updated: May 15, 2026
25 min read
# CHAPTER 9
Security and Best Practices
1. Introduction
GitHub is the epicenter of modern software development, making it the primary target for cyberattacks. A single misconfigured setting or a hastily merged Pull Request can expose a company's entire database, AWS infrastructure, and customer records to the public internet within seconds. The GitHub Flow methodology is designed for speed, but speed without guardrails is catastrophic. In this chapter, we will shift our focus from writing code to protecting it. We will learn how to enforce Branch Protection rules, manage repository secrets, and implement the security best practices required to safeguard enterprise repositories.2. Learning Objectives
By the end of this chapter, you will be able to:- Configure Branch Protection rules to prevent force-pushes and deletions.
- Enforce mandatory Code Reviews and Status Checks.
- Understand the catastrophic danger of committing hardcoded secrets.
- Utilize GitHub Secrets to manage CI/CD credentials securely.
- Enable Dependabot to monitor for vulnerable third-party dependencies.
3. Beginner Explanation
Imagine the vault door of a bank (Themain branch).
- No Security: Any employee, from the CEO to the newest teller, can walk up, open the vault, and take money out at any time. (A repository with no branch protection).
- Branch Protection: You install a dual-key system. A teller can request to open the vault (A Pull Request), but the vault will physically not unlock unless the Bank Manager also turns their key (Mandatory Review). Furthermore, a robotic security camera scans the teller to ensure they aren't wearing a mask before the door clicks open (Required Status Checks / CI Tests).
4. Branch Protection Rules
If a Junior Developer accidentally typesgit push origin main --force, they can instantly overwrite and destroy the entire production history of the company.
To prevent this, administrators use Branch Protection Rules.
- 1. Go to your repository on GitHub -> Settings -> Branches.
- 2. Click Add branch protection rule.
-
3.
Set the Branch name pattern to
main.
-
4.
Require a pull request before merging: This explicitly blocks anyone from typing
git push origin main. All code *must* go through a PR.
- 5. Require approvals: Set this to 1 or 2. This means the PR cannot be merged until a senior developer clicks the "Approve" button.
- 6. Do not allow bypassing the above settings: Ensure even administrators are forced to follow the rules.
5. Managing Secrets (GitHub Actions)
If your GitHub Action needs to deploy code to an AWS server, it needs an AWS password. NEVER type the password into the YAML file. (e.g.,password: supersecret123).
If you do, anyone who can read the repository can steal the password.
The Solution: GitHub Secrets
- 1. Go to repository Settings -> Secrets and variables -> Actions.
- 2. Click New repository secret.
-
3.
Name it
AWSPASSWORDand paste the actual password into the hidden value box.
- 4. In your YAML file, you reference the secret dynamically:
yaml
steps:
-
name: Deploy to AWS
run: ./deploy.sh
env:
PASSWORD: ${{ secrets.AWSPASSWORD }}
`
GitHub will inject the password into the script at the exact moment it runs, and instantly wipe it from memory, ensuring the password is never logged or exposed.
6. Mini Project: Configure Secure GitHub Repository
Let's lock down our practice repository.
Step-by-Step Walkthrough:
-
1.
Navigate to your
github-flow-practice repository on GitHub.
-
2.
Go to Settings, then click Branches on the left menu.
-
3.
Click Add branch protection rule.
-
4.
Type
main into the Branch name pattern.
-
5.
Check Require a pull request before merging.
-
6.
Check Require approvals (Set to 1).
-
7.
Scroll down and click Create.
-
8.
*The Test:* Open your local terminal. Make a quick edit to
README.md. Commit it. Now, try to push directly to main (git push origin main).
-
9.
*The Result:* GitHub will aggressively reject your push, throwing a terminal error stating that direct pushes are protected. You have successfully secured your repository!
7. Dependabot and Vulnerability Scanning
Modern software relies on thousands of downloaded, open-source packages (like React or Express). If a hacker discovers a vulnerability in one of those packages, your app is instantly vulnerable.
GitHub provides a free automated security guard called Dependabot.
-
Go to Settings -> Code security and analysis.
-
Enable Dependabot alerts and Dependabot security updates.
-
If a vulnerability is discovered in a package you are using, Dependabot will automatically open a Pull Request in your repository with the exact code required to update the package to a safe version. All you have to do is click Merge.
8. Best Practices
-
The Principle of Least Privilege: When adding collaborators to a corporate repository, never give everyone "Admin" access. Give them "Write" access. They should have exactly enough power to push code to a feature branch, and zero power to delete the repository or change security settings.
9. Common Mistakes
-
The
.env Panic: A developer accidentally commits a file containing their database password and pushes it to GitHub. Realizing their mistake, they quickly add the file to .gitignore, make a new commit, and push again. The password is still compromised. Hackers scan the raw Git history, not just the current files. If a password touches GitHub for even one second, it is compromised. You must immediately change the password on the database server itself.
10. Exercises
-
1.
Explain the purpose of Branch Protection rules. Why is blocking "Force Pushes" to the
main branch critical?
-
2.
Describe the secure workflow for passing sensitive API keys into a GitHub Actions CI/CD pipeline without hardcoding them in the YAML file.
11. FAQs
Q: Can I protect branches other than main?
A: Yes. If your team maintains a staging branch or long-term release branches like v1.0 and v2.0, you can create specific branch protection rules for each of them to prevent accidental deletions or unreviewed code injections.
12. Summary
In Chapter 9, we fortified our collaborative workflow, establishing the administrative boundaries necessary to operate securely at scale. We eliminated the human error of accidental direct pushes by strictly enforcing Branch Protection rules and mandatory Code Reviews on our main` branch. We addressed the massive attack vector of compromised credentials by mastering GitHub Secrets, ensuring CI/CD pipelines can authenticate securely without exposing plaintext passwords. Finally, we activated automated supply-chain security via Dependabot. Our repository is now not only collaborative and automated, but cryptographically secure.