Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Refine WASM import guardrail

Manav Rathi8b2dfd5223c4
diff --git a/web/scripts/wasm-imports/eslint.config.mjs b/web/scripts/wasm-imports/eslint.config.mjs
index d2f38241cb..186f02d44c 100644
--- a/web/scripts/wasm-imports/eslint.config.mjs
+++ b/web/scripts/wasm-imports/eslint.config.mjs
@@ -1,25 +1,12 @@
export const rawWasmPath = String.raw`(?:^|/)pkg(?:/|$)|\.wasm(?:[?#].*)?$`;
const selectorRawWasmPath = rawWasmPath.replaceAll("/", "\\u002F");
-const payloadImport = `ImportExpression[source.value=/${selectorRawWasmPath}/]`;
-const payloadRequire = `CallExpression[callee.name=require][arguments.0.value=/${selectorRawWasmPath}/]`;
+const payloadImport = `ImportExpression:matches([source.value=/${selectorRawWasmPath}/], [source.type=TemplateLiteral][source.expressions.length=0][source.quasis.0.value.cooked=/${selectorRawWasmPath}/])`;
+const payloadRequire = `CallExpression[callee.name=require]:matches([arguments.0.value=/${selectorRawWasmPath}/], [arguments.0.type=TemplateLiteral][arguments.0.expressions.length=0][arguments.0.quasis.0.value.cooked=/${selectorRawWasmPath}/])`;
const restrictedPayloadRequire = {
selector: payloadRequire,
message: "Load generated WASM with import() inside its wrapper.",
};
-const exportedPayloadLoader = [
- "ExportNamedDeclaration > VariableDeclaration > VariableDeclarator > ArrowFunctionExpression > ImportExpression",
- "ExportNamedDeclaration > VariableDeclaration > VariableDeclarator > ArrowFunctionExpression > AwaitExpression > ImportExpression",
- "ExportNamedDeclaration > VariableDeclaration > VariableDeclarator > ArrowFunctionExpression > BlockStatement > ReturnStatement > ImportExpression",
- "ExportNamedDeclaration > VariableDeclaration > VariableDeclarator > ArrowFunctionExpression > BlockStatement > ReturnStatement > AwaitExpression > ImportExpression",
- "ExportNamedDeclaration > VariableDeclaration > VariableDeclarator > FunctionExpression > BlockStatement > ReturnStatement > ImportExpression",
- "ExportNamedDeclaration > VariableDeclaration > VariableDeclarator > FunctionExpression > BlockStatement > ReturnStatement > AwaitExpression > ImportExpression",
- "ExportNamedDeclaration > FunctionDeclaration > BlockStatement > ReturnStatement > ImportExpression",
- "ExportNamedDeclaration > FunctionDeclaration > BlockStatement > ReturnStatement > AwaitExpression > ImportExpression",
- ":matches(ExportNamedDeclaration, ExportDefaultDeclaration) > ClassDeclaration > ClassBody > PropertyDefinition[static=false]:not([accessibility=private]):not([key.type=PrivateIdentifier]) > ImportExpression",
-]
- .map((selector) => `${selector}[source.value=/${selectorRawWasmPath}/]`)
- .join(",");
export default [
{
@@ -68,16 +55,8 @@ export default [
{
selector:
payloadImport +
- ":not(:matches(FunctionDeclaration, FunctionExpression, ArrowFunctionExpression) ImportExpression)" +
- ":not(PropertyDefinition[static=false] > .value)" +
- ":not(PropertyDefinition[static=false] > .value ImportExpression)",
- message:
- "Defer loading generated WASM until an operation needs it.",
- },
- {
- selector: exportedPayloadLoader,
- message:
- "Export operations instead of the generated WASM module.",
+ ":not(:matches(FunctionDeclaration, FunctionExpression, ArrowFunctionExpression) ImportExpression)",
+ message: "Defer loading generated WASM inside a function.",
},
restrictedPayloadRequire,
],
diff --git a/web/scripts/wasm-imports/test.mjs b/web/scripts/wasm-imports/test.mjs
index c0ed78ed87..1e727160dd 100644
--- a/web/scripts/wasm-imports/test.mjs
+++ b/web/scripts/wasm-imports/test.mjs
@@ -38,45 +38,20 @@ test("WASM inner boundary", async (t) => {
const cases = [
["new package, static caller, and type exports", {}],
[
- "public raw module loader",
- {
- source: 'export const loadWasm = () => import("./pkg/arbitrary-name");',
- error: "no-restricted-syntax",
- },
+ "deferred template-literal payload import",
+ { source: "const wasm = () => import(`./pkg/arbitrary-name`);" },
],
[
- "block-bodied public raw module loader",
+ "eager dynamic import",
{
- source: 'export const loadWasm = () => { return import("./pkg/arbitrary-name"); };',
+ source: 'export const wasm = import("./pkg/arbitrary-name");',
error: "no-restricted-syntax",
},
],
[
- "arrow operation with a private nested loader",
- {
- source: [
- "export const operation = () => {",
- ' const wasm = () => import("./pkg/arbitrary-name");',
- " return wasm().then((module) => module.operation());",
- "};",
- ].join("\n"),
- },
- ],
- [
- "function operation with a private nested loader",
- {
- source: [
- "export function operation() {",
- ' function wasm() { return import("./pkg/arbitrary-name"); }',
- " return wasm().then((module) => module.operation());",
- "}",
- ].join("\n"),
- },
- ],
- [
- "eager dynamic import",
+ "eager template-literal payload import",
{
- source: 'export const wasm = import("./pkg/arbitrary-name");',
+ source: "export const wasm = import(`./pkg/arbitrary-name`);",
error: "no-restricted-syntax",
},
],
@@ -94,6 +69,13 @@ test("WASM inner boundary", async (t) => {
error: "no-restricted-syntax",
},
],
+ [
+ "CommonJS template-literal payload import",
+ {
+ source: "const wasm = require(`./pkg/arbitrary-name`);",
+ error: "no-restricted-syntax",
+ },
+ ],
[
"raw binding re-export",
{
@@ -116,29 +98,23 @@ test("WASM inner boundary", async (t) => {
},
],
[
- "app imports a WASM payload",
+ "app template-literal bypasses the wrapper",
{
- caller: 'export const bypass = () => import("../packages/wasm/new-package/arbitrary.wasm?url");',
+ caller: "export const bypass = () => import(`../packages/wasm/new-package/pkg/arbitrary-name`);",
error: "no-restricted-syntax",
},
],
[
- "private lazy instance initialization",
+ "app requires a WASM payload",
{
- source: 'export class Client { #wasm = import("./pkg/arbitrary-name"); }',
- },
- ],
- [
- "public raw module field",
- {
- source: 'export class Client { wasm = import("./pkg/arbitrary-name"); }',
+ caller: 'const wasm = require("../packages/wasm/new-package/pkg/arbitrary-name");',
error: "no-restricted-syntax",
},
],
[
- "eager static initialization",
+ "app imports a WASM payload",
{
- source: 'export class Client { static wasm = import("./pkg/arbitrary-name"); }',
+ caller: 'export const bypass = () => import("../packages/wasm/new-package/arbitrary.wasm?url");',
error: "no-restricted-syntax",
},
],