Skip to content

Commit

Permalink
fixup!
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed Mar 20, 2023
1 parent e760ab7 commit 86fd9e6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
14 changes: 6 additions & 8 deletions src/api/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,13 @@ MaybeLocal<Value> PrepareStackTraceCallback(Local<Context> context,
if (env == nullptr) {
return exception->ToString(context).FromMaybe(Local<Value>());
}
Realm* current_realm = Realm::GetCurrent(context);
Realm* realm = Realm::GetCurrent(context);
Local<Function> prepare;
if (current_realm != nullptr &&
current_realm->kind() != Realm::Kind::kPrincipal) {
// If we are in a Realm that is not the principal Realm (e.g. ShadowRealm),
// call the realm specific prepareStackTrace callback to avoid passing the
// JS objects (the exception and trace) across the realm boundary with the
// `Error.prepareStackTrace` override.
prepare = current_realm->prepare_stack_trace_callback();
if (realm != nullptr) {
// If we are in a Realm, call the realm specific prepareStackTrace callback
// to avoid passing the JS objects (the exception and trace) across the
// realm boundary with the `Error.prepareStackTrace` override.
prepare = realm->prepare_stack_trace_callback();
} else {
// The context is created with ContextifyContext, call the principal
// realm's prepareStackTrace callback.
Expand Down
1 change: 0 additions & 1 deletion src/node_shadow_realm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
namespace node {
namespace shadow_realm {
using v8::Context;
using v8::EscapableHandleScope;
using v8::HandleScope;
using v8::Local;
using v8::MaybeLocal;
Expand Down
12 changes: 10 additions & 2 deletions test/parallel/test-shadow-realm-prepare-stack-trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@
require('../common');
const assert = require('assert');

let principalRealmPrepareStackTraceCalled = false;
Error.prepareStackTrace = (error, trace) => {
principalRealmPrepareStackTraceCalled = true;
return `${String(error)}\n at ${trace.join('\n at ')}`;
};

{
// Validates inner Error.prepareStackTrace can not leak into the outer realm.
const shadowRealm = new ShadowRealm();

const stack = shadowRealm.evaluate(`
Error.prepareStackTrace = (error, trace) => {
globalThis.leaked = 'inner';
return String(error);
return 'from shadow realm';
};
try {
Expand All @@ -20,7 +26,8 @@ try {
e.stack;
}
`);
assert.strictEqual(stack, 'Error: boom');
assert.ok(!principalRealmPrepareStackTraceCalled);
assert.strictEqual(stack, 'from shadow realm');
assert.strictEqual('leaked' in globalThis, false);
}

Expand All @@ -39,6 +46,7 @@ try {
e.stack;
}
`);
assert.ok(!principalRealmPrepareStackTraceCalled);
const lines = stack.split('\n');
assert.strictEqual(lines[0], 'Error: boom');
assert.match(lines[1], /^ {4}at myFunc \(.*\)/);
Expand Down

0 comments on commit 86fd9e6

Please sign in to comment.