-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
src: allow --disallow-code-generation-from-strings in workers #60549
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -565,11 +565,14 @@ NODE_EXTERN v8::Isolate* NewIsolate( | |
| NODE_EXTERN v8::Local<v8::Context> NewContext( | ||
| v8::Isolate* isolate, | ||
| v8::Local<v8::ObjectTemplate> object_template = | ||
| v8::Local<v8::ObjectTemplate>()); | ||
| v8::Local<v8::ObjectTemplate>(), | ||
| IsolateData* isolate_data = nullptr); | ||
|
|
||
| // Runs Node.js-specific tweaks on an already constructed context | ||
| // Return value indicates success of operation | ||
| NODE_EXTERN v8::Maybe<bool> InitializeContext(v8::Local<v8::Context> context); | ||
| NODE_EXTERN v8::Maybe<bool> InitializeContext( | ||
| v8::Local<v8::Context> context, | ||
| IsolateData* isolate_data = nullptr); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jasnell I think this likely incorrect. Can you recommend a different implementation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mcollina You can look up the current There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @addaleax Unfortunately, The call flow for workers is:
|
||
|
|
||
| // If `platform` is passed, it will be used to register new Worker instances. | ||
| // It can be `nullptr`, in which case creating new Workers inside of | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| 'use strict'; | ||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const { Worker } = require('worker_threads'); | ||
|
|
||
| // Test that --disallow-code-generation-from-strings can be passed to workers | ||
| // and properly blocks eval() and related code generation functions. | ||
|
|
||
| // Test 1: Worker with --disallow-code-generation-from-strings should block eval | ||
| { | ||
| const worker = new Worker(` | ||
| const { parentPort } = require('worker_threads'); | ||
| try { | ||
| eval('"test"'); | ||
| parentPort.postMessage({ evalBlocked: false }); | ||
| } catch (err) { | ||
| parentPort.postMessage({ evalBlocked: true, errorName: err.name }); | ||
| } | ||
| `, { | ||
| eval: true, | ||
| execArgv: ['--disallow-code-generation-from-strings'] | ||
| }); | ||
|
|
||
| worker.on('message', common.mustCall((msg) => { | ||
| assert.strictEqual(msg.evalBlocked, true); | ||
| assert.strictEqual(msg.errorName, 'EvalError'); | ||
| })); | ||
|
|
||
| worker.on('error', common.mustNotCall()); | ||
| } | ||
|
|
||
| // Test 2: Worker without the flag should allow eval | ||
| { | ||
| const worker = new Worker(` | ||
| const { parentPort } = require('worker_threads'); | ||
| try { | ||
| const result = eval('"test"'); | ||
| parentPort.postMessage({ evalBlocked: false, result }); | ||
| } catch (err) { | ||
| parentPort.postMessage({ evalBlocked: true }); | ||
| } | ||
| `, { | ||
| eval: true, | ||
| execArgv: [] | ||
| }); | ||
|
|
||
| worker.on('message', common.mustCall((msg) => { | ||
| assert.strictEqual(msg.evalBlocked, false); | ||
| assert.strictEqual(msg.result, 'test'); | ||
| })); | ||
|
|
||
| worker.on('error', common.mustNotCall()); | ||
| } | ||
|
|
||
| // Test 3: Verify the flag also blocks Function constructor | ||
| { | ||
| const worker = new Worker(` | ||
| const { parentPort } = require('worker_threads'); | ||
| try { | ||
| new Function('return 42')(); | ||
| parentPort.postMessage({ functionBlocked: false }); | ||
| } catch (err) { | ||
| parentPort.postMessage({ functionBlocked: true, errorName: err.name }); | ||
| } | ||
| `, { | ||
| eval: true, | ||
| execArgv: ['--disallow-code-generation-from-strings'] | ||
| }); | ||
|
|
||
| worker.on('message', common.mustCall((msg) => { | ||
| assert.strictEqual(msg.functionBlocked, true); | ||
| assert.strictEqual(msg.errorName, 'EvalError'); | ||
| })); | ||
|
|
||
| worker.on('error', common.mustNotCall()); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
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.
The
V8Option{}is removed innode_options.ccfor--disallow-code-generation-from-strings, this will always betrue. Because the flag is not set in V8.