Home
IT Hub

GitHub Security Checklist: 9 Must-Follow Best Practices

GitHub
Reco Security Experts
Updated
November 16, 2024
November 17, 2024

Did you know that over 83% of organizations experienced at least one successful application exploit in the last 12 months? This statistic from a recent cybersecurity report highlights the critical importance of securing our code repositories and development workflows. As the world's leading platform for version control and collaboration, GitHub has an important role in the software development life cycle for millions of developers and organizations. In this guide, we'll explore nine essential GitHub security best practices that will help you protect your code, your team, and your users in 2024 and beyond.

1. Enable Two-Factor Authentication (2FA)

One of the most fundamental steps in securing your GitHub organization account and repositories is enabling Two-Factor Authentication (2FA). This simple yet powerful security measure adds an extra layer of protection beyond just your password.

Why is 2FA important?

  • 2FA significantly reduces the risk of unauthorized access to your account, even if your password is compromised.
  • It helps protect not only your personal account but also the repositories and organizations you have access to.
  • GitHub is moving towards making 2FA mandatory for all contributors, so it's best to adopt it early.

How to enable 2FA on GitHub

1. Go to your GitHub account settings

The screenshot shows the GitHub dashboard with the "Account Settings" section open. It highlights user account options and configuration settings that are available for customization.

The above screenshot showcases the GitHub dashboard with the Settings option prominently highlighted. This section allows users to manage their account preferences and configure various settings related to their GitHub profile.

2. Click on "Password and authentication"

This screenshot shows the Password and Authentication settings in the GitHub account settings, where users can manage their password, enable two-factor authentication, and adjust security settings for their account.

3. Scroll down to "Two-factor authentication" and click "Enable two-factor authentication"

The screenshot above illustrates the Two-Factor Authentication setup on GitHub, showcasing the available options for users to enhance their account security, including SMS codes and authentication apps.

4. Choose your preferred 2FA method (GitHub Mobile, Passkey, or Authenticator App)

Pro tip: We recommend using Authenticator apps like Google Authenticator or Authy for the most secure 2FA experience because the authentication code changes every few seconds.

2. Implement Strong Access Control

Proper access control is important for maintaining the security of your GitHub repositories, especially when working in teams or on open-source projects.

Best practices for access control

  1. Use the principle of least privilege: Only grant users the minimum level of access they need to perform their tasks.
  2. Regularly review and update access permissions: Set up a schedule to audit user access and remove permissions for inactive members or those who no longer require access.
  3. Utilize GitHub Organizations: Create separate organizations for different projects or teams to better manage access control at scale.
  4. Implement fine-grained access control: Use GitHub's repository roles and team permissions to create a granular access control system.

Example: Setting up team-based access control

Here's a simple table illustrating how you might structure team permissions for a typical software project:

Team Repository Access Permissions
Developers Main repository Read, Write
Code Reviewers Main repository Read, Write, Maintain
QA Team Testing branch Read, Write
DevOps CI/CD configuration Admin
Project Managers All repositories Read

By implementing these access control measures, you can significantly reduce the risk of unauthorized changes to your codebase and limit the potential impact of a compromised account.

To set role-based access in your GitHub organization, Go to the settings of your organization account:

The above screenshot displays the GitHub organization account settings screen, focusing on the highlighted "Settings" option in the top navigation bar, which is the starting point for configuring role-based access.

Open Organization roles menu:

This screenshot displays the GitHub organization settings, highlighting the section for managing organization roles, where administrators can define specific permissions and access levels for members within the organization.

Here, you can learn about role management and what permissions are possible:

Here, the screenshot displays the role management settings in GitHub, showcasing different user role options available for managing access and permissions within the organization. Users can assign roles such as read, write, or triage to team and members.

Now,  In order to implement role-based access within your Ggithub organization teams, Go to “Role Assignment”. To add new role management, click the ‘New role assignment’ button.

This screenshot displays the role assignments section in GitHub, where administrators can view and manage the specific roles assigned to users within the organization. The interface provides options to add or modify user roles, ensuring appropriate access and permissions are granted to team members.

You can assign a single user or a team with a set of permissions as they require.

This screenshot displays available options for assigning teams or individual users to specific roles within the Nactore-org GitHub organization. Users can select from a list of roles to ensure proper access and permissions are granted based on their responsibilities.

3. Use GitHub's Dependency Security Features

GitHub provides a robust set of built-in security features that can help you identify and address potential vulnerabilities in your code. Let's explore some of the most important ones:

Dependabot Alerts

Dependabot alerts notify you when vulnerabilities are detected in your dependencies.

How to enable:

1. Go to your repository's "Security" section

