Changes in Session Security Defaults in PHP 8.6

Changes in Session Security Defaults in PHP 8.6

Since PHP 8.6, three session INI directives have new default values, which make sessions more secure out of the box. Before PHP 8.6, these directives had to be changed manually in php.ini. Applications which rely on the previous behavior must now set these directives explicitly.

DirectiveBefore PHP 8.6Since PHP 8.6Description
session.use_strict_mode01Rejects a session ID which has no data in the session storage, which mitigates session fixation
session.cookie_httponly01Makes the session cookie unavailable to JavaScript through document.cookie
session.cookie_samesiteemptyLaxStops the browser from sending the session cookie on cross-site requests, except top-level navigation with a safe HTTP method

A cross-site POST request, such as a callback from a payment provider, no longer sends the session cookie, so the session is not available in the script which handles it.

The new values are compiled into PHP, so they can be read through the builtin_default_value key, which is also new in PHP 8.6:

<?php

$directives = [
    'session.use_strict_mode',
    'session.cookie_httponly',
    'session.cookie_samesite',
];

foreach ($directives as $name) {
    echo $name.' = '.ini_get_all()[$name]['builtin_default_value'].PHP_EOL;
}

Output:

session.use_strict_mode = 1
session.cookie_httponly = 1
session.cookie_samesite = Lax

The new defaults apply only when php.ini does not set these directives. A php.ini file copied from an older PHP version keeps the previous behavior, because the php.ini-production and php.ini-development files of PHP 8.5 set session.use_strict_mode = 0 and leave the two cookie directives empty. In PHP 8.6, both files set the new values.

Leave a Comment

Cancel reply

Your email address will not be published.