diff --git a/packages/browser/CHANGELOG.md b/packages/browser/CHANGELOG.md index 95c0c646..acff0b6c 100644 --- a/packages/browser/CHANGELOG.md +++ b/packages/browser/CHANGELOG.md @@ -1,7 +1,9 @@ ## 1.27.0 ### ✨ Improvements -* Align the Node.js SDKs with the BFF contract of RFC 10017 by @brionmario in https://github.com/thunder-id/javascript-sdks/pull/192 + +- Align the Node.js SDKs with the BFF contract of RFC 10017 by @brionmario in + https://github.com/thunder-id/javascript-sdks/pull/192 **Full Changelog**: https://github.com/thunder-id/javascript-sdks/compare/sdk/browser/v1.26.1...sdk/browser/v1.27.0 diff --git a/packages/express/CHANGELOG.md b/packages/express/CHANGELOG.md index ab7bab69..e86702a0 100644 --- a/packages/express/CHANGELOG.md +++ b/packages/express/CHANGELOG.md @@ -1,7 +1,9 @@ ## 1.22.0 ### ✨ Improvements -* Align the Node.js SDKs with the BFF contract of RFC 10017 by @brionmario in https://github.com/thunder-id/javascript-sdks/pull/192 + +- Align the Node.js SDKs with the BFF contract of RFC 10017 by @brionmario in + https://github.com/thunder-id/javascript-sdks/pull/192 **Full Changelog**: https://github.com/thunder-id/javascript-sdks/compare/sdk/express/v1.21.1...sdk/express/v1.22.0 diff --git a/packages/express/src/ThunderIDExpressClient.ts b/packages/express/src/ThunderIDExpressClient.ts index 950648da..4860ea3a 100644 --- a/packages/express/src/ThunderIDExpressClient.ts +++ b/packages/express/src/ThunderIDExpressClient.ts @@ -54,13 +54,20 @@ class ThunderIDExpressClient { vi.unstubAllEnvs(); }); - it('is Secure with the __Host- prefix in production, on Path=/', () => { + it('is Secure with the __Host-Http- prefix in production, on Path=/', () => { vi.stubEnv('NODE_ENV', 'production'); const client: ThunderIDExpressClient = clientWith({}); - expect(client.getSessionCookieName()).toBe('__Host-thunderid-session'); + expect(client.getSessionCookieName()).toBe('__Host-Http-thunderid-session'); expect(client.getSessionCookieOptions()).toMatchObject({ httpOnly: true, path: '/', @@ -36,7 +36,13 @@ describe('the Express session cookie', () => { vi.stubEnv('NODE_ENV', 'development'); expect(clientWith({}).getSessionCookieName()).toBe('thunderid-session'); - expect(clientWith({sessionCookie: {secure: true}}).getSessionCookieName()).toBe('__Host-thunderid-session'); + expect(clientWith({sessionCookie: {secure: true}}).getSessionCookieName()).toBe('__Host-Http-thunderid-session'); + }); + + it('drops Http- from the prefix when the app turns off HttpOnly, so browsers still accept the cookie', () => { + vi.stubEnv('NODE_ENV', 'production'); + + expect(clientWith({sessionCookie: {httpOnly: false}}).getSessionCookieName()).toBe('__Host-thunderid-session'); }); it('keeps a name the app chose as it is', () => { @@ -51,7 +57,7 @@ describe('the Express session cookie', () => { clientWith({}).clearSessionCookie({clearCookie} as unknown as express.Response); - expect(clearCookie).toHaveBeenCalledWith('__Host-thunderid-session', { + expect(clearCookie).toHaveBeenCalledWith('__Host-Http-thunderid-session', { httpOnly: true, path: '/', sameSite: 'strict', @@ -72,11 +78,11 @@ describe('the Express sign-in cookie', () => { vi.unstubAllEnvs(); }); - it('is Lax, so it comes back on the redirect to the callback, and Secure with __Host- in production', () => { + it('is Lax, so it comes back on the redirect to the callback, and Secure with __Host-Http- in production', () => { vi.stubEnv('NODE_ENV', 'production'); const client: ThunderIDExpressClient = clientWith({}); - expect(client.getSessionStateCookieName()).toBe('__Host-thunderid-session-state'); + expect(client.getSessionStateCookieName()).toBe('__Host-Http-thunderid-session-state'); expect(client.getSessionStateCookieOptions()).toEqual({ httpOnly: true, maxAge: 900_000, diff --git a/packages/javascript/CHANGELOG.md b/packages/javascript/CHANGELOG.md index ab84b808..ee92657c 100644 --- a/packages/javascript/CHANGELOG.md +++ b/packages/javascript/CHANGELOG.md @@ -1,9 +1,12 @@ ## 1.28.0 ### ✨ Improvements -* Align the Node.js SDKs with the BFF contract of RFC 10017 by @brionmario in https://github.com/thunder-id/javascript-sdks/pull/192 -**Full Changelog**: https://github.com/thunder-id/javascript-sdks/compare/sdk/javascript/v1.27.1...sdk/javascript/v1.28.0 +- Align the Node.js SDKs with the BFF contract of RFC 10017 by @brionmario in + https://github.com/thunder-id/javascript-sdks/pull/192 + +**Full Changelog**: +https://github.com/thunder-id/javascript-sdks/compare/sdk/javascript/v1.27.1...sdk/javascript/v1.28.0 ## 1.27.1 diff --git a/packages/nextjs/CHANGELOG.md b/packages/nextjs/CHANGELOG.md index fdb38903..ee3338ef 100644 --- a/packages/nextjs/CHANGELOG.md +++ b/packages/nextjs/CHANGELOG.md @@ -1,7 +1,9 @@ ## 1.25.0 ### ✨ Improvements -* Align the Node.js SDKs with the BFF contract of RFC 10017 by @brionmario in https://github.com/thunder-id/javascript-sdks/pull/192 + +- Align the Node.js SDKs with the BFF contract of RFC 10017 by @brionmario in + https://github.com/thunder-id/javascript-sdks/pull/192 **Full Changelog**: https://github.com/thunder-id/javascript-sdks/compare/sdk/nextjs/v1.24.1...sdk/nextjs/v1.25.0 diff --git a/packages/nextjs/src/utils/SessionManager.ts b/packages/nextjs/src/utils/SessionManager.ts index 8232b126..0cc04cb2 100644 --- a/packages/nextjs/src/utils/SessionManager.ts +++ b/packages/nextjs/src/utils/SessionManager.ts @@ -59,7 +59,7 @@ class SessionManager { } /** - * Whether the session cookies are `Secure`, and so carry the `__Host-` prefix. + * Whether the session cookies are `Secure`, and so carry the `__Host-Http-` prefix. */ private static isSecure(): boolean { return process.env['NODE_ENV'] === 'production'; diff --git a/packages/nextjs/src/utils/__tests__/SessionManager.encryption.test.ts b/packages/nextjs/src/utils/__tests__/SessionManager.encryption.test.ts index 2a6a3a8d..ff6afe16 100644 --- a/packages/nextjs/src/utils/__tests__/SessionManager.encryption.test.ts +++ b/packages/nextjs/src/utils/__tests__/SessionManager.encryption.test.ts @@ -88,11 +88,11 @@ describe('SessionManager session cookie', () => { await expect(SessionManager.verifySessionToken(token)).resolves.toMatchObject({sub: 'user-1'}); }); - it('names the cookies with __Host- and makes them Secure in production', () => { + it('names the cookies with __Host-Http- and makes them Secure in production', () => { vi.stubEnv('NODE_ENV', 'production'); - expect(SessionManager.getSessionCookieName()).toMatch(/^__Host-/); - expect(SessionManager.getTempSessionCookieName()).toMatch(/^__Host-/); + expect(SessionManager.getSessionCookieName()).toMatch(/^__Host-Http-thunderid-/); + expect(SessionManager.getTempSessionCookieName()).toMatch(/^__Host-Http-thunderid-/); expect(SessionManager.getSessionCookieOptions(60)).toMatchObject({path: '/', secure: true}); }); diff --git a/packages/node/CHANGELOG.md b/packages/node/CHANGELOG.md index 31bbe9ee..0b817d15 100644 --- a/packages/node/CHANGELOG.md +++ b/packages/node/CHANGELOG.md @@ -1,7 +1,9 @@ ## 1.23.0 ### ✨ Improvements -* Align the Node.js SDKs with the BFF contract of RFC 10017 by @brionmario in https://github.com/thunder-id/javascript-sdks/pull/192 + +- Align the Node.js SDKs with the BFF contract of RFC 10017 by @brionmario in + https://github.com/thunder-id/javascript-sdks/pull/192 **Full Changelog**: https://github.com/thunder-id/javascript-sdks/compare/sdk/node/v1.22.1...sdk/node/v1.23.0 diff --git a/packages/node/src/__tests__/SessionCookie.test.ts b/packages/node/src/__tests__/SessionCookie.test.ts index 3ba90415..97486817 100644 --- a/packages/node/src/__tests__/SessionCookie.test.ts +++ b/packages/node/src/__tests__/SessionCookie.test.ts @@ -138,10 +138,16 @@ describe('SessionCookie', () => { }); describe('CookieConfig.withHostPrefix', () => { - it('adds __Host- only to a Secure cookie, and only once', () => { - expect(CookieConfig.withHostPrefix('thunderid-session', true)).toBe('__Host-thunderid-session'); + it('adds __Host-Http- only to a Secure cookie, and only once', () => { + expect(CookieConfig.withHostPrefix('thunderid-session', true)).toBe('__Host-Http-thunderid-session'); expect(CookieConfig.withHostPrefix('thunderid-session', false)).toBe('thunderid-session'); expect(CookieConfig.withHostPrefix('__Host-session', true)).toBe('__Host-session'); + expect(CookieConfig.withHostPrefix('__Host-Http-session', true)).toBe('__Host-Http-session'); + }); + + it('uses only __Host- for a cookie that is not HttpOnly, which __Host-Http- does not allow', () => { + expect(CookieConfig.withHostPrefix('thunderid-session', true, false)).toBe('__Host-thunderid-session'); + expect(CookieConfig.withHostPrefix('thunderid-session', false, false)).toBe('thunderid-session'); }); }); diff --git a/packages/node/src/constants/CookieConfig.ts b/packages/node/src/constants/CookieConfig.ts index c4ae972c..df56034e 100644 --- a/packages/node/src/constants/CookieConfig.ts +++ b/packages/node/src/constants/CookieConfig.ts @@ -6,7 +6,7 @@ import {getVendorPrefix} from '@thunderid/javascript'; class CookieConfig { /** * Builds the session cookie name for a given vendor namespace: `thunderid-session` by default. A `Secure` - * cookie also takes the `__Host-` prefix through `withHostPrefix`, giving `__Host-thunderid-session`. + * cookie also takes the `__Host-Http-` prefix through `withHostPrefix`, giving `__Host-Http-thunderid-session`. * @param vendor - Vendor/brand namespace. Defaults to `VendorConstants.VENDOR_PREFIX` ('thunderid'). */ static getSessionCookieName(vendor?: string): string { @@ -40,19 +40,32 @@ class CookieConfig { } /** - * Adds the `__Host-` prefix to a cookie name when the cookie is `Secure`. A browser then refuses the cookie - * unless it is `Secure`, has `Path=/` and has no `Domain`, so a subdomain cannot set or overwrite it. - * Without `Secure`, as on plain-HTTP development servers, the prefix would make the browser drop the cookie, - * so the name is left as it is. + * Adds the `__Host-Http-` prefix to a cookie name when the cookie is `Secure`, as RFC 10017 §6.1.3.2 recommends. + * The `__Host-` part makes a browser refuse the cookie unless it is `Secure`, has `Path=/` and has no `Domain`, so + * a subdomain cannot set or overwrite it. The `Http-` part makes a browser that supports it refuse the cookie + * unless it is `HttpOnly` and comes from a `Set-Cookie` header, so page script cannot set it either. A browser + * that does not know `Http-` still applies the `__Host-` rules. A cookie that is not `HttpOnly` gets only + * `__Host-`, since a browser would refuse it under `__Host-Http-`. Without `Secure`, as on plain-HTTP development + * servers, any prefix would make the browser drop the cookie, so the name is left as it is. * * @param name - The cookie name. * @param secure - Whether the cookie is set with `Secure`. + * @param httpOnly - Whether the cookie is set with `HttpOnly`. Defaults to `true`. */ - static withHostPrefix(name: string, secure: boolean): string { - return secure && !name.startsWith(CookieConfig.HOST_PREFIX) ? `${CookieConfig.HOST_PREFIX}${name}` : name; + static withHostPrefix(name: string, secure: boolean, httpOnly = true): string { + // A name that already carries a `__Host-` prefix, with or without `Http-`, is left as it is. + if (!secure || name.startsWith(CookieConfig.HOST_ONLY_PREFIX)) { + return name; + } + + return `${httpOnly ? CookieConfig.HOST_PREFIX : CookieConfig.HOST_ONLY_PREFIX}${name}`; } - static readonly HOST_PREFIX: string = '__Host-'; + /** The prefix of a `Secure`, `HttpOnly` cookie, as RFC 10017 recommends. */ + static readonly HOST_PREFIX: string = '__Host-Http-'; + + /** The prefix of a `Secure` cookie that is not `HttpOnly`, which `__Host-Http-` would not allow. */ + static readonly HOST_ONLY_PREFIX: string = '__Host-'; static readonly DEFAULT_MAX_AGE: number = 3600; diff --git a/packages/node/src/models/config.ts b/packages/node/src/models/config.ts index e90581eb..2ba07f4a 100644 --- a/packages/node/src/models/config.ts +++ b/packages/node/src/models/config.ts @@ -33,7 +33,7 @@ export interface SessionCookieConfig { * cross-site request forgery, and logs a warning. The cookie that carries a sign-in in progress is always `'lax'`. */ sameSite?: 'lax' | 'strict' | 'none'; - /** Whether the cookie requires HTTPS. Default: `true` when `NODE_ENV` is `production`, otherwise `false`. A Secure cookie also gets the `__Host-` prefix, unless `name` is set. */ + /** Whether the cookie requires HTTPS. Default: `true` when `NODE_ENV` is `production`, otherwise `false`. A Secure cookie also gets the `__Host-Http-` prefix, unless `name` is set. */ secure?: boolean; /** * Full override of the cookie name. If not set, derived from `vendor`. diff --git a/packages/nuxt/CHANGELOG.md b/packages/nuxt/CHANGELOG.md index a5c7ae2d..d8e959b6 100644 --- a/packages/nuxt/CHANGELOG.md +++ b/packages/nuxt/CHANGELOG.md @@ -1,7 +1,9 @@ ## 1.25.0 ### ✨ Improvements -* Align the Node.js SDKs with the BFF contract of RFC 10017 by @brionmario in https://github.com/thunder-id/javascript-sdks/pull/192 + +- Align the Node.js SDKs with the BFF contract of RFC 10017 by @brionmario in + https://github.com/thunder-id/javascript-sdks/pull/192 **Full Changelog**: https://github.com/thunder-id/javascript-sdks/compare/sdk/nuxt/v1.24.1...sdk/nuxt/v1.25.0 diff --git a/packages/nuxt/src/runtime/server/utils/session.ts b/packages/nuxt/src/runtime/server/utils/session.ts index c3b1b2c2..90cc8eab 100644 --- a/packages/nuxt/src/runtime/server/utils/session.ts +++ b/packages/nuxt/src/runtime/server/utils/session.ts @@ -20,7 +20,7 @@ function getSecret(sessionSecret?: string): string { } /** - * Whether the session cookies are `Secure`, and so carry the `__Host-` prefix. + * Whether the session cookies are `Secure`, and so carry the `__Host-Http-` prefix. */ function isSecure(): boolean { return process.env.NODE_ENV === 'production'; diff --git a/packages/react-router/CHANGELOG.md b/packages/react-router/CHANGELOG.md index 6e45218c..31c6e148 100644 --- a/packages/react-router/CHANGELOG.md +++ b/packages/react-router/CHANGELOG.md @@ -1,9 +1,12 @@ ## 1.24.0 ### ✨ Improvements -* Align the Node.js SDKs with the BFF contract of RFC 10017 by @brionmario in https://github.com/thunder-id/javascript-sdks/pull/192 -**Full Changelog**: https://github.com/thunder-id/javascript-sdks/compare/sdk/react-router/v1.23.1...sdk/react-router/v1.24.0 +- Align the Node.js SDKs with the BFF contract of RFC 10017 by @brionmario in + https://github.com/thunder-id/javascript-sdks/pull/192 + +**Full Changelog**: +https://github.com/thunder-id/javascript-sdks/compare/sdk/react-router/v1.23.1...sdk/react-router/v1.24.0 ## 1.23.1 diff --git a/packages/react/CHANGELOG.md b/packages/react/CHANGELOG.md index edafafa9..cf397e2b 100644 --- a/packages/react/CHANGELOG.md +++ b/packages/react/CHANGELOG.md @@ -1,7 +1,9 @@ ## 1.29.0 ### ✨ Improvements -* Align the Node.js SDKs with the BFF contract of RFC 10017 by @brionmario in https://github.com/thunder-id/javascript-sdks/pull/192 + +- Align the Node.js SDKs with the BFF contract of RFC 10017 by @brionmario in + https://github.com/thunder-id/javascript-sdks/pull/192 **Full Changelog**: https://github.com/thunder-id/javascript-sdks/compare/sdk/react/v1.28.1...sdk/react/v1.29.0 diff --git a/packages/tanstack-router/CHANGELOG.md b/packages/tanstack-router/CHANGELOG.md index 566e7132..3f696cc3 100644 --- a/packages/tanstack-router/CHANGELOG.md +++ b/packages/tanstack-router/CHANGELOG.md @@ -1,9 +1,12 @@ ## 1.24.0 ### ✨ Improvements -* Align the Node.js SDKs with the BFF contract of RFC 10017 by @brionmario in https://github.com/thunder-id/javascript-sdks/pull/192 -**Full Changelog**: https://github.com/thunder-id/javascript-sdks/compare/sdk/tanstack-router/v1.23.1...sdk/tanstack-router/v1.24.0 +- Align the Node.js SDKs with the BFF contract of RFC 10017 by @brionmario in + https://github.com/thunder-id/javascript-sdks/pull/192 + +**Full Changelog**: +https://github.com/thunder-id/javascript-sdks/compare/sdk/tanstack-router/v1.23.1...sdk/tanstack-router/v1.24.0 ## 1.23.1 diff --git a/packages/vue/CHANGELOG.md b/packages/vue/CHANGELOG.md index 3da0533c..2f1885db 100644 --- a/packages/vue/CHANGELOG.md +++ b/packages/vue/CHANGELOG.md @@ -1,7 +1,9 @@ ## 1.26.0 ### ✨ Improvements -* Align the Node.js SDKs with the BFF contract of RFC 10017 by @brionmario in https://github.com/thunder-id/javascript-sdks/pull/192 + +- Align the Node.js SDKs with the BFF contract of RFC 10017 by @brionmario in + https://github.com/thunder-id/javascript-sdks/pull/192 **Full Changelog**: https://github.com/thunder-id/javascript-sdks/compare/sdk/vue/v1.25.1...sdk/vue/v1.26.0