Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

We need the "Partitioned" parameter to be added to setcookie() options. #12646

Open
Fell-x27 opened this issue Nov 10, 2023 · 20 comments
Open

We need the "Partitioned" parameter to be added to setcookie() options. #12646

Fell-x27 opened this issue Nov 10, 2023 · 20 comments

Comments

@Fell-x27
Copy link

Fell-x27 commented Nov 10, 2023

Description

As stated here (link), soon our cookies with SameSite=None; Secured, without the Partitioned parameter, will stop working.

Currently, there is no capability to add this parameter through setcookie(). A temporary solution could be to use header() to form the header, but it would be better to avoid this.

Google Chrome marks these cookies as deprecated already:

image

@damianwadley
Copy link
Member

Precedent: SameSite was implemented in PHP while the standard was still in draft status, with the rationale being that other libraries had already implemented it. Symfony has added support for Partitioned.

This may require an RFC, if just to decide once and for all whether or not to:
(a) Continue supporting the many-parameter syntax for setcookie(), which is becoming more and more unwieldy as more cookie options are invented, though named parameters does alleviate some of that complexity,
(b) Double-down on the options array, and at the same time I'd question whether the array needs to be locked-down as it is or whether it could be opened up to free-form options (🦶🔫),
(c) Switch to a whole new API entirely, such as an OOP builder.

There is a similar RFC for SameSite that's gone stale.

@nielsdos
Copy link
Member

Certainly not hard to implement at our side. Here's a proof-of-concept that adds the option to the setcookie() $options argument: #12652

I'm not sure whether adding the option to the $options array requires an RFC, but probably changing the signature does indeed. I agree with Damian that should be resolved together with "what to do with SameSite argument?" and we should indeed answer the a, b, c questions too.

@Fell-x27
Copy link
Author

Certainly not hard to implement at our side. Here's a proof-of-concept that adds the option to the setcookie() $options argument: #12652

I'm not sure whether adding the option to the $options array requires an RFC, but probably changing the signature does indeed. I agree with Damian that should be resolved together with "what to do with SameSite argument?" and we should indeed answer the a, b, c questions too.

Well, the array already allows more than args, so it wasn't a problem before as I see it :)

@westonruter

This comment was marked as duplicate.

@DustinAPI
Copy link

DustinAPI commented Dec 22, 2023

Google has sent out the notification that 1% of all chrome browsers will be using this on Jan 4, 2024!

https://blog.google/products/chrome/privacy-sandbox-tracking-protection/?utm_campaign=FY24Q2_GL_PRA_CUST_Google-LTI1.3_EN_FOL_CB-60516&utm_medium=email&utm_source=FY24Q2_GL_PRA_CUST_Google-LTI1.3_EN_FOL_CB-60516&utm_content=Product%2FBrand%20Announcement&utm_term=Upcoming%20Google%20Chrome%20changes%20that%20may%20impact%20LTI%20integrations

If this has not been handled elsewhere I think it needs to be put on high priority. Especially the $options version of setcookie as it already added samesite.

@nielsdos
Copy link
Member

As requested by Ilija I sent an email to the mailing list to be able to move forward with the PR.

@Fell-x27
Copy link
Author

It might also be necessary to include this option in the list of arguments for the session_set_cookie_params function.

@dxh9845
Copy link

dxh9845 commented Feb 6, 2024

To be clear, is there an alternative option for setting cookies if the partitioned parameter isn't allowed? I'm looking to support this for a PHP webapp at work that requires cross-site cookies for session management - specifically session_set_cookie_params. Without the Partitioned attribute, Chrome will drop this cookie from being set.

@DustinAPI
Copy link

@dxh9845 there are a couple of other alternatives to partitioned cookies suggested by chrome.

https://developers.google.com/privacy-sandbox/3pcd

@snake
Copy link

snake commented Feb 7, 2024

@dxh9845 it's possible, using header() directly (as suggested on the setcookie docs page) . It's not a particularly elegant solution by any measure, but it works just fine for Chrome partitioning support.

@wbdoll
Copy link

wbdoll commented Feb 8, 2024

@dxh9845 I just tried the following and it seems to work. Maybe someone can let us know if this is not a reliable solution.
<?php
session_start();
$id = session_id();
header("Set-Cookie: PHPSESSID=$id; Secure; Path=/; SameSite=None; Partitioned;");
?>

@rbalik
Copy link

rbalik commented Mar 5, 2024

Hi, glad to see you all are working on this. The warnings in Chrome are getting scarier :)
Was wondering how this would work with session cookies. Would this just be an option passed to session_start?

@jsnajdr
Copy link

jsnajdr commented Mar 14, 2024

It would be nice if the native setcookie function started supporting the Partitioned attribute. Firefox and Chrome already implement it and ship it in production. 3rd party cookies blocking is enabled by default for many users. Both browsers will reject your cookies in certain scenarios if they don't have the Partitioned attribute. Production sites need to be updated if they want to continue working as 3rd party embeds. There is a patch in #12652 ready to be reviewed and merged.

@nielsdos
Copy link
Member

I'm going to work on an RFC, that includes the session and setcookie stuff, sometime in the near future such that 8.4 will support this.

