From 96c6217a83e4ba18017fc51a0e663bccb06e692b Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 20 Sep 2023 16:25:02 +0100 Subject: [PATCH] Fix race where app would be opened with no e2ee key The key hadn't been extractwed from the URL at the point the modal was mounted, so it just didn't get the key. --- src/e2ee/sharedKeyManagement.ts | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/e2ee/sharedKeyManagement.ts b/src/e2ee/sharedKeyManagement.ts index 2af0b463..030cb0cb 100644 --- a/src/e2ee/sharedKeyManagement.ts +++ b/src/e2ee/sharedKeyManagement.ts @@ -25,7 +25,7 @@ import { widget } from "../widget"; export const getRoomSharedKeyLocalStorageKey = (roomId: string): string => `room-shared-key-${roomId}`; -export const useInternalRoomSharedKey = ( +const useInternalRoomSharedKey = ( roomId: string ): [string | null, (value: string) => void] => { const key = useMemo(() => getRoomSharedKeyLocalStorageKey(roomId), [roomId]); @@ -35,34 +35,45 @@ export const useInternalRoomSharedKey = ( return [e2eeEnabled ? roomSharedKey : null, setRoomSharedKey]; }; +const useKeyFromUrl = (roomId: string) => { + const urlParams = useUrlParams(); + const [e2eeSharedKey, setE2EESharedKey] = useInternalRoomSharedKey(roomId); + + useEffect(() => { + if (!urlParams.password) return; + if (urlParams.password === "") return; + if (urlParams.password === e2eeSharedKey) return; + + setE2EESharedKey(urlParams.password); + }, [urlParams, e2eeSharedKey, setE2EESharedKey]); +}; + export const useRoomSharedKey = (roomId: string): string | null => { + // make sure we've extracted the key from the URL first + useKeyFromUrl(roomId); + return useInternalRoomSharedKey(roomId)[0]; }; export const useManageRoomSharedKey = (roomId: string): string | null => { - const { password } = useUrlParams(); - const [e2eeSharedKey, setE2EESharedKey] = useInternalRoomSharedKey(roomId); + const urlParams = useUrlParams(); - useEffect(() => { - if (!password) return; - if (password === "") return; - if (password === e2eeSharedKey) return; + useKeyFromUrl(roomId); - setE2EESharedKey(password); - }, [password, e2eeSharedKey, setE2EESharedKey]); + const [e2eeSharedKey] = useInternalRoomSharedKey(roomId); useEffect(() => { const hash = location.hash; if (!hash.includes("?")) return; if (!hash.includes(PASSWORD_STRING)) return; - if (password !== e2eeSharedKey) return; + if (urlParams.password !== e2eeSharedKey) return; const [hashStart, passwordStart] = hash.split(PASSWORD_STRING); const hashEnd = passwordStart.split("&")[1]; location.replace((hashStart ?? "") + (hashEnd ?? "")); - }, [password, e2eeSharedKey]); + }, [urlParams, e2eeSharedKey]); return e2eeSharedKey; };