How to Fix Insecure Session Configuration in Laravel

Your Laravel session cookies are missing secure flags, enabling session hijacking and cross-site attacks. Fix your session config now.

High severity Application Security Updated 2026-03-01

The Problem

Insecure session configuration allows attackers to steal or manipulate user session cookies through network interception, cross-site scripting, or cross-site request attacks. When session cookies lack the Secure, HttpOnly, and SameSite flags, they can be transmitted over unencrypted connections, accessed by JavaScript, or sent with cross-origin requests, all of which enable session hijacking.

How to Fix

  1. 1

    Set secure session options in config/session.php

    Update your config/session.php with these security settings:

    {{ trim($paragraph)); ?>
  2. 2

    Use database or Redis for session storage

    File-based sessions can be exposed if the storage directory is accessible. Use database or Redis:

    {{ trim($paragraph)); ?>
    {{ trim($paragraph)); ?>

    Or use Redis for better performance:

    {{ trim($paragraph)); ?>

    Both options provide better security than file storage and support multi-server deployments.

  3. 3

    Regenerate session ID after authentication

    Regenerate the session ID after login to prevent session fixation attacks. Laravel does this automatically with its built-in authentication, but if you have custom auth:

    {{ trim($paragraph)); ?>
    {{ trim($paragraph)); ?>
    {{ trim($paragraph)); ?>
  4. 4

    Set appropriate session lifetime

    Do not set excessively long session lifetimes. In config/session.php:

    {{ trim($paragraph)); ?>

    For high-security applications (banking, healthcare):

    {{ trim($paragraph)); ?>

    Consider implementing idle timeout with JavaScript that warns users before session expiration.

How to Verify

Log in to your application and inspect the session cookie in your browser developer tools (Application > Cookies). Verify these flags are set:

- Secure: Yes (checkmark) - HttpOnly: Yes (checkmark) - SameSite: Lax or Strict

Also test with curl:

curl -I https://yourdomain.com/login

Look for Set-Cookie header with: Secure; HttpOnly; SameSite=Lax

Prevention

Include secure session settings in your base Laravel project template. Test cookie flags in your CI pipeline. Review session configuration during security audits. Use StackShield to monitor that session cookies maintain proper security flags across deployments.

Frequently Asked Questions

What does each cookie flag do?

Secure ensures the cookie is only sent over HTTPS, preventing interception on unsecured networks. HttpOnly prevents JavaScript from reading the cookie, blocking XSS-based session theft. SameSite=Lax prevents the cookie from being sent with cross-origin requests (with exceptions for top-level navigation), mitigating CSRF attacks.

Should I use SameSite Strict or Lax?

Lax is the recommended default. It allows the cookie to be sent when users navigate to your site from external links (important for user experience) but blocks it on cross-origin AJAX requests and form submissions. Strict blocks all cross-origin cookie sending, which can break login flows from email links or OAuth callbacks.

Does encrypting the session cookie help?

Yes. Setting encrypt to true in session.php encrypts the session cookie value using your APP_KEY. Even if an attacker intercepts the cookie, they cannot read or modify the session data without the encryption key. Laravel encrypts cookies by default through the EncryptCookies middleware.

Related Security Terms

Detect This Automatically with StackShield

StackShield continuously monitors your Laravel application from the outside and alerts you when security issues are found. No installation required.

Start Free Trial