π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Enforce the Rust binding boundary
Manav Rathi1e15cfbdbc66
diff --git a/rust/checks/binding-boundary/check.mjs b/rust/checks/binding-boundary/check.mjsnew file mode 100644index 0000000000..41891cb759--- /dev/null+++ b/rust/checks/binding-boundary/check.mjs@@ -0,0 +1,69 @@+import { execFileSync } from "node:child_process";+import { existsSync } from "node:fs";+import { isAbsolute, relative, resolve, sep } from "node:path";++const requestedRoot = resolve(process.argv[2]);+const metadata = JSON.parse(+ execFileSync(+ "cargo",+ [+ "metadata",+ "--no-deps",+ "--format-version",+ "1",+ "--locked",+ "--offline",+ ],+ { cwd: resolve(requestedRoot, "rust"), encoding: "utf8" },+ ),+);+const root = resolve(metadata.workspace_root, "..");+const bindings = resolve(root, "rust/bindings");+const framework = /^(?:wasm-bindgen(?:-.*)?|js-sys|web-sys|serde-wasm-bindgen|tsify(?:-.*)?|flutter_rust_bridge(?:_.*)?|uniffi(?:_.*)?|napi(?:-.*)?)$/;+const pathFromRoot = (path) => relative(root, path).split(sep).join("/");+const domain = metadata.packages.filter((pkg) =>+ pathFromRoot(pkg.manifest_path).startsWith("rust/crates/"),+);++if (!domain.length) {+ console.error(`${resolve(root, "rust/crates")}: found no domain crates`);+ process.exitCode = 1;+}++for (const pkg of domain) {+ const manifest = pathFromRoot(pkg.manifest_path);+ for (const dependency of pkg.dependencies) {+ if (!framework.test(dependency.name) && !inside(bindings, dependency.path)) continue;+ console.error(+ `${manifest}: dependency ${JSON.stringify(dependency.name)} crosses the Rust binding boundary`,+ );+ process.exitCode = 1;+ }+}++const registered = new Set(domain.map((pkg) => pathFromRoot(pkg.manifest_path)));+for (const manifest of git(+ "ls-files",+ "--cached",+ "--others",+ "--exclude-standard",+ "-z",+ "--",+ "rust/crates/**/Cargo.toml",+)+ .split("\0")+ .filter((path) => path && existsSync(resolve(root, path)))) {+ if (registered.has(manifest)) continue;+ console.error(`${manifest}: domain crate is not registered in the Cargo workspace`);+ process.exitCode = 1;+}++function git(...args) {+ return execFileSync("git", args, { cwd: root, encoding: "utf8" });+}++function inside(directory, path) {+ if (!path) return false;+ const local = relative(directory, path);+ return local !== ".." && !local.startsWith(`..${sep}`) && !isAbsolute(local);+}diff --git a/rust/checks/binding-boundary/test.mjs b/rust/checks/binding-boundary/test.mjsnew file mode 100644index 0000000000..78c556208d--- /dev/null+++ b/rust/checks/binding-boundary/test.mjs@@ -0,0 +1,119 @@+import assert from "node:assert/strict";+import { spawnSync } from "node:child_process";+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";+import { tmpdir } from "node:os";+import { dirname, join } from "node:path";++const check = join(import.meta.dirname, "check.mjs");+const packageToml = (name, dependencies = "") =>+ `[package]\nname = ${JSON.stringify(name)}\nversion = "0.1.0"\nedition = "2024"\n${dependencies}`;++let result = run();+assert.equal(result.status, 0, result.stderr);+assert.equal(result.stdout, "");+assert.equal(result.stderr, "");++for (const framework of [+ "wasm-bindgen",+ "js-sys",+ "web-sys",+ "serde-wasm-bindgen",+ "tsify-macros",+ "flutter_rust_bridge_macros",+ "uniffi_macros",+ "napi-derive",+]) {+ result = run({ framework });+ assert.equal(result.status, 1, result.stderr);+ assert.match(result.stderr, new RegExp(`dependency "${framework}" crosses`));+}++result = run({ binding: true });+assert.equal(result.status, 1, result.stderr);+assert.match(result.stderr, /dependency "ente-binding" crosses/);++result = run({+ source: '// wasm_bindgen::JsValue is isolated here.\npub const NOTE: &str = "uniffi::export";\n',+});+assert.equal(result.status, 0, result.stderr);++result = run({ unregistered: true });+assert.equal(result.status, 1, result.stderr);+assert.match(result.stderr, /rust\/crates\/unregistered\/Cargo\.toml: domain crate is not registered/);++result = run({ empty: true });+assert.equal(result.status, 1, result.stderr);+assert.match(result.stderr, /found no domain crates/);++result = run({ deleted: true });+assert.equal(result.status, 0, result.stderr);+assert.equal(result.stderr, "");++function run({+ source = "pub fn domain() {}\n",+ framework,+ binding = false,+ empty = false,+ unregistered = false,+ deleted = false,+} = {}) {+ const root = mkdtempSync(join(tmpdir(), "ente-rust-binding-"));+ try {+ spawn("git", ["init", "-q"], root);+ if (empty) {+ write(root, "rust/Cargo.toml", '[workspace]\nresolver = "2"\n');+ } else {+ const members = ["crates/domain", "bindings/wasm/lib"];+ if (framework) members.push("vendor/framework");+ write(+ root,+ "rust/Cargo.toml",+ `[workspace]\nmembers = ${JSON.stringify(members)}\nresolver = "2"\n`,+ );+ const dependencies = framework+ ? `\n[dev-dependencies]\nbinding = { package = ${JSON.stringify(framework)}, path = "../../vendor/framework" }\n`+ : binding+ ? '\n[dependencies]\nbinding = { package = "ente-binding", path = "../../bindings/wasm/lib" }\n'+ : "";+ write(+ root,+ "rust/crates/domain/Cargo.toml",+ packageToml("domain", dependencies),+ );+ write(root, "rust/crates/domain/src/lib.rs", source);+ write(root, "rust/bindings/wasm/lib/Cargo.toml", packageToml("ente-binding"));+ write(root, "rust/bindings/wasm/lib/src/lib.rs", "pub fn binding() {}\n");+ if (framework) {+ write(root, "rust/vendor/framework/Cargo.toml", packageToml(framework));+ write(root, "rust/vendor/framework/src/lib.rs", "pub fn framework() {}\n");+ }+ if (unregistered || deleted) {+ write(root, "rust/crates/unregistered/Cargo.toml", packageToml("unregistered"));+ write(root, "rust/crates/unregistered/src/lib.rs", "pub fn unregistered() {}\n");+ }+ }+ spawn(+ "cargo",+ ["metadata", "--no-deps", "--format-version", "1", "--offline"],+ join(root, "rust"),+ );+ spawn("git", ["add", "."], root);+ if (deleted) rmSync(join(root, "rust/crates/unregistered"), { recursive: true });+ return spawnSync(process.execPath, [check, root], { encoding: "utf8" });+ } finally {+ rmSync(root, { recursive: true });+ }+}++function write(root, path, content) {+ const target = join(root, path);+ mkdirSync(dirname(target), { recursive: true });+ writeFileSync(target, content);+}++function spawn(command, args, cwd) {+ const result = spawnSync(command, args, { cwd, encoding: "utf8" });+ assert.equal(result.error, undefined);+ assert.equal(result.status, 0, result.stderr);+ return result;+}diff --git a/rust/scripts/lint.sh b/rust/scripts/lint.shindex 16f93233c7..d8e4af53d7 100755--- a/rust/scripts/lint.sh+++ b/rust/scripts/lint.sh@@ -8,6 +8,8 @@ python3 checks/cargo-versions/check.py ..python3 checks/cargo-versions/test.pypython3 checks/cargo-workspace-dependencies/check.py ..python3 checks/cargo-workspace-dependencies/test.py+node checks/binding-boundary/check.mjs ..+node checks/binding-boundary/test.mjscargo fmt --checkcargo clippy --locked --all-targets --features museum,ente-ml/ml-assetscargo audit