Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sham use of sham non-trapping for cross-repo testing #2684

Draft
wants to merge 1 commit into
base: markm-sham-no-trapping
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/captp/src/captp.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const reverseSlot = slot => {
};

/**
* @typedef {object} CapTPImportExportTables
* @typedef {object} CapTPImportExportTables
* @property {(value: any) => CapTPSlot} makeSlotForValue
* @property {(slot: CapTPSlot, iface: string | undefined) => any} makeValueForSlot
* @property {(slot: CapTPSlot) => boolean} hasImport
Expand All @@ -58,12 +58,12 @@ const reverseSlot = slot => {
* @property {(slot: CapTPSlot, value: any) => void} markAsExported
* @property {(slot: CapTPSlot) => void} deleteExport
* @property {() => void} didDisconnect

*
* @typedef {object} MakeCapTPImportExportTablesOptions
* @property {boolean} gcImports
* @property {(slot: CapTPSlot) => void} releaseSlot
* @property {(slot: CapTPSlot) => RemoteKit} makeRemoteKit

*
* @param {MakeCapTPImportExportTablesOptions} options
* @returns {CapTPImportExportTables}
*/
Expand Down
18 changes: 16 additions & 2 deletions packages/marshal/src/encodeToCapData.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,22 @@ const {
entries,
fromEntries,
freeze,

// The following is commented out due to
// https://github.com/endojs/endo/issues/2094
// TODO Once fixed, comment this back in and remove the workaround
// immediately below.
//
// // https://github.com/endojs/endo/pull/2673
// // @ts-expect-error TS does not yet have this on ObjectConstructor.
// suppressTrapping = freeze,
} = Object;

// workaround for https://github.com/endojs/endo/issues/2094
// See commented out code and note immediately above.
// @ts-expect-error TS does not yet have this on ObjectConstructor.
export const suppressTrapping = Object.suppressTrapping || freeze;

/**
* Special property name that indicates an encoding that needs special
* decoding.
Expand Down Expand Up @@ -176,10 +190,10 @@ export const makeEncodeToCapData = (encodeOptions = {}) => {
// We harden the entire capData encoding before we return it.
// `encodeToCapData` requires that its input be Passable, and
// therefore hardened.
// The `freeze` here is needed anyway, because the `rest` is
// The `suppressTrapping` here is needed anyway, because the `rest` is
// freshly constructed by the `...` above, and we're using it
// as imput in another call to `encodeToCapData`.
result.rest = encodeToCapDataRecur(freeze(rest));
result.rest = encodeToCapDataRecur(suppressTrapping(rest));
}
return result;
}
Expand Down
20 changes: 19 additions & 1 deletion packages/pass-style/src/passStyle-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,24 @@ const {
getOwnPropertyDescriptor,
getPrototypeOf,
hasOwnProperty: objectHasOwnProperty,
isFrozen,
prototype: objectPrototype,
isFrozen,

// The following is commented out due to
// https://github.com/endojs/endo/issues/2094
// TODO Once fixed, comment this back in and remove the workaround
// immediately below.
//
// // https://github.com/endojs/endo/pull/2673
// // @ts-expect-error TS does not yet have this on ObjectConstructor.
// isNonTrapping = isFrozen,
} = Object;

// workaround for https://github.com/endojs/endo/issues/2094
// See commented out code and note immediately above.
// @ts-expect-error TS does not yet have this on ObjectConstructor.
export const isNonTrapping = Object.isNonTrapping || isFrozen;

const { apply } = Reflect;
const { toStringTag: toStringTagSymbol } = Symbol;

Expand Down Expand Up @@ -167,6 +182,9 @@ const makeCheckTagRecord = checkProto => {
CX(check)`A non-object cannot be a tagRecord: ${tagRecord}`)) &&
(isFrozen(tagRecord) ||
(!!check && CX(check)`A tagRecord must be frozen: ${tagRecord}`)) &&
(isNonTrapping(tagRecord) ||
(!!check &&
CX(check)`A tagRecord must be non-trapping: ${tagRecord}`)) &&
(!isArray(tagRecord) ||
(!!check && CX(check)`An array cannot be a tagRecord: ${tagRecord}`)) &&
checkPassStyle(
Expand Down
47 changes: 36 additions & 11 deletions packages/pass-style/src/passStyleOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,25 @@ import { assertPassableString } from './string.js';
/** @typedef {Exclude<PassStyle, PrimitiveStyle | "promise">} HelperPassStyle */

