detect-secrets

repository·master·Indexed 26 days ago

https://github.com/yelp/detect-secrets

An enterprise-focused tool designed to detect secrets in codebases. It prevents new secrets from entering a repository by using a baseline of existing secrets, allowing teams to manage technical debt systematically. The project provides three primary tools: `detect-secrets scan` for generating or updating baselines, `detect-secrets-hook` for alerting on or blocking new secrets (often used in pre-commit hooks), and `detect-secrets audit` for analyzing baselines to distinguish between true and false positives.

Tokens
6.7K
Snippets
19
Records
43
Agent score
88%

What's inside detect-secrets

  1. Overview of the Scanning Process

    master

    Scanning is primarily managed through the SecretsCollection class. The engine uses a combination of:

    • Plugins: Codified rules that scan strings/lines to find potential secrets.
    • Filters: Pure functions that exclude false positives based on specific criteria.
    • Transformers: Convert files into line-proxies to handle multi-line formats.

    To scan files or diffs programmatically, use the SecretsCollection interface. While the tool provides high-level CLI commands, developers can call SecretsCollection.scan_diff directly for diff-based scanning.

  2. Choose the right detect-secrets tool

    master

    The detect-secrets project provides three distinct tools depending on your workflow stage:

    1. detect-secrets scan: Use this to scan a repository and generate or update a baseline of identified secrets.
    2. detect-secrets-hook: Use this to alert on or block new secrets that are not present in your existing baseline (typically used in pre-commit hooks).
    3. detect-secrets audit: Use this to analyze an existing baseline file to distinguish between true and false positives and optimize plugin settings.
  3. Understand and use Filters in detect-secrets

    master

    Filters are functions that return a boolean value to indicate whether a specific condition should be skipped during a scan. They allow you to systematically exclude results, such as specific files, lines, or secret patterns, from being reported as vulnerabilities.

    Filters are executed at different stages of the scanning process:

    1. Filename stage: Filters operate on the filename alone (e.g., to skip non-existent files).
    2. Line stage: Filters operate on the content of a line.
    3. Secret stage: Filters operate on the filename, line, the raw secret value, and the plugin that identified it.
  4. Use inline allowlisting for false positives

    master

    If you want to exclude a specific false positive from blocking a commit without updating the global baseline, use inline comments to allowlist the secret or the next line.

    secret = "hunter2"      # pragma: allowlist secret
    //  pragma: allowlist nextline secret
    const secret = "hunter2";
  5. Create a secrets baseline

    master
    To begin using detect-secrets, create a baseline file that captures the secrets currently present in your repository. This allows you to prevent new secrets from being added without being forced to clean up existing ones immediately.
  6. Compare baselines using differential analysis

    master

    You can use detect-secrets audit --diff to perform differential analysis between two baseline files. This is useful for determining how changes in configuration (such as plugin options) affect the number of secrets detected. The output will show the status of secrets, such as >> REMOVED << if a secret found in the first baseline is no longer detected in the second.

    $ detect-secrets scan test_data --base64-limit 4 > limit4
    $ detect-secrets scan test_data --base64-limit 5 > limit5
    $ detect-secrets audit --diff limit4 limit5
  7. Write a custom Filter function

    master

    To write a custom filter, define a function that accepts a combination of the following pre-defined variables and returns a bool. If the function returns True, the item is skipped.

    Supported Variables:

    Variable NameTypeDescription
    filenamestringThe file path being scanned.
    linestringThe line being scanned.
    plugindetect_secrets.core.plugins.util.PluginThe plugin that found the secret.
    secretstringThe raw secret value.
    contextdetect_secrets.util.code_snippet.CodeSnippetLines of code surrounding secret.

    Best Practices:

    1. Cache expensive operations: Filters are called frequently (e.g., for every line in every file). Use functools.lru_cache or similar to cache compiled regexes or heavy initializations.
    2. Cache loaded settings: If your filter depends on global settings, read them once during initialization rather than inside the filter function call.
  8. Disable built-in Filters via CLI or Python API

    master

    Via CLI

    Use the --disable-filter flag followed by the full import path of the filter function.

    $ detect-secrets scan test_data --disable-filter detect_secrets.filters.heuristic.is_prefixed_with_dollar_sign

    Via Python API

    If using detect-secrets as a library, customize your settings object by providing a filters_used list containing dictionaries with the filter's path.

    from detect_secrets.core import baseline
    from detect_secrets.settings import transient_settings
    
    config = {
        'filters_used': [
            {
                'path': 'detect_secrets.filters.heuristic.is_potential_uuid',
            },
        ],
    }
    
    with transient_settings(config):
        secrets = baseline.create('.')
  9. Use the Gibberish Detector to reduce false positives

    master

    The Gibberish Detector uses an ML model to determine if a value is likely gibberish rather than a real secret. Note that this may ignore common secrets like password because they are word-like.

    To use this feature, install the gibberish-detector package:

    pip install detect-secrets[gibberish]

    You can use the default pre-trained model or provide your own via the --gibberish-model flag.

    detect-secrets scan --gibberish-model custom.model
  10. Use detect-secrets audit for baseline analysis

    master

    The detect-secrets audit command provides features for manual analysis of existing baselines. Key capabilities include:

    • Manual Labeling: Distinguish between true and false positives.
    • Baseline Comparison: Determine how changes in configuration affect scan results.
  11. Use wordlists to exclude large sets of secrets

    master

    If you have a large list of strings to exclude (rather than a few regex patterns), use the --word-list flag. This requires the pyahocorasick package.

    To install the necessary dependency:

    pip install detect-secrets[word_list]
    detect-secrets scan --word-list wordlist.txt
  12. Manually label secrets using the audit command

    master

    Use the audit command on a pre-generated baseline to manually label secrets. This allows you to distinguish between true positives and false positives by providing feedback for each detected secret. This metadata is added to the secrets but does not prevent new, unlabelled secrets from being detected in future scans.

    $ detect-secrets scan test_data > .secrets.baseline
    $ detect-secrets audit .secrets.baseline