Skip to content

Commit

Permalink
feat(ses): Tame ModuleSource
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Sep 25, 2024
1 parent 806e56c commit 635e630
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/ses/src/lockdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { makeSafeEvaluator } from './make-safe-evaluator.js';
import { initialGlobalPropertyNames } from './permits.js';
import { tameFunctionToString } from './tame-function-tostring.js';
import { tameDomains } from './tame-domains.js';
import { tameModuleSource } from './tame-module-source.js';

import { tameConsole } from './error/tame-console.js';
import tameErrorConstructor from './error/tame-error-constructor.js';
Expand Down Expand Up @@ -286,6 +287,7 @@ export const repairIntrinsics = (options = {}) => {
addIntrinsics(tameRegExpConstructor(regExpTaming));
addIntrinsics(tameSymbolConstructor());
addIntrinsics(shimArrayBufferTransfer());
addIntrinsics(tameModuleSource());

addIntrinsics(getAnonymousIntrinsics());

Expand Down
51 changes: 51 additions & 0 deletions packages/ses/src/tame-module-source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
functionPrototype,
getPrototypeOf,
globalThis,
objectPrototype,
setPrototypeOf,
} from './commons.js';

export const tameModuleSource = () => {
const newIntrinsics = {};

const ModuleSource = globalThis.ModuleSource;
if (ModuleSource !== undefined) {
newIntrinsics.ModuleSource = ModuleSource;

// We introduce ModuleSource.[[Proto]] === AbstractModuleSource
// and ModuleSource.prototype.[[Proto]] === AbstractModuleSource.prototype
// if that layer is absent because the permitting system can more
// gracefully tolerate the absence of an expected prototype than the
// presence of an unexpected prototype,.
function AbstractModuleSource() {
// no-op safe to super()
}

const ModuleSourceProto = getPrototypeOf(ModuleSource);
if (ModuleSourceProto === functionPrototype) {
setPrototypeOf(ModuleSource, AbstractModuleSource);
newIntrinsics['%AbstractModuleSource%'] = AbstractModuleSource;
newIntrinsics['%AbstractModuleSourcePrototype%'] =
AbstractModuleSource.prototype;
} else {
newIntrinsics['%AbstractModuleSource%'] = ModuleSourceProto;
newIntrinsics['%AbstractModuleSourcePrototype%'] =
ModuleSourceProto.prototype;
}

const ModuleSourcePrototype = ModuleSource.prototype;
if (ModuleSourcePrototype !== undefined) {
newIntrinsics['%ModuleSourcePrototype%'] = ModuleSourcePrototype;

// ModuleSource.prototype.__proto__ should be the
// AbstractModuleSource.prototype.
const ModuleSourcePrototypeProto = getPrototypeOf(ModuleSourcePrototype);
if (ModuleSourcePrototypeProto === objectPrototype) {
setPrototypeOf(ModuleSource.prototype, AbstractModuleSource.prototype);
}
}
}

return newIntrinsics;
};

0 comments on commit 635e630

Please sign in to comment.