Skip to content

Commit 106dc81

Browse files
committed
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.
1 parent 798a6ed commit 106dc81

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

src/api/environment.cc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,15 @@ Maybe<bool> InitializeContextRuntime(Local<Context> context) {
556556
Isolate* isolate = context->GetIsolate();
557557
HandleScope handle_scope(isolate);
558558

559+
// Delegate the code generation validation to
560+
// node::ModifyCodeGenerationFromStrings.
561+
bool is_code_generation_from_strings_allowed =
562+
context->IsCodeGenerationFromStringsAllowed();
563+
context->AllowCodeGenerationFromStrings(false);
564+
context->SetEmbedderData(
565+
ContextEmbedderIndex::kAllowCodeGenerationFromStrings,
566+
is_code_generation_from_strings_allowed ? True(isolate) : False(isolate));
567+
559568
// Delete `Intl.v8BreakIterator`
560569
// https://github.com/nodejs/node/issues/14909
561570
{
@@ -665,9 +674,6 @@ Maybe<bool> InitializeContextForSnapshot(Local<Context> context) {
665674
Isolate* isolate = context->GetIsolate();
666675
HandleScope handle_scope(isolate);
667676

668-
context->AllowCodeGenerationFromStrings(false);
669-
context->SetEmbedderData(
670-
ContextEmbedderIndex::kAllowCodeGenerationFromStrings, True(isolate));
671677
context->SetEmbedderData(ContextEmbedderIndex::kAllowWasmCodeGeneration,
672678
True(isolate));
673679

src/node_snapshotable.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,16 @@ const SnapshotData* SnapshotBuilder::GetEmbeddedSnapshotData() {
956956
)";
957957
}
958958

959+
// Reset context settings that need to be initialized again after
960+
// deserialization.
961+
static void ResetContextSettingsBeforeSnapshot(Local<Context> context) {
962+
// Reset the AllowCodeGenerationFromStrings flag to true (default value) so
963+
// that it can be re-initialized with v8 flag
964+
// --disallow-code-generation-from-strings and recognized in
965+
// node::InitializeContextRuntime.
966+
context->AllowCodeGenerationFromStrings(true);
967+
}
968+
959969
Mutex SnapshotBuilder::snapshot_data_mutex_;
960970

961971
const std::vector<intptr_t>& SnapshotBuilder::CollectExternalReferences() {
@@ -1108,6 +1118,9 @@ int SnapshotBuilder::Generate(SnapshotData* out,
11081118
#endif
11091119
}
11101120

1121+
ResetContextSettingsBeforeSnapshot(base_context);
1122+
ResetContextSettingsBeforeSnapshot(main_context);
1123+
11111124
// Global handles to the contexts can't be disposed before the
11121125
// blob is created. So initialize all the contexts before adding them.
11131126
// TODO(joyeecheung): figure out how to remove this restriction.
Lines changed: 9 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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)