DEV Community

Cover image for I Built a 15µs Rust Hook to Stop API Keys from Leaking into Shell History & AI Agents
Hemanath_K
Hemanath_K

Posted on Edited on

I Built a 15µs Rust Hook to Stop API Keys from Leaking into Shell History & AI Agents

Every secret scanner on the market today runs after the damage has already happened.

Tools like GitGuardian, TruffleHog, and pre-commit hooks check your code when you type git commit or push to CI. But if you accidentally paste a token or database string into your shell:

  1. It is already written in plain text to ~/.zsh_history or ~/.bash_history.
  2. It was broadcast live on your screen during a Zoom or Google Meet demo.
  3. Autonomous AI coding agents (like Claude Code or Cursor) already cached your terminal buffer into local workspace configs (.claude/settings.local.json).

I wanted a privacy guard that lives directly inside the terminal execution loop and stops secrets at the keystroke level before commands ever touch persistent disk.

So I built SecretGuard in Rust.


⚡ 15.68 Microseconds Latency

When building a pre-exec shell hook, speed is everything. If the hook introduces even a 20ms lag, your terminal feels sluggish.

SecretGuard is compiled with aggressive release profile optimizations (opt-level = 3, lto = true, codegen-units = 1, panic = "abort"):

  • Average Scan Latency: 15.68 µs (0.016 milliseconds) across 1,000 synthetic commands.
  • Binary Footprint: 1.56 MB standalone static binary.
  • Zero Network / 100% Offline: Never connects to external servers, zero telemetry.

🔍 How Detection Works

SecretGuard uses a dual-engine architecture to catch both known formats and raw unformatted secrets:

1. Compiled Signature Engine

Matches known SaaS tokens in sub-microsecond time using a compiled regex::RegexSet:

  • OpenAI / Anthropic (sk-proj-..., ant-api-...)
  • AWS Access Keys (AKIA...)
  • GitHub Personal Access Tokens (ghp_..., github_pat_...)
  • Stripe live/test API keys (sk_live_..., rk_live_...)
  • Slack bot tokens & webhooks (xoxb-...)
  • PostgreSQL, MySQL, MongoDB URIs with embedded passwords

2. Contextual Shannon Entropy ($H \ge 3.6$)

For arbitrary passwords and non-standard tokens, SecretGuard calculates character probability distributions using Shannon Entropy:

$$H(X) = -\sum_{i=1}^{n} P(x_i) \log_2 P(x_i)$$

To eliminate false positives, it automatically discounts:

  • UUIDs (550e8400-e29b-41d4-a716-446655440000)
  • Git commit SHA-1 / SHA-256 hashes
  • Hexadecimal color codes and standard system paths

🛠️ CLI Toolkit

SecretGuard is more than just a background hook:

1. Live Shell Hook

# Attach to your active Zsh session
secretguard hook zsh
Enter fullscreen mode Exit fullscreen mode

When you hit Enter on a command containing an API key, SecretGuard flags the secret, strips it from shell history, and saves you from a leak.

2. One-Click History Scrubbing

secretguard clean-history
Enter fullscreen mode Exit fullscreen mode

Finds and scrubs historical tokens already trapped inside ~/.zsh_history or ~/.bash_history, creating an automatic timestamped backup first.

3. AI Coding Agent Protection

secretguard protect-agent
Enter fullscreen mode Exit fullscreen mode

Scans .claude/ and .cursor/ workspace directories to ensure cached environment variables and MCP credentials aren't accidentally committed to Git.

4. Real-Time Cloaked Wrappers

secretguard wrap "curl -H 'Authorization: Bearer sk-proj-...' https://api.openai.com/v1/models"
Enter fullscreen mode Exit fullscreen mode

Masks stdout tokens on the fly during screen shares, video recordings, and conference talks.

5. Instant Diagnostics

secretguard doctor
Enter fullscreen mode Exit fullscreen mode

Runs a health check verifying hook status, history hygiene, agent config security, and engine benchmark in under 10ms.


🚀 Quick Install

Via Homebrew (macOS / Linux):

brew tap hemanathk636/tap && brew install secretguard
Enter fullscreen mode Exit fullscreen mode

Via One-Line Installer:

curl -fsSL https://raw.githubusercontent.com/hemanathk636/secretguard/main/install.sh | bash
Enter fullscreen mode Exit fullscreen mode

Via Cargo:

cargo install --git https://github.com/hemanathk636/secretguard.git
Enter fullscreen mode Exit fullscreen mode

🔗 Links & Feedback

I would love to hear feedback from the developer community! What edge cases, token formats, or terminal shells would you like to see supported next? If you find it useful, a star on GitHub means the world! ⭐

Top comments (0)