Skip to content

Commit 3dce231

Browse files
committed
Fix cookie domain not using --proxy-domain
1 parent 4677c9d commit 3dce231

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
@@ -124,6 +124,35 @@ describe("http", () => {
124124
})
125125
})
126126

127+
describe("getCookieDomain", () => {
128+
it("should use the proxy domain for the cookie", () => {
129+
const proxyDomains = ["{{port}}.example.com", "code-{{port}}.test.com"]
130+
expect(http.getCookieDomain("example.com", proxyDomains)).toBe("example.com")
131+
expect(http.getCookieDomain("8080.example.com", proxyDomains)).toBe("example.com")
132+
expect(http.getCookieDomain("8080.example.com:443", proxyDomains)).toBe("example.com")
133+
expect(http.getCookieDomain("code-8080.test.com", proxyDomains)).toBe("test.com")
134+
})
135+
136+
it("should use the highest level proxy domain", () => {
137+
const proxyDomains = ["{{port}}.test.example.com", "{{port}}.example.com"]
138+
expect(http.getCookieDomain("test.example.com", proxyDomains)).toBe("example.com")
139+
expect(http.getCookieDomain("8080.test.example.com", proxyDomains)).toBe("example.com")
140+
})
141+
142+
it("should use the host when no proxy domain matches", () => {
143+
expect(http.getCookieDomain("code.other.com", ["{{port}}.example.com"])).toBe("code.other.com")
144+
expect(http.getCookieDomain("8080.example.com", ["{{port}}.{{host}}"])).toBe("8080.example.com")
145+
expect(http.getCookieDomain("8080.example.com", [])).toBe("8080.example.com")
146+
})
147+
148+
it("should not set a domain for localhost or IP addresses", () => {
149+
const proxyDomains = ["{{port}}.example.com"]
150+
expect(http.getCookieDomain("localhost:8080", proxyDomains)).toBe(undefined)
151+
expect(http.getCookieDomain("127.0.0.1:8080", proxyDomains)).toBe(undefined)
152+
expect(http.getCookieDomain("8080.dev.localhost", proxyDomains)).toBe(undefined)
153+
})
154+
})
155+
127156
describe("constructRedirectPath", () => {
128157
it("should preserve slashes in queryString so they are human-readable", () => {
129158
const mockReq = getMockReq({

0 commit comments

Comments
 (0)