DEV Community

Fazalu Rahman
Fazalu Rahman

Posted on • Originally published at community.leaksnitch.com

How to Prevent API Keys and Secrets from Leaking into LLMs

Note: This article was originally published on the LeakSnitch Developer Community. Join the discussion there for raw telemetry and browser-level security benchmarks.


Developers use AI tools like ChatGPT, Claude, Cursor, and Gemini to ship code faster than ever. However, this productivity surge has introduced a massive data leak vector: accidental secret exfiltration via prompt inputs.

When an engineer copies a multi-line config block or debug log to troubleshoot an error, sensitive data, such as live database URIs, AWS credentials, JWT tokens, and SSH keys often gets transmitted directly to external LLM servers.

In this guide, we will break down why traditional network level security fails, explore the mechanics of detecting secrets locally inside the browser, and look at how to implement real-time DOM-level detection.


Why Traditional Network DLP Fails against LLM Leaks

Most enterprise security strategies rely on Data Loss Prevention (DLP) tools placed at the network layer or API gateway. While effective for standard file transfers, network DLP falls short when protecting against AI prompt leaks for two core reasons:

  1. Payload Construction Happens at the Client: By the time an HTTP request reaches a network gateway, the browser DOM has already constructed the payload. Inspecting HTTPS traffic at the gateway often requires invasive TLS decryption, introducing noticeable latency.
  2. Developer Experience & Blanket Bans: Blocking access to domain endpoints like api.openai.com or claude.ai at the firewall level creates friction. Developers often bypass blanket bans by switching to personal hotspots or unmonitored devices, introducing unmonitored "shadow IT."

To catch secrets without disrupting developer workflow, inspection must happen at the point of intent -inside the browser DOM before the request payload is generated.


The Technical Mechanics of Secret Detection

Detecting secrets in prompt payloads requires balancing accuracy with execution speed. If evaluation takes longer than 50ms, it degrades the user experience.

Two primary techniques are used in tandem: Regex Pattern Matching and Shannon Entropy Analysis.

1. High-Confidence Regex Matching

Structured credentials (like AWS Access Keys or GitHub Personal Access Tokens) follow predictable patterns with clear prefixes.


javascript
// Examples of high-confidence structured secret patterns
const SECRET_PATTERNS = {
  awsAccessKey: /^AKIA[0-9A-Z]{16}$/,
  githubPat: /^ghp_[a-zA-Z0-9]{36}$/,
  slackToken: /^xox[baprs]-[0-9a-zA-Z]{10,48}$/,
  stripeKey: /^sk_live_[0-9a-zA-Z]{24,}$/
};

function checkStructuredSecret(input) {
  for (const [provider, pattern] of Object.entries(SECRET_PATTERNS)) {
    if (pattern.test(input)) {
      return { detected: true, type: provider };
    }
  }
  return { detected: false };
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)