Ship AI with Laravel: I Tricked My Own AI Into Leaking Everything

Published on by

▶️ Watch the video tutorial (13 minutes)

Before we secure anything, I show you how bad it can get. With the right sequence of messages, I socially engineer our own support agent into handing over its entire system prompt, its list of tools, and its internal instructions. Then I point the order lookup tool at an order that isn't mine and it returns another customer's data without hesitation. Both of these are real vulnerabilities that ship in AI apps every single day.

Then we fix them with four layers of defense.

The first layer is the prompt itself. I add explicit security boundaries so the agent refuses to reveal its prompt, tools, or any sensitive data, and treats anyone claiming to be an employee as a regular customer. It helps, but a determined attacker can still talk their way around a prompt, so we don't stop there.

The second layer is a local LLM guard. I use Ollama running Llama 3.2 to build a separate prompt-guard agent whose only job is to classify each incoming message as safe or unsafe and return that as JSON. I wire it in as middleware so unsafe messages get blocked before they ever reach the main agent. Because it runs locally, this screening costs nothing per request.

The third layer is tool-level authorization. I scope the order lookup tool to the authenticated user so it physically cannot return anyone else's orders, no matter what order ID gets passed in. This is the fix that actually closes the data leak.

The fourth layer is output filtering. A safety net middleware that scans responses and redacts patterns like SSNs, credit card numbers, and API keys before anything leaves the system.

By the end the agent is genuinely hard to break. And that closes out the series. We went from composer require laravel/ai to a production-ready, tested, secure AI platform across eleven episodes.

Thanks for building along. If you're just finding this now, start at Episode 1 and work through, every episode builds on the last.

⭐ GitHub: https://github.com/harris21/ship-ai-with-laravel

▶️ Watch Episode 11 now →

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Tinkerwell

Enjoy coding and debugging in an editor designed for fast feedback and quick iterations. It's like a shell for your application – but with multi-line editing, code completion, and more.

Visit Tinkerwell
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media

The latest

View all →
Laravel Quota: Usage Budgets for Calendar Periods image

Laravel Quota: Usage Budgets for Calendar Periods

Read article
Find the Security Vulnerabilities in Your Laravel App with Sensagraph image

Find the Security Vulnerabilities in Your Laravel App with Sensagraph

Read article
Uvora: A macOS Menu Bar App for Finding Laravel Projects image

Uvora: A macOS Menu Bar App for Finding Laravel Projects

Read article
Passwordless Sign-In with Fortify Two-Factor Support in Laravel image

Passwordless Sign-In with Fortify Two-Factor Support in Laravel

Read article
Intercept: Middleware Guardrails for Laravel AI Agents image

Intercept: Middleware Guardrails for Laravel AI Agents

Read article
AI Review for Laravel Upgrades image

AI Review for Laravel Upgrades

Read article