Skip to content

Commit 761b0d2

Browse files
Fix cookie domain not being set against root host (#8076)
1 parent 0901206 commit 761b0d2

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎src/node/http.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,16 @@ export const getCookieDomain = (host: string, proxyDomains: string[]): string |
235235
return undefined
236236
}
237237

238-
proxyDomains.forEach((domain) => {
238+
proxyDomains.forEach((proxyDomain) => {
239+
// Proxy domains are patterns like `{{port}}.domain.tld` or
240+
// `prefix-{{port}}-suffix.domain.tld` so use the part after the label that
241+
// contains the port.
242+
const portIndex = proxyDomain.indexOf("{{port}}")
243+
const domain = portIndex === -1 ? proxyDomain : proxyDomain.substring(proxyDomain.indexOf(".", portIndex) + 1)
244+
// Skip patterns we cannot turn into a domain, like `{{port}}.{{host}}`.
245+
if (!domain || domain.includes("{{")) {
246+
return
247+
}
239248
if (host.endsWith(domain) && domain.length < host.length) {
240249
host = domain
241250
}

‎test/unit/node/http.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,35 @@ describe("http", () => {
230230
})
231231
})
232232

233+
describe("getCookieDomain", () => {
234+
it("should use the proxy domain for the cookie", () => {
235+
const proxyDomains = ["{{port}}.example.com", "code-{{port}}.test.com"]
236+
expect(http.getCookieDomain("example.com", proxyDomains)).toBe("example.com")
237+
expect(http.getCookieDomain("8080.example.com", proxyDomains)).toBe("example.com")
238+
expect(http.getCookieDomain("8080.example.com:443", proxyDomains)).toBe("example.com")
239+
expect(http.getCookieDomain("code-8080.test.com", proxyDomains)).toBe("test.com")
240+
})
241+
242+
it("should use the highest level proxy domain", () => {
243+
const proxyDomains = ["{{port}}.test.example.com", "{{port}}.example.com"]
244+
expect(http.getCookieDomain("test.example.com", proxyDomains)).toBe("example.com")
245+
expect(http.getCookieDomain("8080.test.example.com", proxyDomains)).toBe("example.com")
246+
})
247+
248+
it("should use the host when no proxy domain matches", () => {
249+
expect(http.getCookieDomain("code.other.com", ["{{port}}.example.com"])).toBe("code.other.com")
250+
expect(http.getCookieDomain("8080.example.com", ["{{port}}.{{host}}"])).toBe("8080.example.com")
251+
expect(http.getCookieDomain("8080.example.com", [])).toBe("8080.example.com")
252+
})
253+
254+
it("should not set a domain for localhost or IP addresses", () => {
255+
const proxyDomains = ["{{port}}.example.com"]
256+
expect(http.getCookieDomain("localhost:8080", proxyDomains)).toBe(undefined)
257+
expect(http.getCookieDomain("127.0.0.1:8080", proxyDomains)).toBe(undefined)
258+
expect(http.getCookieDomain("8080.dev.localhost", proxyDomains)).toBe(undefined)
259+
})
260+
})
261+
233262
describe("constructRedirectPath", () => {
234263
it("should preserve slashes in queryString so they are human-readable", () => {
235264
const mockReq = getMockReq({

0 commit comments

Comments
 (0)