const { ownKeys } = Reflect;
const { isFrozen, getOwnPropertyDescriptors, values } = Object;
const {
getOwnPropertyDescriptors,
values,
isFrozen,

// The following is commented out due to
// https://github.com/endojs/endo/issues/2094
// TODO Once fixed, comment this back in and remove the workaround
// immediately below.
//
// // https://github.com/endojs/endo/pull/2673
// // @ts-expect-error TS does not yet have this on ObjectConstructor.
// isNonTrapping = isFrozen,
} = Object;

// workaround for https://github.com/endojs/endo/issues/2094
// See commented out code and note immediately above.
// @ts-expect-error TS does not yet have this on ObjectConstructor.
export const isNonTrapping = Object.isNonTrapping || isFrozen;

/**
* @param {PassStyleHelper[]} passStyleHelpers
Expand Down Expand Up @@ -143,14 +161,17 @@ const makePassStyleOf = passStyleHelpers => {
if (inner === null) {
return 'null';
}
if (!isFrozen(inner)) {
assert.fail(
// TypedArrays get special treatment in harden()
// and a corresponding special error message here.
isTypedArray(inner)
? X`Cannot pass mutable typed arrays like ${inner}.`
: X`Cannot pass non-frozen objects like ${inner}. Use harden()`,
);
if (!isNonTrapping(inner)) {
if (!isFrozen(inner)) {
throw assert.fail(
// TypedArrays get special treatment in harden()
// and a corresponding special error message here.
isTypedArray(inner)
? X`Cannot pass mutable typed arrays like ${inner}.`
: X`Cannot pass non-frozen objects like ${inner}. Use harden()`,
);
}
throw Fail`Cannot pass trapping objects like ${inner}`;
}
if (isPromise(inner)) {
assertSafePromise(inner);
Expand All @@ -177,8 +198,12 @@ const makePassStyleOf = passStyleHelpers => {
return 'remotable';
}
case 'function': {
isFrozen(inner) ||
Fail`Cannot pass non-frozen objects like ${inner}. Use harden()`;
if (!isNonTrapping(inner)) {
if (!isFrozen(inner)) {
throw Fail`Cannot pass non-frozen objects like ${inner}. Use harden()`;
}
throw Fail`Cannot pass trapping objects like ${inner}. Use harden()`;
}
typeof inner.then !== 'function' ||
Fail`Cannot pass non-promise thenables`;
remotableHelper.assertValid(inner, passStyleOfRecur);
Expand Down
27 changes: 22 additions & 5 deletions packages/pass-style/src/remotable.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,25 @@ const { ownKeys } = Reflect;
const { isArray } = Array;
const {
getPrototypeOf,
isFrozen,
prototype: objectPrototype,
getOwnPropertyDescriptors,
isFrozen,

// The following is commented out due to
// https://github.com/endojs/endo/issues/2094
// TODO Once fixed, comment this back in and remove the workaround
// immediately below.
//
// // https://github.com/endojs/endo/pull/2673
// // @ts-expect-error TS does not yet have this on ObjectConstructor.
// isNonTrapping = isFrozen,
} = Object;

// workaround for https://github.com/endojs/endo/issues/2094
// See commented out code and note immediately above.
// @ts-expect-error TS does not yet have this on ObjectConstructor.
export const isNonTrapping = Object.isNonTrapping || isFrozen;

/**
* @param {InterfaceSpec} iface
* @param {Checker} [check]
Expand Down Expand Up @@ -154,10 +168,13 @@ const checkRemotable = (val, check) => {
if (confirmedRemotables.has(val)) {
return true;
}
if (!isFrozen(val)) {
return (
!!check && CX(check)`cannot serialize non-frozen objects like ${val}`
);
if (!isNonTrapping(val)) {
if (!isFrozen(val)) {
return (
!!check && CX(check)`cannot serialize non-frozen objects like ${val}`
);
}
return !!check && CX(check)`cannot serialize trapping objects like ${val}`;
}
// eslint-disable-next-line no-use-before-define
if (!RemotableHelper.canBeValid(val, check)) {
Expand Down
24 changes: 22 additions & 2 deletions packages/pass-style/src/safe-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,26 @@ import { assertChecker, hasOwnPropertyOf, CX } from './passStyle-helpers.js';

/** @import {Checker} from './types.js' */

