Skip to content

Commit 1fd83ac

Browse files
crypto: evict getCiphers/getHashes cache on setFips/setEngine
getCiphers() and getHashes() used cachedResult() which memoizes once and never clears. setFips() changes which algorithms OpenSSL exposes (FIPS-approved only vs. all), and setEngine() can register additional ciphers/hashes from a loaded engine, but neither invalidated the cache. Replace the two cachedResult() calls with manual cache variables (_ciphersCache, _hashesCache) that mirror the existing _hashCache pattern. Add evictCipherHashCache() and call it from both setFips() and setEngine() after they mutate OpenSSL state. getCurves is intentionally left using cachedResult — curves are not affected by FIPS mode or engine loading. Fixes: #62982
1 parent 27abe9c commit 1fd83ac

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

lib/crypto.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ const {
121121
getHashes,
122122
setEngine,
123123
secureHeapUsed,
124+
evictCipherHashCache,
124125
} = require('internal/crypto/util');
125126
const Certificate = require('internal/crypto/certificate');
126127
const {
@@ -263,6 +264,7 @@ function setFips(val) {
263264
throw new ERR_WORKER_UNSUPPORTED_OPERATION('Calling crypto.setFips()');
264265
}
265266
setFipsCrypto(val);
267+
evictCipherHashCache();
266268
}
267269
}
268270

lib/internal/crypto/util.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
ArrayFrom,
77
ArrayPrototypeIncludes,
88
ArrayPrototypePush,
9+
ArrayPrototypeSlice,
910
BigInt,
1011
DataViewPrototypeGetBuffer,
1112
DataViewPrototypeGetByteLength,
@@ -125,8 +126,25 @@ function getCachedHashId(algorithm) {
125126
return result === undefined ? -1 : result;
126127
}
127128

128-
const getCiphers = cachedResult(() => filterDuplicateStrings(_getCiphers()));
129-
const getHashes = cachedResult(() => filterDuplicateStrings(_getHashes()));
129+
let _ciphersCache;
130+
function getCiphers() {
131+
if (_ciphersCache === undefined)
132+
_ciphersCache = filterDuplicateStrings(_getCiphers());
133+
return ArrayPrototypeSlice(_ciphersCache);
134+
}
135+
136+
let _hashesCache;
137+
function getHashes() {
138+
if (_hashesCache === undefined)
139+
_hashesCache = filterDuplicateStrings(_getHashes());
140+
return ArrayPrototypeSlice(_hashesCache);
141+
}
142+
143+
function evictCipherHashCache() {
144+
_ciphersCache = undefined;
145+
_hashesCache = undefined;
146+
}
147+
130148
const getCurves = cachedResult(() => filterDuplicateStrings(_getCurves()));
131149

132150
function setEngine(id, flags) {
@@ -143,6 +161,7 @@ function setEngine(id, flags) {
143161
throw new ERR_CRYPTO_CUSTOM_ENGINE_NOT_SUPPORTED();
144162
if (!_setEngine(id, flags))
145163
throw new ERR_CRYPTO_ENGINE_UNKNOWN(id);
164+
evictCipherHashCache();
146165
}
147166

148167
const getArrayBufferOrView = hideStackFrames((buffer, name, encoding) => {
@@ -855,6 +874,7 @@ module.exports = {
855874
getCurves,
856875
getDataViewOrTypedArrayBuffer,
857876
getHashes,
877+
evictCipherHashCache,
858878
kHandle,
859879
kKeyObject,
860880
setEngine,

0 commit comments

Comments
 (0)