Skip to content

Commit

Permalink
More CC-related issues
Browse files Browse the repository at this point in the history
  • Loading branch information
corrideat committed Oct 28, 2023
1 parent 1a0598b commit b0078d5
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/untrusted/impl/nodejs/nodejsSandboxVm.inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const {

// Needed for CC
const exit = 'exit';
const env = 'env';
const NODE_ENV = 'NODE_ENV';

const close = process[exit].bind(process, 0);
const getRandomValues = globalThis.crypto.getRandomValues.bind(crypto);
Expand Down Expand Up @@ -246,7 +248,7 @@ const nodejsSandbox: TNodejsSandbox = (
]),
);

vm.createContext(
const result = vm.createContext(
context,
g_Object.fromEntries([
[
Expand All @@ -259,6 +261,10 @@ const nodejsSandbox: TNodejsSandbox = (
]),
);

if (!result) {
throw new Error('Unable to create context');
}

if (__buildtimeSettings__.contextifyMessagePort) {
if (__buildtimeSettings__.contextifyMessagePortWorkaroundCrash) {
const messageChannel = new MessageChannel();
Expand Down Expand Up @@ -293,7 +299,7 @@ const nodejsSandbox: TNodejsSandbox = (
}
}

const displayErrors = process.env['NODE_ENV'] !== 'production';
const displayErrors = process[env][NODE_ENV] !== 'production';

// Remove properties from most built-in modules. This should somewhat
// limit the access to system resources in case of an escape
Expand Down Expand Up @@ -331,7 +337,7 @@ const nodejsSandbox: TNodejsSandbox = (
removeAllProperties(require('node:' + v));
});
// Prevent loading CJS modules
Object.defineProperty(global.module.constructor, '_load', {
g_Object.defineProperty(global.module.constructor, '_load', {
['set']: () => {},
});
// Remove references in global and process to prevent calling require, etc.
Expand All @@ -353,7 +359,7 @@ const nodejsSandbox: TNodejsSandbox = (
return vm.compileFunction(
s,
undefined,
Object.fromEntries([
g_Object.fromEntries([
['filename', sandboxId + '-usertext.vm.js'],
['parsingContext', context],
]),
Expand Down

0 comments on commit b0078d5

Please sign in to comment.