🔐
Security

Laravel Application Security: Debug Mode, APP_KEY and Protecting .env

22.11.2026
← All articles

Laravel does a lot of security work for you: it checks CSRF tokens, binds database parameters, hashes passwords and encrypts cookies. Laravel apps still get breached, and the cause is almost always deployment rather than the framework. These are the settings to verify before going live.

Example: a clinic booking system

A private clinic in Namangan gets a Laravel app for online appointments at navbat.example.uz, storing patient names, phone numbers and which doctor they booked. The developer copied the project with local settings: debug mode stayed on in .env, and the document root pointed at the project folder instead of public. Together these meant anyone could read the database password.

The document root must be public

Only public should be reachable by browsers. .env, storage, vendor and config must not be. In ISPmanager, set the web domain's root directory to the public folder.

/home/klinika/navbat/          # Laravel project
/home/klinika/navbat/public/   # document root

curl -I https://navbat.example.uz/.env

You should get 404 or 403. If the file contents appear, rotate every secret in it immediately.

What APP_KEY protects

APP_KEY encrypts cookies, sessions, values stored via Crypt and signs URLs. With a leaked key an attacker can forge cookies, mint signed links and decrypt protected fields; in older Laravel versions a leaked key even enabled remote code execution. Never commit it to Git and use a different key per environment.

php artisan key:generate --show   # prints a new key without writing it
php artisan key:generate          # replaces APP_KEY in .env

Rotating the key logs everyone out and makes old encrypted data unreadable. Recent Laravel versions support APP_PREVIOUS_KEYS so old data can still be decrypted. Rotation is mandatory after a leak; otherwise check for encrypted columns first.

Production .env

APP_ENV=production
APP_DEBUG=false
APP_URL=https://navbat.example.uz
LOG_LEVEL=warning
SESSION_SECURE_COOKIE=true

With APP_DEBUG=true, error pages can reveal environment variables, paths and request details; we cover this in a separate article. Rebuild the config cache after every change, or the old values stay in effect:

composer install --no-dev --optimize-autoloader
php artisan config:cache
php artisan route:cache

Use chmod 600 .env. storage and bootstrap/cache need to be writable, but never 777.

Common code-level mistakes

Mass assignment lets a user add fields such as is_admin to a form. Define $fillable and create models from validated data:

// Risky
Appointment::create($request->all());

// Better
$data = $request->validate([
    'doctor_id' => 'required|exists:doctors,id',
    'phone'     => 'required|string|max:20',
]);
Appointment::create($data);

Never concatenate user input into DB::raw or whereRaw; use bindings. Avoid printing unescaped input with {!! !!} in Blade, which invites XSS. Keep the CSRF exception list to genuine external callbacks such as payment notifications.

Login routes and extra packages

Add the throttle middleware to custom login and SMS code routes so bots cannot brute-force passwords or run up your SMS bill. Disable Telescope or Debugbar in production or restrict them to administrators, since they expose requests, sessions and SQL. Run composer audit before each update to catch packages with known vulnerabilities. Whether the app runs on sayt.uz hosting or a VPS, application settings remain the developer's responsibility.

Related articles

🧩 Checking Composer packages for security: a guide for PHP project owners 🈲 Japanese SEO spam hack: strange pages from your site appear in Google 🐘 Critical PHP vulnerabilities: why you should not delay updating 📚 Securing an OJS Journal Site: Updates, File Storage and Bot Load
🌐 Language
🇺🇿 O⁠'zbek 🇺🇿 Özbek (yangi) 🇺🇿 Ўзбек 🇷🇺 Русский 🇬🇧 English ✓