|
| 1 | +// Flags: --expose-internals |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +// Verify that getCiphers() and getHashes() reflect the current FIPS state |
| 5 | +// rather than returning a stale cached snapshot from before setFips() was |
| 6 | +// called. Regression test for https://github.com/nodejs/node/issues/62982. |
| 7 | + |
| 8 | +const common = require('../common'); |
| 9 | +if (!common.hasCrypto) |
| 10 | + common.skip('missing crypto'); |
| 11 | + |
| 12 | +const { internalBinding } = require('internal/test/binding'); |
| 13 | +const { testFipsCrypto } = internalBinding('crypto'); |
| 14 | + |
| 15 | +if (!testFipsCrypto()) |
| 16 | + common.skip('FIPS not supported in this build'); |
| 17 | + |
| 18 | +const assert = require('assert'); |
| 19 | +const { getCiphers, getHashes, setFips, getFips } = require('crypto'); |
| 20 | + |
| 21 | +// Record the full lists available when FIPS is off. |
| 22 | +const ciphersWithoutFips = getCiphers(); |
| 23 | +const hashesWithoutFips = getHashes(); |
| 24 | + |
| 25 | +assert.ok(ciphersWithoutFips.length > 0, 'expected at least one cipher'); |
| 26 | +assert.ok(hashesWithoutFips.length > 0, 'expected at least one hash'); |
| 27 | + |
| 28 | +// Switch to FIPS mode; the lists must be re-derived, not served from cache. |
| 29 | +setFips(true); |
| 30 | +assert.strictEqual(getFips(), 1); |
| 31 | + |
| 32 | +const ciphersWithFips = getCiphers(); |
| 33 | +const hashesWithFips = getHashes(); |
| 34 | + |
| 35 | +// FIPS mode restricts the visible algorithm set — the lists must shrink |
| 36 | +// (or at minimum differ; some platforms expose only FIPS algorithms by |
| 37 | +// default, but in that case the full list can't be larger than the FIPS one). |
| 38 | +assert.ok( |
| 39 | + ciphersWithFips.length <= ciphersWithoutFips.length, |
| 40 | + `Expected FIPS cipher list (${ciphersWithFips.length}) to be no larger ` + |
| 41 | + `than the full list (${ciphersWithoutFips.length})` |
| 42 | +); |
| 43 | +assert.ok( |
| 44 | + hashesWithFips.length <= hashesWithoutFips.length, |
| 45 | + `Expected FIPS hash list (${hashesWithFips.length}) to be no larger ` + |
| 46 | + `than the full list (${hashesWithoutFips.length})` |
| 47 | +); |
| 48 | + |
| 49 | +// Every FIPS-mode algorithm must also appear in the non-FIPS list. |
| 50 | +for (const cipher of ciphersWithFips) { |
| 51 | + assert.ok( |
| 52 | + ciphersWithoutFips.includes(cipher), |
| 53 | + `FIPS cipher '${cipher}' missing from the non-FIPS list` |
| 54 | + ); |
| 55 | +} |
| 56 | +for (const hash of hashesWithFips) { |
| 57 | + assert.ok( |
| 58 | + hashesWithoutFips.includes(hash), |
| 59 | + `FIPS hash '${hash}' missing from the non-FIPS list` |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +// Restore; the cache must be evicted again so the full lists come back. |
| 64 | +setFips(false); |
| 65 | +assert.strictEqual(getFips(), 0); |
| 66 | + |
| 67 | +assert.deepStrictEqual( |
| 68 | + getCiphers(), |
| 69 | + ciphersWithoutFips, |
| 70 | + 'getCiphers() should match pre-FIPS list after setFips(false)' |
| 71 | +); |
| 72 | +assert.deepStrictEqual( |
| 73 | + getHashes(), |
| 74 | + hashesWithoutFips, |
| 75 | + 'getHashes() should match pre-FIPS list after setFips(false)' |
| 76 | +); |
0 commit comments