Skip to content

Commit

Permalink
Bare sandbox improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
corrideat committed Sep 25, 2023
1 parent 2241187 commit f1c985f
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 23 deletions.
3 changes: 3 additions & 0 deletions src/exports/bare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@
*/

export { default } from '~trusted/impl/bare/bareSandbox.js';
// TODO: Fix issue with these exports in Google Closure Compiler
// export { default as freezePrototypes } from '~untrusted/lib/freezePrototypes.js';
// export { default as hardenGlobals } from '~untrusted/lib/hardenGlobals.js';
10 changes: 6 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

export { default as bareSandbox } from './trusted/impl/bare/bareSandbox.js';
export { default as browserSandbox } from './trusted/impl/browser/browserSandbox.js';
export { default as nodejsSandbox } from './trusted/impl/nodejs/nodejsSandbox.js';
export { default as workerSandbox } from './trusted/impl/worker/workerSandbox.js';
export { default as bareSandbox } from '~trusted/impl/bare/bareSandbox.js';
export { default as browserSandbox } from '~trusted/impl/browser/browserSandbox.js';
export { default as nodejsSandbox } from '~trusted/impl/nodejs/nodejsSandbox.js';
export { default as workerSandbox } from '~trusted/impl/worker/workerSandbox.js';
export { default as freezePrototypes } from '~untrusted/lib/freezePrototypes.js';
export { default as hardenGlobals } from '~untrusted/lib/hardenGlobals.js';
7 changes: 0 additions & 7 deletions src/trusted/impl/bare/bareSandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,10 @@

import { ISandbox } from '~/types/index.js';
import bareSandboxManager from '~/untrusted/impl/bare/bareSandboxManager.js';
import hardenGlobals from '~/untrusted/lib/hardenGlobals.js';
import freezePrototypes from '~/untrusted/lib/freezePrototypes.js';
import setupSandboxListeners from '~trusted/lib/setupSandboxListeners.js';
import createErrorEventListenerFactory from '~untrusted/lib/createErrorEventEventListenerFactory.js';
import createMessageEventListenerFactory from '~untrusted/lib/createMessageEventListenerFactory.js';

/*
TODO: These should be exported or called upon creating bareSandbox.
TODO: Fix issues with Node.js and freezePrototypes */
hardenGlobals();
freezePrototypes();
// TODO: wrap setTimeout and clearTimeout

const bareSandbox: ISandbox = async (
Expand Down
20 changes: 14 additions & 6 deletions src/untrusted/lib/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,19 @@ const logWrapper = <T extends object>(o: T, v: keyof T) =>
? o[v]
: noop;

const trace = debugOnlyLogWrapper(console, 'trace');
const debug = debugOnlyLogWrapper(console, 'debug');
const info = debugOnlyLogWrapper(console, 'info');
const log = debugOnlyLogWrapper(console, 'log');
const warn = logWrapper(console, 'warn');
const error = logWrapper(console, 'error');
const trace = debugOnlyLogWrapper(console, 'trace') as {
(...args: unknown[]): void;
};
const debug = debugOnlyLogWrapper(console, 'debug') as {
(...args: unknown[]): void;
};
const info = debugOnlyLogWrapper(console, 'info') as {
(...args: unknown[]): void;
};
const log = debugOnlyLogWrapper(console, 'log') as {
(...args: unknown[]): void;
};
const warn = logWrapper(console, 'warn') as { (...args: unknown[]): void };
const error = logWrapper(console, 'error') as { (...args: unknown[]): void };

export { trace, debug, info, log, warn, error };
4 changes: 2 additions & 2 deletions src/untrusted/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const aForEach = <TT>(
callbackfn: { (value: TT, index: number, array: TT[]): void },
thisArg?: TAny,
): void => fnCall(l_aForEach, a, callbackfn, thisArg);
export const aFrom = globalThis['Array'].from;
export const aFrom = l_Array.from;
export const aIncludes = <TT>(
a: TT[],
searchElement: TT,
Expand All @@ -80,7 +80,7 @@ export const aIndexOf = <TT>(
): number => fnCall(l_aIndexOf, a, searchElement, fromIndexOf);
export const aJoin = <TT>(a: TT[], separator?: string | undefined): string =>
fnCall(l_aJoin, a, separator);
export const aIsArray = globalThis['Array'].isArray;
export const aIsArray = l_Array.isArray;
export const aMap = <TT, TU>(
a: TT[],
callbackfn: (value: TT, index: number, array: TT[]) => TU,
Expand Down
24 changes: 22 additions & 2 deletions test/e2e/bare/bareSandbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@

import runNodejsTests from '@test/lib/runNodejsTests.js';

import { default as m } from '@dist/exports/bare';
import * as bare from '@dist/exports/bare';

runNodejsTests('Bare', m);
// TODO: Import from '@dist/exports/bare'
import { hardenGlobals, freezePrototypes } from '@dist/index.js';

hardenGlobals();

// See <https://github.com/nodejs/node/issues/49259>
if (process.version) {
const [major, minor] = process.version
.slice(1)
.split('.', 2)
.map((n) => parseInt(n));
if (
(major === 18 && minor >= 18) ||
(major === 20 && minor >= 6) ||
major > 20
) {
freezePrototypes();
}
}

runNodejsTests('Bare', bare.default);
2 changes: 1 addition & 1 deletion test/lib/runNodejsTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const assertRejectsWith = assertRejectsWithFactory((predicate, c) =>
assert.rejects(predicate, c),
);

const runNodejsTests = (name: string, m: ISandbox, ) => {
const runNodejsTests = (name: string, m: ISandbox) => {
describe(name, () => {
describe('Can run tasks', async () => {
it(
Expand Down
5 changes: 4 additions & 1 deletion test/lib/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"lib": ["es2022", "dom"],
"outDir": "dist"
},
"references": [{ "path": "../../src/types" }, { "path": "../../src/untrusted" }],
"references": [
{ "path": "../../src/types" },
{ "path": "../../src/untrusted" }
],
"include": ["**/*", "**/*.json"]
}

0 comments on commit f1c985f

Please sign in to comment.