From 7ca0cd13d0bbf73d3e048662f16e5be5d2dcd9b3 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Tue, 21 Nov 2023 13:48:14 +0000 Subject: [PATCH] Fix "not attempting encryption" warning (#11899) * Fix "not attempting encryption" warning Fixes https://github.com/vector-im/element-web/issues/26589 * Update src/utils/tokens/tokens.ts --- src/utils/tokens/tokens.ts | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/utils/tokens/tokens.ts b/src/utils/tokens/tokens.ts index aa3f9aa016b..864b6b2090a 100644 --- a/src/utils/tokens/tokens.ts +++ b/src/utils/tokens/tokens.ts @@ -126,20 +126,21 @@ export async function persistTokenInStorage( if (pickleKey) { let encryptedToken: IEncryptedPayload | undefined; - try { - if (!token) { - throw new Error("No token: not attempting encryption"); + if (token) { + try { + // try to encrypt the access token using the pickle key + const encrKey = await pickleKeyToAesKey(pickleKey); + encryptedToken = await encryptAES(token, encrKey, initializationVector); + encrKey.fill(0); + } catch (e) { + // This is likely due to the browser not having WebCrypto or somesuch. + // Warn about it, but fall back to storing the unencrypted token. + logger.warn(`Could not encrypt token for ${storageKey}`, e); } - // try to encrypt the access token using the pickle key - const encrKey = await pickleKeyToAesKey(pickleKey); - encryptedToken = await encryptAES(token, encrKey, initializationVector); - encrKey.fill(0); - } catch (e) { - logger.warn("Could not encrypt access token", e); } try { - // save either the encrypted access token, or the plain access - // token if we were unable to encrypt (e.g. if the browser doesn't + // Save either the encrypted access token, or the plain access + // token if there is no token or we were unable to encrypt (e.g. if the browser doesn't // have WebCrypto). await StorageManager.idbSave("account", storageKey, encryptedToken || token); } catch (e) {