Skip to content
Open
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
4 changes: 3 additions & 1 deletion packages/browser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 3 additions & 1 deletion packages/express/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
15 changes: 11 additions & 4 deletions packages/express/src/ThunderIDExpressClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,20 @@ class ThunderIDExpressClient<T extends ExpressClientConfig = ExpressClientConfig
const configuredName: string | undefined = this._expressConfig?.sessionCookie?.name;
const name: string = NodeCookieConfig.resolveSessionCookieName(this._expressConfig?.vendor, configuredName);

// A name the app chose is kept as it is; the default gets `__Host-` when the cookie is Secure.
return configuredName ? name : NodeCookieConfig.withHostPrefix(name, this.getSessionCookieOptions().secure);
if (configuredName) {
// A name the app chose is kept as it is.
return name;
}

// The default gets `__Host-Http-` when the cookie is Secure, or only `__Host-` when the app turned off HttpOnly.
const {httpOnly, secure} = this.getSessionCookieOptions();

return NodeCookieConfig.withHostPrefix(name, secure, httpOnly !== false);
}

/**
* The attributes the session cookie is set with. `Secure` defaults to on in production, and the
* cookie is always on `Path=/` with no `Domain`, so it can carry the `__Host-` prefix.
* cookie is always on `Path=/` with no `Domain`, so it can carry the `__Host-Http-` prefix.
*/
public getSessionCookieOptions(): express.CookieOptions & {maxAge: number; secure: boolean} {
const sc = this._expressConfig?.sessionCookie;
Expand Down Expand Up @@ -108,7 +115,7 @@ class ThunderIDExpressClient<T extends ExpressClientConfig = ExpressClientConfig
}

/**
* The name of the cookie that carries a sign-in in progress: `thunderid-session-state`, with `__Host-` when
* The name of the cookie that carries a sign-in in progress: `thunderid-session-state`, with `__Host-Http-` when
* `Secure`. It holds the session ID the authorization request's `state` and PKCE verifier are stored under.
*/
public getSessionStateCookieName(): string {
Expand Down
18 changes: 12 additions & 6 deletions packages/express/src/__tests__/sessionCookie.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ describe('the Express session cookie', () => {
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: '/',
Expand All @@ -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', () => {
Expand All @@ -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',
Expand All @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions packages/javascript/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 3 additions & 1 deletion packages/nextjs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs/src/utils/SessionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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});
});

Expand Down
4 changes: 3 additions & 1 deletion packages/node/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
10 changes: 8 additions & 2 deletions packages/node/src/__tests__/SessionCookie.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

Expand Down
29 changes: 21 additions & 8 deletions packages/node/src/constants/CookieConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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-';
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

/** 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;

Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/models/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
4 changes: 3 additions & 1 deletion packages/nuxt/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/src/runtime/server/utils/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
7 changes: 5 additions & 2 deletions packages/react-router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 3 additions & 1 deletion packages/react/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 5 additions & 2 deletions packages/tanstack-router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 3 additions & 1 deletion packages/vue/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Loading