11. GitHub Advanced Security: Dependabot, CodeQL, and Secret Scanning
GitHub Advanced Security provides tools for securing your code and dependencies. Dependabot alerts and updates dependencies. CodeQL performs code analysis to find vulnerabilities. Secret scanning detects exposed secrets. These tools are essential for maintaining secure repositories. Security is not an afterthought — it's built into the development workflow.
The key insight is that security is built into the development workflow. Dependabot, CodeQL, and secret scanning work automatically in the background, finding issues before they become problems. This is a shift-left security approach. Instead of finding vulnerabilities after deployment, you find them during development. This is much cheaper and safer.
This module covers each security feature, how to enable them, and how to respond to alerts. You'll learn to use Dependabot for dependency management, CodeQL for code analysis, and secret scanning for secrets detection.
Dependabot: Dependency Management
Dependabot automatically monitors dependencies and creates pull requests when updates are available. It supports npm, pip, Gradle, Maven, and many other package managers. Dependabot also alerts you to security vulnerabilities in dependencies. Dependabot is essential for keeping dependencies up to date. It reduces the effort of manual dependency management and improves security. Configure Dependabot in the dependabot.yml file.
Dependabot is one of the most valuable GitHub features. It automatically finds outdated or vulnerable dependencies and creates PRs to update them. This keeps your code secure without manual effort. It also reduces the risk of leaving known vulnerabilities in your codebase.
CodeQL: Code Analysis
CodeQL is a semantic code analysis engine that finds vulnerabilities in code. It supports many languages including JavaScript, Python, Java, Go, and C++. CodeQL analyzes code for security vulnerabilities, bugs, and code quality issues. CodeQL runs in GitHub Actions. It analyzes every pull request and push to main. Results appear in the Security tab. CodeQL is essential for catching vulnerabilities before they reach production. It's the standard code analysis tool in GitHub.
CodeQL is different from simple linting tools. It understands the semantics of your code — how data flows, what functions are called, and what security vulnerabilities could be exploited. This allows it to find subtle security issues that other tools miss. CodeQL is the security analysis tool used by GitHub itself.
Secret Scanning
Secret scanning detects exposed secrets (API keys, passwords, tokens) in repositories. It scans every commit for known secret patterns. When a secret is found, GitHub alerts the repository owner and provides remediation advice. Secret scanning is essential for preventing credential leaks. It's enabled by default for public repositories. For private repositories, it requires GitHub Advanced Security. Enable secret scanning to protect your credentials.
Secret scanning is your first line of defense against credential leaks. It scans your code for patterns that look like API keys, tokens, and passwords. If it finds one, it alerts you immediately. This allows you to revoke the exposed credential before it can be exploited. This is essential for any production repository.
Step-by-Step: Setting Up GitHub Advanced Security
Follow these steps to enable GitHub Advanced Security features for your repository.
Step 1: Enable Dependabot
Create .github/dependabot.yml:version: 2<br>updates:<br> - package-ecosystem: <strong><code>npm
directory: /
schedule:
interval: weekly
open-pull-requests-limit: 10
Commit and push. Dependabot will start scanning your dependencies and creating PRs for updates.
Step 2: Enable CodeQL
Create .github/workflows/codeql.yml:name: <strong><code>CodeQL
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
schedule:
- cron: 0 0 * * 1
jobs:
analyze:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: github/codeql-action/init@v2
with:
languages: javascript
- uses: github/codeql-action/analyze@v2
Commit and push. CodeQL will analyze your code for vulnerabilities.
Step 3: Enable Secret Scanning
Go to your repository settings. Click Security in the left sidebar. Click Enable under Secret scanning if it's not already enabled.
Step 4: Review alerts
After these features are enabled, go to the Security tab of your repository. You'll see Dependabot alerts, CodeQL alerts, and secret scanning alerts. Review and fix any issues found.
Try It Yourself: Add a vulnerable dependency to your package.json (like an old version of lodash with known vulnerabilities) and watch Dependabot detect it. Add a fake secret to your code (like AWS_SECRET_KEY=sk_test_123) and watch secret scanning detect it.
Knowledge Check
Ready to test your understanding of 11. GitHub Advanced Security: Dependabot, CodeQL, and Secret Scanning?