Skip to content

Instantly share code, notes, and snippets.

@marcinwasowicz
Created November 27, 2023 11:54
Show Gist options
  • Save marcinwasowicz/3f1478ede2702b8e96d168b5d8a52b2c to your computer and use it in GitHub Desktop.
Save marcinwasowicz/3f1478ede2702b8e96d168b5d8a52b2c to your computer and use it in GitHub Desktop.
Fix invalid use of `CryptoKey` type in newer flow version.
diff --git a/lib/flow-typed/web-crypto-common.js b/lib/flow-typed/web-crypto-common.js
index 571e479ba..70dd21d90 100644
--- a/lib/flow-typed/web-crypto-common.js
+++ b/lib/flow-typed/web-crypto-common.js
@@ -20,7 +20,16 @@ type CryptoKey$Usages =
| 'wrapKey'
| 'unwrapKey';
-declare type CryptoKey = SubtleCrypto$JsonWebKey;
+declare type CryptoKey = {
+ +algorithm:
+ | SubtleCrypto$AesKeyGenParams
+ | SubtleCrypto$RsaHashedKeyGenParams
+ | SubtleCrypto$EcKeyGenParams
+ | SubtleCrypto$HmacKeyGenParams,
+ +extractable: boolean,
+ +type: CryptoKey$Type,
+ +usages: $ReadOnlyArray<CryptoKey$Usages>,
+};
type SubtleCrypto$KeyFormatWithoutJwk = 'pkcs8' | 'raw' | 'spki';
type SubtleCrypto$KeyFormat = 'jwk' | SubtleCrypto$KeyFormatWithoutJwk;
diff --git a/web/account/account-hooks.js b/web/account/account-hooks.js
index 15902a83e..60c6740ba 100644
--- a/web/account/account-hooks.js
+++ b/web/account/account-hooks.js
@@ -229,11 +229,13 @@ function WebNotificationsSessionCreatorProvider(props: Props): React.Node {
);
const persistEncryptionKeyPromise = (async () => {
- let cryptoKeyPersistentForm = encryptionKey;
+ let cryptoKeyPersistentForm;
if (isDesktopSafari) {
// Safari doesn't support structured clone algorithm in service
// worker context so we have to store CryptoKey as JSON
cryptoKeyPersistentForm = await exportKeyToJWK(encryptionKey);
+ } else {
+ cryptoKeyPersistentForm = encryptionKey;
}
await localforage.setItem(
diff --git a/web/push-notif/notif-crypto-utils.js b/web/push-notif/notif-crypto-utils.js
index 7dc826966..f2357050d 100644
--- a/web/push-notif/notif-crypto-utils.js
+++ b/web/push-notif/notif-crypto-utils.js
@@ -52,21 +52,29 @@ async function decryptWebNotification(
const { id, encryptedPayload } = encryptedNotification;
const retrieveEncryptionKeyPromise: Promise<?CryptoKey> = (async () => {
- const persistedCryptoKey = await localforage.getItem<CryptoKey>(
- NOTIFICATIONS_OLM_DATA_ENCRYPTION_KEY,
- );
- if (isDesktopSafari && persistedCryptoKey) {
+ if (isDesktopSafari) {
// Safari doesn't support structured clone algorithm in service
// worker context so we have to store CryptoKey as JSON
- return await importJWKKey(persistedCryptoKey);
+ const persistedCryptoKey =
+ await localforage.getItem<SubtleCrypto$JsonWebKey>(
+ NOTIFICATIONS_OLM_DATA_ENCRYPTION_KEY,
+ );
+ const cryptoKey = persistedCryptoKey
+ ? await importJWKKey(persistedCryptoKey)
+ : null;
+ return cryptoKey;
}
- return persistedCryptoKey;
+ return await localforage.getItem<CryptoKey>(
+ NOTIFICATIONS_OLM_DATA_ENCRYPTION_KEY,
+ );
})();
const [encryptedOlmData, encryptionKey, utilsData] = await Promise.all([
localforage.getItem<EncryptedData>(NOTIFICATIONS_OLM_DATA_CONTENT),
retrieveEncryptionKeyPromise,
- localforage.getItem<WebNotifsServiceUtilsData>(WEB_NOTIFS_SERVICE_UTILS_KEY),
+ localforage.getItem<WebNotifsServiceUtilsData>(
+ WEB_NOTIFS_SERVICE_UTILS_KEY,
+ ),
]);
if (!utilsData) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment