From b96154bb177a0250e3bb81705dcfaf88f51e44bb Mon Sep 17 00:00:00 2001 From: Mark Russell Date: Fri, 13 Nov 2026 00:42:46 -0500 Subject: [PATCH] fix(eio-client): send same-origin cookies with the fetch() transport With `withCredentials: false` (the default), the fetch() transport used the "omit" credentials mode, which drops the cookies even for same-origin requests and ignores the Set-Cookie headers of the responses. The XHR transport sends them, so sticky-session cookies (load balancer, `cookie` option of the server) were lost when switching to fetch(). The "same-origin" mode matches the XHR behavior. Co-Authored-By: Claude Opus 5.5 --- .../lib/transports/polling-fetch.ts | 4 +- packages/engine.io-client/test/node.js | 46 ++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/packages/engine.io-client/lib/transports/polling-fetch.ts b/packages/engine.io-client/lib/transports/polling-fetch.ts index 7d85b66c62..c4dccf6bac 100644 --- a/packages/engine.io-client/lib/transports/polling-fetch.ts +++ b/packages/engine.io-client/lib/transports/polling-fetch.ts @@ -52,7 +52,9 @@ export class Fetch extends Polling { method: isPost ? "POST" : "GET", body: isPost ? data : null, headers, - credentials: this.opts.withCredentials ? "include" : "omit", + // same behavior as XMLHttpRequest: cookies are always sent for same-origin requests, and only with + // `withCredentials: true` for cross-origin requests + credentials: this.opts.withCredentials ? "include" : "same-origin", }).then((res) => { // @ts-ignore getSetCookie() was added in Node.js v19.7.0 this.socket._cookieJar?.parseCookies(res.headers.getSetCookie()); diff --git a/packages/engine.io-client/test/node.js b/packages/engine.io-client/test/node.js index 1f756a13bd..5e3fb2cb7c 100644 --- a/packages/engine.io-client/test/node.js +++ b/packages/engine.io-client/test/node.js @@ -1,6 +1,6 @@ const path = require("node:path"); const { execFile } = require("node:child_process"); -const { Socket } = require("../"); +const { Socket, Fetch } = require("../"); const { repeat } = require("./util"); const expect = require("expect.js"); const { parse } = require("../build/cjs/globals.node.js"); @@ -109,6 +109,50 @@ describe("node.js", () => { }); }); + describe("credentials mode of the fetch() transport", () => { + const originalFetch = globalThis.fetch; + let credentials; + + beforeEach(() => { + credentials = []; + globalThis.fetch = (url, init) => { + credentials.push(init.credentials); + return originalFetch(url, init); + }; + }); + + afterEach(() => { + globalThis.fetch = originalFetch; + }); + + it("should send same-origin cookies with withCredentials: false", (done) => { + const socket = new Socket({ + transports: [Fetch], + withCredentials: false, + }); + + socket.on("open", () => { + // like XMLHttpRequest, and unlike "omit", which drops the cookies of the page origin + expect(credentials).to.eql(["same-origin"]); + socket.close(); + done(); + }); + }); + + it("should send cross-origin cookies with withCredentials: true", (done) => { + const socket = new Socket({ + transports: [Fetch], + withCredentials: true, + }); + + socket.on("open", () => { + expect(credentials).to.eql(["include"]); + socket.close(); + done(); + }); + }); + }); + it("should not modify the extraHeaders option with withCredentials: true", (done) => { const extraHeaders = { foo: "bar" }; const socket = new Socket("http://localhost:3000", {