From f5c329030dc2bcb8ffb6f36ad01b4447527770f1 Mon Sep 17 00:00:00 2001 From: Bernd Schoolmann Date: Mon, 3 Jul 2023 14:12:42 +0200 Subject: [PATCH] Make Argon2 WebAssembly module unload after use (#5072) --- .../services/web-crypto-function.service.ts | 1 + patches/argon2-browser+1.18.0.patch | 24 +++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/libs/common/src/platform/services/web-crypto-function.service.ts b/libs/common/src/platform/services/web-crypto-function.service.ts index 18bec1a8f6..65ee74aa63 100644 --- a/libs/common/src/platform/services/web-crypto-function.service.ts +++ b/libs/common/src/platform/services/web-crypto-function.service.ts @@ -69,6 +69,7 @@ export class WebCryptoFunctionService implements CryptoFunctionService { hashLen: 32, type: argon2.ArgonType.Argon2id, }); + argon2.unloadRuntime(); return result.hash; } diff --git a/patches/argon2-browser+1.18.0.patch b/patches/argon2-browser+1.18.0.patch index 7161e9aa6e..160661cd91 100644 --- a/patches/argon2-browser+1.18.0.patch +++ b/patches/argon2-browser+1.18.0.patch @@ -1,5 +1,5 @@ diff --git a/node_modules/argon2-browser/lib/argon2.js b/node_modules/argon2-browser/lib/argon2.js -index ffa77a5..b498e2d 100644 +index ffa77a5..98b2f13 100644 --- a/node_modules/argon2-browser/lib/argon2.js +++ b/node_modules/argon2-browser/lib/argon2.js @@ -78,16 +78,27 @@ @@ -36,10 +36,16 @@ index ffa77a5..b498e2d 100644 return fetch(wasmPath) .then((response) => response.arrayBuffer()) .then((ab) => new Uint8Array(ab)); -@@ -354,6 +365,24 @@ - } - } - +@@ -351,7 +362,28 @@ + loadModule._module.unloadRuntime(); + delete loadModule._promise; + delete loadModule._module; ++ if (typeof require !== 'undefined') { ++ delete require.cache[require.resolve('../dist/argon2.js')] ++ } ++ } ++ } ++ + // ref: https://stackoverflow.com/a/47880734/1090359 + // ref: https://github.com/GoogleChromeLabs/wasm-feature-detect/blob/main/src/detectors/simd/module.wat (translated with wat2wasm) + function checkIfSIMDSupported() { @@ -54,10 +60,8 @@ index ffa77a5..b498e2d 100644 + } + } catch { + return false; -+ } + } + return false; -+ } -+ + } + return { - ArgonType, - hash: argon2Hash,