From 5027f622c4d50fddc7db463ec106379621c67bfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Thu, 26 Nov 2026 17:46:18 +0100 Subject: [PATCH 1/2] fix(mercure): subscribe with the 1.0 protocol --- src/mercure/createSubscription.test.ts | 38 ++++++++++++++++++++++++++ src/mercure/createSubscription.ts | 11 ++++++-- src/types.ts | 2 ++ 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 src/mercure/createSubscription.test.ts diff --git a/src/mercure/createSubscription.test.ts b/src/mercure/createSubscription.test.ts new file mode 100644 index 00000000..5f8e25a5 --- /dev/null +++ b/src/mercure/createSubscription.test.ts @@ -0,0 +1,38 @@ +import createSubscription from './createSubscription.js'; +import type { MercureOptions } from '../types.js'; + +let urls: string[] = []; + +function FakeEventSource(url: string) { + urls.push(url); + + return { addEventListener: () => {} }; +} + +const options = (protocolVersion?: MercureOptions['protocolVersion']) => ({ + hub: 'https://example.com/.well-known/mercure', + jwt: null, + topicUrl: new URL('https://example.com/api'), + ...(protocolVersion ? { protocolVersion } : {}), +}); + +beforeEach(() => { + urls = []; + global.EventSource = FakeEventSource as unknown as typeof EventSource; +}); + +test('Subscribe with the Mercure 1.0 match parameter by default', () => { + createSubscription(options(), '/api/books/1', () => {}); + + expect(urls).toEqual([ + 'https://example.com/.well-known/mercure?match=https%3A%2F%2Fexample.com%2Fapi%2Fbooks%2F1', + ]); +}); + +test('Subscribe with the topic parameter of the Mercure 0.x protocol', () => { + createSubscription(options('0.x'), '/api/books/1', () => {}); + + expect(urls).toEqual([ + 'https://example.com/.well-known/mercure?topic=https%3A%2F%2Fexample.com%2Fapi%2Fbooks%2F1', + ]); +}); diff --git a/src/mercure/createSubscription.ts b/src/mercure/createSubscription.ts index 2f8336a8..e99e2486 100644 --- a/src/mercure/createSubscription.ts +++ b/src/mercure/createSubscription.ts @@ -21,11 +21,18 @@ const createSubscription = ( }; } + const legacy = mercure.protocolVersion === '0.x'; const url = new URL(mercure.hub, window.origin); - url.searchParams.append('topic', new URL(topic, mercure.topicUrl).toString()); + url.searchParams.append( + legacy ? 'topic' : 'match', + new URL(topic, mercure.topicUrl).toString(), + ); if (mercure.jwt !== null) { - document.cookie = `mercureAuthorization=${mercure.jwt}; Path=${mercure.hub}; Secure; SameSite=None`; + const cookieName = legacy + ? 'mercureAuthorization' + : '__Secure-mercure_access_token'; + document.cookie = `${cookieName}=${mercure.jwt}; Path=${mercure.hub}; Secure; SameSite=None`; } const eventSource = new EventSource(url.toString(), { diff --git a/src/types.ts b/src/types.ts index 2a67f5e3..27b76c52 100644 --- a/src/types.ts +++ b/src/types.ts @@ -153,6 +153,8 @@ export interface MercureOptions { hub: string | null; jwt: string | null; topicUrl: URL; + /** Protocol spoken by the hub: '1.0' (default) or '0.x' for hubs older than Mercure 1.0. */ + protocolVersion?: '1.0' | '0.x'; } export interface MercureSubscription { From 45f768569c5e5995f4e4950fe768e3245592bdf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Thu, 26 Nov 2026 18:45:05 +0100 Subject: [PATCH 2/2] fix(mercure): drop the client-side auth cookie A cookie written from JavaScript can't be HttpOnly, can't reach a hub on another host, and its Path got the absolute hub URL, which browsers reject. Let the server set the cookie and expose withCredentials to send it. --- src/dataProvider/adminDataProvider.ts | 1 - src/hydra/dataProvider.ts | 1 - src/mercure/createSubscription.test.ts | 17 +++++++++++++++-- src/mercure/createSubscription.ts | 9 +-------- src/openapi/dataProvider.ts | 1 - src/types.ts | 3 ++- 6 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/dataProvider/adminDataProvider.ts b/src/dataProvider/adminDataProvider.ts index 868550e0..e2e5d14c 100644 --- a/src/dataProvider/adminDataProvider.ts +++ b/src/dataProvider/adminDataProvider.ts @@ -16,7 +16,6 @@ export default ( const mercure: MercureOptions | null = factoryParams.mercure ? { hub: null, - jwt: null, topicUrl: entrypointUrl, ...(factoryParams.mercure === true ? {} : factoryParams.mercure), } diff --git a/src/hydra/dataProvider.ts b/src/hydra/dataProvider.ts index 385cc405..a2910fd9 100644 --- a/src/hydra/dataProvider.ts +++ b/src/hydra/dataProvider.ts @@ -201,7 +201,6 @@ function dataProvider( const mercure: MercureOptions | null = factoryParams.mercure ? { hub: null, - jwt: null, topicUrl: entrypointUrl, ...(factoryParams.mercure === true ? {} : factoryParams.mercure), } diff --git a/src/mercure/createSubscription.test.ts b/src/mercure/createSubscription.test.ts index 5f8e25a5..8bdab9a4 100644 --- a/src/mercure/createSubscription.test.ts +++ b/src/mercure/createSubscription.test.ts @@ -2,22 +2,24 @@ import createSubscription from './createSubscription.js'; import type { MercureOptions } from '../types.js'; let urls: string[] = []; +let inits: (EventSourceInit | undefined)[] = []; -function FakeEventSource(url: string) { +function FakeEventSource(url: string, init?: EventSourceInit) { urls.push(url); + inits.push(init); return { addEventListener: () => {} }; } const options = (protocolVersion?: MercureOptions['protocolVersion']) => ({ hub: 'https://example.com/.well-known/mercure', - jwt: null, topicUrl: new URL('https://example.com/api'), ...(protocolVersion ? { protocolVersion } : {}), }); beforeEach(() => { urls = []; + inits = []; global.EventSource = FakeEventSource as unknown as typeof EventSource; }); @@ -36,3 +38,14 @@ test('Subscribe with the topic parameter of the Mercure 0.x protocol', () => { 'https://example.com/.well-known/mercure?topic=https%3A%2F%2Fexample.com%2Fapi%2Fbooks%2F1', ]); }); + +test('Leave the authorization cookie to the server', () => { + createSubscription( + { ...options(), withCredentials: true }, + '/api/books/1', + () => {}, + ); + + expect(inits).toEqual([{ withCredentials: true }]); + expect(document.cookie).toBe(''); +}); diff --git a/src/mercure/createSubscription.ts b/src/mercure/createSubscription.ts index e99e2486..68d398e1 100644 --- a/src/mercure/createSubscription.ts +++ b/src/mercure/createSubscription.ts @@ -28,15 +28,8 @@ const createSubscription = ( new URL(topic, mercure.topicUrl).toString(), ); - if (mercure.jwt !== null) { - const cookieName = legacy - ? 'mercureAuthorization' - : '__Secure-mercure_access_token'; - document.cookie = `${cookieName}=${mercure.jwt}; Path=${mercure.hub}; Secure; SameSite=None`; - } - const eventSource = new EventSource(url.toString(), { - withCredentials: mercure.jwt !== null, + withCredentials: mercure.withCredentials ?? false, }); const eventListener = (event: MessageEvent) => { const document = transformData(JSON.parse(event.data)); diff --git a/src/openapi/dataProvider.ts b/src/openapi/dataProvider.ts index e409a88f..a028420d 100644 --- a/src/openapi/dataProvider.ts +++ b/src/openapi/dataProvider.ts @@ -57,7 +57,6 @@ function dataProvider( const mercure: MercureOptions | null = factoryParams.mercure ? { hub: null, - jwt: null, topicUrl: entrypointUrl, ...(factoryParams.mercure === true ? {} : factoryParams.mercure), } diff --git a/src/types.ts b/src/types.ts index 27b76c52..f7eb3ee3 100644 --- a/src/types.ts +++ b/src/types.ts @@ -151,7 +151,8 @@ export interface SchemaAnalyzer { export interface MercureOptions { hub: string | null; - jwt: string | null; + /** Sends the hub's authorization cookie, which the server must set as HttpOnly. */ + withCredentials?: boolean; topicUrl: URL; /** Protocol spoken by the hub: '1.0' (default) or '0.x' for hubs older than Mercure 1.0. */ protocolVersion?: '1.0' | '0.x';