Skip to main content

Secret Scanning

Rafter scans your code for secrets and credentials to prevent accidental leaks.

Quick Start

Scan a directory for secrets:
Note: rafter agent scan still works but is deprecated — it will be removed in a future major version.

Detected Secret Types

The built-in patterns engine detects 28 secret types:
  • AWS Access Key ID & Secret Access Key (the example keys from the AWS docs, ending in EXAMPLE and EXAMPLEKEY, are not reported)
  • Google API Key & OAuth credentials
  • DigitalOcean Personal Access Token
  • GitHub Personal Access Tokens
  • GitHub OAuth Tokens
  • GitHub App Tokens
  • GitHub Refresh Tokens
  • OpenAI API Key (current and legacy formats)
  • Supabase Secret Key
  • Stripe API Keys (live & restricted)
  • Slack Tokens & Webhooks
  • Twilio API Keys
  • SendGrid API Key
  • Mailchimp API Key
  • HashiCorp Vault Token
  • npm Access Tokens
  • PyPI API Tokens
  • Database connection strings (postgres, mysql, mongodb)
  • Private keys (RSA, DSA, EC, OpenSSH)
  • JSON Web Tokens
  • Bearer Tokens
  • Generic API keys & generic secrets
Betterleaks adds providers beyond the built-in set; --engine auto runs both.

Usage Examples

Scan Specific File

Scan Directory

Quiet Mode (CI/CD)

Only output if secrets are found:
Exits with code 1 if secrets found, perfect for CI pipelines.

JSON Output

Get machine-readable results on stdout (status messages stay on stderr, so the pipe below is clean JSON):
The raw secret value is never included. engines is present only in --engine auto (the default): it lists which engine(s) flagged that match (["betterleaks"], ["patterns"], or both when they agree), and is omitted entirely for a single-engine scan. If .rafter.yml ignore: rules or .rafterignore hid any findings, an _suppressed array is added alongside results — see Suppressing Findings. Exit code 1 is returned when at least one non-suppressed finding remains; suppression never changes the exit code.

SARIF Output

For GitHub/GitLab code-scanning integrations:
Emits SARIF 2.1.0 JSON (runs[].tool.driver.rules + runs[].results), usable with github/codeql-action/upload-sarif or an equivalent GitLab job.

Watch Mode

Watch a path for file changes and re-scan automatically:
Findings are printed inline and logged to audit.jsonl in real time. Press Ctrl+C to stop. Watch mode does not exit on findings — it keeps watching.
Requires chokidar (Node, bundled) or watchdog (Python: pip install watchdog).

Diff Scanning

Scan only files changed since a git ref:
--diff <ref> and --staged (below) only scan added/modified lines in the unified diff (git diff -U0) — a pre-existing secret on an untouched line in a touched file is not re-flagged. A ref that looks like an option (starts with -) is rejected before it reaches git, rather than risking git misreading it. Useful for CI pipelines that only need to check new or modified files.

Staged Files

Scan exactly what’s about to be committed:
This is what the installed pre-commit hook and rafter agent exec "git commit ..." run under the hood. If the staged diff can’t be read, the scan exits 2 and the commit is blocked. Rafter reads staged diffs up to 256 MiB.
--diff and --staged always scan with the built-in patterns engine, regardless of --engine — betterleaks’s whole-file scan doesn’t fit scanning only the added/modified lines of a diff. A full scan (rafter secrets .) is still --engine auto (both engines) by default.

History Scanning

Scan every commit on every ref, not just the working tree — useful for finding a secret that was committed and later deleted:
Both engines read history: the patterns engine streams git log -p over every ref, and betterleaks uses betterleaks git; --engine auto unions them as in a directory scan. Findings are reported as absolute paths under the repository root, with the line number from the commit that added them — even if the file no longer exists in the working tree. Uncommitted changes are not scanned. Outside a git repository, --history exits 2.

Output Format

When secrets are found, Rafter shows (paths are always printed absolute, regardless of how you specified the scan target):
The severity label is a colored badge in a terminal (or [CRITICAL] etc. under -a/--agent, which disables color and emoji for plain-text consumption); there are no per-severity icons. -a/--agent is a global flag, placed before the subcommand (rafter -a secrets .), not after it.

Severity Levels

Severity Indicators

  • Critical: most built-in patterns — cloud/API keys, OAuth tokens, database connection strings, private keys
  • High: generic API keys, generic secrets, bearer tokens, JSON Web Tokens, Slack webhooks
  • No built-in pattern is currently rated medium or low. Both severities exist in the schema and are available to custom patterns, but nothing in the default 28-pattern set uses them today.

Smart Redaction

