Dev.to Security 🔐 Cybersecurity 👁 0 📖 3 min read

How We Built a 165 µs Reversible PII Anonymizer for LLMs & MCP in Rust (212x Faster Than Presidio)

Every time an AI agent, RAG pipeline, or MCP tool sends a prompt to an external LLM API, there is a high chance it carries sensitive data: customer emails, credit card numbers, IBANs, national tax IDs, PostgreSQL connect

Every time an AI agent, RAG pipeline, or MCP tool sends a prompt to an external LLM API, there is a high chance it carries sensitive data: customer emails, credit card numbers, IBANs, national tax IDs, PostgreSQL connection strings, or leaked API keys.

Most teams try to solve this in one of two ways:

  1. Python NLP pipelines (like Microsoft Presidio + spaCy): ~35 ms per prompt, ~450 MB RAM footprint, and no native support for restoring streamed SSE tokens.
  2. Cloud DLP APIs (like AWS Comprehend PII): 80–250 ms of network latency, \$100+ per million requests, and you still have to send raw PII over the wire to a third-party cloud.

At GLASTOR®, we needed sub-millisecond, zero-egress, reversible PII protection that works inline with OpenAI/Anthropic SSE streams and Model Context Protocol (MCP) agents.

So we built pii-veil in Rust — and you can test it right now in your browser (100% client-side WebAssembly, zero data leaves your device):

👉 Live WASM Playground: https://pii-veil.pages.dev

👉 Open-Source Repository: https://github.com/glastor-core/pii-veil

The Hard Problem: Reversible Streaming Restoration

Redacting an outgoing prompt is only half the problem. If you replace [email protected] with [EMAIL_1] and 4532-0151-1283-0366 with [CREDIT_CARD_1], the LLM responds using those semantic placeholders:

"Invoice sent to [EMAIL_1] for card [CREDIT_CARD_1]."

In production, LLMs stream responses token-by-token over Server-Sent Events (SSE). A placeholder like [EMAIL_1] frequently arrives split across chunk boundaries:

Chunk 1: "Invoice sent to [EMA"
Chunk 2: "IL_1] for card [CREDIT_"
Chunk 3: "CARD_1]."

Naive regex replacement breaks immediately on split chunks. pii-veil solves this with StreamingRestorer, a state-machine buffer that detects partial placeholder prefixes at chunk boundaries and restores them in 69.65 µs per SSE chunk without buffering the entire response.

Benchmarks: pii-veil vs. Presidio vs. AWS Comprehend

Measured on standard x86_64 hardware using Rust's criterion statistical harness (reproducible via cargo bench -p pii-veil-core --bench bench_throughput):

Metric / Capability 🛡️ GLASTOR® pii-veil 🐍 Microsoft Presidio ☁️ AWS Comprehend
Typical Prompt Latency (1 KB, 7 entities) 165.68 µs (0.165 ms) ~35 ms ~140 ms
Single-Entity Scan Latency 4.01 µs ~12 ms ~90 ms
1 MB Throughput Buffer 20.58 ms (~46.3 MiB/s) ~4,200 ms (4.2 s) Not supported
Speedup Baseline (1x) ~212x faster ~848x faster
SSE Streaming Restoration 69.65 µs / chunk ❌ Not supported ❌ Not supported
Memory Footprint < 15 MB (Distroless) ~450 MB (spaCy) Cloud Service
Cost per 1M Requests \$0.00 (Self-Hosted) Compute cost \$100.00+ USD

Why is it ~212x faster?

Presidio: Prompt -> Python -> spaCy NLP -> Tokenizer -> Non-SIMD Regex -> Copies [25–60 ms]
pii-veil: &str -> Rust Zero-Copy Slices -> Aho-Corasick SIMD Pre-Filter -> Checksum Validators -> In-Place Vault [0.16 ms]

Instead of loading a 450 MB ML model to detect structured PII and secrets, pii-veil combines SIMD-accelerated aho-corasick pre-filtering with 18 detectors backed by mathematical checksums (Luhn for credit cards, ISO 13616 Mod-97 for IBANs, Mod-23 for Spain DNI/NIE, Mod-11 for Argentina CUIT/CUIL and Chile RUT) to eliminate false positives.

Drop-In Usage: CLI, Proxy, MCP & GitHub Actions

1. Redact & Restore with an Authenticated Encrypted Vault

veil redact \
  --text "Charge [email protected] (CUIT 20-12345678-6) on card 4532-0151-1283-0366" \
  --vault-out vault.enc \
  --vault-pass "my-secret-passphrase"

veil restore \
  --text "Invoice sent to [EMAIL_1] (tax ID [NATIONAL_ID_1]) for card [CREDIT_CARD_1]." \
  --vault vault.enc \
  --vault-pass "my-secret-passphrase"

2. Built-in HTTP/SSE Proxy & MCP Server

  • veil proxy --listen 127.0.0.1:8080 --echo: Transparently redacts outgoing LLM JSON payloads and restores incoming text/event-stream SSE responses.
  • veil mcp: Zero-dependency JSON-RPC 2.0 MCP server over stdio exposing veil_redact, veil_restore, veil_redact_json, and veil_scan directly to Claude Desktop, Cursor, and Windsurf.

3. CI/CD DLP Gatekeeper (glastor-core/pii-veil@v1)

- name: 🛡️ PII & Secret DLP Audit
  uses: glastor-core/pii-veil@v1
  with:
    path: "."
    exclude: "examples,tests"
    fail-on-detect: "true"

Try It in 3 Seconds

Engineered by *GLASTOR®** — High-Performance Rust Systems, AI Infrastructure & DevSecOps Engineering. Need on-premise air-gapped deployments, SOC 2 / HIPAA / GDPR compliance logs, or custom DLP rules? Reach out at [email protected].*

📰 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.