diff --git a/src/node/http.ts b/src/node/http.ts index 1429b09a15ef..31165f4aaf44 100644 --- a/src/node/http.ts +++ b/src/node/http.ts @@ -235,7 +235,16 @@ export const getCookieDomain = (host: string, proxyDomains: string[]): string | return undefined } - proxyDomains.forEach((domain) => { + proxyDomains.forEach((proxyDomain) => { + // Proxy domains are patterns like `{{port}}.domain.tld` or + // `prefix-{{port}}-suffix.domain.tld` so use the part after the label that + // contains the port. + const portIndex = proxyDomain.indexOf("{{port}}") + const domain = portIndex === -1 ? proxyDomain : proxyDomain.substring(proxyDomain.indexOf(".", portIndex) + 1) + // Skip patterns we cannot turn into a domain, like `{{port}}.{{host}}`. + if (!domain || domain.includes("{{")) { + return + } if (host.endsWith(domain) && domain.length < host.length) { host = domain } diff --git a/test/unit/node/http.test.ts b/test/unit/node/http.test.ts index 26fe9be7940b..60baad5d5989 100644 --- a/test/unit/node/http.test.ts +++ b/test/unit/node/http.test.ts @@ -124,6 +124,35 @@ describe("http", () => { }) }) + describe("getCookieDomain", () => { + it("should use the proxy domain for the cookie", () => { + const proxyDomains = ["{{port}}.example.com", "code-{{port}}.test.com"] + expect(http.getCookieDomain("example.com", proxyDomains)).toBe("example.com") + expect(http.getCookieDomain("8080.example.com", proxyDomains)).toBe("example.com") + expect(http.getCookieDomain("8080.example.com:443", proxyDomains)).toBe("example.com") + expect(http.getCookieDomain("code-8080.test.com", proxyDomains)).toBe("test.com") + }) + + it("should use the highest level proxy domain", () => { + const proxyDomains = ["{{port}}.test.example.com", "{{port}}.example.com"] + expect(http.getCookieDomain("test.example.com", proxyDomains)).toBe("example.com") + expect(http.getCookieDomain("8080.test.example.com", proxyDomains)).toBe("example.com") + }) + + it("should use the host when no proxy domain matches", () => { + expect(http.getCookieDomain("code.other.com", ["{{port}}.example.com"])).toBe("code.other.com") + expect(http.getCookieDomain("8080.example.com", ["{{port}}.{{host}}"])).toBe("8080.example.com") + expect(http.getCookieDomain("8080.example.com", [])).toBe("8080.example.com") + }) + + it("should not set a domain for localhost or IP addresses", () => { + const proxyDomains = ["{{port}}.example.com"] + expect(http.getCookieDomain("localhost:8080", proxyDomains)).toBe(undefined) + expect(http.getCookieDomain("127.0.0.1:8080", proxyDomains)).toBe(undefined) + expect(http.getCookieDomain("8080.dev.localhost", proxyDomains)).toBe(undefined) + }) + }) + describe("constructRedirectPath", () => { it("should preserve slashes in queryString so they are human-readable", () => { const mockReq = getMockReq({