Skip to content

Commit

Permalink
feat!: Remove support for globalLexicals (merge #1343)
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal authored Oct 28, 2022
2 parents b0b2fe7 + 3b90d5d commit 6df2e00
Show file tree
Hide file tree
Showing 31 changed files with 178 additions and 853 deletions.
4 changes: 4 additions & 0 deletions packages/compartment-mapper/NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
User-visible changes to the compartment mapper:

# Next release

- Removes support for `globalLexicals`.

# 0.7.7 (2022-06-28)

- Adds `require.resolve` support and provides its implementation from
Expand Down
11 changes: 2 additions & 9 deletions packages/compartment-mapper/src/import-archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,8 @@ export const parseArchive = async (

/** @type {ExecuteFn} */
const execute = options => {
const {
globals,
globalLexicals,
modules,
transforms,
__shimTransforms__,
Compartment,
} = options || {};
const { globals, modules, transforms, __shimTransforms__, Compartment } =
options || {};
const makeImportHook = makeArchiveImportHookMaker(
get,
compartments,
Expand All @@ -309,7 +303,6 @@ export const parseArchive = async (
makeImportHook,
parserForLanguage,
globals,
globalLexicals,
modules,
transforms,
__shimTransforms__,
Expand Down
2 changes: 0 additions & 2 deletions packages/compartment-mapper/src/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export const loadLocation = async (readPowers, moduleLocation, options) => {
const execute = async (options = {}) => {
const {
globals,
globalLexicals,
modules,
transforms,
__shimTransforms__,
Expand All @@ -78,7 +77,6 @@ export const loadLocation = async (readPowers, moduleLocation, options) => {
makeImportHook,
parserForLanguage,
globals,
globalLexicals,
modules,
transforms,
moduleTransforms,
Expand Down
2 changes: 0 additions & 2 deletions packages/compartment-mapper/src/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,6 @@ export const link = (
makeImportHook,
parserForLanguage,
globals = {},
globalLexicals = {},
transforms = [],
moduleTransforms = {},
__shimTransforms__ = [],
Expand Down Expand Up @@ -401,7 +400,6 @@ export const link = (
moduleMapHook,
transforms,
__shimTransforms__,
globalLexicals,
name: location,
});

Expand Down
1 change: 0 additions & 1 deletion packages/compartment-mapper/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ export {};
/**
* @typedef {Object} ExecuteOptions
* @property {Object} [globals]
* @property {Object} [globalLexicals]
* @property {Array<Transform>} [transforms]
* @property {Array<Transform>} [__shimTransforms__]
* @property {Record<string, Object>} [modules]
Expand Down
Binary file modified packages/compartment-mapper/test/app.agar
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 1 addition & 13 deletions packages/compartment-mapper/test/scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,10 @@ export const readPowers = makeReadPowers({ fs, crypto, url });
const globals = {
// process: { _rawDebug: process._rawDebug }, // useful for debugging
globalProperty: 42,
globalLexical: 'global', // should be overshadowed
TextEncoder,
TextDecoder,
};

const globalLexicals = {
globalLexical: 'globalLexical',
};

let modules;

const builtinLocation = new URL(
Expand All @@ -49,7 +44,7 @@ export async function setup() {
builtin: namespace,
};
}
return { modules, globals, globalLexicals };
return { modules, globals };
}

export function scaffold(
Expand All @@ -75,7 +70,6 @@ export function scaffold(
return assertFixture(t, {
namespace,
globals,
globalLexicals,
testCategoryHint,
});
});
Expand All @@ -90,7 +84,6 @@ export function scaffold(
});
const { namespace } = await application.import({
globals,
globalLexicals,
modules,
Compartment,
});
Expand All @@ -103,7 +96,6 @@ export function scaffold(

const { namespace } = await importLocation(readPowers, fixture, {
globals,
globalLexicals,
modules,
Compartment,
dev: true,
Expand Down Expand Up @@ -132,7 +124,6 @@ export function scaffold(
});
const { namespace } = await application.import({
globals,
globalLexicals,
modules,
Compartment,
});
Expand All @@ -159,7 +150,6 @@ export function scaffold(
});
const { namespace } = await application.import({
globals,
globalLexicals,
modules,
Compartment,
});
Expand Down Expand Up @@ -192,7 +182,6 @@ export function scaffold(
});
const { namespace } = await application.import({
globals,
globalLexicals,
modules,
Compartment,
});
Expand Down Expand Up @@ -220,7 +209,6 @@ export function scaffold(
});
const { namespace } = await importArchive(fakeRead, 'app.agar', {
globals,
globalLexicals,
modules,
Compartment,
});
Expand Down
24 changes: 6 additions & 18 deletions packages/compartment-mapper/test/test-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@ const fixture = new URL(
).toString();
const archiveFixture = new URL('app.agar', import.meta.url).toString();

const assertFixture = (
t,
{ namespace, globals, globalLexicals, testCategoryHint },
) => {
const assertFixture = (t, { namespace, globals, testCategoryHint }) => {
const {
avery,
brooke,
clarke,
danny,
builtin,
receivedGlobalProperty,
receivedGlobalLexical,
typecommon,
typemodule,
typemoduleImplied,
Expand All @@ -43,11 +39,6 @@ const assertFixture = (
t.is(builtin, 'builtin', 'exports builtin');

t.is(receivedGlobalProperty, globals.globalProperty, 'exports global');
t.is(
receivedGlobalLexical,
globalLexicals.globalLexical,
'exports global lexical',
);
t.deepEqual(
typecommon,
[42, 42, 42, 42, 42],
Expand All @@ -70,7 +61,7 @@ const assertFixture = (
);
};

const fixtureAssertionCount = 12;
const fixtureAssertionCount = 11;

scaffold('fixture-0', test, fixture, assertFixture, fixtureAssertionCount);

Expand All @@ -82,19 +73,18 @@ test/app.agar) or the test fixture and corresponding assertions have changed.
In the latter case, running node test/app.agar-make.js will sync the fixture
with the current test fixture.`);
t.plan(fixtureAssertionCount);
const { globals, globalLexicals, modules } = await setup();
const { globals, modules } = await setup();

const { namespace } = await importArchive(readPowers.read, archiveFixture, {
globals,
globalLexicals,
modules,
Compartment,
});
assertFixture(t, { namespace, globals, globalLexicals });
assertFixture(t, { namespace, globals });
});

test('no dev dependencies', async t => {
const { globals, globalLexicals, modules } = await setup();
const { globals, modules } = await setup();

await t.throwsAsync(
async () => {
Expand All @@ -103,7 +93,6 @@ test('no dev dependencies', async t => {
});
await application.import({
globals,
globalLexicals,
modules,
Compartment,
});
Expand All @@ -116,7 +105,7 @@ test('no dev dependencies', async t => {
});

test('no transitive dev dependencies', async t => {
const { globals, globalLexicals, modules } = await setup();
const { globals, modules } = await setup();

const noTransitiveDevDepencenciesFixture = new URL(
'fixtures-no-trans-dev-deps/node_modules/app/index.js',
Expand All @@ -133,7 +122,6 @@ test('no transitive dev dependencies', async t => {
);
await application.import({
globals,
globalLexicals,
modules,
Compartment,
});
Expand Down
12 changes: 1 addition & 11 deletions packages/import-bundle/src/compartment-wrapper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export function wrapInescapableCompartment(
OldCompartment,
inescapableTransforms,
inescapableGlobalLexicals,
inescapableGlobalProperties,
) {
// This is the new Compartment constructor. We name it `Compartment` so
Expand All @@ -12,20 +11,11 @@ export function wrapInescapableCompartment(
modules,
oldOptions = {},
) {
const {
transforms: oldTransforms = [],
globalLexicals: oldGlobalLexicals = {},
...otherOptions
} = oldOptions;
const { transforms: oldTransforms = [], ...otherOptions } = oldOptions;
const newTransforms = [...oldTransforms, ...inescapableTransforms];
const newGlobalLexicals = {
...oldGlobalLexicals,
...inescapableGlobalLexicals,
};
const newOptions = {
...otherOptions,
transforms: newTransforms,
globalLexicals: newGlobalLexicals,
};

// The real Compartment is defined as a class, so 'new Compartment()'
Expand Down
63 changes: 14 additions & 49 deletions packages/import-bundle/src/compartment-wrapper.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ Impose inescapable options on a `Compartment`.

[compartment][Compartments] provide a way to evaluate ECMAScript code against a specific global object, which can be used to limit that code's authority by simply removing access to host-provided IO properties like `Request`.

Compartments can also apply a transform to all the code they evaluate, and provide additional global lexicals to its environment, which can futher control its behavior. For example, the transform might inject a "metering check" into the beginning of each block, which decrements a counter and throws an exception if it underflows. This can be used to prevent the confined code from consuming excessive CPU.

The Compartment's configured transform will be applied to any code evaluated through external calls to `c.evaluate(code)` and `c.import(module)`. It will also be applied to internal evaluations via `eval(code)` and `new Function(code)`. However, the transforms and other options are not automatically propagated to new child Compartments.
Compartments can also apply a transform or add globals to all the code they evaluate which can futher control its behavior. Some of those transforms or globals may need to be inescapable, such that any child compartment (transitively) will have these transforms and globals. Particularly, to ensure that a Compartment and its transitive child Compartments all have a `WeakMap` or `WeakSet` that tracks the content of all guest `Weak*` collections, these endowments must be "inescapable".

To prevent code from escaping a transform by evaluating its code in a new child `Compartment`, the creator of the confined compartment must replace its `Compartment` constructor with a wrapped version. The wrapper will modify the arguments to include the transforms (and other options). It must merge the provided options with the imposed ones in the right order, to ensure they cannot be overridden (i.e. the imposed transforms must appear at the *end* of the list). Finally, it must also propogate the wrapper itself to the new child Compartment, by modifying `c.thisGlobal.Compartment` on each newly created compartment.

Expand All @@ -18,64 +16,31 @@ This module provides a function to create a `Compartment` constructor that enfor
```js
import { wrapInescapableCompartment } from '.../compartment-wrapper';

// Allow oldSrc to increment the odometer, but not read it. SES offers much
// easier ways to do this, of course.

function milageTransform(oldSrc) {
if (oldSrc.indexOf('getOdometer') !== -1) {
throw Error(`forbidden access to 'getOdometer' in oldSrc`);
}
return oldSrc.replace(/addMilage\(\)/g, 'getOdometer().add(1)');
}
const { WrappedWeakMap, WrappedWeakSet } = wrapWeakCollections();

// add a lexical that the original code cannot reach
function makeOdometer() {
let counter = 0;
function add(count) {
counter += count;
}
function read() {
return counter;
}
return { add, read };
}
const odometer = makeOdometer();
function getOdometer() {
return odometer;
}
const inescapableGlobalProperties = {
WeakMap: WrappedWeakMap,
WeakSet: WrappedWeakSet,
};
const WrappedCompartment = wrapInescapableCompartment(
Compartment,
inescapableGlobalProperties,
);

const inescapableTransforms = [milageTransform];
const inescapableGlobalLexicals = { getOdometer };
const WrappedCompartment = wrapInescapableCompartment(Compartment,
inescapableTransforms,
inescapableGlobalLexicals);
const endowments = {};
const modules = {};
const options = {};
const c = new WrappedCompartment(endowments, modules, options);
c.evaluate(confinedCode);
// c.import(confinedModule);
```

If `confinedCode` creates its own Compartment with something like:
If `confinedCode` creates its own `Compartment`, in which `WeakMap` will
still produced wrapped `WeakMap` instances.

```js
const c2 = new Compartment(endowments, modules, { transforms, globalLexicals });
const c2 = new Compartment();
c2.evaluate(`new WeakMap()`);
```

then its Compartment constructor will actually be invoked like this:

```js
new Compartment(endowments, modules,
{
transforms: [...transforms, milageTransform],
globalLexicals: { ...globalLexicals, getOdometer },
});
```

except that it could not make such an invocation itself, because it won't have access to `milageTransform` (which doesn't appear in the globals or global lexicals), or `getOdometer` (which *does* appear in the global lexicals, but the transform ensures that it cannot be reached by the pre-transformed `confinedCode`).



[Compartments]: ../../ses/README.md#compartment
[SES]: ../../ses/README.md
Loading

0 comments on commit 6df2e00

Please sign in to comment.