Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 36 ยท Security Features

Code Scanning and Secret Scanning

Find bugs and leaked credentials automatically: CodeQL code scanning, alerts on pull requests, secret scanning and push protection. Try a runnable secret-detector stand-in.

Dependencies are one half of security; the code you write and the secrets you accidentally commit are the other. GitHub has two features for these: code scanning looks for vulnerabilities in your own source, and secret scanning looks for credentials in your repository. Both report findings as alerts and integrate with pull requests, so problems are caught before they merge.

Code scanning and CodeQL

Code scanning runs an analysis tool over your code and shows the results as alerts in the Security tab and as annotations on pull requests. GitHub's own engine is CodeQL: it turns your source into a database you can query like data, then runs a library of queries that describe vulnerable patterns such as SQL injection, cross-site scripting, path traversal, hard-coded credentials and unsafe deserialization. CodeQL follows how data flows through the program, from an untrusted source (a request parameter) to a dangerous sink (a database query), which finds more than simple text search can.

Supported languages include C and C++, C#, Go, Java and Kotlin, JavaScript and TypeScript, Python, Ruby, Swift, Rust and GitHub Actions workflows themselves. Code scanning is free for public repositories; private repositories need GitHub Code Security (part of GitHub Advanced Security).

Turning it on

The quickest path is Settings -> Advanced Security -> Code scanning -> Set up -> Default. GitHub detects your languages, runs CodeQL on pushes to the default branch, on pull requests, and on a weekly schedule. For more control, choose Advanced, which adds a workflow file:

YAML
name: CodeQL

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
  schedule:
    - cron: "17 3 * * 2"

permissions:
  contents: read
  security-events: write

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

security-events: write lets the job upload results. The queries option picks a larger suite (security-extended, security-and-quality). Compiled languages may need a build step between init and analyze, unless the default no-build mode is enough.

Reading and fixing alerts

An alert shows the rule, the severity, the file and line, and a path explaining how tainted data reaches the sink. On a pull request, a new alert appears as a comment on the changed line and can be configured to block merging through a required check. Typical statuses: open, fixed (the code no longer triggers it), dismissed (with a reason: false positive, used in tests, won't fix). Some alerts offer Copilot Autofix, which proposes a patch you can review. You can also upload results from third-party tools in the standard SARIF format with github/codeql-action/upload-sarif.

A scanner is not an oracle. It misses issues (false negatives) and sometimes flags safe code (false positives). Use it as one layer next to code review, tests and dependency alerts.

Secret scanning

People paste tokens into code by accident: a cloud key in a config file, a database URL in a test, an API token in a log. Once a secret is in Git history it stays there, even if a later commit deletes it, and bots scrape public repositories within minutes.

Secret scanning searches your repository content and its full history, as well as issues, pull request text and comments, for strings that match known credential formats from many providers. When it finds one:

  • you receive an alert (who, where, which pattern);
  • for many providers (partner patterns), GitHub also notifies the provider, who may revoke the token automatically;
  • for custom formats, organizations can define custom patterns with regular expressions.

Secret scanning for partner patterns runs on all public repositories for free. Alerts for your own private repository need GitHub Secret Protection.

Push protection

Better than finding a leak is stopping it. Push protection scans a git push before it is accepted. If a supported secret is detected, the push is blocked and the terminal prints where it is. You can remove the secret and amend the commit, or, if it is a false alarm or a test value, bypass the block with a stated reason (used in tests, false positive, or "I'll fix it later"), which is recorded for reviewers. Push protection is enabled for public repositories by default.

If a secret leaks anyway

The order matters:

  1. Revoke or rotate the credential first. Assume it is already compromised.
  2. Check the provider's logs for misuse.
  3. Remove it from the code and read it from a secret store or Actions secrets.
  4. Only then consider rewriting history (with git filter-repo); that is cleanup for tidiness, not a fix, because forks and clones may keep the old data.

A stand-in: a tiny secret detector

The idea behind secret scanning is pattern matching with high-signal formats (a fixed prefix plus a fixed length) and, for generic tokens, an entropy check. The stand-in below uses a few invented formats and builds its sample "secrets" at run time from repeated characters, so nothing real appears in this lesson. Real scanners use hundreds of provider-verified patterns.

JS Stand-in: secret detector (invented patterns)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice the last label string is repetitive and not flagged, while the token and the URL are. Tuning this balance between missing secrets and crying wolf is the hard part of real scanners.

A security checklist for every repository

  • Turn on Dependabot alerts, code scanning and secret scanning with push protection.
  • Require the code scanning check to pass before merging (see rulesets).
  • Keep an eye on the Security -> Overview page, where an organization sees alert counts across repositories.
  • Publish a security policy so researchers know how to report a problem.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does CodeQL do differently from a plain text search?

    CodeQL builds a queryable database of the code and tracks data flow to find vulnerabilities.
  2. 2. What is push protection?

    The push is rejected before it lands, so the secret never enters the repository.
  3. 3. A token was committed and deleted in the next commit. What should you do first?

    History keeps the old data and clones may exist, so assume compromise and revoke immediately.
  4. 4. Why does a code scanning job need security-events: write?

    That permission allows the job to publish results to the Security tab.

Summary

  • Code scanning (CodeQL or any SARIF tool) finds vulnerabilities in your code and comments on pull requests.
  • Secret scanning finds known credential formats in content and history; push protection blocks them before they land.
  • When a secret leaks, revoke first, clean up later.
  • Scanners complement review and tests; they do not replace them.

Next: Security Policy, Advisories and Supply Chain