Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions client/src/services/__tests__/serverDetection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,19 @@ describe("mixedContentBlockReason", () => {
expect(mixedContentBlockReason("wss://play.example.com/ws")).toBeNull();
});
});

describe("parseJoinCode — bracketed IPv6 host", () => {
it("does not corrupt a bracketed IPv6 host that has no port", () => {
// `[::1]` ends in `]`; its last colon is inside the address. Splitting there
// produced a broken host `[:` and a malformed `wss://[::1/ws`.
const r = parseJoinCode("ABC123@[::1]");
expect(r.code).toBe("ABC123");
expect(r.serverAddress).toBe("wss://[::1]/ws");
});

it("still splits a bracketed IPv6 host that DOES carry a port", () => {
expect(parseJoinCode("ABC123@[::1]:9000").serverAddress).toBe(
"wss://[::1]:9000/ws",
);
});
});
9 changes: 8 additions & 1 deletion client/src/services/serverDetection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,15 @@ export function parseJoinCode(input: string): { code: string; serverAddress?: st
}

// Split host:port on the last colon so a host:port pair splits correctly.
// A bracketed IPv6 literal with no port (e.g. `[::1]`) ends in `]`, and its
// last colon is INSIDE the address — splitting there yields a broken host like
// `[:`. When the host is a bracketed literal with no trailing `:port`, there
// is no port to split off.
const colonIndex = hostPort.lastIndexOf(":");
const hasPort = colonIndex !== -1 && colonIndex < hostPort.length - 1;
const hasPort =
colonIndex !== -1 &&
colonIndex < hostPort.length - 1 &&
!hostPort.endsWith("]");
const host = hasPort ? hostPort.slice(0, colonIndex) : hostPort;

const isLocal = host === "localhost" || host === "127.0.0.1";
Expand Down
Loading