Skip to content

Commit 0f4e98b

Browse files
legendecasRafaelGSS
authored andcommitted
src: restore context default IsCodeGenerationFromStringsAllowed value
Context's default IsCodeGenerationFromStringsAllowed value can be changed by v8 flag `--disallow-code-generation-from-strings`. Restore the value at runtime when delegating the code generation validation to `node::ModifyCodeGenerationFromStrings`. The context's settings are serialized in the snapshot. Reset the setting values to its default values before the serialization so that it can be correctly re-initialized after deserialization at runtime. PR-URL: #44324 Fixes: #44287 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 3c53548 commit 0f4e98b

4 files changed

+45
-3
lines changed

src/api/environment.cc

+16-3
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,19 @@ Maybe<bool> InitializeContextRuntime(Local<Context> context) {
549549
Isolate* isolate = context->GetIsolate();
550550
HandleScope handle_scope(isolate);
551551

552+
// When `IsCodeGenerationFromStringsAllowed` is true, V8 takes the fast path
553+
// and ignores the ModifyCodeGenerationFromStrings callback. Set it to false
554+
// to delegate the code generation validation to
555+
// node::ModifyCodeGenerationFromStrings.
556+
// The `IsCodeGenerationFromStringsAllowed` can be refreshed by V8 according
557+
// to the runtime flags, propagate the value to the embedder data.
558+
bool is_code_generation_from_strings_allowed =
559+
context->IsCodeGenerationFromStringsAllowed();
560+
context->AllowCodeGenerationFromStrings(false);
561+
context->SetEmbedderData(
562+
ContextEmbedderIndex::kAllowCodeGenerationFromStrings,
563+
is_code_generation_from_strings_allowed ? True(isolate) : False(isolate));
564+
552565
if (per_process::cli_options->disable_proto == "") {
553566
return Just(true);
554567
}
@@ -641,11 +654,11 @@ Maybe<bool> InitializeMainContextForSnapshot(Local<Context> context) {
641654
Isolate* isolate = context->GetIsolate();
642655
HandleScope handle_scope(isolate);
643656

644-
context->AllowCodeGenerationFromStrings(false);
645-
context->SetEmbedderData(
646-
ContextEmbedderIndex::kAllowCodeGenerationFromStrings, True(isolate));
657+
// Initialize the default values.
647658
context->SetEmbedderData(ContextEmbedderIndex::kAllowWasmCodeGeneration,
648659
True(isolate));
660+
context->SetEmbedderData(
661+
ContextEmbedderIndex::kAllowCodeGenerationFromStrings, True(isolate));
649662

650663
if (InitializeBaseContextForSnapshot(context).IsNothing()) {
651664
return Nothing<bool>();

src/node_snapshotable.cc

+13
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,16 @@ const SnapshotData* SnapshotBuilder::GetEmbeddedSnapshotData() {
855855
)";
856856
}
857857

858+
// Reset context settings that need to be initialized again after
859+
// deserialization.
860+
static void ResetContextSettingsBeforeSnapshot(Local<Context> context) {
861+
// Reset the AllowCodeGenerationFromStrings flag to true (default value) so
862+
// that it can be re-initialized with v8 flag
863+
// --disallow-code-generation-from-strings and recognized in
864+
// node::InitializeContextRuntime.
865+
context->AllowCodeGenerationFromStrings(true);
866+
}
867+
858868
Mutex SnapshotBuilder::snapshot_data_mutex_;
859869

860870
const std::vector<intptr_t>& SnapshotBuilder::CollectExternalReferences() {
@@ -944,6 +954,7 @@ int SnapshotBuilder::Generate(SnapshotData* out,
944954
if (base_context.IsEmpty()) {
945955
return BOOTSTRAP_ERROR;
946956
}
957+
ResetContextSettingsBeforeSnapshot(base_context);
947958

948959
Local<Context> main_context = NewContext(isolate);
949960
if (main_context.IsEmpty()) {
@@ -1012,6 +1023,8 @@ int SnapshotBuilder::Generate(SnapshotData* out,
10121023
size_str.c_str());
10131024
}
10141025
#endif
1026+
1027+
ResetContextSettingsBeforeSnapshot(main_context);
10151028
}
10161029

10171030
// Global handles to the contexts can't be disposed before the
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Flags: --disallow-code-generation-from-strings
2+
'use strict';
3+
4+
require('../common');
5+
const assert = require('assert');
6+
7+
// Verify that v8 option --disallow-code-generation-from-strings is still
8+
// respected
9+
assert.throws(() => eval('"eval"'), EvalError);

test/parallel/test-eval.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
6+
// Verify that eval is allowed by default.
7+
assert.strictEqual(eval('"eval"'), 'eval');

0 commit comments

Comments
 (0)