Laravel Security Best Practices
Comprehensive security guidelines for Laravel applications to protect against common vulnerabilities.
Laravel 11+ apps have no app/Http/Kernel.php, RouteServiceProvider, AuthServiceProvider, or VerifyCsrfToken class. Register middleware, CSRF exceptions, and trusted proxies in bootstrap/app.php (->withMiddleware(function (Middleware $middleware) { $middleware->validateCsrfTokens(except: ['stripe/*']); })) and gates, policies, and rate limiters in AppServiceProvider::boot(). Snippets below that name those classes apply only to Laravel 10 and earlier: check bootstrap/app.php first.
When to Activate
- Setting up Laravel authentication and authorization (Sanctum, Passport, Jetstream, Breeze)
- Implementing user roles, permissions, and policies
- Configuring production security settings and environment variables
- Reviewing Laravel applications for security vulnerabilities
- Deploying Laravel applications to production
- Writing secure Eloquent queries and migrations
How to use this skill
Work through the checklist below. For each item that applies, open the matching section of reference.md for the code: production configuration and HTTPS, authentication (Sanctum, passwords, sessions), authorization (gates, policies, middleware), Eloquent mass assignment and SQL injection, CSRF, XSS, input validation, API security (rate limits, Sanctum vs Passport, CORS), file uploads, dependencies and secrets, queues, and security logging.
Quick Security Checklist
| Check |
Description |
APP_DEBUG=false |
Never run with debug enabled in production |
APP_KEY set |
Always run php artisan key:generate |
| HTTPS enforced |
Force HTTPS in production via middleware or proxy |
$fillable whitelisted |
Never use $guarded = [] |
| CSRF active |
@csrf on all state-changing forms |
| Sanctum/Passport configured |
API authentication with token abilities/scopes |
| Rate limiting applied |
Throttle API and auth endpoints |
| Input validation |
FormRequest with specific rules, never $request->all() |
| File upload restrictions |
Validate MIME types, size, dimensions |
composer audit in CI |
Check dependencies for known vulnerabilities |
password_hash / password_verify |
Use Laravel's built-in hashing (bcrypt/Argon2) |
| Session regeneration on login |
Call $request->session()->regenerate() |
| Security headers middleware |
CSP, X-Frame-Options, X-Content-Type-Options |
| Logged security events |
Audit log for auth failures, role changes, suspicious activity |
.env not committed |
Verify .gitignore includes .env |
APP_KEY exposure |
A leaked key lets attackers forge encrypted cookies and payloads; rotate with APP_PREVIOUS_KEYS (Laravel 11+) so sessions survive |
hash_equals() for tokens and signatures |
Never use ==. PHP reads numeric strings such as 0e123 as numbers, so two different hashes can compare equal |
| Webhook signatures |
Verify over the raw body ($request->getContent()) before parsing, reject old timestamps, and skip event ids you have already handled |
| Queued jobs |
A job keeps the ids it was queued with. Re-check tenant, owner and state inside handle(), because the row may have changed or moved |
| One-time actions |
Coupons, refunds, invites and OTPs run once. Inside DB::transaction(), either lock the row with lockForUpdate(), or run a conditional update such as WHERE used_at IS NULL and act only when the update returns 1. Test that a repeated request has no second effect |
| Server-side money |
Recompute prices and totals on the server. Accept amounts as integer cents, and reject negatives, zero where it is wrong, and huge values |
| Trusted proxies |
Behind a load balancer, set trusted proxies in bootstrap/app.php. If X-Forwarded-For is trusted from any address, a client can fake its IP and dodge IP rate limits |
Review output
Laravel: [version] - Skeleton: [10-style Kernel | 11+ bootstrap/app.php]
Findings: P0/P1/P2 - [file:line] - [issue] - [fix]
Verification: composer audit; php artisan route:list -v (auth and throttle middleware on every sensitive route); grep for `$guarded = []`, `{!!`, `$request->all()` into create/update, raw SQL with interpolation
Unverified: [...]
Related Skills
bosskuai-laravel-development — Laravel architecture, routing, Eloquent, and API patterns
bosskuai-laravel-tdd — Laravel testing with PHPUnit and Pest
bosskuai-laravel-verification — full lint → test → security → deploy-readiness gate
bosskuai-cybersecurity-risk — broader threat modeling beyond the Laravel layer
bosskuai-tenant-isolation-security — tenant scoping and data-leak review
../../references/checklists/engineering-quality-bar.md: the shared bar, with the scenario walk and production data checks.
1---2name: bosskuai-laravel-security3description: "Use when reviewing or hardening a Laravel codebase before a security-sensitive merge or release: auth, authorization, Eloquent safety, CSRF/XSS, API security, production config."4license: MIT5metadata:6 author: affaan-m/ECC7 source: https://skillmd.com/skills/wankimmy/bosskuai-laravel-security8 upstream: laravel-security9---1011# Laravel Security Best Practices1213Comprehensive security guidelines for Laravel applications to protect against common vulnerabilities.1415> Laravel 11+ apps have no `app/Http/Kernel.php`, `RouteServiceProvider`, `AuthServiceProvider`, or `VerifyCsrfToken` class. Register middleware, CSRF exceptions, and trusted proxies in `bootstrap/app.php` (`->withMiddleware(function (Middleware $middleware) { $middleware->validateCsrfTokens(except: ['stripe/*']); })`) and gates, policies, and rate limiters in `AppServiceProvider::boot()`. Snippets below that name those classes apply only to Laravel 10 and earlier: check `bootstrap/app.php` first.1617## When to Activate1819- Setting up Laravel authentication and authorization (Sanctum, Passport, Jetstream, Breeze)20- Implementing user roles, permissions, and policies21- Configuring production security settings and environment variables22- Reviewing Laravel applications for security vulnerabilities23- Deploying Laravel applications to production24- Writing secure Eloquent queries and migrations2526## How to use this skill2728Work through the checklist below. For each item that applies, open the matching section of [`reference.md`](reference.md) for the code: production configuration and HTTPS, authentication (Sanctum, passwords, sessions), authorization (gates, policies, middleware), Eloquent mass assignment and SQL injection, CSRF, XSS, input validation, API security (rate limits, Sanctum vs Passport, CORS), file uploads, dependencies and secrets, queues, and security logging.2930## Quick Security Checklist3132| Check | Description |33|-------|-------------|34| `APP_DEBUG=false` | Never run with debug enabled in production |35| `APP_KEY` set | Always run `php artisan key:generate` |36| HTTPS enforced | Force HTTPS in production via middleware or proxy |37| `$fillable` whitelisted | Never use `$guarded = []` |38| CSRF active | `@csrf` on all state-changing forms |39| Sanctum/Passport configured | API authentication with token abilities/scopes |40| Rate limiting applied | Throttle API and auth endpoints |41| Input validation | FormRequest with specific rules, never `$request->all()` |42| File upload restrictions | Validate MIME types, size, dimensions |43| `composer audit` in CI | Check dependencies for known vulnerabilities |44| `password_hash` / `password_verify` | Use Laravel's built-in hashing (bcrypt/Argon2) |45| Session regeneration on login | Call `$request->session()->regenerate()` |46| Security headers middleware | CSP, X-Frame-Options, X-Content-Type-Options |47| Logged security events | Audit log for auth failures, role changes, suspicious activity |48| `.env` not committed | Verify `.gitignore` includes `.env` |49| `APP_KEY` exposure | A leaked key lets attackers forge encrypted cookies and payloads; rotate with `APP_PREVIOUS_KEYS` (Laravel 11+) so sessions survive |50| `hash_equals()` for tokens and signatures | Never use `==`. PHP reads numeric strings such as `0e123` as numbers, so two different hashes can compare equal |51| Webhook signatures | Verify over the raw body (`$request->getContent()`) before parsing, reject old timestamps, and skip event ids you have already handled |52| Queued jobs | A job keeps the ids it was queued with. Re-check tenant, owner and state inside `handle()`, because the row may have changed or moved |53| One-time actions | Coupons, refunds, invites and OTPs run once. Inside `DB::transaction()`, either lock the row with `lockForUpdate()`, or run a conditional update such as `WHERE used_at IS NULL` and act only when the update returns 1. Test that a repeated request has no second effect |54| Server-side money | Recompute prices and totals on the server. Accept amounts as integer cents, and reject negatives, zero where it is wrong, and huge values |55| Trusted proxies | Behind a load balancer, set trusted proxies in `bootstrap/app.php`. If `X-Forwarded-For` is trusted from any address, a client can fake its IP and dodge IP rate limits |5657## Review output5859```text60Laravel: [version] - Skeleton: [10-style Kernel | 11+ bootstrap/app.php]61Findings: P0/P1/P2 - [file:line] - [issue] - [fix]62Verification: composer audit; php artisan route:list -v (auth and throttle middleware on every sensitive route); grep for `$guarded = []`, `{!!`, `$request->all()` into create/update, raw SQL with interpolation63Unverified: [...]64```6566## Related Skills6768- `bosskuai-laravel-development` — Laravel architecture, routing, Eloquent, and API patterns69- `bosskuai-laravel-tdd` — Laravel testing with PHPUnit and Pest70- `bosskuai-laravel-verification` — full lint → test → security → deploy-readiness gate71- `bosskuai-cybersecurity-risk` — broader threat modeling beyond the Laravel layer72- `bosskuai-tenant-isolation-security` — tenant scoping and data-leak review73- `../../references/checklists/engineering-quality-bar.md`: the shared bar, with the scenario walk and production data checks.