The image displays the GitHub repository interface, focusing on the "Security" section in the sidebar. This area provides access to security-related features and settings for managing the repository's safety and integrity.

2. Enable "Dependabot alerts"

The above screenshot displays the Dependabot alerts feature in GitHub, allowing users to manage and disable alerts related to outdated dependencies in their repository for better security management

3. It will redirect you to the settings page, where you just have to click on the “Enable” button

The above screenshot displays the GitHub settings page, emphasizing the highlighted "Enable" button users must click to activate the selected feature.

4. Once you enable it, you can set your custom rules for alerts

This screenshot displays the settings for creating custom alert rules in GitHub after enabling the feature. Users can configure specific criteria to manage notifications effectively.

Dependabot Security Updates

This feature automatically creates pull requests to update vulnerable dependencies to the minimum version that resolves the vulnerability.

Here, this screenshot displays the option to enable Dependable Security Updates, allowing users to receive notifications and automatically apply security fixes for their repository dependencies.

Code Scanning with CodeQL

Code scanning uses CodeQL to analyze your code and find security vulnerabilities and coding errors.

How to enable:

1. Go to your repository's settings

2. Click on "Code Security” under "Security”.

3. Scroll Down to Code scanning.

This screenshot shows CodeQL's code scanning capabilities, highlighting its role in analyzing code to detect security vulnerabilities and coding errors, ensuring enhanced code quality and security.

4. Choose "Set up" next to "CodeQL analysis" and choose “Default”.

This screenshot highlights the default settings for CodeQL analysis, indicating that the tool will automatically detect and apply the optimal configuration for identifying security vulnerabilities and coding errors within the repository.

5. Click on “Enable CodeQL”

This screenshot displays the options for enabling CodeQL analysis, highlighting the "Enable CodeQL" button. Users can customize their analysis settings to optimize code scanning for security vulnerabilities and coding errors, ensuring robust code quality and safety.

By leveraging these built-in security features, you can proactively identify and address potential security issues in your codebase.

4. Implement Branch Protection Rules

Branch protection rules are a powerful feature in GitHub that allows you to enforce certain workflows and prevent unauthorized changes to important branches.

Key branch protection rules to consider

  1. Require pull request reviews: Enforce code review before merging changes.
  2. Require status checks to pass: Ensure CI/CD pipelines and tests pass before allowing merges.
  3. Require signed commits: Enforce commit signing to verify the authenticity of changes.
  4. Restrict who can push to matching branches: Limit direct pushes to protected branches.

How to set up branch protection rules

1. Go to your repository's settings

2. Click on "Branches"

This screenshot displays the GitHub repository settings, specifically the "Branches" section, where users can manage branch protections, default branches, and related settings. It guides users in figuring out how branches are handled within their repository.

3. Under "Branch protection rules," click "Add branch ruleset"

This screenshot displays the branch protection rules section in the GitHub repository settings, emphasizing the option to add a new ruleset for enhanced branch protection.

4. Add a new ruleset and name it. You can also define a bypass list to exempt teams or users from going beyond such rules.

The above screenshot displays the option to create a new branch protection ruleset by naming it and specifying a bypass list. Users can exempt certain teams or individuals from the defined rules, allowing for more flexible branch management while maintaining security standards.

5. Configure the desired protection settings and add a target branch.

Here, the screenshot allows users to configure branch protection settings to specify the desired protections and select the target branch to which these rules will apply.

6. Click "Create" to save the changes.

Pro tip: Start with strict protection rules on your main branch and gradually apply them to other important branches as your team becomes comfortable with the workflow.

5. Secure Your CI/CD Pipelines

Continuous Integration and Continuous Deployment (CI/CD) pipelines are essential for modern software development, but they can also introduce security risks if not properly secured. Here are some best practices for securing your CI/CD pipelines on GitHub:

  1. Use GitHub-hosted runners: When possible, use GitHub-hosted runners to ensure you're working with a clean, up-to-date environment.
  2. Limit permissions for GitHub Actions: Use the principle of least privilege when setting up permissions for your GitHub Actions workflows.
  3. Secure secrets and credentials: Use GitHub's secret management features to securely store and use sensitive information in your workflows.
  4. Implement checks and gates: Add security checks and approval gates in your CI/CD pipeline to prevent vulnerable code from being deployed.
  5. Regularly update Actions: Keep your GitHub Actions up to date to ensure you have the latest security patches and features.

Securing secrets in GitHub Actions

Here's an example of how to securely use a secret in a GitHub Actions workflow:

name: Deploy to Production

