| | |

GitHub 21 ๐ŸŒ GitHub Secret Scanning and CodeQL Analysis

โฑ๏ธ Estimated read: 8 min | |

GitHub provides two distinct static analysis security features: Secret Scanning, which detects leaked credentials in code and text, and CodeQL Analysis, which performs semantic code analysis to find vulnerabilities like SQL injection or cross-site scripting. A critical change occurred in May 2025: CodeQL no longer detects hardcoded secrets. Secret Scanning is now the sole mechanism for credential detection, offering higher accuracy and eliminating duplicate alerts between the two tools .

Key point: Secret Scanning catches leaked API keys and tokens (including in Git history and issues), while CodeQL traces data flow to find logic flaws. They are complementary, but their responsibilities are strictly separated: CodeQL no longer looks for secrets.


Why Secret Scanning and CodeQL exist

The credential leak problem. Committing an API key or a database password is one of the most common security mistakes. Secret Scanning scans the entire Git history, issues, pull requests, and wikis for known secret patterns (over 300 providers). If a secret is detected, it generates an alert. Push Protection goes further, blocking the commit from ever reaching the repository .

The semantic vulnerability problem. Linters find style issues; Secret Scanning finds keys. Neither finds a SQL injection vulnerability that spans multiple files. CodeQL treats code as a queryable database, building an abstract syntax tree and data-flow graph to trace how untrusted input reaches sensitive functions (like a database query). This is semantic analysis, not pattern matching .

The product split problem. In 2025, GitHub split Advanced Security (GHAS) into two purchasable products: GitHub Code Security (for CodeQL) and GitHub Secret Protection (for Secret Scanning and Push Protection). For public repositories, basic Secret Scanning and CodeQL are free. For private repositories, you need the respective license .

The noise problem. Before May 2025, both CodeQL and Secret Scanning could detect a hardcoded password, creating two alerts for the same issue. GitHub disabled the CodeQL queries for hardcoded secrets to consolidate alert management into a single, more accurate tool .

The trade-off. CodeQL is powerful but slow. Building the database and running queries can take 20โ€“40 minutes for large codebases. Secret Scanning is fast but only finds secrets it has patterns for (though custom patterns can be defined) .


a. Configuring Secret Scanning and Push Protection

Secret Scanning must be enabled in the repository settings. Push Protection is a separate toggle that blocks commits at the command line or web interface.

Settings โ†’ Advanced Security โ†’ Secret Protection โ†’ Enable
Settings โ†’ Advanced Security โ†’ Push protection โ†’ Enable

When Push Protection is active and you attempt to push a commit containing a secret, the push is rejected with an error. You have three options:

  1. Remove the secret from the commit (using git commit --amend or git rebase -i) .
  2. Bypass the protection by clicking a link in the error message and providing a reason (e.g., “used in tests”) .
  3. If delegated bypass is enabled, request permission from a security manager.

You can exclude specific directories (like test/fixtures/) from scanning using a .github/secret_scanning.yml file with a paths-ignore list .


b. Setting up CodeQL Analysis

CodeQL runs via GitHub Actions. The easiest setup is the Default configuration, which automatically selects the languages and triggers. For more control, use Advanced setup, which generates a codeql.yml workflow file.

name: CodeQL
on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
  schedule:
    - cron: '30 6 * * 1' # Weekly

jobs:
  analyze:
    runs-on: ubuntu-latest
    permissions:
      security-events: write
    strategy:
      matrix:
        language: [ 'javascript-typescript' ]
    steps:
      - uses: actions/checkout@v4
      - uses: github/codeql-action/init@v4
        with:
          languages: ${{ matrix.language }}
          queries: security-extended
      - uses: github/codeql-action/analyze@v4

The security-events: write permission is required to upload the SARIF results to GitHub . The queries: security-extended option runs more queries than the default suite.

For compiled languages (C/C++, Java), you may need to specify a build-mode. autobuild attempts to build automatically; manual allows you to insert custom build commands .


Complete Example Session

# ============================================
# PART 1: ENABLE SECRET SCANNING
# ============================================

