test(control-ui): cover IPv6 loopback token scope

This commit is contained in:
ssdiwu 2026-03-12 15:01:27 +08:00
parent 894ba946e4
commit 135b62cb69
2 changed files with 43 additions and 5 deletions

View File

@ -249,6 +249,48 @@ describe("loadSettings default gateway URL derivation", () => {
});
});
it("reuses a session token across IPv6 and IPv4 loopback aliases", async () => {
setTestLocation({
protocol: "http:",
host: "[::1]:18789",
pathname: "/",
});
const { loadSettings, saveSettings } = await import("./storage.ts");
saveSettings({
gatewayUrl: "ws://[::1]:18789",
token: "loopback-token",
sessionKey: "main",
lastActiveSessionKey: "main",
theme: "system",
chatFocusMode: false,
chatShowThinking: true,
splitRatio: 0.6,
navCollapsed: false,
navGroupsCollapsed: {},
});
localStorage.setItem(
"openclaw.control.settings.v1",
JSON.stringify({
gatewayUrl: "ws://127.0.0.1:18789",
sessionKey: "main",
lastActiveSessionKey: "main",
theme: "system",
chatFocusMode: false,
chatShowThinking: true,
splitRatio: 0.6,
navCollapsed: false,
navGroupsCollapsed: {},
}),
);
expect(loadSettings()).toMatchObject({
gatewayUrl: "ws://127.0.0.1:18789",
token: "loopback-token",
});
});
it("does not reuse a loopback token across different ports", async () => {
setTestLocation({
protocol: "http:",

View File

@ -45,11 +45,7 @@ function normalizeGatewayTokenScope(gatewayUrl: string): string {
const parsed = base ? new URL(trimmed, base) : new URL(trimmed);
const host = parsed.hostname.trim().toLowerCase();
const isLoopbackHost =
host === "localhost" ||
host === "127.0.0.1" ||
host === "::1" ||
host === "[::1]" ||
host.startsWith("127.");
host === "localhost" || host === "127.0.0.1" || host === "[::1]" || host.startsWith("127.");
const pathname =
parsed.pathname === "/" ? "" : parsed.pathname.replace(/\/+$/, "") || parsed.pathname;
const hostPort = isLoopbackHost