snake added a commit to snake/moodle that referenced this issue Mar 21, 2024
To opt a cookie in to Chrome's 3rd party cookie partitioning solution,
CHIPS, the property 'Partitioned;' needs to be set. This adds a helper
class supporting this, for a given cookie(s).

Note also, PHP's native
cookie APIs (setcookie, etc) don't support this cookie property yet -
(php/php-src#12646).

Since this class is intended to allow existing Set-Cookie headers to be
modified before being sent (e.g. allowing clients to set a property on a
cookie set elsewhere in code), it deals with the headers directly anyway
but it means that new cookies must also use this helper to opt-in,
instead of relying on setcookie(). E.g. where the intent is to add
partitioning support to a new cookie, that cookie must first be set
(setcookie) and then it may opt-in to partitioning via this helper;
partitioning support cannot be achieved directly through setcookie and
friends yet.
snake added a commit to snake/moodle that referenced this issue Mar 21, 2024
To opt a cookie in to Chrome's 3rd party cookie partitioning solution,
CHIPS, the property 'Partitioned;' needs to be set. This adds a helper
class supporting this, for a given cookie(s).

Note also, PHP's native
cookie APIs (setcookie, etc) don't support this cookie property yet -
(php/php-src#12646).

Since this class is intended to allow existing Set-Cookie headers to be
modified before being sent (e.g. allowing clients to set a property on a
cookie set elsewhere in code), it deals with the headers directly anyway
but it means that new cookies must also use this helper to opt-in,
instead of relying on setcookie(). E.g. where the intent is to add
partitioning support to a new cookie, that cookie must first be set
(setcookie) and then it may opt-in to partitioning via this helper;
partitioning support cannot be achieved directly through setcookie and
friends yet.
snake added a commit to snake/moodle that referenced this issue Mar 21, 2024
To opt a cookie in to Chrome's 3rd party cookie partitioning solution,
CHIPS, the property 'Partitioned;' needs to be set. This adds a helper
class supporting this, for a given cookie(s).

Note also, PHP's native
cookie APIs (setcookie, etc) don't support this cookie property yet -
(php/php-src#12646).

Since this class is intended to allow existing Set-Cookie headers to be
modified before being sent (e.g. allowing clients to set a property on a
cookie set elsewhere in code), it deals with the headers directly anyway
but it means that new cookies must also use this helper to opt-in,
instead of relying on setcookie(). E.g. where the intent is to add
partitioning support to a new cookie, that cookie must first be set
(setcookie) and then it may opt-in to partitioning via this helper;
partitioning support cannot be achieved directly through setcookie and
friends yet.
snake added a commit to snake/moodle that referenced this issue Mar 21, 2024
To opt a cookie in to Chrome's 3rd party cookie partitioning solution,
CHIPS, the property 'Partitioned;' needs to be set. This adds a helper
class supporting this, for a given cookie(s).

Note also, PHP's native
cookie APIs (setcookie, etc) don't support this cookie property yet -
(php/php-src#12646).

Since this class is intended to allow existing Set-Cookie headers to be
modified before being sent (e.g. allowing clients to set a property on a
cookie set elsewhere in code), it deals with the headers directly anyway
but it means that new cookies must also use this helper to opt-in,
instead of relying on setcookie(). E.g. where the intent is to add
partitioning support to a new cookie, that cookie must first be set
(setcookie) and then it may opt-in to partitioning via this helper;
partitioning support cannot be achieved directly through setcookie and
friends yet.
@running-grass
Copy link

ini_set('session.cookie_path', '/; Partitioned');

It tried it, it looks good.

@scollovati
Copy link

scollovati commented Apr 29, 2024

Setting the cookie via the HTTP Header (which is the way implemented by Symfony https://symfony.com/blog/new-in-symfony-6-4-chips-cookies) is not feasible with session cookies. The option for partitioned ones needs to be set also there: https://www.php.net/manual/en/session.configuration.php

@Fell-x27
Copy link
Author

Fell-x27 commented May 8, 2024

Setting the cookie via the HTTP Header (which is the way implemented by Symfony https://symfony.com/blog/new-in-symfony-6-4-chips-cookies) is not feasible with session cookies. The option for partitioned ones needs to be set also there: https://www.php.net/manual/en/session.configuration.php

But you can still write your own session cookie handler/manager... Not the best solution, but still better than nothing.

@renandecarlo
Copy link

This worked for me:

setcookie('cookie', $cipherData, [
	'expires' => strtotime('+ 3 hours'), 
	'path' => '/',
	'httponly' => true,
	'secure' => true,
	'samesite' => 'None; Partitioned' /* Hack to make CHIPS: https://github.com/php/php-src/issues/12646 */ 
]);

@lianee
Copy link

lianee commented May 30, 2024

This worked for me:

If you want that all cookies use this (even the PHPSESSID), you can do something like this this before session_start:

session_set_cookie_params(['path' => '/', 'samesite' => 'None; Partitioned', 'secure' => true]);

@AlainArachnid
Copy link

AlainArachnid commented Oct 30, 2024

I use the following, for apache2/PHP, that works with firefox 132.0 and chrome 130.0.6723.92.
Before each session_start()

ini_set('session.cookie_path', '/; Partitioned; Secure; SameSite=None');
session_name('APPNAMESESS');
session_start();

session_name is here to be able to track things in a cross-domain case, both on PHP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests