Skip to content

Commit 902c8ec

Browse files
committed
src: disable eval() by default and add --enable-eval support
Modify context initialization to set kAllowCodeGenerationFromStrings to false by default in both snapshot and runtime. Update ModifyCodeGenerationFromStrings callback to strictly respect the embedder data, effectively disabling eval() and new Function() unless the --enable-eval flag is provided.
1 parent 1c01440 commit 902c8ec

2 files changed

Lines changed: 4 additions & 4 deletions

File tree

src/api/environment.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,8 @@ Maybe<void> InitializeContextRuntime(Local<Context> context) {
822822
// The `IsCodeGenerationFromStringsAllowed` can be refreshed by V8 according
823823
// to the runtime flags, propagate the value to the embedder data.
824824
bool is_code_generation_from_strings_allowed =
825-
context->IsCodeGenerationFromStringsAllowed();
825+
context->IsCodeGenerationFromStringsAllowed() &&
826+
per_process::cli_options->per_isolate->enable_eval;
826827
context->AllowCodeGenerationFromStrings(false);
827828
context->SetEmbedderData(
828829
ContextEmbedderIndex::kAllowCodeGenerationFromStrings,
@@ -923,7 +924,7 @@ Maybe<void> InitializeMainContextForSnapshot(Local<Context> context) {
923924
context->SetEmbedderData(ContextEmbedderIndex::kAllowWasmCodeGeneration,
924925
True(isolate));
925926
context->SetEmbedderData(
926-
ContextEmbedderIndex::kAllowCodeGenerationFromStrings, True(isolate));
927+
ContextEmbedderIndex::kAllowCodeGenerationFromStrings, False(isolate));
927928

928929
if (InitializeBaseContextForSnapshot(context).IsNothing()) {
929930
return Nothing<void>();

src/node_errors.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,8 +659,7 @@ v8::ModifyCodeGenerationFromStringsResult ModifyCodeGenerationFromStrings(
659659

660660
Local<Value> allow_code_gen = context->GetEmbedderData(
661661
ContextEmbedderIndex::kAllowCodeGenerationFromStrings);
662-
bool codegen_allowed =
663-
allow_code_gen->IsUndefined() || allow_code_gen->IsTrue();
662+
bool codegen_allowed = allow_code_gen->IsTrue();
664663
return {
665664
codegen_allowed,
666665
{},

0 commit comments

Comments
 (0)