Skip to content

Commit 4669570

Browse files
gibfahnMylesBorins
authored andcommitted
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 <riclau@uk.ibm.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 27af0bb commit 4669570

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
@@ -6134,11 +6134,14 @@ void GetFipsCrypto(const FunctionCallbackInfo<Value>& args) {
61346134
void SetFipsCrypto(const FunctionCallbackInfo<Value>& args) {
61356135
Environment* env = Environment::GetCurrent(args);
61366136
#ifdef NODE_FIPS_MODE
6137-
bool mode = args[0]->BooleanValue();
6137+
const bool enabled = FIPS_mode();
6138+
const bool enable = args[0]->BooleanValue();
6139+
if (enable == enabled)
6140+
return; // No action needed.
61386141
if (force_fips_crypto) {
61396142
return env->ThrowError(
61406143
"Cannot set FIPS mode, it was forced with --force-fips at startup.");
6141-
} else if (!FIPS_mode_set(mode)) {
6144+
} else if (!FIPS_mode_set(enable)) {
61426145
unsigned long err = ERR_get_error(); // NOLINT(runtime/int)
61436146
return ThrowCryptoError(env, err);
61446147
}

test/parallel/test-crypto-fips.js

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

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

0 commit comments

Comments
 (0)