GitHub Security
Learn about GitHub's security features to protect your code, detect vulnerabilities, and implement secure development practices.
GitHub Security
GitHub provides a comprehensive set of security features designed to help developers identify and fix vulnerabilities, enforce secure development practices, and protect their code. These tools integrate directly into your GitHub workflow, making security a natural part of your development process.
Security Features Overview
- Dependabot Alerts: Notifications about vulnerable dependencies
- Dependabot Updates: Automatic pull requests to update dependencies
- Dependency Graph: Visualization of project dependencies
- Dependency Review: Review dependency changes in pull requests
- Code Scanning: Static analysis to find security issues
- Secret Scanning: Detection of leaked secrets
- Security Advisories: Create and publish security advisories
- Security Policies: Define security reporting guidelines
- Branch Protection: Enforce code review and status checks
- Two-factor authentication: Enhanced login security
- SAML SSO: Enterprise identity management
- IP Allow Lists: Restrict access by IP address
- Security Keys: Support for physical security keys
Setting Up GitHub Security Features
Dependency Security
-
Enable the Dependency Graph (enabled by default for public repositories)
- For private repositories: Repository Settings > Security & analysis > Enable Dependency graph
-
Enable Dependabot alerts
- Go to Repository Settings > Security & analysis
- Click "Enable" next to Dependabot alerts
-
Configure Dependabot security updates
- Go to Repository Settings > Security & analysis
- Click "Enable" next to Dependabot security updates
-
For more control, create a
dependabot.yml
file in.github
directory:version: 2 updates: - package-ecosystem: "npm" directory: "/" schedule: interval: "weekly" open-pull-requests-limit: 10
You can configure different update schedules for different package ecosystems in the same repository, allowing for ecosystem-specific update strategies.
Code Scanning with CodeQL
- Go to your repository's Security tab
- Select "Code scanning" from the menu
- Click "Set up code scanning"
- Choose "CodeQL Analysis"
- Commit the workflow file to your repository
Example CodeQL workflow:
name: "CodeQL"
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
schedule:
- cron: '0 0 * * 0'
jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write
strategy:
fail-fast: false
matrix:
language: [ 'javascript', 'python' ]
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
- name: Autobuild
uses: github/codeql-action/autobuild@v2
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
Secret Scanning
GitHub automatically scans public repositories for known secret formats. For private repositories:
- Go to Repository Settings > Security & analysis
- Click "Enable" next to Secret scanning
If a secret is detected, GitHub notifies the service provider who issued the secret. They can then revoke the compromised secret, issue a new one, or contact you directly.
Creating a Security Policy
A security policy helps others know how to report security vulnerabilities:
- Go to your repository's Security tab
- Click "Security policy"
- Click "Start setup"
- Edit the SECURITY.md template
- Commit the file to your repository
Example security policy:
# Security Policy
## Supported Versions
These versions are currently supported with security updates:
| Version | Supported |
| ------- | ------------------ |
| 5.1.x | :white_check_mark: |
| 5.0.x | :x: |
| 4.0.x | :white_check_mark: |
| < 4.0 | :x: |
## Reporting a Vulnerability
To report a vulnerability:
1. Go to the Security tab of this repository
2. Click "Report a vulnerability"
3. Fill out the form with details about the vulnerability
We will respond within 48 hours and keep you updated on our progress.
Security Best Practices for GitHub Repositories
Configure branch protection rules:
- Go to repository Settings > Branches
- Click "Add rule" next to Branch protection rules
- Configure these recommended settings:
- Require pull request reviews before merging
- Require status checks to pass before merging
- Require signed commits
- Include administrators
- Regularly audit repository access
- Follow the principle of least privilege
- Use fine-grained personal access tokens
- Enable two-factor authentication for all contributors
- Review and prune inactive collaborators
- Use a
.gitignore
file to prevent committing sensitive files - Store secrets in GitHub Secrets, not in code
- Include security testing in CI/CD pipelines
- Review dependency license compliance
- Implement codeowners to enforce reviews by security experts
Security Advisories
When you discover a vulnerability in your project:
- Go to the Security tab of your repository
- Click "Advisories"
- Click "New draft security advisory"
- Fill in details about the vulnerability
- Work privately to fix the issue
- Publish the advisory when ready
Creating a security advisory automatically creates a temporary private fork where you can collaborate on fixing the vulnerability before disclosure.