diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 7f9f71ba74223a..eb84d35985f769 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -530,7 +530,8 @@ void ContextifyContext::PropertySetterCallback( if (is_declared_on_sandbox && ctx->sandbox() ->GetOwnPropertyDescriptor(context, property) - .ToLocal(&desc)) { + .ToLocal(&desc) && + !desc->IsUndefined()) { 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..869124fa86d1f5 --- /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 cause 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