fix(gateway): preserve unbracketed IPv6 host headers
This commit is contained in:
@@ -5,8 +5,21 @@ import {
|
||||
isTrustedProxyAddress,
|
||||
pickPrimaryLanIPv4,
|
||||
resolveGatewayListenHosts,
|
||||
resolveHostName,
|
||||
} from "./net.js";
|
||||
|
||||
describe("resolveHostName", () => {
|
||||
it("returns hostname without port for IPv4/hostnames", () => {
|
||||
expect(resolveHostName("localhost:18789")).toBe("localhost");
|
||||
expect(resolveHostName("127.0.0.1:18789")).toBe("127.0.0.1");
|
||||
});
|
||||
|
||||
it("handles bracketed and unbracketed IPv6 loopback hosts", () => {
|
||||
expect(resolveHostName("[::1]:18789")).toBe("::1");
|
||||
expect(resolveHostName("::1")).toBe("::1");
|
||||
});
|
||||
});
|
||||
|
||||
describe("isTrustedProxyAddress", () => {
|
||||
describe("exact IP matching", () => {
|
||||
it("returns true when IP matches exactly", () => {
|
||||
|
||||
@@ -40,6 +40,10 @@ export function resolveHostName(hostHeader?: string): string {
|
||||
return host.slice(1, end);
|
||||
}
|
||||
}
|
||||
// Unbracketed IPv6 host (e.g. "::1") has no port and should be returned as-is.
|
||||
if (net.isIP(host) === 6) {
|
||||
return host;
|
||||
}
|
||||
const [name] = host.split(":");
|
||||
return name ?? "";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user