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 99977c5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 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
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 99977c5

Please sign in to comment.