|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +require('../common'); |
| 4 | + |
| 5 | +const assert = require('node:assert'); |
| 6 | +const { spawnSync } = require('node:child_process'); |
| 7 | +const { writeFileSync } = require('node:fs'); |
| 8 | + |
| 9 | +const tmpdir = require('../common/tmpdir'); |
| 10 | +tmpdir.refresh(); |
| 11 | + |
| 12 | +const testCVE = 'CVE-2009-TEST'; |
| 13 | +const testVulnerabilityTitle = 'Non-existent vulnerability for testing'; |
| 14 | +const testCVEWarningMessage = `SECURITY WARNING: Reverting ${testCVE}: ${testVulnerabilityTitle}`; |
| 15 | +const testProcKey = `REVERT_${testCVE.replace(/-/g, '_')}`; |
| 16 | +const invalidCVE = 'CVE-2009-NOPE'; |
| 17 | +const invalidCVERegExp = new RegExp(`^.+: Error: Attempt to revert an unknown CVE \\[${invalidCVE}\\]$`); |
| 18 | + |
| 19 | +const node = process.execPath; |
| 20 | + |
| 21 | +function run({ env, dotenv, args, otherEnv }) { |
| 22 | + const dotenvPath = tmpdir.resolve('.env'); |
| 23 | + if (dotenv != null) { |
| 24 | + writeFileSync(dotenvPath, `NODE_SECURITY_REVERT=${dotenv}\n`); |
| 25 | + } |
| 26 | + |
| 27 | + return spawnSync(node, [ |
| 28 | + ...(dotenv != null ? ['--env-file', dotenvPath] : []), |
| 29 | + ...(args ?? []), |
| 30 | + '--print', `JSON.stringify([ |
| 31 | + process.env.NODE_SECURITY_REVERT, |
| 32 | + Object.entries(process).filter(([name]) => name.startsWith("REVERT_")), |
| 33 | + child_process.execFileSync(${JSON.stringify(node)}, [ |
| 34 | + '--print', 'process.env.NODE_SECURITY_REVERT' |
| 35 | + ]).toString().trim().split('\\n'), |
| 36 | + ])`, |
| 37 | + ], { |
| 38 | + env: { |
| 39 | + ...process.env, |
| 40 | + ...(otherEnv ?? {}), |
| 41 | + NODE_SECURITY_REVERT: env, |
| 42 | + }, |
| 43 | + encoding: 'utf8' |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +function expectSuccess(params) { |
| 48 | + const { status, stdout, stderr } = run(params); |
| 49 | + assert.strictEqual(status, 0, `${status} ${stderr}`); |
| 50 | + assert.strictEqual(stderr.length, 0, stderr); |
| 51 | + const lines = stdout.trim().split(/\r?\n/g); |
| 52 | + assert.notStrictEqual(lines.length, 0); |
| 53 | + const output = lines.pop(); |
| 54 | + return [lines, JSON.parse(output)]; |
| 55 | +} |
| 56 | + |
| 57 | +function expectError(params) { |
| 58 | + const { status, stdout, stderr } = run(params); |
| 59 | + assert.notStrictEqual(status, 0); |
| 60 | + return [stdout.trim(), stderr.trim()]; |
| 61 | +} |
| 62 | + |
| 63 | +for (const method of ['env', 'dotenv']) { |
| 64 | + // Default: no security revert. |
| 65 | + assert.deepStrictEqual(expectSuccess({}), [[], [null, [], ['undefined']]]); |
| 66 | + |
| 67 | + // Revert a single CVE patch without child processes inheriting this behavior. |
| 68 | + assert.deepStrictEqual(expectSuccess({ [method]: testCVE }), [ |
| 69 | + [testCVEWarningMessage], |
| 70 | + [null, [[testProcKey, true]], ['undefined']], |
| 71 | + ]); |
| 72 | + |
| 73 | + // Revert a single CVE patch with child processes inheriting this behavior. |
| 74 | + assert.deepStrictEqual(expectSuccess({ [method]: `${testCVE}+sticky` }), [ |
| 75 | + [testCVEWarningMessage], |
| 76 | + [`${testCVE}+sticky`, [[testProcKey, true]], [testCVEWarningMessage, `${testCVE}+sticky`]], |
| 77 | + ]); |
| 78 | + |
| 79 | + // Try to revert a CVE patch that does not exist. |
| 80 | + for (const suffix of ['', '+sticky']) { |
| 81 | + const [stdout, stderr] = expectError({ [method]: `${invalidCVE}${suffix}` }); |
| 82 | + assert.strictEqual(stdout, ''); |
| 83 | + assert.match(stderr, invalidCVERegExp); |
| 84 | + } |
| 85 | + |
| 86 | + // Try to revert two CVE patches, one of which does not exist. |
| 87 | + for (const suffix of ['', '+sticky']) { |
| 88 | + const [stdout, stderr] = expectError({ [method]: `${testCVE},${invalidCVE}${suffix}` }); |
| 89 | + assert.strictEqual(stdout, testCVEWarningMessage); |
| 90 | + assert.match(stderr, invalidCVERegExp); |
| 91 | + } |
| 92 | + |
| 93 | + // The command-line argument should take precedence over the environment variable |
| 94 | + // and is never inherited by child processes. |
| 95 | + assert.deepStrictEqual(expectSuccess({ |
| 96 | + [method]: invalidCVE, |
| 97 | + args: ['--security-revert', testCVE], |
| 98 | + }), [ |
| 99 | + [testCVEWarningMessage], |
| 100 | + [null, [[testProcKey, true]], ['undefined']], |
| 101 | + ]); |
| 102 | +} |
| 103 | + |
| 104 | +// The environment variable should take precedence over a dotenv file. |
| 105 | +assert.deepStrictEqual(expectSuccess({ |
| 106 | + env: testCVE, |
| 107 | + dotenv: invalidCVE |
| 108 | +}), [ |
| 109 | + [testCVEWarningMessage], |
| 110 | + [null, [[testProcKey, true]], ['undefined']], |
| 111 | +]); |
| 112 | + |
| 113 | +// We don't want security reverts to be inherited implicitly, thus, neither |
| 114 | +// --security-revert nor --env-file should be allowed in NODE_OPTIONS. |
| 115 | +for (const NODE_OPTIONS of [ |
| 116 | + `--env-file=${tmpdir.resolve('.env')}`, |
| 117 | + `--security-revert=${testCVE}`, |
| 118 | +]) { |
| 119 | + const [stdout, stderr] = expectError({ |
| 120 | + otherEnv: { NODE_OPTIONS } |
| 121 | + }); |
| 122 | + assert.strictEqual(stdout, ''); |
| 123 | + const optPrefix = NODE_OPTIONS.substring(0, NODE_OPTIONS.indexOf('=') + 1); |
| 124 | + assert.match(stderr, new RegExp(`^.+: ${optPrefix} is not allowed in NODE_OPTIONS$`)); |
| 125 | +} |
0 commit comments