Security Scanning in CI
Dependabot, CodeQL, Trivy image scanning, and supply chain security — catch vulnerabilities before they reach production
Skills taught: cicd.dependabot, cicd.codeql, cicd.trivy, cicd.npm-audit, cicd.supply-chain
The three layers of security scanning
Security scanning in CI operates at three levels: dependency vulnerabilities (known CVEs in your npm packages), code vulnerabilities (SQL injection, XSS, hardcoded secrets in your own code), and container vulnerabilities (vulnerable OS packages and binaries in Docker images). Automated tools cover all three — no security expertise required.
Dependabot — automated dependency updates
# Dependabot is not a workflow — it's a GitHub configuration file
# Place it at: .github/dependabot.yml
version: 2
updates:
# Keep npm dependencies up to date
- package-ecosystem: 'npm'
directory: '/'
schedule:
interval: 'weekly' # or 'daily', 'monthly'
day: 'monday'
time: '09:00'
# Group minor/patch updates into one PR (less noise)
groups:
non-major:
update-types:
- 'minor'
- 'patch'
# Ignore major updates to specific packages (breaking changes)
ignore:
- dependency-name: 'react'
update-types: ['version-update:semver-major']
# Keep GitHub Actions up to date
- package-ecosystem: 'github-actions'
directory: '/'
schedule:
interval: 'monthly'
npm audit in CI
- name: Audit dependencies
run: npm audit --audit-level=high
# Levels: low, moderate, high, critical
# Fails only for high/critical severity vulnerabilities
# 'npm audit fix' auto-fixes where possible (run locally)
# Alternative: use 'audit-ci' for more control
- name: Audit with audit-ci
run: npx audit-ci --high
# audit-ci allows allowlisting specific CVEs with a reason:
# audit-ci.json: { "allowlist": ["GHSA-xxxx-xxxx-xxxx"] }
CodeQL — static analysis for your code
name: CodeQL Analysis
on:
push:
branches: [main]
pull_request:
schedule:
- cron: '0 12 * * 1' # weekly scan
jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
security-events: write
contents: read
steps:
- uses: actions/checkout@v4
# Initialize CodeQL for JavaScript/TypeScript
- name: Initialize CodeQL
uses: github/codeql-action/init@v3
with:
languages: javascript-typescript
# CodeQL checks for:
# - XSS vulnerabilities
# - SQL injection
# - Path traversal
# - Prototype pollution
# - Hardcoded credentials
# Results appear in: Security → Code scanning alerts
# Auto-build (CodeQL traces the build)
- uses: github/codeql-action/autobuild@v3
# Run the analysis and upload results
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
with:
category: '/language:javascript-typescript'
Trivy — scan Docker images for CVEs
- name: Build Docker image
run: docker build -t myapp:${{ github.sha }} .
- name: Scan image with Trivy
uses: aquasecurity/trivy-action@master
with:
image-ref: myapp:${{ github.sha }}
format: sarif
output: trivy-results.sarif
# Fail the job on HIGH or CRITICAL CVEs:
exit-code: '1'
severity: 'HIGH,CRITICAL'
ignore-unfixed: true # skip CVEs with no available fix
- name: Upload Trivy SARIF
if: always()
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: trivy-results.sarif
# Results appear in Security → Code scanning alerts as inline annotations
Warning: Pinning action versions is critical for supply chain security. Using uses: some-action@main or uses: some-action@v3 means any push to that repo's main branch or any new v3 tag can silently change the code your workflow runs. Instead, pin to a full commit SHA: uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683. Then use Dependabot to keep the SHA up-to-date — you get reproducibility AND updates.
Frequently Asked Questions
CodeQL flagged something as a vulnerability but I believe it's a false positive. What do I do?
Go to Security → Code scanning alerts on GitHub, open the alert, and click 'Dismiss alert' with a reason ('False positive' or 'Won't fix'). Add a comment explaining why. Dismissed alerts don't count against your security posture metrics and won't re-alert on the same location. This is better than disabling the query globally.