From d7b33ee959ed74238f679b043ff52be87a4ddd70 Mon Sep 17 00:00:00 2001 From: David Baker Date: Mon, 9 Oct 2023 17:43:50 +0100 Subject: [PATCH] Always store room passwords with the right room ID Take the room ID from the URL rather than just assuming it's still the one that was in URL params before: if only the hash changes, the app won't reload. Fixes https://github.com/vector-im/element-call/issues/1708 --- src/e2ee/sharedKeyManagement.ts | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/e2ee/sharedKeyManagement.ts b/src/e2ee/sharedKeyManagement.ts index 06183878..61f26373 100644 --- a/src/e2ee/sharedKeyManagement.ts +++ b/src/e2ee/sharedKeyManagement.ts @@ -17,7 +17,7 @@ limitations under the License. import { useEffect, useMemo } from "react"; import { useEnableE2EE } from "../settings/useSetting"; -import { useLocalStorage } from "../useLocalStorage"; +import { setLocalStorageItem, useLocalStorage } from "../useLocalStorage"; import { useClient } from "../ClientContext"; import { useUrlParams } from "../UrlParams"; import { widget } from "../widget"; @@ -25,27 +25,29 @@ import { widget } from "../widget"; export const getRoomSharedKeyLocalStorageKey = (roomId: string): string => `room-shared-key-${roomId}`; -const useInternalRoomSharedKey = ( - roomId: string -): [string | null, (value: string) => void] => { +const useInternalRoomSharedKey = (roomId: string): string | null => { const key = useMemo(() => getRoomSharedKeyLocalStorageKey(roomId), [roomId]); const [e2eeEnabled] = useEnableE2EE(); - const [roomSharedKey, setRoomSharedKey] = useLocalStorage(key); + const roomSharedKey = useLocalStorage(key)[0]; - return [e2eeEnabled ? roomSharedKey : null, setRoomSharedKey]; + return e2eeEnabled ? roomSharedKey : null; }; const useKeyFromUrl = (roomId: string): string | null => { const urlParams = useUrlParams(); - const [e2eeSharedKey, setE2EESharedKey] = useInternalRoomSharedKey(roomId); + const e2eeSharedKey = useInternalRoomSharedKey(roomId); useEffect(() => { if (!urlParams.password) return; if (urlParams.password === "") return; if (urlParams.password === e2eeSharedKey) return; + if (!urlParams.roomId) return; - setE2EESharedKey(urlParams.password); - }, [urlParams, e2eeSharedKey, setE2EESharedKey]); + setLocalStorageItem( + getRoomSharedKeyLocalStorageKey(urlParams.roomId), + urlParams.password + ); + }, [urlParams, e2eeSharedKey]); return urlParams.password ?? null; }; @@ -57,7 +59,7 @@ export const useRoomSharedKey = (roomId: string): string | null => { // time for us to read it out again). const passwordFormUrl = useKeyFromUrl(roomId); - return useInternalRoomSharedKey(roomId)[0] ?? passwordFormUrl; + return useInternalRoomSharedKey(roomId) ?? passwordFormUrl; }; export const useIsRoomE2EE = (roomId: string): boolean | null => {