π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Enforce WASM handle usage and typed declarations
Manav Rathiabc96299523f
diff --git a/web/checks/wasm-exports/check.mjs b/web/checks/wasm-exports/check.mjsindex 3eddc0e66e..1b11cb9813 100644--- a/web/checks/wasm-exports/check.mjs+++ b/web/checks/wasm-exports/check.mjs@@ -2,17 +2,17 @@ import { execFileSync } from "node:child_process";import { existsSync, globSync, readFileSync } from "node:fs";import { basename, dirname, relative, resolve } from "node:path";import ts from "typescript";-import { rustFunctionExports, unusedExports } from "./exports.mjs";+import { rustExports, unusedExports } from "./exports.mjs";const cwd = resolve(import.meta.dirname, "../..");const rustDirectory = resolve(cwd, "../rust/bindings/wasm");-const rustExports = (crate) =>- rustFunctionExports(+const crateExports = (crate) =>+ rustExports(globSync(`${crate}/src/**/*.rs`, { cwd: rustDirectory }).map((file) =>resolve(rustDirectory, file),),);-const sharedExports = rustExports("lib");+const sharedExports = crateExports("lib");const rustOrigins = new Map();const files = [];for (const manifestPath of globSync("packages/wasm/*/package.json", { cwd })) {@@ -30,7 +30,7 @@ for (const manifestPath of globSync("packages/wasm/*/package.json", { cwd })) {throw new Error(`Build WASM before checking exports: ${bindings}`);rustOrigins.set(bindings,- new Map([...sharedExports, ...rustExports(basename(directory))]),+ new Map([...sharedExports, ...crateExports(basename(directory))]),);files.push(bindings,diff --git a/web/checks/wasm-exports/exports.mjs b/web/checks/wasm-exports/exports.mjsindex 8b32d19e14..1ff88c9720 100644--- a/web/checks/wasm-exports/exports.mjs+++ b/web/checks/wasm-exports/exports.mjs@@ -10,22 +10,38 @@ const isProductionSource = (source) =>const declarationKey = (node) => `${node.getSourceFile().fileName}:${node.pos}`;-export function rustFunctionExports(files) {+export function rustExports(files) {const exports = new Map();for (const file of files) {const source = readFileSync(file, "utf8");- const declaration =- /^#\[wasm_bindgen(?:\(([^)]*)\))?\]\s*pub (?:async )?fn (\w+)/gm;- for (const match of source.matchAll(declaration)) {- const name =- /js_name\s*=\s*"?(\w+)/.exec(match[1] ?? "")?.[1] ?? match[2];+ const add = (name, offset) => {if (exports.has(name))throw new Error(`Duplicate Rust WASM export: ${name}`);exports.set(name, {file,name,- line: source.slice(0, match.index).split("\n").length,+ line: source.slice(0, offset).split("\n").length,});+ };+ const jsName = (attributes, fallback) =>+ /js_name\s*=\s*"?(\w+)/.exec(attributes ?? "")?.[1] ?? fallback;+ const declaration =+ /^#\[wasm_bindgen(?:\(([^)]*)\))?\]\s*pub (?:async )?fn (\w+)/gm;+ for (const match of source.matchAll(declaration)) {+ add(jsName(match[1], match[2]), match.index);+ }+ const impl =+ /^#\[wasm_bindgen(?:\([^)]*\))?\]\s*impl (\w+) \{([\s\S]*?)^\}/gm;+ for (const block of source.matchAll(impl)) {+ const method =+ /^ (?:#\[wasm_bindgen(?:\(([^)]*)\))?\]\s*)?pub (?:async )?fn (\w+)/gm;+ const offset = block.index + block[0].indexOf(block[2]);+ for (const match of block[2].matchAll(method)) {+ const name = /\bconstructor\b/.test(match[1] ?? "")+ ? "constructor"+ : jsName(match[1], match[2]);+ add(`${block[1]}.${name}`, offset + match.index);+ }}}return exports;@@ -40,20 +56,21 @@ export function unusedExports(files, projects, rustOrigins = new Map()) {ts.ScriptTarget.Latest,true,);- const add = (node) => {+ const add = (node, qualifiedName) => {if (- !node.name ||- (source.isDeclarationFile && node.name.getText() === "start")+ (!node.name && !qualifiedName) ||+ (source.isDeclarationFile && node.name?.getText() === "start"))return;if (- ts.isObjectBindingPattern(node.name) ||- ts.isArrayBindingPattern(node.name)+ node.name &&+ (ts.isObjectBindingPattern(node.name) ||+ ts.isArrayBindingPattern(node.name))) {for (const element of node.name.elements) add(element);return;}- const name = node.name.getText();+ const name = qualifiedName ?? node.name.getText();const origin = rustOrigins.get(file)?.get(name);if (source.isDeclarationFile && rustOrigins.has(file) && !origin) {throw new Error(`${file}: cannot locate Rust export ${name}`);@@ -78,6 +95,23 @@ export function unusedExports(files, projects, rustOrigins = new Map()) {if (ts.isFunctionDeclaration(node)) add(node);if (ts.isClassDeclaration(node) && !source.isDeclarationFile)add(node);+ if (ts.isClassDeclaration(node) && source.isDeclarationFile) {+ for (const member of node.members) {+ if (+ member.modifiers?.some(+ (m) => m.kind === ts.SyntaxKind.PrivateKeyword,+ ) ||+ ["free", "[Symbol.dispose]"].includes(+ member.name?.getText(),+ )+ )+ continue;+ const name = ts.isConstructorDeclaration(member)+ ? "constructor"+ : member.name.getText();+ add(member, `${node.name.text}.${name}`);+ }+ }if (ts.isVariableStatement(node)) {for (const declaration of node.declarationList.declarations)add(declaration);@@ -89,16 +123,21 @@ export function unusedExports(files, projects, rustOrigins = new Map()) {for (const { entryFiles, options } of projects) {const program = withWorkers(entryFiles, options);const checker = program.getTypeChecker();+ const referenced = new Set();+ const forwarded = new Map();+ const referenceDeclaration = (declaration) => {+ const key = declarationKey(declaration);+ referenced.add(key);+ const candidate = candidates.get(key);+ if (candidate) used.add(candidate.key);+ };const reference = (symbol) => {if (!symbol) return;if (symbol.flags & ts.SymbolFlags.Alias)symbol = checker.getAliasedSymbol(symbol);for (const root of checker.getRootSymbols(symbol)) {for (const declaration of root.declarations ?? []) {- const candidate = candidates.get(- declarationKey(declaration),- );- if (candidate) used.add(candidate.key);+ referenceDeclaration(declaration);}}};@@ -111,6 +150,42 @@ export function unusedExports(files, projects, rustOrigins = new Map()) {ts.isTypeNode(node))return;+ if (ts.isNewExpression(node) || ts.isCallExpression(node)) {+ const signature = checker.getResolvedSignature(node);+ if (signature?.declaration)+ referenceDeclaration(signature.declaration);+ if (signature?.declaration?.typeParameters?.length) {+ const typeArguments =+ checker.getTypeArgumentsForResolvedSignature(+ signature,+ );+ for (const [index, parameter] of (+ signature.declaration.typeParameters ?? []+ ).entries()) {+ if (+ !parameter.constraint ||+ !typeArguments?.[index]+ )+ continue;+ const constraint = checker.getTypeFromTypeNode(+ parameter.constraint,+ );+ for (const property of constraint.getProperties()) {+ const target = checker.getPropertyOfType(+ typeArguments[index],+ property.name,+ );+ for (const declaration of property.declarations ??+ []) {+ const key = declarationKey(declaration);+ const targets = forwarded.get(key) ?? [];+ targets.push(target);+ forwarded.set(key, targets);+ }+ }+ }+ }+ }if (ts.isBindingElement(node) &&ts.isObjectBindingPattern(node.parent)@@ -145,6 +220,9 @@ export function unusedExports(files, projects, rustOrigins = new Map()) {};visit(source);}+ for (const key of referenced) {+ for (const target of forwarded.get(key) ?? []) reference(target);+ }}return [...new Map(diff --git a/web/checks/wasm-exports/test.mjs b/web/checks/wasm-exports/test.mjsindex 501a3c6148..9086960c28 100644--- a/web/checks/wasm-exports/test.mjs+++ b/web/checks/wasm-exports/test.mjs@@ -4,7 +4,7 @@ import { tmpdir } from "node:os";import { join } from "node:path";import { test } from "node:test";import ts from "typescript";-import { rustFunctionExports, unusedExports } from "./exports.mjs";+import { rustExports, unusedExports } from "./exports.mjs";test("production references cross lazy loaders, aliases, destructuring and workers", () => {const directory = mkdtempSync(join(tmpdir(), "wasm-exports-"));@@ -27,6 +27,16 @@ test("production references cross lazy loaders, aliases, destructuring and workeexport function typeOnly(): void;export function wrapperOnly(): void;export function start(): void;+ export class Handle {+ constructor();+ free(): void;+ [Symbol.dispose](): void;+ readonly key: string;+ run(): void;+ orphan(): void;+ testOnly(): void;+ constraintOnly(): void;+ }`,);const wrapper = file(@@ -49,6 +59,14 @@ test("production references cross lazy loaders, aliases, destructuring and workeimport { aliased as action } from "./raw";execute();action();+ import { Handle } from "./raw";+ const handle = new Handle();+ handle.run();+ console.log(handle.key);+ const ignore = <T extends { constraintOnly(): void }>(handle: T) => {};+ ignore(handle);+ const unrelated = { orphan() {} };+ unrelated.orphan();type Signature = typeof import("./raw").typeOnly;new Worker(new URL("./worker.ts", import.meta.url));`,@@ -57,7 +75,7 @@ test("production references cross lazy loaders, aliases, destructuring and workefile("unused.ts", `import { orphan } from "./raw"; orphan();`);file("binding.test.ts",- `import { testOnly } from "./raw"; testOnly();`,+ `import { testOnly, Handle } from "./raw"; testOnly(); new Handle().testOnly();`,);const projects = [{@@ -73,6 +91,9 @@ test("production references cross lazy loaders, aliases, destructuring and worke.map(({ name }) => name).sort();assert.deepEqual(check(), [+ "Handle.constraintOnly",+ "Handle.orphan",+ "Handle.testOnly","orphan","testOnly","typeOnly",@@ -87,6 +108,9 @@ test("production references cross lazy loaders, aliases, destructuring and worke};`,);assert.deepEqual(check(), [+ "Handle.constraintOnly",+ "Handle.orphan",+ "Handle.testOnly","orphan","testOnly","typeOnly",@@ -112,6 +136,12 @@ test("copies of a Rust export need one caller across artifacts", () => {pub fn shared_operation() {}pub fn internal_helper() {}++#[wasm_bindgen]+impl Session {+ #[wasm_bindgen(js_name = updateAuthToken)]+ pub fn update_auth_token(&self, token: String) {}+}`,);const appSource = (app) =>@@ -127,12 +157,17 @@ pub fn local_operation() {}const declarations = `export function sharedOperation(): void;export function localOperation(): void;+ export class Session {+ private constructor();+ free(): void;+ updateAuthToken(token: string): void;+ }`;const left = file("left.d.ts", declarations);const right = file("right.d.ts", declarations);const origins = new Map([- [left, rustFunctionExports([shared, leftSource])],- [right, rustFunctionExports([shared, rightSource])],+ [left, rustExports([shared, leftSource])],+ [right, rustExports([shared, rightSource])],]);const entry = file("entry.ts",@@ -140,6 +175,12 @@ pub fn local_operation() {}import { sharedOperation, localOperation } from "./left";sharedOperation();localOperation();+ import type { Session } from "./left";+ const cache = <T extends { updateAuthToken(token: string): void }>(open: () => Promise<T>) => {+ void open().then(session => session.updateAuthToken("token"));+ };+ declare const open: () => Promise<Session>;+ cache(open);`,);file(@@ -175,6 +216,7 @@ pub fn local_operation() {});assert.deepEqual(check(), [{ file: shared, name: "sharedOperation" },+ { file: shared, name: "Session.updateAuthToken" },{ file: rightSource, name: "localOperation" },]);diff --git a/web/checks/wasm-names/eslint.config.mjs b/web/checks/wasm-names/eslint.config.mjsindex 8eba84fc69..1286a4ed78 100644--- a/web/checks/wasm-names/eslint.config.mjs+++ b/web/checks/wasm-names/eslint.config.mjs@@ -7,6 +7,7 @@ export default [languageOptions: { parser: tseslint.parser },plugins: { "@typescript-eslint": tseslint.plugin },rules: {+ "@typescript-eslint/no-explicit-any": "error","@typescript-eslint/naming-convention": ["error",{diff --git a/web/checks/wasm-names/test.mjs b/web/checks/wasm-names/test.mjsindex a9bd4cd256..c84bd68975 100644--- a/web/checks/wasm-names/test.mjs+++ b/web/checks/wasm-names/test.mjs@@ -58,3 +58,21 @@ test("low-level WASM declarations are excluded", async () => {),);});++test("generated APIs cannot expose any", async () => {+ const [result] = await eslint.lintText(+ `/* eslint-disable */+ export interface ResultData { items: any[]; }+ export class Handle { read(): Promise<any>; }+ export function open(input: any): Handle;`,+ { filePath },+ );+ assert.equal(result.messages.length, 3);+ assert.ok(+ result.messages.every(+ ({ ruleId, severity }) =>+ ruleId === "@typescript-eslint/no-explicit-any" &&+ severity === 2,+ ),+ );+});