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 24, 2024
1 parent 52325c7 commit 1bfee1b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/ses/src/commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const {
JSON,
Map,
Math,
ModuleSource,
Number,
Object,
Promise,
Expand Down
3 changes: 3 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 @@ -267,6 +268,8 @@ export const repairIntrinsics = (options = {}) => {

tameDomains(domainTaming);

tameModuleSource();

// Replace Function.prototype.toString with one that recognizes
// shimmed functions as honorary native functions.
const markVirtualizedNativeFunction = tameFunctionToString();
Expand Down
35 changes: 35 additions & 0 deletions packages/ses/src/tame-module-source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
ModuleSource,
functionPrototype,
getPrototypeOf,
objectPrototype,
setPrototypeOf,
} from './commons.js';

export const tameModuleSource = () => {
if (ModuleSource !== undefined) {
// 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);
}

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

0 comments on commit 1bfee1b

Please sign in to comment.