From 3328fa2af6f4df2bab9ca012c09edc8cf7b852fe Mon Sep 17 00:00:00 2001 From: rishikesh Date: Fri, 27 Nov 2026 09:45:05 +0530 Subject: [PATCH 1/2] fix(client): resolve SSE endpoint URL and origin against redirected response URL --- packages/client/src/client/sse.ts | 13 +++++++-- packages/client/test/client/redirects.test.ts | 27 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/packages/client/src/client/sse.ts b/packages/client/src/client/sse.ts index 55c346ac85..2c3398d569 100644 --- a/packages/client/src/client/sse.ts +++ b/packages/client/src/client/sse.ts @@ -253,6 +253,7 @@ export class SSEClientTransport implements Transport { const fetchImpl = this._redirects( (eventSourceFetch ? (this._dpop?.(eventSourceFetch as FetchLike) ?? eventSourceFetch) : (this._fetch ?? fetch)) as FetchLike ); + let responseUrl: URL | undefined; let redirect: string | undefined; return new Promise((resolve, reject) => { this._eventSource = new EventSource(this._url.href, { @@ -264,6 +265,13 @@ export class SSEClientTransport implements Transport { ...init, headers }); + if (response.url) { + try { + responseUrl = new URL(response.url); + } catch { + // Ignore synthetic or malformed response URLs and fall back to this._url. + } + } redirect = this._unfollowedRedirect(url, response); if (response.status === 401) { @@ -334,8 +342,9 @@ export class SSEClientTransport implements Transport { const messageEvent = event as MessageEvent; try { - this._endpoint = new URL(messageEvent.data, this._url); - if (this._endpoint.origin !== this._url.origin) { + const baseUrl = responseUrl ?? this._url; + this._endpoint = new URL(messageEvent.data, baseUrl); + if (this._endpoint.origin !== baseUrl.origin) { throw new Error(`Endpoint origin does not match connection origin: ${this._endpoint.origin}`); } } catch (error) { diff --git a/packages/client/test/client/redirects.test.ts b/packages/client/test/client/redirects.test.ts index 02354e6e80..d5cfd2bdd6 100644 --- a/packages/client/test/client/redirects.test.ts +++ b/packages/client/test/client/redirects.test.ts @@ -512,6 +512,33 @@ describe('redirects', () => { expect(otherRequests[0]).toMatchObject({ method: 'GET', url: '/sse', headers: { 'x-api-key': 'key-1' } }); }); + test('SSE endpoint event resolves relative URL and origin against final redirected URL', async () => { + const redirectedRequests: Recorded[] = []; + const redirected = record( + redirectedRequests, + sseEndpoint((_req, res) => { + res.writeHead(202).end(); + }) + ); + const redirectedUrl = await listenOnRandomPort(redirected); + try { + handle = redirectTo(new URL('/sse', redirectedUrl)); + + const client = await sseStarted(follow); + await client.send(notification); + + expect(redirectedRequests).toMatchObject([ + { method: 'GET', url: '/sse' }, + { method: 'POST', url: '/messages', body: JSON.stringify(notification), headers: { 'x-api-key': 'key-1' } } + ]); + } finally { + await transport?.close(); + transport = undefined; + redirected.closeAllConnections(); + await new Promise(resolve => redirected.close(resolve)); + } + }); + test('SSE POST to another origin is followed with its headers and body', async () => { handle = sseEndpoint(toOther); const client = await sseStarted(follow); From 1d15d0f01387e5078436cdb7d29788ea9749e77c Mon Sep 17 00:00:00 2001 From: rishikesh Date: Sat, 28 Nov 2026 08:32:40 +0530 Subject: [PATCH 2/2] fix(client): keep endpoint origin check pinned to configured URL --- packages/client/src/client/sse.ts | 4 +- packages/client/test/client/redirects.test.ts | 47 ++++++++++++++++--- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/packages/client/src/client/sse.ts b/packages/client/src/client/sse.ts index 2c3398d569..ded66def60 100644 --- a/packages/client/src/client/sse.ts +++ b/packages/client/src/client/sse.ts @@ -269,7 +269,7 @@ export class SSEClientTransport implements Transport { try { responseUrl = new URL(response.url); } catch { - // Ignore synthetic or malformed response URLs and fall back to this._url. + // fall back to this._url } } redirect = this._unfollowedRedirect(url, response); @@ -344,7 +344,7 @@ export class SSEClientTransport implements Transport { try { const baseUrl = responseUrl ?? this._url; this._endpoint = new URL(messageEvent.data, baseUrl); - if (this._endpoint.origin !== baseUrl.origin) { + if (this._endpoint.origin !== this._url.origin) { throw new Error(`Endpoint origin does not match connection origin: ${this._endpoint.origin}`); } } catch (error) { diff --git a/packages/client/test/client/redirects.test.ts b/packages/client/test/client/redirects.test.ts index d5cfd2bdd6..6f50095c3f 100644 --- a/packages/client/test/client/redirects.test.ts +++ b/packages/client/test/client/redirects.test.ts @@ -512,7 +512,7 @@ describe('redirects', () => { expect(otherRequests[0]).toMatchObject({ method: 'GET', url: '/sse', headers: { 'x-api-key': 'key-1' } }); }); - test('SSE endpoint event resolves relative URL and origin against final redirected URL', async () => { + test('SSE endpoint event still rejects foreign-origin endpoints after a cross-origin redirect', async () => { const redirectedRequests: Recorded[] = []; const redirected = record( redirectedRequests, @@ -524,12 +524,11 @@ describe('redirects', () => { try { handle = redirectTo(new URL('/sse', redirectedUrl)); - const client = await sseStarted(follow); - await client.send(notification); - - expect(redirectedRequests).toMatchObject([ - { method: 'GET', url: '/sse' }, - { method: 'POST', url: '/messages', body: JSON.stringify(notification), headers: { 'x-api-key': 'key-1' } } + await expect(sseWith(follow).start()).rejects.toThrow( + `Endpoint origin does not match connection origin: ${redirectedUrl.origin}` + ); + expect(redirectedRequests).toEqual([ + { method: 'GET', url: '/sse', body: '', headers: expect.objectContaining({ 'x-api-key': 'key-1' }) } ]); } finally { await transport?.close(); @@ -572,6 +571,40 @@ describe('redirects', () => { }); describe("'same-origin' is the default", () => { + test.each<[string, string, string]>([ + ['/new/sse', 'messages?sessionId=test', '/new/messages?sessionId=test'], + ['/new/sse/', 'messages?sessionId=test', '/new/sse/messages?sessionId=test'], + ['/new/sse', '/new/messages?sessionId=test', '/new/messages?sessionId=test'] + ])( + 'SSE endpoint %s with data %s resolves POST to %s after same-origin redirect', + async (redirectedPath, endpointData, expectedPostUrl) => { + handle = (req, res, recorded) => { + if (req.method === 'GET' && req.url === '/old/sse') { + redirectTo(redirectedPath)(req, res, recorded); + return; + } + if (req.method === 'GET' && req.url === redirectedPath) { + res.writeHead(200, { 'content-type': 'text/event-stream' }); + res.write(`event: endpoint\ndata: ${endpointData}\n\n`); + return; + } + res.writeHead(202).end(); + }; + const client = new SSEClientTransport(new URL('/old/sse', endpointUrl), { requestInit }); + client.onerror = () => {}; + transport = client; + await client.start(); + + await client.send(notification); + + expect(endpointRequests.at(-1)).toMatchObject({ + method: 'POST', + url: expectedPostUrl, + body: JSON.stringify(notification) + }); + } + ); + describe.each(started)('%s', (_name, create) => { test.each([undefined, 'same-origin', 'other'] as const)( 'set to %s, a redirect to another origin is not followed',