# Settings โ†’ Advanced Security โ†’ Secret Protection โ†’ Enable
# This scans the entire history for known secret patterns.
# ============================================
# PART 2: ENABLE PUSH PROTECTION
# ============================================

# Settings โ†’ Advanced Security โ†’ Push protection โ†’ Enable
# ============================================
# PART 3: TEST PUSH PROTECTION
# ============================================

# Create a file with a fake AWS key:
echo "aws_access_key_id = AKIAIOSFODNN7EXAMPLE" > test.txt
git add test.txt
git commit -m "Add test file"
git push
# The push is blocked by Push Protection.
# ============================================
# PART 4: BYPASS PUSH PROTECTION
# ============================================

# Follow the link in the error message.
# Select "It's used in tests" as the reason.
# Re-push within 3 hours.
# An alert is created and auto-closed.
# ============================================
# PART 5: CONFIGURE CODEQL DEFAULT SETUP
# ============================================

# Settings โ†’ Code security and analysis
# CodeQL analysis โ†’ Set up โ†’ Default
# GitHub creates the workflow automatically.
# ============================================
# PART 6: VIEW CODEQL ALERT
# ============================================

# Security โ†’ Code scanning alerts
# Click an alert to see the data flow path.
# Copilot Autofix may suggest a patch.
# ============================================
# PART 7: CONFIGURE ADVANCED SETUP
# ============================================

# Settings โ†’ Code security and analysis
# CodeQL analysis โ†’ Set up โ†’ Advanced
# A codeql.yml file is generated.
# Commit it to the repository.
# ============================================
# PART 8: ADD CUSTOM SECRET PATTERN
# ============================================

# Settings โ†’ Advanced Security โ†’ Secret Protection
# Custom patterns โ†’ New pattern
# Define a regex for your internal API key format.
# ============================================
# PART 9: REVIEW ALERTS
# ============================================

# Security tab โ†’ Secret scanning alerts
# Security tab โ†’ Code scanning alerts
# Each alert links to the location and provides remediation guidance.

Quick Reference

Secret Scanning vs CodeQL

FeatureSecret ScanningCodeQL
DetectsCredentials (keys, tokens)Vulnerabilities (injection, XSS)
MethodPattern matchingSemantic data-flow analysis
SpeedFastSlow (20โ€“40 min for large repos)
Push ProtectionYesNo
Public RepoFree (basic)Free
Private RepoRequires Secret ProtectionRequires Code Security

Secret Scanning Configuration

SettingLocation
Enable Secret ProtectionSettings โ†’ Advanced Security
Enable Push ProtectionSettings โ†’ Advanced Security
Exclude paths.github/secret_scanning.yml
Custom patternsSettings โ†’ Advanced Security

CodeQL Configuration

OptionValue
Workflow file.github/workflows/codeql.yml
Required permissionsecurity-events: write
Default query suitesecurity-extended
Build modesnone, autobuild, manual

Best Practices

โœ… Do This:

permissions:
  security-events: write
<!-- Required for CodeQL to upload results -->        # โœ…
# Enable Push Protection
<!-- Blocks secrets before they enter the repository --> # โœ…
# Use custom patterns for internal API keys
<!-- Detects secrets specific to your organization --> # โœ…

โŒ Don’t Do This:

# Rely on CodeQL to find secrets
<!-- CodeQL no longer detects hardcoded secrets -->   # โŒ
# Ignore a Secret Scanning alert
<!-- The credential is compromised; revoke it -->     # โŒ
# Skip the security-events permission
<!-- CodeQL analysis cannot upload results -->        # โŒ

Common Pitfalls

PitfallWhy It HappensFix
CodeQL not finding secretsFeature was removedUse Secret Scanning instead
Push Protection blocks pushSecret detectedRemove secret or bypass with reason
CodeQL workflow failsMissing permissionsAdd security-events: write
Secret alert on test fileSecret is fake but matches patternExclude path or bypass
No alerts on private repoLicense not enabledPurchase Secret Protection or Code Security

Real-World Examples

1. Enable Secret Protection

Settings โ†’ Advanced Security โ†’ Secret Protection

2. Enable Push Protection

Settings โ†’ Advanced Security โ†’ Push protection

3. Exclude Path

