Skip to content

Commit 3d2a230

Browse files
committed
fix: ensure replacements of globals can't be bypassed
1 parent bd421ff commit 3d2a230

File tree

5 files changed

+27
-8
lines changed

5 files changed

+27
-8
lines changed

packages/SwingSet/src/kernel/liveSlots.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const DEFAULT_VIRTUAL_OBJECT_CACHE_SIZE = 3; // XXX ridiculously small value to
2727
* @param {*} vatParameters
2828
* @param {*} gcTools { WeakRef, FinalizationRegistry, waitUntilQuiescent }
2929
* @param {Console} console
30-
* @returns {*} { vatGlobals, inescapableGlobalLexicals, dispatch, setBuildRootObject }
30+
* @returns {*} { vatGlobals, inescapableGlobalProperties, dispatch, setBuildRootObject }
3131
*
3232
* setBuildRootObject should be called, once, with a function that will
3333
* create a root object for the new vat The caller provided buildRootObject
@@ -803,7 +803,7 @@ function build(
803803
makeKind,
804804
});
805805

806-
const inescapableGlobalLexicals = harden({
806+
const inescapableGlobalProperties = harden({
807807
WeakMap: RepairedWeakMap,
808808
WeakSet: RepairedWeakSet,
809809
});
@@ -901,7 +901,7 @@ function build(
901901
// we return 'deadSet' for unit tests
902902
return harden({
903903
vatGlobals,
904-
inescapableGlobalLexicals,
904+
inescapableGlobalProperties,
905905
setBuildRootObject,
906906
dispatch,
907907
m,
@@ -921,7 +921,7 @@ function build(
921921
* @param {boolean} enableDisavow
922922
* @param {*} gcTools { WeakRef, FinalizationRegistry, waitUntilQuiescent }
923923
* @param {Console} [liveSlotsConsole]
924-
* @returns {*} { vatGlobals, inescapableGlobalLexicals, dispatch, setBuildRootObject }
924+
* @returns {*} { vatGlobals, inescapableGlobalProperties, dispatch, setBuildRootObject }
925925
*
926926
* setBuildRootObject should be called, once, with a function that will
927927
* create a root object for the new vat The caller provided buildRootObject
@@ -971,14 +971,14 @@ export function makeLiveSlots(
971971
);
972972
const {
973973
vatGlobals,
974-
inescapableGlobalLexicals,
974+
inescapableGlobalProperties,
975975
dispatch,
976976
setBuildRootObject,
977977
deadSet,
978978
} = r; // omit 'm'
979979
return harden({
980980
vatGlobals,
981-
inescapableGlobalLexicals,
981+
inescapableGlobalProperties,
982982
dispatch,
983983
setBuildRootObject,
984984
deadSet,

packages/SwingSet/src/kernel/vatManager/manager-local.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ export function makeLocalVatManagerFactory(tools) {
129129
assert,
130130
});
131131
const inescapableTransforms = [];
132-
const inescapableGlobalLexicals = { ...ls.inescapableGlobalLexicals };
132+
const inescapableGlobalProperties = { ...ls.inescapableGlobalProperties };
133+
const inescapableGlobalLexicals = {};
133134
if (metered) {
134135
const getMeter = meterRecord.getMeter;
135136
inescapableTransforms.push(src => transformMetering(src, getMeter));
@@ -141,6 +142,7 @@ export function makeLocalVatManagerFactory(tools) {
141142
endowments,
142143
inescapableTransforms,
143144
inescapableGlobalLexicals,
145+
inescapableGlobalProperties,
144146
});
145147

146148
let dispatch;

packages/import-bundle/src/compartment-wrapper.js

+10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export function wrapInescapableCompartment(
22
OldCompartment,
33
inescapableTransforms,
44
inescapableGlobalLexicals,
5+
inescapableGlobalProperties,
56
) {
67
// This is the new Compartment constructor. We name it `Compartment` so
78
// that it's .name property is correct, but we hold it in 'NewCompartment'
@@ -51,6 +52,15 @@ export function wrapInescapableCompartment(
5152
// there are details to work out.
5253
c.globalThis.Compartment = NewCompartment;
5354

55+
for (const prop of Object.keys(inescapableGlobalProperties)) {
56+
Object.defineProperty(c.globalThis, prop, {
57+
value: inescapableGlobalProperties[prop],
58+
writable: true,
59+
enumerable: false,
60+
configurable: true,
61+
});
62+
}
63+
5464
return c;
5565
};
5666

packages/import-bundle/src/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export async function importBundle(bundle, options = {}) {
1616
transforms = [],
1717
inescapableTransforms = [],
1818
inescapableGlobalLexicals = {},
19+
inescapableGlobalProperties = {},
1920
} = options;
2021
const endowments = {
2122
TextEncoder,
@@ -26,12 +27,14 @@ export async function importBundle(bundle, options = {}) {
2627
let CompartmentToUse = Compartment;
2728
if (
2829
inescapableTransforms.length ||
29-
Object.keys(inescapableGlobalLexicals).length
30+
Object.keys(inescapableGlobalLexicals).length ||
31+
Object.keys(inescapableGlobalProperties).length
3032
) {
3133
CompartmentToUse = wrapInescapableCompartment(
3234
Compartment,
3335
inescapableTransforms,
3436
inescapableGlobalLexicals,
37+
inescapableGlobalProperties,
3538
);
3639
}
3740

packages/import-bundle/test/test-compartment-wrapper.js

+4
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ function check(t, c, odometer, n) {
121121
{ message: /Not available/ },
122122
`${n} .constructor is tamed`,
123123
);
124+
125+
t.is(c.evaluate('WeakMap'), 'replaced');
124126
}
125127

126128
test('wrap', t => {
@@ -131,10 +133,12 @@ test('wrap', t => {
131133

132134
const inescapableTransforms = [milageTransform];
133135
const inescapableGlobalLexicals = { getOdometer };
136+
const inescapableGlobalProperties = { WeakMap: 'replaced' };
134137
const WrappedCompartment = wrapInescapableCompartment(
135138
Compartment,
136139
inescapableTransforms,
137140
inescapableGlobalLexicals,
141+
inescapableGlobalProperties,
138142
);
139143
const endowments = { console };
140144
const c1 = new WrappedCompartment(endowments);

0 commit comments

Comments
 (0)