Stop Leaking PII to LLMs: A Deterministic Laravel 13 Redaction Pipeline
When Laravel teams embed AI into support desks, error triage, or back-office intake, the most dangerous production bug rarely looks like an obvious hallucination. It looks like a normal customer ticket silently shipping
When Laravel teams embed AI into support desks, error triage, or back-office intake, the most dangerous production bug rarely looks like an obvious hallucination.
It looks like a normal customer ticket silently shipping raw credentials and payment cards straight to an external LLM API.
Customers routinely paste Stripe or OpenAI keys (sk-proj-...), GitHub tokens (ghp_...), Visa card numbers, and personal emails into free-text forms. Once that payload leaves your Laravel server, third-party retention windows and enterprise security questionnaires turn a helpful AI feature into a compliance incident.
Prompt instructions like "ignore any secrets or credit card numbers" do not protect youβbecause the leak already happened the millisecond the HTTP request left PHP.
(Note: The deterministic workflow discussed below was recently featured on LaravelMade!)
Why Naive Regex Fails in Production
The fastest way to break a real SaaS AI workflow with naive PII scrubbing is a blanket \d{16} regex.
In e-commerce, fintech, and B2B SaaS payloads, 16-digit numeric strings appear everywhere: order references (ORD-1234567890123456), deployment job IDs, and ledger references. If a regex blindly masks every 13-to-19 digit string, your AI support triage loses the exact order reference it needs to summarize the ticket.
The Pre-LLM Redaction Boundary
Instead of asking a probabilistic model to behave securely, the solution is to run a deterministic shield inside PHP at the ingress boundary.
I built an open-source package for this: warrenboswell/laravel-ai-guardrails.
It intercepts and replaces sensitive values with reversible numbered placeholders ([API_KEY_1], [CREDIT_CARD_1], [EMAIL_1]) before any model sees the prompt. To prevent the order ID problem, the credit_card detector extracts 13-to-19 digit candidates and validates each one through a Luhn checksum (passesLuhn()) before assigning a placeholder.
-
4532-0151-1283-0366passes the Luhn checksum β replaced with[CREDIT_CARD_1]. -
ORD-1234567890123456fails the Luhn checksum β preserved intact.
Production Laravel 13 Implementation
You can install the package via Composer:
composer require warrenboswell/laravel-ai-guardrails
Then, run AiGuardrails::redactPii() directly before invoking your Laravel AI SDK agent:
use App\Ai\Agents\SandboxPiiAgent;
use Illuminate\Support\Facades\Log;
use WarrenBoswell\LaravelAiGuardrails\Facades\AiGuardrails;
use WarrenBoswell\LaravelAiGuardrails\Guards\PiiRedactionGuard;
$redaction = AiGuardrails::redactPii(
$rawSupportTicket,
PiiRedactionGuard::DEFAULT_TYPES,
);
// 1. Log-safe audit payload: omits raw secret values from $redaction->tokenMap
Log::info('Pre-LLM PII scan completed', $redaction->toArray());
// 2. External LLM receives ONLY $redaction->redactedText
$agentResponse = (new SandboxPiiAgent)->prompt($redaction->redactedText);
// 3. Selectively restore ONLY safe contact placeholders ([EMAIL_*], [PHONE_*])
// while keeping [API_KEY_*] and [CREDIT_CARD_*] permanently scrubbed
$safeContactMap = array_filter(
$redaction->tokenMap,
static fn (string $placeholder): bool =>
str_starts_with($placeholder, '[EMAIL_') ||
str_starts_with($placeholder, '[PHONE_'),
ARRAY_FILTER_USE_KEY,
);
$replyForCustomer = strtr($agentResponse['safe_reply_draft'], $safeContactMap);
3 Details That Matter Here:
-
Zero raw secrets in audit logs:
PiiRedactionResult::toArray()intentionally exports only the counts, detected types, and placeholder keys (like['[API_KEY_1]', '[CREDIT_CARD_1]'])βnever the raw values. - Selective Rehydration: Notice the asymmetry at the output step. Emails and phone numbers can be selectively rehydrated inside Laravel when drafting an outbound customer reply, while API keys and cards remain permanently scrubbed so your system never echoes a secret back over email.
-
Severity Classification: Even though the LLM never sees the raw OpenAI key or Visa number, seeing
[API_KEY_1]lets your AI agent classify the event as aHIGH_SECRET_LEAKand recommend key rotation to the customer.
If you want to test this exact pipeline without writing any code, I set up a live, interactive demo where you can toggle individual detectors and inspect the exact sanitized text sent to the LLM:
π Try the Live PII & Secret Shield Sandbox
You can also check out the source code directly on GitHub.
How is your team currently handling PII before hitting the OpenAI or Gemini APIs? Let me know in the comments.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.