Beta: the full scanner is free while it is in beta. Sign in and scan a site

ScanBastion Start Free Beta Scan

Learn

Laravel production security checklist: debug mode, .env, APP_KEY and dashboards

Most Laravel incidents on client sites do not start with a framework vulnerability. They start with a production server that still behaves like a laptop: debug mode on, the project root served instead of public/, a .env file one request away, or Telescope open to anyone. This checklist covers the settings to confirm before a Laravel site goes live or changes hands, with configuration for Laravel 11 and later, and which items an external scan can check from outside.

Code examples use the bootstrap/app.php structure introduced in Laravel 11. On Laravel 10 and earlier, the same settings live in app/Http/Kernel.php and the published middleware classes.

1. Debug mode off

  • APP_ENV=production and APP_DEBUG=false in the production .env. With debug on, an exception shows visitors the stack trace, source excerpts, request headers and, on Laravel 11 and later, the database queries of the request. php artisan about on the server shows both values.
  • Ignition on older sites. Laravel 10 and earlier shipped Ignition as the debug page (facade/ignition, later spatie/laravel-ignition). facade/ignition before 2.5.2 allowed unauthenticated remote code execution on sites with debug mode on (CVE-2021-3129). Keep it in require-dev only and run composer install --no-dev in production.
  • Run php artisan optimize during deployment. It caches configuration, routes, events and views, and once configuration is cached the .env file is no longer read.

2. The document root is public/

The web server must serve public/ and nothing above it. Served from the project root, .env, composer.json, storage/logs and the source code all become downloadable. The nginx configuration in the Laravel documentation does this and denies every dotfile except .well-known:

root /srv/example.com/public;

location ~ /\.(?!well-known).* {
    deny all;
}

Shared hosting is where this goes wrong, usually through a copied index.php or a control panel that cannot change the root. If you inherit such a site, move the project above the web root rather than relying on an .htaccess rule.

3. .env exposure and APP_KEY rotation

If .env was ever readable, assume it was copied, and rotate every secret in it: database and mail passwords, API keys, and APP_KEY. Rotate APP_KEY too when it was copied from another project or shared with people who no longer work on the site.

Changing APP_KEY logs every user out, because Laravel encrypts its cookies, and makes values encrypted with the old key unreadable. For a routine rotation, list the old key in APP_PREVIOUS_KEYS: Laravel encrypts with the new key and falls back to the old ones when decrypting.

APP_KEY="base64:...new key..."
APP_PREVIOUS_KEYS="base64:...old key..."

After a leak, do not list the old key: Laravel would still accept cookies and values made with it. Accept the logouts, and re-encrypt any stored data that used the old key before you remove it. php artisan key:generate --show prints a new key without writing it to .env.

4. storage/ and logs

  • Logs stay outside the web root. storage/logs/laravel.log holds exception messages, file paths and sometimes request data. With the document root correct it is not reachable; check that it is not, and that log rotation is on.
  • storage/app/public is public. php artisan storage:link serves it at /storage, so invoices, exports or ID documents stored on the public disk are downloadable by anyone with the URL. Store private files on a private disk and serve them through a route that checks authorisation.
  • Nothing extra in public/: no phpinfo.php, Adminer, SQL dump or zipped backup left by a previous developer.

5. Telescope, Horizon and other dashboards

  • Telescope records requests, queries, jobs, mail and exceptions with their data. Install it with --dev and register it only in the local environment, as the Telescope documentation describes. If it must run in production, restrict the viewTelescope gate in TelescopeServiceProvider. The documentation is explicit: if APP_ENV is not production, the dashboard is public.
  • Horizon at /horizon uses the same pattern with the viewHorizon gate in HorizonServiceProvider. Check that the gate lists real, current people, not the placeholder address.

6. Session cookie flags

In config/session.php (or the matching variables in .env):

  • SESSION_SECURE_COOKIE=true. The skeleton leaves it unset, and then the cookie is marked Secure only when Laravel sees the request as HTTPS. Behind a proxy that terminates TLS, without trusted proxies (section 7), it does not, and the session cookie goes out without the flag.
  • http_only defaults to true; leave it. same_site defaults to lax, which suits most sites.
  • SESSION_DOMAIN unset unless the session must be shared with subdomains, since any subdomain can then read it.
  • XSRF-TOKEN is readable by scripts by design: Axios and similar libraries copy it into the X-XSRF-TOKEN header, so it must not be HttpOnly. Some scanners report it as a session cookie readable by scripts; ScanBastion does not, though it still reports it if it is sent without Secure or SameSite. Record any such finding as accepted rather than changing the cookie.

7. Trusted proxies and hosts

Behind Cloudflare or a load balancer, Laravel needs to trust the proxy to see HTTPS and the visitor's address:

->withMiddleware(function (Middleware $middleware): void {
    $middleware->trustProxies(at: ['10.0.0.0/8']);
    $middleware->trustHosts(at: ['^example\.com$']);
})

trustProxies(at: '*') is acceptable only when the origin cannot be reached except through the proxy. Otherwise anyone can send their own X-Forwarded-For and change the IP address that rate limiting and logs see.

8. Security headers middleware

Laravel sends no security headers by default. Set them at the web server or CDN, or in one middleware appended to every response:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class SecurityHeaders
{
    public function handle(Request $request, Closure $next): Response
    {
        $response = $next($request);
        $response->headers->set('X-Content-Type-Options', 'nosniff');
        $response->headers->set('X-Frame-Options', 'DENY');
        $response->headers->set('Referrer-Policy', 'strict-origin-when-cross-origin');
        $response->headers->set('Permissions-Policy', 'camera=(), microphone=(), geolocation=()');

        return $response;
    }
}

Then register it for every request in bootstrap/app.php:

->withMiddleware(function (Middleware $middleware): void {
    $middleware->append(\App\Http\Middleware\SecurityHeaders::class);
})

Pick one layer per site; the guide to configuring headers on Cloudflare, nginx and Vercel explains why. HSTS is usually better set at the server or CDN, and a CSP with nonces can use Vite::useCspNonce() in the same middleware.

What an external scan checks

ScanBastion's Standard profile requests /.env, /.env.backup and /.env.production and reports a match only when the response contains keys such as APP_KEY or DB_PASSWORD. It also checks /telescope/requests and Ignition's /_ignition/health-check, readable composer.json and .git, phpinfo() pages, database tools, SQL dumps and directory listings, whether or not Laravel was identified. Every profile checks the headers and cookie flags on each crawled page and looks for debug output and database errors in the pages it reads: Whoops, Ignition, and the exception page Laravel 11 and later show with APP_DEBUG=true, including on pages that answer with a server error. It identifies Laravel from the session cookie named after APP_NAME together with XSRF-TOKEN.

It does not check Horizon, laravel.log or files under /storage, and it sees the debug page only if a page it crawls happens to fail: it never sends requests designed to cause an error, so a clean scan does not prove debug mode is off. Confirm sections 1, 4 and 5 on the server, then run a scan for the rest. The detection templates page lists the full set.