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

Add support for partitioned cookies to CookieStore. #206

Merged
merged 9 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions explainer.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
+ [Read a cookie](#read-a-cookie)
+ [Read multiple cookies](#read-multiple-cookies)
+ [Read the cookies for a specific URL](#read-the-cookies-for-a-specific-url)
+ [Read a partitioned cookie](#read-a-partitioned-cookie)
* [The Modifications API](#the-modifications-api)
+ [Write a cookie](#write-a-cookie)
+ [Write a partitioned cookie](#write-a-partitioned-cookie)
+ [Delete a cookie](#delete-a-cookie)
+ [Delete a partitioned cookie](#delete-a-partitioned-cookie)
+ [Access all the cookie data](#access-all-the-cookie-data)
* [The Change Events API](#the-change-events-api)
+ [Get change events in documents](#get-change-events-in-documents)
Expand Down Expand Up @@ -263,6 +266,22 @@ any URL under their scope.
Documents can only obtain the cookies at their current URL. In other words,
the only valid `url` value in Document contexts is the document's URL.

### Read a partitioned cookie

If the user agent supports
DCtheTall marked this conversation as resolved.
Show resolved Hide resolved
[cookie partitioning](https://github.com/DCtheTall/CHIPS), then the cookie
DCtheTall marked this conversation as resolved.
Show resolved Hide resolved
objects will have a boolean value indicating if the cookie is partitioned.

```javascript
// Read a cookie set without the Partitioned attribute.
const cookie = await cookieStore.get('session_id');
console.log(cookie.partitioned); // -> false

// Read a Partitioned cookie from a third-party context.
DCtheTall marked this conversation as resolved.
Show resolved Hide resolved
const cookie = await cookieStore.get('__Host-third_party_session_id');
console.log(cookie.partitioned); // -> true
```

## The Modifications API

Both documents and service workers access the same modification API, via the
Expand Down Expand Up @@ -292,6 +311,21 @@ await cookieStore.set({
});
```

### Write a partitioned cookie

If the user agent supports [cookie partitioning](https://github.com/WICG/CHIPS)
DCtheTall marked this conversation as resolved.
Show resolved Hide resolved
then you can set a partitioned cookie in a third-party context using the following.

```javascript
await cookieStore.set({
name: '__Host-third_party_session_id',
value: 'foobar',
path: '/',
sameSite: 'none'
// `Secure` is implicitly set
});
```

### Delete a cookie

```javascript
Expand All @@ -316,6 +350,27 @@ try {
}
```

### Delete a partitioned cookie

If the user agent supports [cookie partitioning](https://github.com/WICG/CHIPS)
DCtheTall marked this conversation as resolved.
Show resolved Hide resolved
then it is possible for a site to set both a partitioned and unpartitioned
cookie with the same name.

In this edge case, if a site would like to distinguish between whether they
DCtheTall marked this conversation as resolved.
Show resolved Hide resolved
want to delete their partitioned and unpartitioned cookie, they can provide
a `partitioned` attribute. If the site wants to delete the partitioned cookie,
the site could use:

```javascript
await cookieStore.delete({
name: '__Host-third_party_session_id',
partitioned: true
});
```

If the site wants to delete the unpartitioned cookie, change the `partitioned`
field to `false`.

### Access all the cookie data

The objects returned by `get` and `getAll` contain all the information in the
Expand Down
15 changes: 14 additions & 1 deletion index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -500,12 +500,14 @@ dictionary CookieInit {
USVString? domain = null;
USVString path = "/";
CookieSameSite sameSite = "strict";
boolean partitioned = false;
};

dictionary CookieStoreDeleteOptions {
required USVString name;
USVString? domain = null;
USVString path = "/";
boolean partitioned;
};

dictionary CookieListItem {
Expand All @@ -516,6 +518,7 @@ dictionary CookieListItem {
DOMTimeStamp? expires;
boolean secure;
CookieSameSite sameSite;
boolean partitioned;
};

typedef sequence<CookieListItem> CookieList;
Expand Down Expand Up @@ -689,6 +692,9 @@ The <dfn method for=CookieStore>set(|options|)</dfn> method steps are:
|options|["{{CookieInit/domain}}"],
|options|["{{CookieInit/path}}"], and
|options|["{{CookieInit/sameSite}}"].
1. If the |options|["{{CookieInit/partitioned}}"] attribute is present and the user agent supports [cookie partitioning](https://github.com/WICG/CHIPS), then
1. If the current browsing context is a service worker, set the cookie's partition key be |origin|'s [=site=].
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should linkify "current browsing context" and "service worker"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also.. this is run after the cookie is set, which seems unusual. ISTM the partition key should be captured before "set a cookie" and passed into it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And should we toss a definition of "partition key" into the cookie-concept section?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the partition key can be up to RFC6265bis to define, since the CookieStore API is not actually concerned with partition key values and the cookie RFC will (some day) specify how to select which partitioned cookies to include in a request. So I do not think this spec needs to be concerned with that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes... but https://wicg.github.io/cookie-store/#cookie-concept basically has "proxy" definitions for RFC6265bis terms, since I don't think we can link directly to them. We can add it later though.

1. If the current browsing context is an HTML document, then the cookie's partition key is the [=site=] of the topmost frame.
DCtheTall marked this conversation as resolved.
Show resolved Hide resolved
1. If |r| is failure, then [=reject=] |p| with a {{TypeError}} and abort these steps.
1. [=/Resolve=] |p| with undefined.
1. Return |p|.
Expand Down Expand Up @@ -743,6 +749,11 @@ The <dfn method for=CookieStore>delete(|options|)</dfn> method steps are:
|options|["{{CookieStoreDeleteOptions/name}}"],
|options|["{{CookieStoreDeleteOptions/domain}}"], and
|options|["{{CookieStoreDeleteOptions/path}}"].
1. If the |options|["{{CookieStoreDeleteOptions/partitioned}}"] field is present and the user agent supports [cookie partitioning](https://github.com/WICG/CHIPS), then
DCtheTall marked this conversation as resolved.
Show resolved Hide resolved
1. If the value is |false|, then |r| should only match cookies with no partition key.
1. If the value is |true|, then
1. If the browsing context is a service worker, then |r| should only match cookies whose partition key is the [=site=] of |origin|.
1. If the browsing context is an HTML document, then |r| should only match cookies whose partition key is the [=site=] of the topmost frame.
1. If |r| is failure, then [=reject=] |p| with a {{TypeError}} and abort these steps.
1. [=/Resolve=] |p| with undefined.
1. Return |p|.
Expand Down Expand Up @@ -1067,14 +1078,16 @@ To <dfn>create a {{CookieListItem}}</dfn> from |cookie|, run the following steps
: \``Lax`\`
:: Let |sameSite| be "{{CookieSameSite/lax}}".
</dl>
1. Let |partitioned| be a boolean indicating that the user agent supports [cookie partitioning](https://github.com/WICG/CHIPS) and that that |cookie| has a partition key.
1. Return «[
"name" → |name|,
"value" → |value|,
"domain" → |domain|,
"path" → |path|,
"expires" → |expires|,
"secure" → |secure|,
"sameSite" → |sameSite|
"sameSite" → |sameSite|,
"partitioned" → |partitioned|

Note: The |cookie|'s
Expand Down