Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
corrideat committed Oct 5, 2023
1 parent d1f2d66 commit 227d609
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 45 deletions.
13 changes: 13 additions & 0 deletions @types/build-settings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,17 @@ declare namespace __buildtimeSettings__ {
* using two message channels
*/
const contextifyMessagePortWorkaroundCrash: boolean;

/**
* Use eval to feature-detect function constructors
*
* If true, function constructors (e.g., AsyncFunction, Function, etc.)
* will be obtained using eval (e.g., eval('(async ()=>{})')), which
* works in environments that do not support the function declaration
* syntax.
*
* If false, function constructors will be defined statically, without
* feature detection.
*/
const featureDetectFunctionConstructors: boolean;
}
36 changes: 33 additions & 3 deletions esbuild.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,37 @@ const exactRealtyClosureBuilderPlugin = {
},
};

/**
* @type {esbuild.Plugin}
**/
const lotExportsBuilderPlugin = {
name: '@exact-realty/lot/exports',
setup(build) {
const extension = (() => {
switch (build.initialOptions.format) {
case 'cjs':
return 'cjs';
case 'esm':
return 'mjs';
case 'iife':
return 'js';
}
})();
build.onResolve(
{ filter: /^@exports\//, namespace: 'file' },
(args) => {
const path = `./${args.path
.slice(1)
.replace(/(?<=\.)js$/, extension)}`;
return {
path,
external: true,
};
},
);
},
};

const sequence = () => {
const generator = (function* () {
yield String(17);
Expand All @@ -153,7 +184,6 @@ const options = {
minify: true,
entryNames: '[name]',
platform: 'node',
external: ['esbuild'],
pure: ['Logger'],
define: {
'__buildtimeSettings__.buildTarget': '"generic"',
Expand Down Expand Up @@ -181,6 +211,7 @@ const options = {
'__buildtimeSettings__.isolationStategyIframeWorker': 'true',
'__buildtimeSettings__.contextifyMessagePort': 'true',
'__buildtimeSettings__.contextifyMessagePortWorkaroundCrash': 'true',
'__buildtimeSettings__.featureDetectFunctionConstructors': 'true',
// Enums
'EMessageTypes.SANDBOX_READY': EMessageTypesSequence.next(),
'EMessageTypes.REQUEST': EMessageTypesSequence.next(),
Expand All @@ -191,12 +222,11 @@ const options = {
},
};

void exactRealtyClosureBuilderPlugin;

// TODO: Use Google Closure Compiler globally
const plugins = [];

plugins.push(
lotExportsBuilderPlugin,
inlineScripts({
...options,
target: 'es2015',
Expand Down
1 change: 1 addition & 0 deletions import_map.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"@test/": "./test/",
"~trusted/": "./src/trusted/",
"~untrusted/": "./src/untrusted/",
"@exports/": "./src/exports/",
"@dist/": "./dist/"
}
}
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

export { default as bareSandbox } from '~trusted/impl/bare/bareSandbox.js';
export { default as browserSandbox } from '~trusted/impl/browser/browserSandbox.js';
export { default as nodejsSandbox } from '~trusted/impl/nodejs/nodejsSandbox.js';
export { default as workerSandbox } from '~trusted/impl/worker/workerSandbox.js';
export { default as bareSandbox } from '@exports/bare.js';
export { default as browserSandbox } from '@exports/browser.js';
export { default, default as nodejsSandbox } from '@exports/nodejs.js';
export { default as workerSandbox } from '@exports/worker.js';

export { default as freezePrototypes } from '~untrusted/lib/freezePrototypes.js';
export { default as hardenGlobals } from '~untrusted/lib/hardenGlobals.js';
24 changes: 13 additions & 11 deletions src/trusted/impl/browser/browserSandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,19 @@ const browserSandbox: ISandbox = async (
iframeSrcUrl + '#' + initMessageKeyA + '-' + initMessageKeyB,
);

const iframeStyles = {
['display']: 'none',
['height']: '1px',
['left']: '-9999px',
['opacity']: '0',
['position']: 'absolute',
['top']: '-9999px',
['visibility']: 'hidden',
['width']: '1px',
};
Object.assign(iframeContainer.style, iframeStyles);
/* List of CSS rules to attempt to ensure the iframe is never visible */
const iframeStyles: [string, string][] = [
['display', 'none'],
['position', 'absolute'],
['transform', 'scale(0)'],
];
iframeStyles.forEach((descriptor) => {
iframeContainer.style.setProperty(
descriptor[0],
descriptor[1],
'important',
);
});

if (typeof HTMLElement.prototype.attachShadow === 'function') {
const shadow = iframeContainer.attachShadow({ ['mode']: 'closed' });
Expand Down
51 changes: 51 additions & 0 deletions src/untrusted/lib/functionTypeSpecimensList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* Copyright © 2023 Exact Realty Limited.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/

import { aFilter, aMap } from './utils.js';

const functionTypeSpecimensList =
__buildtimeSettings__.featureDetectFunctionConstructors
? aFilter(
aMap(
[
'(function(){})',
'(function*(){})',
'(async function(){})',
'(async function*(){})',
],
(source, i) => {
try {
return (0, eval)(source);
} catch {
if (i === 0) {
return function () {
/**/
};
}
}
},
),
Boolean as unknown as {
(v?: FunctionConstructor): v is FunctionConstructor;
},
)
: [
function () {},
function* () {},
async function () {},
async function* () {},
];

export default functionTypeSpecimensList;
29 changes: 2 additions & 27 deletions src/untrusted/lib/hardenGlobals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@

import {
EE,
aFilter,
aForEach,
aMap,
fnApply,
oDefineProperties,
oDefineProperty,
Expand All @@ -27,6 +25,7 @@ import {
oSetPrototypeOf,
} from './utils.js';

import functionTypeSpecimensList from './functionTypeSpecimensList.js';
import global from './global.js';

/**
Expand Down Expand Up @@ -127,33 +126,9 @@ const hardenGlobals: { (): void } = __buildtimeSettings__.hardenGlobals
? () => {
const FERAL_FUNCTION = hardenGlobals.constructor;

const list = aFilter(
aMap(
[
'(function(){})',
'(function*(){})',
'(async function(){})',
'(async function*(){})',
],
(source, i) => {
try {
return (0, eval)(source);
} catch {
if (i === 0)
return function () {
/**/
};
}
},
),
Boolean as unknown as {
(v?: FunctionConstructor): v is FunctionConstructor;
},
);

try {
// Attempt to tame function constructors
aForEach(list, (fnIntrinsic) => {
aForEach(functionTypeSpecimensList, (fnIntrinsic) => {
try {
const prototype = oGetPrototypeOf(fnIntrinsic);
const origConstructor = prototype.constructor;
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"typeRoots": ["./node_modules/@types", "./@types"],
"paths": {
"@dist/*": ["./dist/*"],
"@exports/*": ["./src/exports/*"],
"~/*": ["./src/*"],
"@test/*": ["./test/*"],
"~trusted/*": ["./src/trusted/*"],
Expand Down

0 comments on commit 227d609

Please sign in to comment.