DEV Community

Riley Lin
Riley Lin

Posted on

Don't Send the Database Password: A Practical Secret Filter for AI Coding

When you paste a stack trace into an AI assistant, you are not just sending an error message; you are often shipping environment variables, connection strings, and absolute file paths that reveal your entire infrastructure. Every free token you consume may become a permanent artifact in a training corpus or a support ticket, so the act of copying and pasting should be treated as a publish action. This article gives you a trust-boundary habit and a small Python script that catches the most common secrets before they ever leave your machine.

Disclosure: This article was prepared as part of MonkeyCode's product outreach. MonkeyCode is an open-source coding assistant that offers free model access and a free server option, including a generous allowance of ten million tokens for experimentation. That same openness means you should assume anything you paste is public, so the filtering workflow below is deliberately tool-agnostic.

Think of your AI tool as an outsourced contractor who sees only the fragments you bring to the meeting. You would not hand them the production database dump, yet many developers happily paste a full stack trace that includes a signed URL or a Redis password in the middleware logs. The gap between what you intend to share and what your clipboard actually contains is the trust boundary, and it almost never gets inspected.

A stack trace is only the obvious leak. Application logs routinely include request headers, session identifiers, and query parameters, and those parameters sometimes carry OAuth tokens or Stripe keys. When you copy a log snippet to ask why your deployment fails, you are also copying the exact values that would let an attacker impersonate your service account. The model cannot tell that a long random string is sensitive, so you need to filter before you paste.

Here is a minimal secret detector in Python that you can run on any file or clipboard content before sending it to a model. It checks for common patterns like AWS access keys, GitHub tokens, private key blocks, and high-entropy strings that look like passwords, and it returns a non-zero exit code if anything suspicious is found.

#!/usr/bin/env python3
import re
import sys
import math
import collections

SIGNATURES = {
    "AWS Access Key": r"AKIA[0-9A-Z]{16}",
    "AWS Secret Key": r"(?i)aws(.{0,20})?['\"][0-9a-zA-Z/+]{40}['\"]",
    "GitHub Token": r"gh[pousr]_[0-9A-Za-z]{36,255}",
    "Slack Token": r"xox[baprs]-[0-9A-Za-z-]{10,}",
    "Private Key": r"-----BEGIN (RSA|EC|OPENSSH|DSA) PRIVATE KEY-----",
    "Generic API Key": r"(?i)(api[_-]?key|apikey|secret|token|password)\s*[:=]\s*['\"]?[0-9a-zA-Z-_]{16,}",
}

def entropy(s):
    if not s:
        return 0
    freq = collections.Counter(s)
    length = len(s)
    return -sum((count/length) * math.log2(count/length) for count in freq.values())

def check(text):
    found = []
    lines = text.splitlines()
    for i, line in enumerate(lines, 1):
        for name, pattern in SIGNATURES.items():
            if re.search(pattern, line):
                found.append((i, name, line[:80]))
        for token in re.findall(r"['\"][0-9a-zA-Z_-]{20,}['\"]", line):
            if entropy(token) > 3.5:
                found.append((i, "High-Entropy Literal", token[:40]))
    return found

if __name__ == "__main__":
    data = sys.stdin.read() if not sys.argv[1:] else open(sys.argv[1]).read()
    leaks = check(data)
    if leaks:
        print(f"{len(leaks)} potential secret(s) found:")
        for line, kind, snippet in leaks:
            print(f"  line {line}: {kind}: {snippet}...")
        sys.exit(1)
    else:
        print("No obvious secrets detected.")
Enter fullscreen mode Exit fullscreen mode

Save this as secret_gate.py and pipe your log file or clipboard through it. For example, before you paste an error message into an AI chat, you might run cat error.log | python secret_gate.py and watch the exit code. When the script finds a match, it shows the line number and the category, which gives you a chance to redact or replace the value with a placeholder like <REDACTED>.

The script is intentionally naive, and that is a feature, not a bug. A simple pattern list is easy for you to extend with your own formats, such as a JWT prefix or a base64-encoded credential used in your internal tools, and the entropy check catches long random values that would otherwise pass a literal regex. False positives are common, especially with hashes or UUIDs, so treat the output as a warning rather than a definitive verdict.

There are limits to any client-side filter. An attacker who has access to your machine can intercept the output anyway, and a determined insider can always bypass a script. The real defense is to define, in your team's pull request template, a rule that any code snippet including a credential-shaped string must be redacted before becoming an AI prompt. Encourage your colleagues to shorten stack traces to the first few frames and to mask values with sed -E 's/(key|token|password)=[^ ]+/\1=<REDACTED>/g' as a habit.

For teams that use a shared AI server, like the kind MonkeyCode offers as part of its free tier, the shared context makes filtering even more important. You do not want one engineer's accidental DATABASE_URL paste to become the next prompt that all teammates see in a shared log. If you run your own local proxy, you can pipe every prompt through secret_gate.py automatically and refuse to forward anything that trips the alarm, giving you a hard boundary instead of a soft habit.

One practical workflow is to create a small wrapper function in your terminal that copies the current selection into a temporary file, runs the detector, and only updates the clipboard when no secrets are found. This moves the check from your memory to your environment, which matters because attention is the scarcest resource in development. The effort costs you about twenty minutes today and saves you from the kind of breach notification that takes far longer to handle.

When you do find a secret, treat it as compromised. Rotate the key, invalidate the token, and change the password even if you never pasted it, because the fact that it was sitting in a log means it has already been exposed to anyone with read access to that file. Your AI assistant did not create that vulnerability; it simply made it more visible, and the fix starts at the source where the value was printed.

Finally, remember that no filter is a substitute for good hygiene. Store secrets in a vault or environment-specific files, avoid logging them in the first place, and keep them out of source files entirely. The script you built here is a tripwire, not a fence, but a tripwire still catches many intruders, and in the world of AI-assisted development, the intruder is often your own clipboard. If you want to test this workflow without spending money, MonkeyCode's free model access gives you ten million tokens to practice on, and the free server option lets you host a shared filter for your team without provisioning a box.

Top comments (0)