on:
  push:
    branches: [ main ]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Deploy to server
      env:
        SERVER_SSH_KEY: ${{ secrets.SERVER_SSH_KEY }}
      run: |
        echo "$SERVER_SSH_KEY" > deploy_key.pem
        chmod 600 deploy_key.pem
        ssh -i deploy_key.pem user@example.com './deploy.sh'
        rm deploy_key.pem        

In this example, we're using a GitHub secret (SERVER_SSH_KEY) to securely store and use an SSH key for deployment. The secret is never exposed in logs or to unauthorized users.

To set the secrets for your repository

1. Go to the repository “Settings”

2. Click on “Secrets and Variables”

This screenshot displays the "Secrets and Variables" section within the repository settings, where users can manage environment secrets. It emphasizes the importance of securing sensitive information necessary for the repository's operation.

3. Click on “Manage Environment Secrets”

4. Click on “New environment”

This screenshot highlights the "New Environment" option, which allows users to create environments that workflows can utilize for deployment purposes. It underscores the role of environments in managing deployment processes efficiently.

Here, the screenshot highlights the "New Environment" form, prompting users to input a name for their new environment. It emphasizes the simplicity of creating custom environments for managing deployment workflows effectively.

5. Click on “Configure environment” so that it will start managing your key environments. After that,  you can keep on adding secret keys and its values to the same environment.

The above screenshot highlights the "Configure Environment" button, allowing users to start managing their key environments. Once clicked, users can add secret keys and their corresponding values to the environment, facilitating secure management of sensitive information during deployments.

Here, this screenshot highlights the "Add Secret" option, enabling users to input the name and value of a new secret. This feature allows for the secure storage of sensitive information utilized in workflows and deployments within the repository.

6. Add your secret and now you will be able to use it in your deployment scripts.

6. Use Secret Scanning

Secret scanning is a security feature that helps prevent the accidental exposure of sensitive information in your GitHub repositories. It automatically detects tokens, private keys, and other secrets that have been committed to your repository.

How secret scanning works

  1. GitHub scans repositories for known types of secrets
  2. When a match is found, GitHub notifies the appropriate service provider
  3. The service provider may validate the secret and either revoke it or contact you directly

Enabling secret scanning

  1. Go to your repository's settings
  2. Click on "Code Security"
  3. Enable "Secret scanning"

Here, it highlights the "Secret Scanning" feature in the repository settings, displaying the option to disable scanning. Enabling secret scanning helps identify and protect sensitive information in code, ensuring that no credentials or secrets are exposed during development and collaboration.

Best practices for handling secrets

  1. Never commit secrets directly: Use environment variables or secure secret management tools instead.
  2. Rotate exposed secrets immediately: If a secret is exposed, revoke and replace it as soon as possible.
  3. Use GitHub's secret management: For GitHub Actions, use repository or organization secrets to securely store sensitive information.

7. Monitor Repository Activity

Monitoring your repository activity is crucial for detecting and responding to potential security threats quickly. GitHub provides several tools to help you keep track of what's happening in your repositories.

You can visit it from the “Insights” Tab of your project

The screenshot highlights the "Insights" tab in the repository, where users can monitor activity to spot and address any security issues. GitHub provides tools here to help track what's happening in your repositories, making it easier to stay updated on changes and activities.

Key areas to monitor

  1. Commit activity: Watch for unusual commit patterns or unexpected changes.
  2. Pull requests: Monitor for suspicious pull requests, especially from new contributors.
  3. Issue tracker: Keep an eye on reported security issues or unusual activity in the issue tracker.
  4. Access changes: Monitor for changes in repository access or team memberships.
  5. Audit logs: Regularly review audit logs for any suspicious activity.

8. Implement a Security Policy

A clear and comprehensive security policy is essential for maintaining the security of your GitHub repositories and guiding your team's security practices. It also allows contributors or users to report bugs related to security.

Key components of a GitHub security policy

  1. Reporting security vulnerabilities: Provide clear instructions on how to report security issues.
  2. Response process: Outline how your team will respond to reported vulnerabilities.
  3. Disclosure policy: Explain your approach to disclosing vulnerabilities (e.g., responsible disclosure timeframes).
  4. Scope: Clearly define which repositories and versions are covered by the policy.
  5. Bug bounty program: If applicable, provide details about your bug bounty program.

Creating a SECURITY Policy File

1. Go to the Security tab of your project

2. Click on “Set up a Security Policy”

Here, the above screenshot highlights the "Set up a Security Policy" option in the Security tab of your project. Users can create a SECURITY policy file by following this prompt, helping outline the repository's security guidelines and procedures.

GitHub recommends creating a SECURITY.md file in the root of your repository to communicate your security policy. Here's a basic template:

# Security Policy

## Reporting a Vulnerability

If you discover a security vulnerability in this project, please report it to us as soon as possible. We appreciate your efforts to responsibly disclose your findings.

To report a vulnerability, please email security@example.com with the following information:
- A description of the vulnerability
- Steps to reproduce the issue
- Possible impacts of the vulnerability
- Any potential mitigations you've identified

## Response Process

We are committed to the following response targets:
- Initial response: Within 48 hours
- Confirmation of the issue: Within 5 business days
- Resolution timeframe: Varies depending on the complexity of the issue

## Disclosure Policy

We follow a coordinated disclosure process:
1. Once a security report is received, we will work diligently to validate the issue.
2. We will prepare fixes for all supported versions of the project.
3. We will notify the reporter when we are ready to publish the fix.
4. We will release the fix and publicly disclose the issue.

## Scope

This security policy applies to the latest version of our main branch.

Thank you for helping to keep our project and our users secure!

Adjust this template to fit your project's specific needs and processes.

9. Educate Your Team on Security Best Practices

Creating a security-conscious culture within your development team is crucial for maintaining the overall security of your GitHub repositories and workflows. Here are some key areas to focus on when educating your team:

  1. Password hygiene: Encourage the use of strong, unique passwords and password managers.
  2. 2FA adoption: Ensure all team members understand the importance of Two-Factor Authentication and how to use it effectively.
  3. Secure coding practices: Train developers on common security vulnerabilities and how to avoid them in their code.
  4. GitHub-specific security features: Familiarize the team with GitHub's security features and how to use them effectively.
  5. Incident response: Establish clear procedures for reporting and responding to security incidents.

Sample security training schedule

Topic Frequency Format
General Security Awareness Quarterly Interactive webinar
Secure Coding Practices Monthly Hands-on workshop
GitHub Security Features Bi-monthly Lunch and learn session
New Hire Security Onboarding As needed One-on-one training
Security Tool Updates As needed Email newsletter

Security education is an ongoing process, so regularly reinforcing key concepts and keeping the team updated on new security threats and best practices will be helpful in the long run.

GitHub Security Checklist Summary

Here’s a quick reference table summarizing the key GitHub security best practices, their purposes, and tips for implementation. Use this as a handy checklist to enhance your GitHub security.

Best Practice Purpose Implementation Tip
Enable 2FA Prevent unauthorized access Enable in Account Settings
Strong Access Control Limit access to essential personnel Use least privilege, review access regularly
Dependency Security Find and fix dependency issues Enable Dependabot and CodeQL
Branch Protection Rules Enforce branch security rules Require PR reviews, signed commits
Secure CI/CD Pipelines Protect deployment process Secure secrets, limit permissions
Secret Scanning Detect exposed secrets Enable in Security Settings
Monitor Activity Track suspicious changes Review audit logs regularly
Security Policy Guide vulnerability handling Create SECURITY.md file
Team Security Education Promote security practices Conduct regular training

By implementing these best practices and staying vigilant, you can create a strong security posture for your GitHub repositories and contribute to a safer open-source ecosystem for everyone or even have a healthy and secure closed-source organization administration.

Conclusion

To ensure robust security for your GitHub repositories, following best practices is essential. By enabling 2FA, managing access controls, and using GitHub’s built-in security tools like Dependabot and CodeQL, you add layers of protection to your codebase. Implementing branch protection rules, securing your CI/CD pipelines, and using secret scanning further fortify your projects. Regular monitoring, a well-crafted security policy, and continuous team education complete a comprehensive security strategy. Adhering to these measures helps maintain a secure and resilient GitHub environment.

FAQs

Q: How can I stay updated on the latest GitHub security features?

A: Follow the GitHub Security Lab (https://securitylab.github.com/), subscribe to the GitHub blog, and regularly check the GitHub Changelog for updates on new security features and best practices.

Q: How often should I update my dependencies?

A: We recommend reviewing and updating dependencies at least monthly, with critical security updates applied as soon as possible.

Q: Is it necessary to enable 2FA for all team members?

A: Yes, enabling 2FA for all team members is crucial for maintaining the overall security of your repositories and organization.

Q: How can I encourage my team to follow security best practices?

A: Regular training, clear documentation, and leading by example are effective ways to encourage good security practices. Consider implementing security champions within your team to promote and support security initiatives.

Q: What should I do if I discover a security vulnerability in my repository?

A: If you discover a vulnerability, assess its severity, develop and test a fix, and release an update as soon as possible. If the vulnerability affects users, consider issuing a security advisory through GitHub.

Explore More
See more articles from our Hub

Start Securing Your Entire SaaS Lifecycle

Request a demo