From 949b23e8318f768e6e0e2e528ac1ed66ebc8da4f Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Tue, 2 Jan 2024 16:18:17 -0500 Subject: [PATCH] fix(ses): Support an incomplete shimmed globalEnv.process Fixes #1917 --- packages/ses/src/error/tame-console.js | 24 ++++++++++++------- .../ses/test/test-lockdown-shimmed-process.js | 22 +++++++++++++++++ 2 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 packages/ses/test/test-lockdown-shimmed-process.js diff --git a/packages/ses/src/error/tame-console.js b/packages/ses/src/error/tame-console.js index 000f08207f..f240044404 100644 --- a/packages/ses/src/error/tame-console.js +++ b/packages/ses/src/error/tame-console.js @@ -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. @@ -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); } } diff --git a/packages/ses/test/test-lockdown-shimmed-process.js b/packages/ses/test/test-lockdown-shimmed-process.js new file mode 100644 index 0000000000..2465043e9d --- /dev/null +++ b/packages/ses/test/test-lockdown-shimmed-process.js @@ -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); +});