Rafter uses smart redaction to show context without exposing secrets:
  • Short secrets (≤8 chars): Fully redacted (********)
  • Long secrets (>8 chars): Show first 4 and last 4 characters, with the masked middle capped at 16 asterisks regardless of the secret’s actual length (so a PEM block or a long JWT doesn’t print a line of a thousand asterisks)
Example: a fake key like AKIAIOSFODNN7ABCDEFG redacts to AKIA************DEFG.
A key ending in EXAMPLE (the canonical AWS-docs placeholder, e.g. AKIAIOSFODNN7EXAMPLE) is deliberately not flagged by either engine — it’s a well-known public non-secret. Don’t use an EXAMPLE-suffixed key when testing detection; use a different fake suffix instead.

Pre-Commit Scanning

Integrate with git commits:

Excluding Files

The built-in patterns engine automatically skips:
  • Binary files (images, PDFs, executables)
  • Build directories (node_modules, dist, build, .next, .trigger, coverage, bower_components)
  • Version control (.git)
  • IDE folders (.vscode, .idea)
  • Vendored, virtual-env, and tool-cache directories (vendor, .venv, venv, __pycache__, .tox, .mypy_cache, .pytest_cache, .ruff_cache, .terraform, results)
Node and Python share this default exclude list. Betterleaks does not use it. On top of it, when the scan target is inside a git work tree, .gitignore is honored by default (see below) — the two mechanisms are independent and both apply.

CI/CD Integration

GitHub Actions

Exit code 1 will fail the pipeline if secrets are detected.

GitLab CI

Audit Trail

Each one-shot rafter secrets scan writes one scan_executed entry to ~/.rafter/audit.jsonl, in every output format and whether or not it finds anything. The entry holds counts and pattern names only, never a matched value. Individual findings reach the audit log as secret_detected from two other paths:
  • --watch mode logs a secret_detected entry for every finding, as it finds it
  • the PreToolUse hook logs secret_detected when it blocks a file write or staged commit containing a secret
rafter agent exec logs command_intercepted for the command itself, separately from any secret scan it runs.
See Audit Log for the full schema.

False Positives

If you encounter false positives:
  1. Suppress the specific finding, with a reason, via .rafter.yml ignore: rules — see Suppressing Findings. (There is no config key that excludes a secret pattern globally; agent config set will write any key path you give it without validating that anything reads it.)
  2. Report issues: Help improve detection at rafter-cli/issues

Advanced Usage

Scan with Custom Patterns

Define custom patterns in .rafter.yml:
See Policy File for full configuration options.

Engine Selection

Rafter ships two scan engines, selectable via --engine:
auto runs both engines when a Betterleaks binary is available and unions the findings — a secret one engine misses is still caught by the other. When both engines flag the same secret at the same file, line, and column, it’s reported once, carrying an engines array naming which engine(s) found it, and betterleaks’s rule name/severity is used for the merged entry. The matched text itself doesn’t need to agree — the two engines can capture different spans of the same secret (e.g. betterleaks grabbing a whole PEM block where the pattern engine only matches its header). auto degrades to patterns-only when no betterleaks binary is available, or when a betterleaks run fails or times out (a warning names the cause). --engine betterleaks / --engine patterns stay single-engine and never fall back. Rafter looks for betterleaks at ~/.rafter/bin/betterleaks (the rafter-managed copy, auto-updated by default — see --no-auto-update in rafter secrets --help) and falls back to one on PATH (e.g. a Homebrew install). Install the managed copy via rafter agent init --with-betterleaks or rafter agent update-betterleaks.

Respecting .gitignore

When the scan target sits inside a git work tree, Rafter honors .gitignore by default for both engines — files the repo has excluded (build outputs, vendored deps, scratch envs) are not scanned. Every gitignore semantic git itself supports is honored: nested .gitignore files, negations, .git/info/exclude, and the configured global excludes file. A bare pattern like secret.env also works as you’d expect from .gitignore itself.
Scans against directories outside a git work tree (a plain unversioned folder) fall back to scanning every candidate file, since there’s no work tree for the filter to consult. The built-in patterns engine filters its own directory walk. Betterleaks has no gitignore support of its own, so Rafter runs one betterleaks walk of the scan root under a generated config that skips what git ignores (plus submodules and untracked nested repositories). That config carries the one betterleaks would otherwise use (from BETTERLEAKS_CONFIG or BETTERLEAKS_CONFIG_TOML, else the repo’s .betterleaks.toml or legacy .gitleaks.toml), so your rules still apply, including ones that extend the defaults.

Best Practices

Recommended Workflow

  1. Run rafter secrets before every commit
  2. Configure pre-commit hooks for automation
  3. Use --quiet mode in CI/CD pipelines
  4. Review audit logs regularly
  5. Report false positives to improve accuracy

Next Steps

Command Execution

Learn about safe command execution

Command Reference

Complete CLI command reference