Dev.to Security πŸ” Cybersecurity πŸ‘ 2 πŸ“– 6 min read

Pre-Inference Guardrails & Prompt DLP: Stopping PII, Secrets & Prompt Injection Before Your LLM

When engineering teams adopt AI coding assistants (VS Code Continue, Cline, Roo Code, Cursor) and internal chat portals (Open WebUI), security teams almost always ask the same terrifying question: "What happens the day

When engineering teams adopt AI coding assistants (VS Code Continue, Cline, Roo Code, Cursor) and internal chat portals (Open WebUI), security teams almost always ask the same terrifying question:

"What happens the day a developer pastes a production .env file, a customer IBAN, a database URI with credentials, or a real SQL dump into a prompt?"

Even if you self-host your LLMs on-premise, post-hoc auditing is too late:

  • The raw secret or PII has already entered the model context window.
  • It has already been persisted in observability traces (Langfuse) and object storage (MinIO).
  • You now have a GDPR / LOPDGDD / PCI-DSS / NIS2 / DORA compliance incident on your hands.

To solve this at the root, we built a Pre-Inference Guardrails & Prompt DLP (Data Loss Prevention) Engine directly into Sarrera β€” our open-source, self-hosted Enterprise AI Inference Gateway.

In this post, I'll walk through how the engine works, how we avoid false positives in source code using real mathematical checksums, how policy precedence is enforced (Global > User > Group), and how every incident is logged to a tamper-evident SHA-256 SIEM chain without ever storing raw PII.

Zero-Bypass Edge Architecture

Most gateway guardrails only protect /v1/chat/completionsβ€”leaving Open WebUI, /v1/embeddings, or /v1/responses as accidental bypass paths.

In Sarrera, every single inference path flows through the Guardrails Gatekeeper before it touches LiteLLM, Ollama, or Langfuse:

  [ VS Code / Cline / Cursor / curl ]       [ Open WebUI (Chat Portal) ]
                  β”‚                                      β”‚
                  β–Ό (HTTPS :443)                         β–Ό (OPENAI_API_BASE_URL)
         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                             β”‚
         β”‚   Caddy Edge    │──────────────────────────────
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                             β–Ό
                                     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                     β”‚   Sarrera Guardrails & DLP Engine    β”‚
                                     β”‚   (Sub-millisecond Pre-Inference)    β”‚
                                     β”‚                                      β”‚
                                     β”‚  1. Resolve Identity (Key / SSO)     β”‚
                                     β”‚  2. Evaluate Global > User > Group   β”‚
                                     β”‚  3. Run 23 Checksum + AI Detectors   β”‚
                                     β”‚  4. Enforce: BLOCK | REDACT | AUDIT  β”‚
                                     β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                            β”‚                      β”‚ (Async, masked preview only)
                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”           β–Ό
                       β–Ό (BLOCK: HTTP 400, 0 GPU)      β–Ό      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
               [ Immediate Error ]            [ Clean Prompt ]β”‚ SHA-256 SIEM Chain (CEF) β”‚
                                                       β”‚      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                       β–Ό
                                         [ LiteLLM β†’ Ollama β†’ Langfuse ]

Because inspection happens pre-inference:

  • Blocked prompts return an OpenAI-compatible HTTP 400 (guardrails_violation) in under a millisecondβ€”consuming zero GPU slots and zero tokens.
  • Redacted prompts replace sensitive spans in-place ([REDACTED:AWS_KEY], [REDACTED:FIN_IBAN]) across messages, prompt, and input so developers keep working without interruption, while neither the LLM nor Langfuse ever sees the raw secret.

23 Built-In Detectors (With Mathematical Checksums)

Naive regex-only DLP engines are notorious for crying wolf on source codeβ€”flagging random 16-digit constants as credit cards or arbitrary strings as IDs.

Sarrera combines Cloudflare Zero Trust DLP Predefined Profiles, Cloudflare AI Gateway Guardrails (P1 + S1–S14), and real mathematical checksum validation to keep false positives near zero:

Category Built-In Detectors Validation & Standards
National IDs & Health / PHI es_dni_nie, es_cif, es_nss, cf_eu_us_national_id, cf_health_phi, pii_email, pii_phone Spanish DNI/NIF & NIE (modulo-23 check letter), CIF control digit, Social Security (NSS) mod-97, US SSN, EU passports, medical/PHI records (GDPR Art. 9 / HIPAA), allowlist-aware emails & phones.
Financial (PCI-DSS / DORA) fin_iban, fin_credit_card, cf_swift_bic Spanish & SEPA IBAN (ISO 7064 MOD-97), Visa/Mastercard/Amex/Discover (Luhn algorithm), SWIFT/BIC & ABA routing codes.
Developer Secrets (OWASP LLM02) secret_cloud_api_keys, secret_private_keys, secret_jwt_bearer, secret_db_uri AWS (AKIA/ASIA), Azure SAS/Storage, GCP (AIza), Cloudflare API tokens, GitHub (ghp_/github_pat_), GitLab, Slack, Stripe, OpenAI/Anthropic (sk-), PEM/SSH private keys, JWT/Bearer tokens, DB URIs with embedded passwords.
AI Guardrails (P1 / S1–S14) sec_prompt_injection, sec_obfuscation, cf_safety_harmful, cf_s6_specialized_advice P1 Prompt Injection & Jailbreak (EN & ES), S14 Code Interpreter Abuse / reverse shells / destructive commands & Unicode smuggling, S1–S11 malware/phishing/harmful content, S6 regulated medical/legal/financial advice.
Corporate IP & Abuse Limits infra_private_ip, corp_confidential_markers, corp_sql_dump, cf_s8_intellectual_property, limit_prompt_size RFC 1918 internal IPs/domains, confidentiality headers, production INSERT INTO SQL dumps, S8 license/DRM bypass markers, and max prompt size caps (Denial of Wallet protection).

