Skip to content

Commit

Permalink
fix(ses): Support an incomplete shimmed globalEnv.process
Browse files Browse the repository at this point in the history
Fixes #1917
  • Loading branch information
gibson042 committed Jan 2, 2024
1 parent 3e9e0dd commit 949b23e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
24 changes: 16 additions & 8 deletions packages/ses/src/error/tame-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,29 @@ export const tameConsole = (
/* eslint-disable @endo/no-polymorphic-call */

// Node.js
if (errorTrapping !== 'none' && globalThis.process !== undefined) {
globalThis.process.on('uncaughtException', error => {
const globalProcess = globalThis.process;
if (
errorTrapping !== 'none' &&
globalProcess &&
typeof globalProcess === 'object' &&
typeof globalProcess.on === 'function'
) {
globalProcess.on('uncaughtException', error => {
// causalConsole is born frozen so not vulnerable to method tampering.
ourConsole.error(error);
if (errorTrapping === 'platform' || errorTrapping === 'exit') {
globalThis.process.exit(globalThis.process.exitCode || -1);
globalProcess.exit(globalProcess.exitCode || -1);
} else if (errorTrapping === 'abort') {
globalThis.process.abort();
globalProcess.abort();
}
});
}

if (
unhandledRejectionTrapping !== 'none' &&
globalThis.process !== undefined
globalProcess &&
typeof globalProcess === 'object' &&
typeof globalProcess.on === 'function'
) {
const handleRejection = reason => {
// 'platform' and 'report' just log the reason.
Expand All @@ -119,9 +127,9 @@ export const tameConsole = (
const h = makeRejectionHandlers(handleRejection);
if (h) {
// Rejection handlers are supported.
globalThis.process.on('unhandledRejection', h.unhandledRejectionHandler);
globalThis.process.on('rejectionHandled', h.rejectionHandledHandler);
globalThis.process.on('exit', h.processTerminationHandler);
globalProcess.on('unhandledRejection', h.unhandledRejectionHandler);
globalProcess.on('rejectionHandled', h.rejectionHandledHandler);
globalProcess.on('exit', h.processTerminationHandler);
}
}

Expand Down
22 changes: 22 additions & 0 deletions packages/ses/test/test-lockdown-shimmed-process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* global globalThis */

import test from 'ava';
import '../index.js';

test('shimmed globalThis.process', t => {
const process = {};
Object.defineProperty(globalThis, 'process', {
value: process,
configurable: false,
writable: false,
});
t.is(globalThis.process, process);
t.is(globalThis.process.on, undefined);
lockdown({
consoleTaming: 'safe',
errorTrapping: 'platform',
unhandledRejectionTrapping: 'report',
});
t.is(globalThis.process, process);
t.is(globalThis.process.on, undefined);
});

0 comments on commit 949b23e

Please sign in to comment.