-
Notifications
You must be signed in to change notification settings - Fork 30.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
process: introduce codeGenerationFromString event #48295
base: main
Are you sure you want to change the base?
Conversation
The 'codeGenerationFromString' event is emitted when a call is made to `eval` or `new Function`. Co-authored-by: Thomas Watson <[email protected]>
With newer versions of v8 this doesn't seem to be needed
I know this needs to be done upstream. But while experimenting, it's easiest to do inline.
Review requested:
|
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.
Code changes themselves LGTM but I'd be interested to see the performance impact.
bool val = args[0]->BooleanValue(args.GetIsolate()); | ||
if (val) { |
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.
bool val = args[0]->BooleanValue(args.GetIsolate()); | |
if (val) { | |
if (args[0]->IsTrue()) { |
bool val = args[0]->BooleanValue(args.GetIsolate()); | ||
if (val) { | ||
context->AllowCodeGenerationFromStrings(false); | ||
isolate->SetModifyCodeGenerationFromStringsCallback(CodeGenCallback); |
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.
This would replace the ModifyCodeGenerationFromStrings
callback we already have for source maps and also override the isolate setting from embedders. I think we could simply configure a per-environment flag here to decide whether we want to call ProcessEmit
in the existing ModifyCodeGenerationFromStrings
callback.
I also noticed that AllowCodeGenerationFromStrings()
is never set to true in our contexts because we always delegate that decision to the callback, so the fast path is never taken. Not sure if that's intentional though, it seems to me that it should only be set to false when we need the callback (e.g. when source maps are enabled)? @legendecas
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.
Yeah, the callback can be installed when needed (either with the cli flag --enable-source-maps
or programmatically process.setSourceMapsEnabled(true)
). However, we'll need an additional flag to memorize if an embedder's callback is installed in SetIsolateMiscHandlers
instead so that the embedder's callback is not overridden.
In this PR I'm picking up the work from the now closed #35157. I've updated the code from #35157 to now work with the newest version of
main
.The 'codeGenerationFromString' event is emitted on
process
when a call is made toeval
orFunction
.The goal is to provide a way to listen to unsafe code generation from strings as it is not possible to monkeypatch
eval
.The event will only be emitted if there is at least one listener on it and removing all listeners on this event will result in the handler in V8 to never be called. In other words, if there is no listener on this event, there should be no performance impact on calling
eval
or theFunction
constructor.While working on the to-do's below, I'll add WIP commits to the PR which modifies the V8 code directly. These commits are ment only as a place to discuss the V8 changes that needs to land upstream, and before marking this PR as ready for review, these commits obviously needs to be removed.
Todo
SetModifyCodeGenerationFromStringsCallback
can safely call JavaScript code. Today, this is problematic, as an exception thrown inside of the JavaScript code might be swallowed. This is because the calling V8 C++ function doesn't expect the callback to call into JavaScript.codeGenerationFromString
event to be backported to older versions of Node.js.Tests
Below are a few example scripts that can be used to verify if uncaught exceptions from within the event callback are swallowed or not.
❌ This script should generate an uncaught exception, but doesn't:
✅ This script should generate an uncaught exception, and it does:
✅ This script should generate an uncaught exception, and it does:
❌ This script should generate an uncaught exception, but doesn't: