DEV Community

Cover image for Scrubbing API Keys, JWTs, and PII Before Pasting Logs into AI Chats (100% Client-Side)
Lark & Lever
Lark & Lever

Posted on

Scrubbing API Keys, JWTs, and PII Before Pasting Logs into AI Chats (100% Client-Side)

Every developer has had the same split-second hesitation before hitting Ctrl+V into ChatGPT, Claude, or an AI coding prompt: does this stack trace, .env snippet, or HTTP header still contain a live token, database password, or customer email?

Uploading sensitive logs to an online "sanitizer" hosted on someone else's server defeats the entire point of redacting them. A clean sanitizer belongs 100% inside the browser with zero network requests and deterministic placeholders so the LLM still understands relationships across lines.

Here is the exact client-side pipeline behind our free AI Prompt Secret & PII Redactor.

1. Deterministic Placeholders Preserve Context

Replacing every secret with *** breaks debugging when you need an LLM to compare two tokens or trace why user_a failed while user_b succeeded. Mapping each unique (category, rawValue) pair to an indexed placeholder keeps structure intact:

function makeTokenizer() {
  const seen = new Map();
  const counters = {};

  return function tokenFor(label, value) {
    const key = `${label}\0${value}`;
    if (seen.has(key)) return seen.get(key);
    counters[label] = (counters[label] || 0) + 1;
    const tok = `[${label}_${counters[label]}]`;
    seen.set(key, tok);
    return tok;
  };
}
Enter fullscreen mode Exit fullscreen mode

If alex@acme.dev appears three times in a log, all three occurrences become [EMAIL_1], while a second address becomes [EMAIL_2].

2. Order Patterns from Most Specific to Generic

If a generic KEY=value rule runs before provider-specific rules, an OPENAI_API_KEY=sk-proj-... line gets tagged generically or loses prefix context. Ordering specific signatures first keeps detections accurate:

  1. Multi-line PEM Private Keys (-----BEGIN ... PRIVATE KEY-----)
  2. Database URIs with Embedded Passwords (postgres://user:pass@host)
  3. JWTs (eyJ... . eyJ... . ...)
  4. Provider Prefixes: Anthropic (sk-ant-), OpenAI (sk-proj- / sk-), GitHub (ghp_, github_pat_), AWS (AKIA/ASIA), Stripe (sk_live_/rk_live_), Slack (xoxb-), Google (AIza...)
  5. Header & Env Assignments preserving the key name (Authorization: Bearer [BEARER_TOKEN_1], SECRET_KEY=[ENV_SECRET_1])
  6. Network & Personal Identifiers: emails, phones, IPv4s, and credit card candidates.

3. Don't Regex-Match Random 16-Digit IDs as Credit Cards

Long order IDs, Unix nanosecond timestamps, and trace IDs constantly trigger naive \d{13,19} card regexes. Filtering candidates through a 12-line Luhn checksum eliminates almost all false positives:

function passesLuhn(digits) {
  let sum = 0;
  let dbl = false;
  for (let i = digits.length - 1; i >= 0; i--) {
    let n = digits.charCodeAt(i) - 48;
    if (dbl) {
      n *= 2;
      if (n > 9) n -= 9;
    }
    sum += n;
    dbl = !dbl;
  }
  return sum % 10 === 0;
}
Enter fullscreen mode Exit fullscreen mode

Try It Locally Right Now

Top comments (0)