# .github/secret_scanning.yml
paths-ignore:
  - "test/fixtures/**"

4. CodeQL Workflow

uses: github/codeql-action/init@v4

5. Query Suite

queries: security-extended

6. Bypass Push Protection

Click link โ†’ Select reason โ†’ Allow push

7. View Secret Alert

Security โ†’ Secret scanning alerts

8. View CodeQL Alert

Security โ†’ Code scanning alerts

9. Custom Pattern

Settings โ†’ Advanced Security โ†’ Custom patterns

10. Copilot Autofix

Code scanning alert โ†’ Generate fix

Visual

Security Tool Responsibilities

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  SECRET SCANNING                             โ”‚
โ”‚  โ”œโ”€โ”€ API keys, tokens, passwords            โ”‚
โ”‚  โ”œโ”€โ”€ Scans Git history, issues, PRs         โ”‚
โ”‚  โ”œโ”€โ”€ Push Protection blocks commits         โ”‚
โ”‚  โ””โ”€โ”€ No code analysis                       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  CODEQL                                      โ”‚
โ”‚  โ”œโ”€โ”€ SQL injection, XSS, path traversal     โ”‚
โ”‚  โ”œโ”€โ”€ Data-flow analysis                     โ”‚
โ”‚  โ”œโ”€โ”€ No secret detection (since May 2025)   โ”‚
โ”‚  โ””โ”€โ”€ Slow but deep                          โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The split is deliberate: credentials are pattern-matched, vulnerabilities are semantically analyzed.

Push Protection Flow

git push
    โ”‚
    โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Secret detected in commit?                  โ”‚
โ”‚  โ”œโ”€ NO  โ†’ Push succeeds                      โ”‚
โ”‚  โ””โ”€ YES โ†’ Push blocked                       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Bypass options:                             โ”‚
โ”‚  โ”œโ”€ Remove secret and amend commit           โ”‚
โ”‚  โ”œโ”€ Bypass with reason (alert auto-closed)   โ”‚
โ”‚  โ””โ”€ Request delegated bypass                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Push Protection prevents the secret from ever entering the repository.


Summary

ItemValue
Secret ScanningFinds credentials
CodeQLFinds vulnerabilities (not secrets)
Push ProtectionBlocks secrets at push time
CodeQL permissionsecurity-events: write
CodeQL workflow.github/workflows/codeql.yml
Secret exclusions.github/secret_scanning.yml
Public repoFree (basic)
Private repoRequires license

Key takeaways:

  • Secret Scanning and CodeQL have different jobs. Secret Scanning finds leaked credentials. CodeQL finds semantic vulnerabilities like injection flaws. Since May 2025, CodeQL no longer detects hardcoded secrets .
  • Push Protection is the strongest defense. It blocks the secret before it ever enters the repository. Bypassing it requires a deliberate action and creates an alert .
  • CodeQL needs the right permission. The workflow must have security-events: write to upload results. Without it, the analysis runs but the alerts never appear .
  • CodeQL is semantic, not a linter. It builds a database and traces data flow, which is why it catches bugs that pattern matching missesโ€”and why it is slower .
  • The product split matters. Private repositories need GitHub Secret Protection for Secret Scanning and GitHub Code Security for CodeQL. Public repositories get basic versions for free .

Remember: Secret Scanning protects the keys. CodeQL protects the logic. Push Protection stops the leak before it happens. The two tools are complementary, but their scopes are separate: CodeQL no longer looks for secrets, and Secret Scanning does not analyze data flow. Use both, and configure the permissions and licenses they require.

Was this helpful?


Stop using slow, ad-bloated tool sites! ๐Ÿคฎ

๐Ÿ”Ž Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
โœ… Finance (Mortgage, Interest, Inflation)
โœ… Tech (Base64, JSON, Dev Suite, IP)
โœ… Health (BMI, BMR, TDEE)
โœ… Productivity (Timer, Workspace, QR)

โšก๏ธ Fast & Private
๐Ÿ”’ No data leaves your device
๐Ÿ’Ž 100% Free

๐Ÿ”— Use it now: https://tools.kandz.me
๐Ÿ”– Bookmark itโ€”youโ€™ll need it later!