From 184fc2dd827940b4ef72d77bf77b837192efe37b Mon Sep 17 00:00:00 2001 From: Feng Yu Date: Tue, 9 May 2023 12:12:46 -0700 Subject: [PATCH] vm: fix crash when setting __proto__ on context's globalThis --- src/node_contextify.cc | 12 +++++++----- .../test-vm-set-proto-null-on-globalthis.js | 13 +++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 test/parallel/test-vm-set-proto-null-on-globalthis.js diff --git a/src/node_contextify.cc b/src/node_contextify.cc index a21acf06a32781..09cecf8bd8d19b 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -529,11 +529,13 @@ void ContextifyContext::PropertySetterCallback( if (ctx->sandbox()->Set(context, property, value).IsNothing()) return; - Local desc; - if (is_declared_on_sandbox && - ctx->sandbox() - ->GetOwnPropertyDescriptor(context, property) - .ToLocal(&desc)) { + if (is_declared_on_sandbox) { + Local desc; + bool success = ctx->sandbox() + ->GetOwnPropertyDescriptor(context, property) + .ToLocal(&desc); + if (!success || desc->IsUndefined()) return; + Environment* env = Environment::GetCurrent(context); Local desc_obj = desc.As(); diff --git a/test/parallel/test-vm-set-proto-null-on-globalthis.js b/test/parallel/test-vm-set-proto-null-on-globalthis.js new file mode 100644 index 00000000000000..38668ea83294ff --- /dev/null +++ b/test/parallel/test-vm-set-proto-null-on-globalthis.js @@ -0,0 +1,13 @@ +'use strict'; +require('../common'); + +// Setting __proto__ on vm context's globalThis should not causes a crash +// Regression test for https://github.com/nodejs/node/issues/47798 + +const vm = require('vm'); +const context = vm.createContext(); + +const contextGlobalThis = vm.runInContext('this', context); + +// Should not crash. +contextGlobalThis.__proto__ = null; // eslint-disable-line no-proto