Deterministic Policy Precedence: Global > User > Group

Enterprise security teams often need two operating modes:

  • Corporate Lockdown Mode: One strict global policy applies to everyone, everywhere.
  • Delegated Mode: Different departments (e.g., Finance, Platform Engineering, R&D) or individual power users have tailored rules.

Sarrera enforces a strict, deterministic precedence hierarchy:

Is Global Policy ACTIVE (`global_policy.enabled == true`)?
 β”œβ”€β”€ YES ──▢ Enforce ONLY the Global Policy (User & Group policies are overridden)
 └── NO  ──▢ Delegated Mode:
              1. Does the User have an active policy?  ──▢ Enforce User Policy
              2. Else, does any of their Groups have an active policy?
                 ──▢ Merge active Group policies using most-restrictive action:
                     block > redact > audit
              3. Otherwise                             ──▢ Pass-through

Even when Global Policy is active, administrators can still pre-configure User and Group policies in the portalβ€”they display a clear OVERRIDDEN BY GLOBAL indicator so teams can prepare delegated rules safely without weakening corporate enforcement.

Identity (user_id, user_email, groups) is resolved automatically from LiteLLM Virtual API Keys (/key/info + /user/info) and Sarrera SSO headers (X-User-Email, X-User-Groups).

⚑ 3 Enforcement Actions

Every policy defines a default action and supports per-rule overrides:

Action Behavior Best For
block Rejects the request immediately with HTTP 400 and a clear explanation. Zero tokens or GPU slots used. Secrets, private keys, prompt injection, strict GDPR/PCI-DSS boundaries.
redact Masks sensitive spans in-place ([REDACTED:FIN_IBAN]) before forwarding to LiteLLM, with an optional subtle stream notice. Uninterrupted developer flow with automatic PII scrubbing.
audit Lets the request through while logging a masked preview to the incident log and SIEM chain. Dry-run rollouts and tuning before enforcing blocks.

Interactive Governance Console & Live DLP Playground

The Sarrera Admin Portal includes a dedicated Guardrails & DLP section (now organized cleanly in the collapsible left sidebar) with four specialized views:

  1. Global Policy: Toggle global enforcement, apply 4 one-click Cloudflare/Enterprise Preset Profiles, fine-tune any built-in rule in-place (min_matches thresholds, extra regexes, extra keywords, per-rule exceptions, custom redaction tags), create custom rules with a full CRUD editor, and Import/Export policies as JSON.
  2. Group & User Policies: Manage delegated policies per company group or individual user with real-time precedence badges.
  3. Live DLP Playground: Test any prompt against a simulated user_id, group, and model with 1-click realistic test payloads (DNI, IBAN, AWS Key, Prompt Injection, SQL Dump) and inspect the exact effective policy, matched rules, and redacted output before rolling out to production.
  4. Incident History & KPIs: Real-time counters for blocks, redactions, and audit alerts, with direct deep-linking to Sarrera's SIEM SHA-256 Chain (/certificates?tab=siem).

Tamper-Evident SHA-256 SIEM Logging (Zero Raw PII Stored)

Every block, redact, or audit event is forwarded asynchronously to POST /auth/compliance/internal-event and appended to Sarrera's cryptographically hash-chained SHA-256 CEF:0 SIEM audit log (prev_hash β†’ entry_hash).

Crucially, raw sensitive values are never stored in logsβ€”only safe masked previews (e.g., 12****78Z or AKIA****MPLE) and rule metadata, keeping your audit trail compliant with GDPR, NIS2, DORA, and ISO 27001.

Built for Scale: Ready for Standalone Extraction

Today, the engine runs in-process inside services/discovery/guardrails_engine.py with sub-millisecond latency and zero extra RAM footprint, persisting policies in a dedicated discovery_data Docker volume.

Need heavy ML/NLP scanners like Microsoft Presidio or ProtectAI LLM Guard down the road? Sarrera already exposes a GUARDRAILS_URL (external_guardrails_url) hook so you can offload scanning to a dedicated ai-guardrails microservice without touching a single line of gateway routing.

Try Sarrera Yourself

Sarrera is 100% open source and runs on Docker Compose:

How are you protecting against prompt data leakage and secret exposure in your AI coding workflows today? Would love to hear your thoughts and detector ideas in the comments!

πŸ“° 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.