Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell PHP to use SameSite=None for cross-site cookies?

According to the article here https://php.watch/articles/PHP-Samesite-cookies and PHP documenation at https://www.php.net/manual/en/session.security.ini.php, There are only 2 possible config options for this new feature, added in PHP 7.3:

  1. session.cookie_samesite=Lax
  2. session.cookie_samesite=Strict

Yet, according to the Chrome console, this needs to be set to "None":

A cookie associated with a cross-site resource at URL was set without the SameSite attribute. It has been blocked, as Chrome now only delivers cookies with cross-site requests if they are set with SameSite=None and Secure. You can review cookies in developer tools under Application>Storage>Cookies and see more details at URL and URL.

Because of this, I can no longer set cross-site cookies. What is the workaround?

like image 215
Dane Iracleous Avatar asked Dec 30 '19 18:12

Dane Iracleous


People also ask

How do I set SameSite cookies to none?

SameSite=None requires Secure The warning appears because any cookie that requests SameSite=None but is not marked Secure will be rejected. To fix this, you will have to add the Secure attribute to your SameSite=None cookies. A Secure cookie is only sent to the server with an encrypted request over the HTTPS protocol.

Is it safe to set SameSite to none?

The none value won't give any kind of protection. The browser attaches the cookies in all cross-site browsing contexts. The default value of the SameSite attribute differs with each browser, therefore it is advised to explicitly set the value of the attribute.

How do you set the SameSite attribute of cookies to LAX strict in PHP?

You can use the $options array to set the samesite value, for example: setcookie($name, $value, [ 'expires' => time() + 86400, 'path' => '/', 'domain' => 'domain. example', 'secure' => true, 'httponly' => true, 'samesite' => 'None', ]); The value of the samesite element should be either None , Lax or Strict .

Should I use SameSite lax or strict?

The SameSite=Strict value will only allow first party cookies to be sent. This setting is good for user actions like login credentials, but the cookie will not be sent on the initial request to the webpage. The SameSite=Lax setting will allow the user to maintain a logged in status while arriving from an external link.

What is the SameSite cookie attribute?

The SameSite cookie attribute prevents cross-site request forgery (CSRF) attacks by stopping browsers from sending cookies to other sites. A CSRF is an attack that forces end-users to execute unwanted actions on the web applications where they are currently authenticated.

Is it possible to set cross-site cookies in chrome?

It has been blocked, as Chrome now only delivers cookies with cross-site requests if they are set with SameSite=None and Secure. You can review cookies in developer tools under Application>Storage>Cookies and see more details at URL and URL. Because of this, I can no longer set cross-site cookies. What is the workaround? Show activity on this post.

Why are my cookies not working on SameSite?

If the issue is primarily a browser or tab crashing or hanging, it is less likely to be caused by the new SameSite cookie behavior. First, check if the problem persists after setting the SameSite flags above to “Disabled” (note: setting them to “Default” may or may not disable the features).

What is the new rule for cross-site cookies?

The new rule demands that all cross-site cookies set in a browser have to be set with Secure attribute if they are to have None as their SameSite value. This is esoterically for cookies meant to be served in cross-site contexts only.


2 Answers

You can set the value to "None" using ini_set. There's no check that the value is supported when that function is used:

ini_set('session.cookie_samesite', 'None');
session_start();

session_set_cookie_params can also set it:

session_set_cookie_params(['samesite' => 'None']);
session_start();

The bug report for this to be supported in php.ini is here.


As @shrimpwagon said in a comment below, session.cookie_secure must be true for this to work. PHP doesn't require it, but browsers do.

like image 200
Anonymous Avatar answered Oct 19 '22 02:10

Anonymous


ini_set('session.cookie_secure', "1"); ini_set('session.cookie_httponly', "1"); ini_set('session.cookie_samesite','None'); session_start();

php 7.4 samesite in phpinfo enter image description here

php 7.2 samesite does not exist in phpinfo enter image description here

$currentCookieParams = session_get_cookie_params();
$cookie_domain= 'your domain';
if (PHP_VERSION_ID >= 70300) {
session_set_cookie_params([
    'lifetime' =>  $currentCookieParams["lifetime"],
    'path' => '/',
    'domain' => $cookie_domain,
    'secure' => "1",
    'httponly' => "1",
    'samesite' => 'None',
]);
} else {
session_set_cookie_params(
    $currentCookieParams["lifetime"],
    '/; samesite=None',
    $cookie_domain,
    "1",
    "1"
);
}
session_start();
like image 5
Farhad Aghasaghloo Avatar answered Oct 19 '22 03:10

Farhad Aghasaghloo