const { isFrozen, getPrototypeOf, getOwnPropertyDescriptor } = Object;
const {
getPrototypeOf,
getOwnPropertyDescriptor,
isFrozen,

// The following is commented out due to
// https://github.com/endojs/endo/issues/2094
// TODO Once fixed, comment this back in and remove the workaround
// immediately below.
//
// // https://github.com/endojs/endo/pull/2673
// // @ts-expect-error TS does not yet have this on ObjectConstructor.
// isNonTrapping = isFrozen,
} = Object;

// workaround for https://github.com/endojs/endo/issues/2094
// See commented out code and note immediately above.
// @ts-expect-error TS does not yet have this on ObjectConstructor.
export const isNonTrapping = Object.isNonTrapping || isFrozen;

const { ownKeys } = Reflect;
const { toStringTag } = Symbol;

Expand Down Expand Up @@ -88,7 +107,7 @@ const checkPromiseOwnKeys = (pr, check) => {
if (
typeof val === 'object' &&
val !== null &&
isFrozen(val) &&
isNonTrapping(val) &&
getPrototypeOf(val) === Object.prototype
) {
const subKeys = ownKeys(val);
Expand Down Expand Up @@ -133,6 +152,7 @@ const checkPromiseOwnKeys = (pr, check) => {
const checkSafePromise = (pr, check) => {
return (
(isFrozen(pr) || CX(check)`${pr} - Must be frozen`) &&
(isNonTrapping(pr) || CX(check)`${pr} - Must be non-trapping`) &&
(isPromise(pr) || CX(check)`${pr} - Must be a promise`) &&
(getPrototypeOf(pr) === Promise.prototype ||
CX(check)`${pr} - Must inherit from Promise.prototype: ${q(
Expand Down
6 changes: 3 additions & 3 deletions packages/pass-style/test/passStyleOf.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const harden = /** @type {import('ses').Harden & { isFake?: boolean }} */ (
global.harden
);

const { getPrototypeOf, defineProperty, freeze } = Object;
const { getPrototypeOf, defineProperty, suppressTrapping } = Object;
const { ownKeys } = Reflect;

test('passStyleOf basic success cases', t => {
Expand Down Expand Up @@ -217,7 +217,7 @@ test('passStyleOf testing remotables', t => {
*
* @type {any} UNTIL https://github.com/microsoft/TypeScript/issues/38385
*/
const farObj2 = freeze({ __proto__: tagRecord2 });
const farObj2 = suppressTrapping({ __proto__: tagRecord2 });
if (harden.isFake) {
t.is(passStyleOf(farObj2), 'remotable');
} else {
Expand Down Expand Up @@ -423,7 +423,7 @@ test('remotables - safety from the gibson042 attack', t => {
* explicitly make this non-trapping, which we cannot yet express.
* @see https://github.com/endojs/endo/blob/master/packages/ses/docs/preparing-for-stabilize.md
*/
const makeInput = () => freeze({ __proto__: mercurialProto });
const makeInput = () => suppressTrapping({ __proto__: mercurialProto });
const input1 = makeInput();
const input2 = makeInput();

Expand Down
22 changes: 22 additions & 0 deletions packages/ses/src/commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
/* global globalThis */
/* eslint-disable no-restricted-globals */

import './sham-non-trapping/non-trapping-shim.js';

// We cannot use globalThis as the local name since it would capture the
// lexical name.
const universalThis = globalThis;
Expand Down Expand Up @@ -75,8 +77,25 @@ export const {
setPrototypeOf,
values,
fromEntries,
// The following is commented out due to
// https://github.com/endojs/endo/issues/2094
// TODO Once fixed, comment this back in and remove the workaround
// immediately below.
//
// // https://github.com/endojs/endo/pull/2673
// // @ts-expect-error TS does not yet have this on ObjectConstructor.
// isNonTrapping = isFrozen,
// // @ts-expect-error TS does not yet have this on ObjectConstructor.
// suppressTrapping = freeze,
} = Object;

// workaround for https://github.com/endojs/endo/issues/2094
// See commented out code and note immediately above.
// @ts-expect-error TS does not yet have this on ObjectConstructor.
export const isNonTrapping = Object.isNonTrapping || isFrozen;
// @ts-expect-error TS does not yet have this on ObjectConstructor.
export const suppressTrapping = Object.suppressTrapping || freeze;

export const {
species: speciesSymbol,
toStringTag: toStringTagSymbol,
Expand Down Expand Up @@ -125,6 +144,9 @@ export const {
ownKeys,
preventExtensions: reflectPreventExtensions,
set: reflectSet,
// https://github.com/endojs/endo/pull/2673
isNonTrapping: reflectIsNonTrapping,
suppressTrapping: reflectSuppressTrapping,
} = Reflect;

export const { isArray, prototype: arrayPrototype } = Array;
Expand Down
18 changes: 16 additions & 2 deletions packages/ses/src/make-hardener.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
apply,
arrayForEach,
defineProperty,
freeze,
getOwnPropertyDescriptor,
getOwnPropertyDescriptors,
getPrototypeOf,
Expand All @@ -49,6 +48,8 @@ import {
FERAL_STACK_GETTER,
FERAL_STACK_SETTER,
isError,
isFrozen,
suppressTrapping,
} from './commons.js';
import { assert } from './error/assert.js';

Expand Down Expand Up @@ -128,6 +129,10 @@ const freezeTypedArray = array => {
* @returns {Harden}
*/
export const makeHardener = () => {
// TODO Get the native hardener to suppressTrapping at each step,
// rather than freeze. Until then, we cannot use it, which is *expensive*!
// TODO Comment out the following to skip the native hardener.
//
// Use a native hardener if possible.
if (typeof globalThis.harden === 'function') {
const safeHarden = globalThis.harden;
Expand Down Expand Up @@ -182,8 +187,17 @@ export const makeHardener = () => {
// Also throws if the object is an ArrayBuffer or any TypedArray.
if (isTypedArray(obj)) {
freezeTypedArray(obj);
if (isFrozen(obj)) {
// After `freezeTypedArray`, the typed array might actually be
// frozen if
// - it has no indexed properties
// - it is backed by an Immutable ArrayBuffer as proposed.
// In either case, this makes it a candidate to be made
// non-trapping.
suppressTrapping(obj);
}
} else {
freeze(obj);
suppressTrapping(obj);
}

// we rely upon certain commitments of Object.freeze and proxies here
Expand Down
6 changes: 6 additions & 0 deletions packages/ses/src/permits.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,9 @@ export const permitted = {
groupBy: fn,
// Seen on QuickJS
__getClass: false,
// https://github.com/endojs/endo/pull/2673
isNonTrapping: fn,
suppressTrapping: fn,
},

'%ObjectPrototype%': {
Expand Down Expand Up @@ -1624,6 +1627,9 @@ export const permitted = {
set: fn,
setPrototypeOf: fn,
'@@toStringTag': 'string',
// https://github.com/endojs/endo/pull/2673
isNonTrapping: fn,
suppressTrapping: fn,
},

Proxy: {
Expand Down
Loading