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

Cookies with samesite=none must have the secure attr set #570

Merged
merged 2 commits into from
Jan 5, 2022
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
15 changes: 3 additions & 12 deletions src/auth0-session/cookie-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,7 @@ export default class CookieStore {
debug('clearing all matching session cookies');
for (const cookieName of Object.keys(cookies)) {
if (cookieName.match(`^${sessionName}(?:\\.\\d)?$`)) {
clearCookie(res, cookieName, {
domain: cookieConfig.domain,
path: cookieConfig.path
});
clearCookie(res, cookieName, cookieConfig);
}
}
return;
Expand Down Expand Up @@ -198,19 +195,13 @@ export default class CookieStore {
setCookie(res, chunkCookieName, chunkValue, cookieOptions);
}
if (sessionName in cookies) {
clearCookie(res, sessionName, {
domain: cookieConfig.domain,
path: cookieConfig.path
});
clearCookie(res, sessionName, cookieConfig);
}
} else {
setCookie(res, sessionName, value, cookieOptions);
for (const cookieName of Object.keys(cookies)) {
if (cookieName.match(`^${sessionName}\\.\\d$`)) {
clearCookie(res, cookieName, {
domain: cookieConfig.domain,
path: cookieConfig.path
});
clearCookie(res, cookieName, cookieConfig);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/auth0-session/transient-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,18 @@ export default class TransientStore {
*/
read(key: string, req: IncomingMessage, res: ServerResponse): string | undefined {
const cookie = getCookie(req, key);
const { domain, path } = this.config.session.cookie;
const cookieConfig = this.config.session.cookie;

let value = getCookieValue(key, cookie, this.keyStore);
clearCookie(res, key, { domain, path });
clearCookie(res, key, cookieConfig);

if (this.config.legacySameSiteCookie) {
const fallbackKey = `_${key}`;
if (!value) {
const fallbackCookie = getCookie(req, fallbackKey);
value = getCookieValue(fallbackKey, fallbackCookie, this.keyStore);
}
clearCookie(res, fallbackKey, { domain, path });
clearCookie(res, fallbackKey, cookieConfig);
}

return value;
Expand Down
15 changes: 14 additions & 1 deletion src/auth0-session/utils/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,18 @@ export const set = (res: ServerResponse, name: string, value: string, options: C
};

export const clear = (res: ServerResponse, name: string, options: CookieSerializeOptions = {}): void => {
set(res, name, '', { ...options, maxAge: 0 });
const { domain, path, secure, sameSite } = options;
Copy link
Contributor

Choose a reason for hiding this comment

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

sameSite is not being passed to set, because it's never added to clearOptions.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ooops didn't see you had already closed the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's intentional - you only need to pass path and domain (and sometime secure) when clearing the cookie, I'm just closing this because I can't reproduce #569 on https://localhost and I'm doing a bit more investigation

const clearOptions: CookieSerializeOptions = {
domain,
path,
maxAge: 0
};
// If SameSite=None is set, the cookie Secure attribute must also be set (or the cookie will be blocked)
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite#none
if (sameSite === 'none') {
clearOptions.secure = secure;
clearOptions.sameSite = sameSite;
}

set(res, name, '', clearOptions);
};
23 changes: 23 additions & 0 deletions tests/auth0-session/handlers/logout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,27 @@ describe('logout route', () => {
expect(res.statusCode).toEqual(302);
expect(res.headers.location).toEqual(returnTo);
});

it('should clear session cookie when SameSite=None', async () => {
const baseURL = await setup(
{ ...defaultConfig, idpLogout: false, session: { cookie: { sameSite: 'none' } } },
{ https: true }
);
const cookieJar = await login(baseURL);
cookieJar.setCookieSync('foo=bar', baseURL);

await get(baseURL, '/session', { cookieJar });
expect(fromCookieJar(cookieJar, baseURL)).toMatchObject({
appSession: expect.any(String),
foo: 'bar'
});

const { res } = await get(baseURL, '/logout', { cookieJar, fullResponse: true });
const cookies = fromCookieJar(cookieJar, baseURL);
const sessionCookie = res.headers['set-cookie'].find((s: string) => /^appSession/.test(s));
expect(sessionCookie).toMatch(/Secure/);
expect(sessionCookie).toMatch(/SameSite=None/);
expect(cookies).toHaveProperty('foo');
expect(cookies).not.toHaveProperty('appSession');
});
});