-
Notifications
You must be signed in to change notification settings - Fork 29.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
src: add NODE_SECURITY_REVERT environment variable #52365
Open
tniessen
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
tniessen:env-security-revert
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
|
||
const assert = require('node:assert'); | ||
const { spawnSync } = require('node:child_process'); | ||
const { writeFileSync } = require('node:fs'); | ||
|
||
const tmpdir = require('../common/tmpdir'); | ||
tmpdir.refresh(); | ||
|
||
const testCVE = 'CVE-2009-TEST'; | ||
const testVulnerabilityTitle = 'Non-existent vulnerability for testing'; | ||
const testCVEWarningMessage = `SECURITY WARNING: Reverting ${testCVE}: ${testVulnerabilityTitle}`; | ||
const testProcKey = `REVERT_${testCVE.replace(/-/g, '_')}`; | ||
const invalidCVE = 'CVE-2009-NOPE'; | ||
const invalidCVERegExp = new RegExp(`^.+: Error: Attempt to revert an unknown CVE \\[${invalidCVE}\\]$`); | ||
|
||
const node = process.execPath; | ||
|
||
function run({ env, dotenv, args, otherEnv }) { | ||
const dotenvPath = tmpdir.resolve('.env'); | ||
if (dotenv != null) { | ||
writeFileSync(dotenvPath, `NODE_SECURITY_REVERT=${dotenv}\n`); | ||
} | ||
|
||
return spawnSync(node, [ | ||
...(dotenv != null ? ['--env-file', dotenvPath] : []), | ||
...(args ?? []), | ||
'--print', `JSON.stringify([ | ||
process.env.NODE_SECURITY_REVERT, | ||
Object.entries(process).filter(([name]) => name.startsWith("REVERT_")), | ||
child_process.execFileSync(${JSON.stringify(node)}, [ | ||
'--print', 'process.env.NODE_SECURITY_REVERT' | ||
]).toString().trim().split('\\n'), | ||
])`, | ||
], { | ||
env: { | ||
...process.env, | ||
...(otherEnv ?? {}), | ||
NODE_SECURITY_REVERT: env, | ||
}, | ||
encoding: 'utf8' | ||
}); | ||
} | ||
|
||
function expectSuccess(params) { | ||
const { status, stdout, stderr } = run(params); | ||
assert.strictEqual(status, 0, `${status} ${stderr}`); | ||
assert.strictEqual(stderr.length, 0, stderr); | ||
const lines = stdout.trim().split(/\r?\n/g); | ||
assert.notStrictEqual(lines.length, 0); | ||
const output = lines.pop(); | ||
return [lines, JSON.parse(output)]; | ||
} | ||
|
||
function expectError(params) { | ||
const { status, stdout, stderr } = run(params); | ||
assert.notStrictEqual(status, 0); | ||
return [stdout.trim(), stderr.trim()]; | ||
} | ||
|
||
for (const method of ['env', 'dotenv']) { | ||
// Default: no security revert. | ||
assert.deepStrictEqual(expectSuccess({}), [[], [null, [], ['undefined']]]); | ||
|
||
// Revert a single CVE patch without child processes inheriting this behavior. | ||
assert.deepStrictEqual(expectSuccess({ [method]: testCVE }), [ | ||
[testCVEWarningMessage], | ||
[null, [[testProcKey, true]], ['undefined']], | ||
]); | ||
|
||
// Revert a single CVE patch with child processes inheriting this behavior. | ||
assert.deepStrictEqual(expectSuccess({ [method]: `${testCVE}+sticky` }), [ | ||
[testCVEWarningMessage], | ||
[`${testCVE}+sticky`, [[testProcKey, true]], [testCVEWarningMessage, `${testCVE}+sticky`]], | ||
]); | ||
|
||
// Try to revert a CVE patch that does not exist. | ||
for (const suffix of ['', '+sticky']) { | ||
const [stdout, stderr] = expectError({ [method]: `${invalidCVE}${suffix}` }); | ||
assert.strictEqual(stdout, ''); | ||
assert.match(stderr, invalidCVERegExp); | ||
} | ||
|
||
// Try to revert two CVE patches, one of which does not exist. | ||
for (const suffix of ['', '+sticky']) { | ||
const [stdout, stderr] = expectError({ [method]: `${testCVE},${invalidCVE}${suffix}` }); | ||
assert.strictEqual(stdout, testCVEWarningMessage); | ||
assert.match(stderr, invalidCVERegExp); | ||
} | ||
|
||
// The command-line argument should take precedence over the environment variable | ||
// and is never inherited by child processes. | ||
assert.deepStrictEqual(expectSuccess({ | ||
[method]: invalidCVE, | ||
args: ['--security-revert', testCVE], | ||
}), [ | ||
[testCVEWarningMessage], | ||
[null, [[testProcKey, true]], ['undefined']], | ||
]); | ||
} | ||
|
||
// The environment variable should take precedence over a dotenv file. | ||
assert.deepStrictEqual(expectSuccess({ | ||
env: testCVE, | ||
dotenv: invalidCVE | ||
}), [ | ||
[testCVEWarningMessage], | ||
[null, [[testProcKey, true]], ['undefined']], | ||
]); | ||
|
||
// We don't want security reverts to be inherited implicitly, thus, neither | ||
// --security-revert nor --env-file should be allowed in NODE_OPTIONS. | ||
for (const NODE_OPTIONS of [ | ||
`--env-file=${tmpdir.resolve('.env')}`, | ||
`--security-revert=${testCVE}`, | ||
]) { | ||
const [stdout, stderr] = expectError({ | ||
otherEnv: { NODE_OPTIONS } | ||
}); | ||
assert.strictEqual(stdout, ''); | ||
const optPrefix = NODE_OPTIONS.substring(0, NODE_OPTIONS.indexOf('=') + 1); | ||
assert.match(stderr, new RegExp(`^.+: ${optPrefix} is not allowed in NODE_OPTIONS$`)); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we need to make them exclusive. I think it is already possible to revert a CVE twice from the command line so not sure we need to only allow one or the other?