Dev.to Security πŸ” Cybersecurity πŸ‘ 0 πŸ“– 1 min read

How to Reversibly Mask API Keys and PII Before Sending Prompts to LLMs (Zero Dependencies)

Every developer building LLM features or autonomous coding agents runs into the same uncomfortable moment: A user prompt, a system dump, or a log file contains a sensitive secret β€” an OpenAI key (sk-proj-...), an AWS cr

Every developer building LLM features or autonomous coding agents runs into the same uncomfortable moment:

A user prompt, a system dump, or a log file contains a sensitive secret β€” an OpenAI key (sk-proj-...), an AWS credential (AKIA...), a private PEM key, or client bank details (IBAN, payment cards).

If you pass that prompt directly to an external model endpoint, that sensitive data leaves your process boundary and ends up in external inference logs and training pipelines.

Most teams try one of two extremes:

  1. The heavy NLP stack: Run a separate Python microservice (like Microsoft Presidio) with spaCy/HuggingFace models in a 500MB+ container. Powerful, but overkill for a simple Node or edge application.
  2. Naive regex replacements: A quick .replace() on strings. This usually breaks order numbers, destroys formatted outputs, and β€” worst of all β€” is irreversible. Once you replace the secret with ***, the LLM’s answer has no way to reference the original value back to your user.

Here is how to solve this locally in Node.js, the browser, or edge workers with zero dependencies.

The Concept: Reversible In-Memory Shielding

The idea is straightforward:

  1. Before the API call: Scan the prompt locally. Replace detected secrets and PII with semantic placeholders (e.g., [API_KEY_1], [IBAN_1], [NAME_1]).
  2. Hold the map in memory: The mapping table ([API_KEY_1] β†’ sk-proj-xyz...) lives exclusively in your process's local RAM. It never touches the network.
  3. External inference: The model only ever sees the placeholder tokens.
  4. After the API call: When the model replies, restore the original values in the output text using the local map.

text
User input:  "Deploy with key sk-proj-AbC123... and refund Mr. Miller, IBAN CH93 0076 2011 6238 5295 7"
                                      β”‚
                         [ Local Shield Engine ]
                                      β”‚
Model sees:  "Deploy with key [API_KEY_1] and refund Mr. [NAME_1], IBAN [IBAN_1]"
                                      β”‚
                         [ External LLM Inference ]
                                      β”‚
Model reply: "Refunding 150 CHF to [IBAN_1] for Mr. [NAME_1] using key [API_KEY_1]."
                                      β”‚
                         [ Local In-Memory Restore ]
                                      β”‚
User gets:   "Refunding 150 CHF to CH93 0076... for Mr. Miller using key sk-proj-AbC123..."
πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.