diff --git a/src/UrlParams.test.ts b/src/UrlParams.test.ts index a188fbbc..048bb942 100644 --- a/src/UrlParams.test.ts +++ b/src/UrlParams.test.ts @@ -82,14 +82,11 @@ describe("UrlParams", () => { getRoomIdentifierFromUrl("", `?roomId=${ROOM_ID}`, "").roomId, ).toBe(ROOM_ID); }); - it("(roomId with unprintable chatacters)", () => { + it("handles params with unprintable characters", () => { const invisibleChar = "\u2066"; expect( - getRoomIdentifierFromUrl( - "", - `?roomId=${invisibleChar}${ROOM_ID}${invisibleChar}`, - "", - ).roomId, + getRoomIdentifierFromUrl("", `?roomId=${ROOM_ID}${invisibleChar}`, "") + .roomId, ).toBe(ROOM_ID); }); }); diff --git a/src/UrlParams.ts b/src/UrlParams.ts index 3b55ed2a..43698a21 100644 --- a/src/UrlParams.ts +++ b/src/UrlParams.ts @@ -220,6 +220,8 @@ class ParamParser { private queryParams: URLSearchParams; public constructor(search: string, hash: string) { + // Replace any non-printable characters that another client may have inserted. + search = search.replaceAll(/^[^ -~]+|[^ -~]+$/g, ""); this.queryParams = new URLSearchParams(search); const fragmentQueryStart = hash.indexOf("?"); @@ -258,6 +260,8 @@ export const getUrlParams = ( search = window.location.search, hash = window.location.hash, ): UrlParams => { + // Replace any non-printable characters that another client may have inserted. + search = search.replaceAll(/^[^ -~]+|[^ -~]+$/g, ""); const parser = new ParamParser(search, hash); const fontScale = parseFloat(parser.getParam("fontScale") ?? ""); @@ -387,14 +391,10 @@ export function getRoomIdentifierFromUrl( // Make sure roomId is valid let roomId: string | null = parser.getParam("roomId"); - if (roomId !== null) { - // Replace any non-printable characters that another client may have inserted. - roomId = roomId.replaceAll(/^[^ -~]+|[^ -~]+$/g, ""); - if (!roomId.startsWith("!")) { - roomId = null; - } else if (!roomId.includes("")) { - roomId = null; - } + if (!roomId?.startsWith("!")) { + roomId = null; + } else if (!roomId.includes("")) { + roomId = null; } return {