Last month, I had a client in Abu Dhabi—a mid-sized logistics company—ask me to secure their Laravel/Next.js platform before launch. They wanted SQL injection, XSS, and CSRF defense done in a single two-week sprint. I told them it was possible but warned it’d require breaking some code and rerouting workflows. By the end? The app wasn’t just secure; it became a template for future projects.
SQL Injection: Why I Stop Using Raw Queries Cold Turkey
Every time I see DB::select in a Laravel app, I cringe. Yes, Eloquent is slower for complex queries, but parameterized statements are non-negotiable. For this sprint, I replaced all raw SQL with $request->input() filters and where() clauses. When I had to keep a raw query for a reporting dashboard (against my better judgment), I forced myself to test it with 1=1-- injection payloads. It failed gloriously—thankfully in staging.
To sanitize inputs further, I added regex rules in Laravel’s FormRequest files. For example:
'invoice_number' => 'required|regex:/^\w{8,20}$/' If I had to use raw SQL, I’d use PostgreSQL’s pg_escape_string() function instead of hoping Laravel’s query builder would handle it.
I also made it a rule: no exceptions. If a query required dynamic SQL, we refactored it. No “just this once” moves.
XSS Prevention: React Native and Next.js Are My Allies
This client’s app had a user-generated reviews section in React Native and a bilingual booking form in Next.js. Both areas were vulnerable to tags getting stored and executed later.
React Native’s Text component escapes content by default. But in the reviews screen, I had to render HTML (like bold text). So I used a package called react-native-htmlview with custom tag handlers that stripped dangerous elements. For freeform search bars, I added a client-side sanitization step using a tiny DOMPurify wrapper before saving to Firebase.
In Next.js, I used dangerouslySetInnerHTML only for pre-vetted content—like admin-uploaded HTML banners. Even then, I forced a sanitization step using the xss npm package on the backend.
A tip I learned the hard way: browsers don’t always escape characters like < properly in dynamic JavaScript strings. Always double-check payloads in dev tools when users submit stuff like test or .
CSRF Protection: Laravel’s Built-In Tools Save Time
CSRF tokens were the easiest to fix but the most annoying to test. Laravel automatically inserts meta tags and middleware checks if you use Blade. But this project ditched Blade for a Next.js frontend hitting Laravel APIs.
So I had to:
- Set
SameSite=StrictandSecureon session cookies inconfig/session.php. - Return CSRF tokens via a
/csrf-tokenAPI endpoint and store them in browser memory (not localStorage or cookies). - Add middleware to both Laravel and Next.js routes that compares the
XSRF-TOKENheader with the server’s current value.
Yes, this meant rewriting 80% of their AJAX requests to include the header. But after testing with Postman—faking a request from an external domain—the CSRF attack vectors were gone.
Testing All Three Flavors—Fast and Dirty
I automated basic checks in GitHub Actions with:
- •SQLMap scans on all API endpoints
- •XSS payloads (
alert("xss"),, etc.) in form submissions - •CSRF cookie mismatches via Cypress tests
The full pipeline, linked here, runs in 12 minutes. It fails the build if any tool reports a vulnerability.
One annoying discovery: The client’s “remember me” token in Laravel’s session cookie had HttpOnly=false. It took me an hour to realize it was exposing the session ID to JS. Fixed with a single config tweak in config/session.php.
FAQs from Clients Who Think Security Is Optional
What’s the fastest way to stop SQL injection?
Parameterized queries, period. In Laravel, use Eloquent or query builder. For Next.js APIs, escape inputs with Postgres extensions (pg_escape_string) or use Prisma.
How do I prevent XSS in a React Native app?
Sanitize inputs before saving/storing. Rely on React Native’s Text component for user-generated content. If rendering HTML is a must, use a safe parsing library and whitelist tags.
What are the minimum steps for CSRF protection?
Set SameSite=Strict cookies, send CSRF tokens in headers (not URLs), and verify them on the backend. In Laravel, enable middleware like VerifyCsrfToken and avoid disabling it for APIs.
Why do I still get XSS warnings after validation?
Validation isn’t enough. HTML tags like can bypass regex. Use libraries to parse and sanitize, like DOMPurify, instead of relying on string replacement.
If you want a security audit without the lecture-style advice, book a free consultation. We’ll walk through your stack together—Laravel, Next.js, Firebase, whatever—and find the cheapest lines of code with the biggest payoff.