Skip to content

Commit 0919dff

Browse files
committed
crypto: only try to set FIPS mode if different
Turning FIPS mode on (or off) when it's already on (or off) should be a no-op, not an error. PR-URL: #12210 Fixes: #11849 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 7d55b81 commit 0919dff

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/node_crypto.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6021,11 +6021,14 @@ void GetFipsCrypto(const FunctionCallbackInfo<Value>& args) {
60216021
void SetFipsCrypto(const FunctionCallbackInfo<Value>& args) {
60226022
Environment* env = Environment::GetCurrent(args);
60236023
#ifdef NODE_FIPS_MODE
6024-
bool mode = args[0]->BooleanValue();
6024+
const bool enabled = FIPS_mode();
6025+
const bool enable = args[0]->BooleanValue();
6026+
if (enable == enabled)
6027+
return; // No action needed.
60256028
if (force_fips_crypto) {
60266029
return env->ThrowError(
60276030
"Cannot set FIPS mode, it was forced with --force-fips at startup.");
6028-
} else if (!FIPS_mode_set(mode)) {
6031+
} else if (!FIPS_mode_set(enable)) {
60296032
unsigned long err = ERR_get_error(); // NOLINT(runtime/int)
60306033
return ThrowCryptoError(env, err);
60316034
}

test/parallel/test-crypto-fips.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,15 @@ testHelper(
212212
'require("crypto").fips = false',
213213
process.env);
214214

215+
// --force-fips makes setFipsCrypto enable a no-op (FIPS stays on)
216+
testHelper(
217+
compiledWithFips() ? 'stdout' : 'stderr',
218+
['--force-fips'],
219+
compiledWithFips() ? FIPS_ENABLED : OPTION_ERROR_STRING,
220+
'(require("crypto").fips = true,' +
221+
'require("crypto").fips)',
222+
process.env);
223+
215224
// --force-fips and --enable-fips order does not matter
216225
testHelper(
217226
'stderr',

0 commit comments

Comments
 (0)