Every week, an engineer somewhere highlights a failing Kubernetes manifest, .env file, Terraform plan, or curl trace, pastes it into ChatGPT or Claude to debug an edge case, and realizes half a second too late that a live sk-proj-... key, AWS AKIA... credential, or production Postgres URI just left their machine.
Most security-conscious developers try to solve this manually by doing a quick find-and-replace to turn credentials into ***REDACTED*** before prompting.
Unfortunately, naive ***REDACTED*** scrubbing silently breaks two critical parts of an AI-assisted debugging loop:
-
Destroying Cross-Variable Identity (Topology Loss): Suppose a distributed trace references two internal nodes (
10.24.8.15and10.24.8.99) and passes a bearer token across three microservices, where service C accidentally receives a stale token. If every IP and token becomes***REDACTED***, the LLM loses equivalence relationships—it can no longer tell whether two masked strings were identical or distinct. -
Roundtrip Re-Insertion Friction: When Claude or ChatGPT hands back a 60-line refactored TypeScript or YAML block filled with
***REDACTED***literals, pasting it back into your editor breaks your local build until you manually hunt down and restore every single credential without fat-fingering a quote.
To fix both problems without routing sensitive clipboard contents through any cloud proxy or bloated native binary, I built and open-sourced PasteShield—a zero-dependency, <1ms reversible secret sanitizer running 100% in local RAM across VS Code / Cursor, Node/Bun CLI pipelines, and an offline browser sandbox.
Here is a look under the hood at the three architectural problems we had to solve: deterministic bidirectional interning, ordered multi-pass regex precedence, and a zero-dependency local stdio MCP server with closure-isolated Pro activation.
1. Designing a Prototype-Safe Bidirectional SessionVault
Instead of destroying entropy with static asterisks, PasteShield maps each unique (category, rawSecret) pair to a numbered semantic token such as <OPENAI_KEY_1>, <DB_URI_1>, <INTERNAL_IP_1>, and <INTERNAL_IP_2>.
Inside sanitizer-core.js, the vault lives strictly in volatile process memory using null-prototype dictionaries (Object.create(null)) so maliciously crafted log strings like __proto__ or constructor can never trigger prototype pollution:
function SessionVault() {
this.secretToToken = Object.create(null);
this.tokenToSecret = Object.create(null);
this.categoryCounters = Object.create(null);
}
SessionVault.prototype.intern = function (category, rawSecret) {
var compositeKey = category + '\u0000' + rawSecret;
if (this.secretToToken[compositeKey]) {
return this.secretToToken[compositeKey];
}
var nextIndex = (this.categoryCounters[category] || 0) + 1;
var candidate = '<' + category + '_' + nextIndex + '>';
while (this.tokenToSecret[candidate] && this.tokenToSecret[candidate] !== rawSecret) {
nextIndex += 1;
candidate = '<' + category + '_' + nextIndex + '>';
}
this.categoryCounters[category] = nextIndex;
this.secretToToken[compositeKey] = candidate;
this.tokenToSecret[candidate] = rawSecret;
return candidate;
};
Notice two subtle invariants here:
-
Referential Stability Across Multi-File Prompts: Because
secretToTokenkeys oncategory + '\u0000' + rawSecret, copying.envfirst anddocker-compose.ymlfive minutes later guarantees that the same Redis password resolves to<DB_URI_1>in both snippets. The LLM sees exact structural isomorphism while never seeing a single character of the credential. -
Collision-Proof Angle-Bracket Syntax: Tokens follow
/^<[A-Z][A-Z0-9_]{1,40}_[1-9]\d*>$/. LLMs naturally treat<DB_URI_1>as an opaque symbolic variable and preserve it verbatim inside refactored string templates, JSON payloads, and TypeScript configs. When you copy the AI's response and pressCmd+Alt+R(Ctrl+Alt+R), a single regex pass over/<([A-Z][A-Z0-9_]{1,40})_([1-9]\d*)>/gswaps every placeholder back to its exact original value in under a millisecond.
2. Why Regex Execution Order Makes or Breaks Secret Scrubbers
If you run 10 independent regular expressions in arbitrary order, smaller patterns will cannibalize larger structured secrets and corrupt restoration!
Consider a production PostgreSQL connection URI:
DATABASE_URL="postgresql://svc_billing:sk-live-secret99@10.24.8.15:5432/ledger"
If your INTERNAL_IP detector (10.24.8.15) or generic .env key-value detector fires before your DB_URI detector, the URI gets partially mutated into:
postgresql://svc_billing:sk-live-secret99@<INTERNAL_IP_1>:5432/ledger
Now the DB_URI regex fails to match, leaving svc_billing:sk-live-secret99 exposed in plain text!
PasteShield prevents partial-token cannibalization through strict coarse-to-fine pass ordering paired with an idempotency guard (hasExistingPlaceholder):
-
Pass 1 — Multi-Line PEM / OpenSSH / PGP Private Key Blocks: Captures entire
-----BEGIN ... PRIVATE KEY-----blocks across newlines before line-oriented matchers touch base64 lines. -
Pass 2 — Composite Cloud Connection Strings: Matches multi-segment Azure Storage, Service Bus, Cosmos DB, IoT Hub, and SQL connection strings (
DefaultEndpointsProtocol=...;AccountKey=...). -
Pass 3 — Authenticated Database & Broker URIs: Captures full
postgres://,mysql://,mongodb+srv://,rediss://,amqps://, andclickhouse://URIs containinguser:pass@host. -
Pass 4 — High-Prefix Vendor Tokens: Captures
sk-proj-.../sk-ant-...(OpenAI & Anthropic),AKIA.../ASIA...(AWS IAM),ghp_/github_pat_(GitHub), Slackxoxb-, Striperk_live_, and 3-segmenteyJ...signed JWTs. -
Pass 5 — Network & Identity Primitives: Captures RFC1918 (
10.x,172.16-31.x,192.168.x) plus Tailscale/CGNAT (100.64-127.x) private IPv4s and email addresses. -
Pass 6 — Contextual Key-Value Assignments: Captures
.env, YAML, and JSON assignments matching*_SECRET,*_PASSWORD,*_TOKEN, or*_API_KEY, while explicitly skipping benign placeholders (true,false,null,changeme,$ENV_REF,{{handlebars}}, and already-interned<TOKEN_1>markers).
Because every replacement checks if (hasExistingPlaceholder(match)) return match;, already-sanitized regions are completely immune to double-wrapping.
3. Zero-Dependency Local stdio MCP Server & Closure-Isolated Gumroad Pro Vault
To make PasteShield equally useful inside Claude Desktop, Cursor MCP, and Windsurf agent workflows without spinning up any cloud backend, we also shipped a zero-dependency JSON-RPC 2.0 stdio MCP Server (npx -y github:963239327/pasteshield-secret-sanitizer --mcp):
-
sanitize_snippet&restore_snippet: Deterministic<OPENAI_KEY_1>/<DB_URI_1>roundtrip right inside your MCP host's local memory. -
audit_workspace_secrets: Recursive workspace secret leak audit before opening a pull request or sharing a context bundle.
Our core 10 detectors, bidirectional VS Code / Open VSX commands, and CLI/MCP server are 100% free and MIT-licensed forever, with zero telemetry during sanitization. Optional Pro features—like custom corporate domain redaction (*.corp.acme.internal → <COMPANY_DOMAIN_1>) and multi-file Markdown AI Context Packs—keep entitlement state strictly inside a private JavaScript closure (WeakMap), verifying personal Gumroad License Keys directly against Gumroad's license endpoint only when a user explicitly activates Pro.
Try It in Your Workflow (100% Local & MIT Open Source)
Whether you live inside VS Code, Cursor, or terminal pbpaste | ... | pbcopy pipelines, you can wire reversible sanitization into your daily AI loop in 30 seconds:
- 🧩 Install on VS Code Marketplace: PasteShield — AI Prompt & Clipboard Secret Sanitizer
- ⭐ Clone Source & Zero-Dependency CLI on GitHub:
github.com/963239327/pasteshield-secret-sanitizer - 🌐 Test Live in Browser (No Install): Interactive Client-Side Playground
- 🔑 Support Indie OSS: Optional $9 Lifetime PasteShield Pro Key or $19 CraftLeaf Studio All-Access Pass.
If there is a cloud vendor token format or log pattern your team hits frequently, open an issue or PR on GitHub—happy to add first-class detectors for it!
Top comments (0)