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 sameSite option to sessions #5774

Merged
merged 4 commits into from
May 23, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .changeset/shiny-drinks-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@keystone-next/website': minor
'@keystone-next/keystone': minor
---

Added `sameSite` option to session options for cookies
5 changes: 4 additions & 1 deletion docs/pages/apis/session.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default config({
secure: true,
path: '/',
domain: 'localhost',
sameSite: 'lax',
}),
{ User: 'name isAdmin' }
),
Expand Down Expand Up @@ -63,8 +64,10 @@ Options
For Firefox, the `https:` requirements are ignored when the `secure` attribute is set by localhost (since Firefox 75).
- `path` (default: `'/'`): A path that must exist in the requested URL, or the browser won't send the cookie header.
The forward slash (`/`) character is interpreted as a directory separator, and subdirectories will be matched as well: for `path: '/docs'`, `/docs`, `/docs/Web/`, and `/docs/Web/HTTP` will all match.
- `domain` (default: current document URL): Host to which the cookie will be sent. See [here](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#attributes) for more details on the `domain` cookie attribute..
- `domain` (default: current document URL): Host to which the cookie will be sent. See [here](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#attributes) for more details on the `domain` cookie attribute.
**Note**: Only one domain is allowed. If a domain is specified then subdomains are always included.
- `sameSite` (default: `'lax'`): Controls whether the cookie is sent with cross-origin requests. Can be one of `true`, `false`, `'strict'`, `'lax'` or `'none'`. See [here](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#attributes) for more details on the `sameSite` cookie attribute.
**Note**: The `secure` attribute must also be set when `sameSite` is set to `none`!

### Session stores

Expand Down
11 changes: 9 additions & 2 deletions packages-next/keystone/src/session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ type StatelessSessionsOptions = {
* @default current domain
*/
domain?: string;
/**
* Specifies the boolean or string to be the value for the {@link https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.7|`SameSite` `Set-Cookie` attribute}.
*
* @default 'lax'
*/
sameSite?: true | false | 'lax' | 'strict' | 'none';
};

type FieldSelections = {
Expand Down Expand Up @@ -122,6 +128,7 @@ export function statelessSessions<T>({
secure = process.env.NODE_ENV === 'production',
ironOptions = Iron.defaults,
domain,
sameSite = 'lax',
}: StatelessSessionsOptions): () => SessionStrategy<T> {
return () => {
if (!secret) {
Expand All @@ -148,7 +155,7 @@ export function statelessSessions<T>({
httpOnly: true,
secure,
path,
sameSite: 'lax',
sameSite,
domain,
})
);
Expand All @@ -164,7 +171,7 @@ export function statelessSessions<T>({
httpOnly: true,
secure,
path,
sameSite: 'lax',
sameSite,
domain,
})
);
Expand Down