Skip to content

rtk grep should hard-exclude secret files by default (it currently shells to GNU grep, not ripgrep, and ignores .gitignore) #2817

Open

Description

@KevinFaurholt

Summary

rtk grep will happily print the contents of .env, private keys, and other credential files, even inside a git repo where those files are correctly listed in .gitignore. The root cause: rtk grep shells out to plain GNU grep, not ripgrep, despite --help advertising ripgrep-style flags (e.g. --glob) that error when passed to GNU grep. Because it's not actually ripgrep, it gets none of ripgrep's default protections (gitignore respect, hidden-file skipping). This is made worse by an inconsistency within rtk itself: rtk find is gitignore-aware, but rtk grep is not — so a user who has verified rtk find respects .gitignore reasonably (but wrongly) assumes rtk grep does too.

Root cause

Verified against the running binary (rtk 0.43.0, Linux), not just the docs. Repro uses only a synthetic, obviously-fake value — no real credential involved:

  1. Create a scratch directory with a .gitignore containing secret.txt, and a secret.txt containing a fake token, e.g. PAX_TEST_TOKEN=zzz_synthetic_never_real_zzz.
  2. git init the directory.
  3. rtk find "*.txt" <dir> correctly excludes secret.txt — confirms rtk find respects .gitignore.
  4. rtk grep -r "PAX_TEST_TOKEN" <dir> matches and prints the file and the unmasked value — in the exact same directory where rtk find just excluded that file.
  5. rtk grep --glob '!secret.txt' -r ... fails with /usr/bin/grep: unrecognized option '--glob' — proof rtk grep is backed by GNU grep, not ripgrep, despite the help text implying rg-compatible flags.

So: .gitignore correctly listed the file, one rtk subcommand honored it, and the other silently didn't.

The same gap extends to hidden dotfiles in general: rtk grep -r matches and prints a gitignored .hidden_conf that ripgrep would skip by default (ripgrep ignores dotfiles unless --hidden is passed; GNU grep has no such default). Re-verified against rtk 0.43.0 using synthetic fake values only.

Why this matters

A .gitignore entry is the most common way people expect to keep a secrets file out of tooling. When rtk grep ignores it, secret values get printed straight to stdout/logs — and increasingly, into AI-agent transcripts and conversation history, since rtk is commonly invoked by coding agents as a search tool. A silent, per-subcommand inconsistency (find protects you, grep doesn't) is worse than uniform behavior, because it creates false confidence rather than an obvious gap.

This isn't a hypothetical edge case: several other tools in this space have independently converged on hard file-exclusion for secrets rather than relying on .gitignore alone (see References). At least one competing agentic-coding-tool team has explicitly stated that ignored files "can still be read by tools like rg or cat" and should not be trusted as the only guard.

Proposed fix

Primary — a hard-coded, unconditional default exclude list, independent of .gitignore, applied by every filesystem-walking subcommand (rtk grep at minimum; rtk find and glob-based rtk read should be audited for the same gap). This should not depend on being inside a git repo or on .gitignore existing/being correct. Suggested baseline set, matching the shape used by git-deny-patterns.json / ban-sensitive-files:

.env, .env.*, **/.env, **/.env.*
*.pem, *.key
id_rsa*, id_ed25519*, id_dsa*, id_ecdsa*
.npmrc*, .pypirc*, .netrc*
.git-credentials
**/.aws/credentials, **/.aws/config
**/.azure/**, **/.config/gcloud/**, **/.kube/config
secrets/**, **/secrets/**
*.p12, *.kdbx
.dockercfg, **/.docker/**
.pgpass
*.tfstate, *.tfvars

Two implementation notes for whoever builds this: (a) match the resolved target of symlinks, not just the literal path — rtk grep currently dereferences an innocuously-named symlink that points at a secret file and prints the target's contents, so filename-only matching can be bypassed; (b) if the exclude list is implemented via GNU grep's --exclude (fnmatch), note that it is case-sensitive, so an all-lowercase list misses SECRET.TXT/.ENV-style variants — match case-insensitively.

Secondary (defense in depth) — unconditional output redaction, gitleaks---redact-style: mask all but a short prefix/suffix of any secret-shaped match (known prefixes like AKIA, sk-, xoxb-, PEM headers, high-entropy strings) on every call, regardless of whether the matched file was supposed to be excluded. This should draw from a separate, content/entropy-based pattern list — not the same list used for file exclusion — so a bad edit to one control can't silently degrade the other. Note this redaction should not be scoped to file search alone: rtk env likewise prints environment-variable values in cleartext (no masking flag exists), so a secret held in an env var is exposed exactly as a secret in a file is — the same content-redaction gap via a different source. Any subcommand that surfaces values a user didn't explicitly ask to see raw (grep, read, env) should run through the same redaction path.

Optional opt-out — a flag like --include-secrets for legitimate cases (e.g. auditing a secrets file's own structure) is reasonable, but it should only lift file exclusion. It should never also suppress redaction — an agent-reachable flag that unmasks raw values on request is a much bigger risk surface than one that merely lets a file be searched.

Worth noting: rtk already ships a separate, gitignore-aware rtk rg subcommand — so the ripgrep engine is already in the toolbox; rtk grep simply routes to GNU grep instead. That sharpens the inconsistency: whether a user is protected depends on which search subcommand they happen to pick. Making rtk grep's backing engine match rtk rg would get .gitignore respect and hidden-file skipping for free — but that's still a nice-to-have on top of the hard exclude list, not a substitute for it (gitignore respect alone is opt-out-able, depends on the ignore file being correct, and users won't reliably pick the safe subcommand). The hard exclude list should apply to rtk grep regardless of which engine backs it.

References

  • ripgrep — default behavior respects .gitignore/.ignore/.git/info/exclude and skips dotfiles unless --hidden is passed.
  • ban-sensitive-files (npm) and its source list git-deny-patterns.json (jandre/safe-commit-hook) — canonical community-maintained secret-filename deny-list.
  • gitleaks' own --redact flag — precedent for unconditional, output-side value redaction as a secondary control.
  • git-secrets (AWS Labs) — a different architecture (commit-hook, scans staged diffs) but the same underlying principle: keep the operation scoped away from the secret rather than relying on catching it after the fact.
  • trufflehog#3545 — a cautionary counter-example: truffleHog's Redacted output field is not actually redacted by default, showing that redaction-only (without file-level exclusion) tends to lag or get overlooked in practice.

Activity

  1. pszymkowiak commented on Jul 4, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 83%

    Summary

    rtk grep silently shells out to GNU grep instead of ripgrep, so it ignores .gitignore/hidden-file defaults and can print secrets that rtk find/rtk rg correctly exclude — needs a hard-coded secret-file exclude list plus symlink-aware matching and optional output redaction.

    📁 Relevant files
    • src/commands/grep.rs
    • src/commands/find.rs
    • src/commands/rg.rs

    Triaged automatically by wshm · This is an automated analysis, not a human review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions