π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Deny unsafe Rust and protect lint exceptions
Manav Rathi6c8da37d9484
diff --git a/.github/checks/change-approval/check.mjs b/.github/checks/change-approval/check.mjsindex 4da2ed7fbb..5e1671e78d 100644--- a/.github/checks/change-approval/check.mjs+++ b/.github/checks/change-approval/check.mjs@@ -25,6 +25,109 @@ const guardrailFiles = new Set([const configFile =/(^|\/)(\.gitattributes|rust-toolchain\.toml|\.cargo\/(config|audit)\.toml|\.npmrc|\.nvmrc|\.tool-versions|\.node-version|\.python-version|gradle-wrapper\.properties)$/;+const cargoLints = (source) =>+ execFileSync("python3", ["-c", `+import json, sys, tomllib+cargo = tomllib.loads(sys.stdin.read())+workspace = cargo.get("workspace", {})+selection = {key: workspace.get(key) for key in ("members", "exclude", "default-members")}+print(json.dumps([cargo.get("lints"), workspace.get("lints"), selection], sort_keys=True))+`], { encoding: "utf8", input: source });++function rustLintScopes(source) {+ const lexer = /\/\/[^\n]*|\/\*|[bc]?r(#+)?"[\s\S]*?"\1|[bc]?"(?:\\[\s\S]|[^"\\])*"|b?'(?:\\(?:u\{[\da-fA-F_]+\}|x[\da-fA-F]{2}|[\s\S])|[^'\\\r\n])'|(?:r#)?[a-zA-Z_]\w*|[^\s]/gu;+ const tokens = [];+ let match;+ while ((match = lexer.exec(source))) {+ const token = match[0];+ if (token.startsWith("//")) continue;+ if (token === "/*") {+ const comments = /\/\*|\*\//g;+ comments.lastIndex = lexer.lastIndex;+ for (let depth = 1; depth;) {+ const comment = comments.exec(source);+ if (!comment) throw new Error("Unclosed Rust comment");+ depth += comment[0] === "/*" ? 1 : -1;+ }+ lexer.lastIndex = comments.lastIndex;+ } else {+ tokens.push(token);+ }+ }+ const pairs = { "(": ")", "[": "]", "{": "}" };+ let cursor = 0;+ const group = (closing) => {+ const nodes = [];+ while (cursor < tokens.length) {+ const token = tokens[cursor++];+ if (token === closing) return nodes;+ if (Object.hasOwn(pairs, token)) nodes.push([token, ...group(pairs[token]), pairs[token]]);+ else if ([")", "]", "}"].includes(token)) throw new Error("Unmatched Rust delimiter");+ else nodes.push(token);+ }+ if (closing) throw new Error("Unclosed Rust delimiter");+ return nodes;+ };+ const lint = (nodes, levels = /^(?:r#)?(?:allow|expect|warn|deny|forbid)$/) => nodes.some((node, i) =>+ Array.isArray(node) ? lint(node, levels) : levels.test(node) && nodes[i + 1]?.[0] === "(",+ );+ const scopes = [];+ const visit = (nodes, context) => {+ let boundary = 0;+ let angles = 0;+ for (let i = 0; i < nodes.length; i++) {+ if (nodes[i] === "#") {+ let end = i;+ let hasLint = false;+ let exception = false;+ let inner = false;+ while (nodes[end] === "#") {+ const bang = nodes[end + 1] === "!";+ const attribute = nodes[end + (bang ? 2 : 1)];+ if (!Array.isArray(attribute) || attribute[0] !== "[") break;+ hasLint ||= lint(attribute);+ exception ||= lint(attribute, /^(?:r#)?(?:allow|expect)$/);+ inner ||= bang;+ end += bang ? 3 : 2;+ }+ if (hasLint) {+ let stop = end;+ const statement = nodes[end] === "let";+ let angles = 0;+ let whereClause = false;+ while (stop < nodes.length) {+ const node = nodes[stop++];+ whereClause ||= node === "where";+ if (!statement && node === "<") angles++;+ if (!statement && node === ">") angles = Math.max(0, angles - 1);+ if (node === ";" || (!statement && !angles && ((node === "," && !whereClause) || (Array.isArray(node) && node[0] === "{")))) break;+ }+ const scope = inner ? (exception ? nodes : nodes.slice(i, end)) : nodes.slice(i, stop);+ if (!inner && !exception && scope.at(-1)?.[0] === "{") scope[scope.length - 1] = ["{", "}"];+ scopes.push(JSON.stringify([context, scope]));+ }+ if (end > i) {+ i = end - 1;+ continue;+ }+ }+ if (Array.isArray(nodes[i])) {+ visit(nodes[i].slice(1, -1), [...context, nodes.slice(boundary, i), nodes[i][0]]);+ if (nodes[i][0] === "{" && !angles) boundary = i + 1;+ } else if (nodes[i] === ";") {+ boundary = i + 1;+ angles = 0;+ } else if (nodes[i] === "<") {+ angles++;+ } else if (nodes[i] === ">") {+ angles = Math.max(0, angles - 1);+ }+ }+ };+ visit(group(), []);+ return JSON.stringify(scopes.sort());+}+const tomlPackages = (text) =>text.split("[[package]]\n")@@ -171,6 +274,15 @@ const guardrails = [...[...numstat("--diff-filter=A"), ...additions].map(([, , file]) => file).filter((file) => file.startsWith(".github/")),];const configs = [...numstat(), ...additions].map(([, , file]) => file).filter((file) => configFile.test(file));+const lintPolicies = [...numstat(), ...additions]+ .map(([, , file]) => file)+ .filter((file) => file.endsWith(".rs") || path.basename(file) === "Cargo.toml")+ .filter((file) => {+ const before = kept.includes(file) ? git("show", `${mergeBase}:${file}`) : "";+ const after = sizes.has(file) ? (local ? readFileSync(file, "utf8") : git("show", `HEAD:${file}`)) : "";+ const policy = file.endsWith(".rs") ? rustLintScopes : cargoLints;+ return policy(before) !== policy(after);+ });const categories = [["binary file", "binary files", binaries.length],@@ -179,6 +291,7 @@ const categories = [["dependency source change", "dependency source changes", moved],["guardrail file", "guardrail files", guardrails.length],["config file", "config files", configs.length],+ ["Rust lint policy file", "Rust lint policy files", lintPolicies.length],].filter(([, , n]) => n);const summary = categories.map(([one, many, n]) => `${n} ${n === 1 ? one : many}`).join(", ");const detail = [@@ -190,6 +303,7 @@ const detail = [.join("\n\n")}`,guardrails.length && `## Guardrail changes\n\n${list(guardrails.map(code))}`,configs.length && `## Toolchain and registry config\n\n${list(configs.map(code))}`,+ lintPolicies.length && `## Rust lint policy and exceptions\n\n${list(lintPolicies.map(code))}`,].filter(Boolean).join("\n\n");diff --git a/.github/checks/change-approval/test.mjs b/.github/checks/change-approval/test.mjsindex ad79f15a93..b0c9e2b3a5 100644--- a/.github/checks/change-approval/test.mjs+++ b/.github/checks/change-approval/test.mjs@@ -441,3 +441,100 @@ test("CI mode with nothing flagged", (t) => {test("ordinary change is silent", (t) => {assert.equal(scan(t, { "src/a.txt": "a\n" }, { "src/a.txt": "b\n", "src/b.txt": "c\n" }), "");});++test("Cargo lint changes need approval across TOML layouts", (t) => {+ for (const [before, after] of [+ ['', '[workspace.lints.rust]\nunsafe_code = "deny"\n'],+ ['[workspace.lints.rust]\nunsafe_code = "deny"\n', '[workspace.lints.rust]\nunsafe_code = "warn"\n'],+ ['[workspace.lints.rust]\nunsafe_code = "deny"\n', ''],+ ['[lints]\nworkspace = true\n', '[lints.rust]\nunsafe_code = "allow"\n'],+ ['', 'workspace.lints = { rust = { unsafe_code = "allow" } }\n'],+ ['[workspace]\nmembers = ["a", "b"]\n', '[workspace]\nmembers = ["a", "b"]\ndefault-members = ["a"]\n'],+ ['[workspace]\nmembers = ["a", "b"]\n', '[workspace]\nmembers = ["a"]\nexclude = ["b"]\n'],+ ]) {+ const { output, summary } = scan(t, { "rust/Cargo.toml": before }, { "rust/Cargo.toml": after }, { ci: true });+ assert.equal(output, 'categories=["Rust lint policy files"]\n');+ assert.match(summary, /## Rust lint policy and exceptions\n\n- `rust\/Cargo.toml`/);+ }+});++test("Cargo dependency edits and equivalent lint layouts need no lint approval", (t) => {+ assert.equal(scan(t, {+ "rust/Cargo.toml": '[workspace.lints.rust]\nunsafe_code = "deny"\ndead_code = "warn"\n[workspace.dependencies]\nserde = "1"\n',+ }, {+ "rust/Cargo.toml": 'workspace.lints.rust = { dead_code = "warn", unsafe_code = "deny" }\n[workspace.dependencies]\nserde = "2"\n',+ }), "");+});++test("Rust lint attributes, conditions and scopes need approval", (t) => {+ const expect = '#[expect(unsafe_code, reason = "FFI") ]';+ const body = 'fn call() { unsafe { ffi(); } }';+ for (const [before, after] of [+ [body, `${expect}\n${body}`],+ [`${expect}\n${body}`, body],+ [`${expect}\n${body}`, `${expect.replace("FFI", "New reason")}\n${body}`],+ [`${expect}\n${body}`, `${expect}\n${body.replace("ffi();", "ffi(); more_ffi();")}`],+ [`#[cfg(unix)]\n${expect}\n${body}`, `${expect}\n${body}`],+ [`#[cfg_attr(unix, expect(unsafe_code, reason = "FFI"))]\n${body}`, `#[cfg_attr(test, expect(unsafe_code, reason = "FFI"))]\n${body}`],+ [`${expect}\n${body}\nfn other() {}`, `${body}\n${expect}\nfn other() {}`],+ [body, `#![allow(clippy::allow_attributes, clippy::allow_attributes_without_reason, unsafe_code)]\n${body}`],+ [body, `#[allow(unsafe_code, reason = "FFI")]\n${body}`],+ [`#[deny(unsafe_code)]\nmod guarded {}`, 'mod guarded {}'],+ [body, `#[r#expect(unsafe_code, reason = "FFI")]\n${body}`],+ ]) {+ assert.match(scan(t, { "src/lib.rs": before }, { "src/lib.rs": after }), /^1 Rust lint policy file\n/);+ }+});++test("Rust statement exceptions cover the whole initializer and their enclosing function", (t) => {+ const binding = '#[expect(unsafe_code, reason = "FFI")] let x = unsafe { ffi() };';+ const before = `fn a() { ${binding} } fn b() {}`;+ for (const after of [+ `fn a() {} fn b() { ${binding} }`,+ before.replace('ffi() };', 'ffi() } + unsafe { other() };'),+ ]) {+ assert.match(scan(t, { "src/lib.rs": before }, { "src/lib.rs": after }), /^1 Rust lint policy file\n/);+ }+});++test("Rust generic signatures do not truncate an exception's scope", (t) => {+ for (const signature of ['fn f<T, U>() -> Array<{ 2 }>', 'fn f<T>() where T: A, T: B']) {+ const before = `#[expect(unsafe_code, reason = "FFI")] ${signature} { unsafe { ffi() } }`;+ assert.match(scan(t, { "src/lib.rs": before }, { "src/lib.rs": before.replace('ffi()', 'other()') }), /^1 Rust lint policy file\n/);+ }+ const binding = '#[expect(unsafe_code, reason = "FFI")] let x = unsafe { ffi() };';+ assert.match(scan(t, {+ "src/lib.rs": `fn a<T, U>() -> Array<{ 2 }> { ${binding} } fn b<T, U>() -> Array<{ 2 }> {}`,+ }, {+ "src/lib.rs": `fn a<T, U>() -> Array<{ 2 }> {} fn b<T, U>() -> Array<{ 2 }> { ${binding} }`,+ }), /^1 Rust lint policy file\n/);+});++test("Rust closure parameters do not truncate an exception's initializer", (t) => {+ const before = 'fn f() { #[expect(unsafe_code, reason = "FFI")] let f = |p, q| unsafe { ffi(p, q) }; }';+ assert.match(scan(t, { "src/lib.rs": before }, { "src/lib.rs": before.replace('ffi(p, q)', 'other(p, q)') }), /^1 Rust lint policy file\n/);+});++test("Rust comments, strings and unrelated edits do not change exceptions", (t) => {+ const decoys = '/* nested /* #[allow(unsafe_code)] */ comment */\nconst EXAMPLE: &str = r##"#[expect(unsafe_code)]"##;\n';+ const before = `${decoys}#[expect(unsafe_code, reason = "brackets: ] [ escaped: \\\"")] fn call() { unsafe { ffi(); } }\nfn constructor() {}`;+ const after = before+ .replace('fn constructor() {}', "fn constructor() { let c = ']'; }")+ .replace('unsafe { ffi(); }', 'unsafe { /* reason */ ffi( ); }')+ .replace('expect(unsafe_code, reason', 'expect(\n unsafe_code,\n reason');+ assert.equal(scan(t, { "src/lib.rs": before }, { "src/lib.rs": after }), "");+});++test("code under deny or forbid can change without changing lint policy", (t) => {+ for (const attribute of ['#![forbid(unsafe_code)]', '#[deny(unsafe_code)]']) {+ const before = `${attribute}\nfn a() { first(); }`;+ assert.equal(scan(t, { "src/lib.rs": before }, { "src/lib.rs": before.replace('first()', 'second()') }), "");+ assert.match(scan(t, { "src/lib.rs": before }, { "src/lib.rs": before.replace('unsafe_code', 'dead_code') }), /^1 Rust lint policy file\n/);+ }+});++test("new untracked Rust exceptions and deleted exception files are scanned", (t) => {+ const source = '#[expect(dead_code, reason = "Shared helper")] fn helper() {}';+ assert.match(scan(t, {}, { "src/lib.rs": source }, { commit: false }), /^1 Rust lint policy file\n/);+ assert.match(scan(t, { "src/lib.rs": source }, { "src/lib.rs": null }), /^1 Rust lint policy file\n/);+});diff --git a/rust/Cargo.toml b/rust/Cargo.tomlindex a9244de706..26a6bb73e9 100644--- a/rust/Cargo.toml+++ b/rust/Cargo.toml@@ -109,7 +109,8 @@ zeroize = { version = "1.9.0", features = ["derive"] }zip = { version = "8.6.0", default-features = false, features = ["deflate-flate2-zlib-rs"] }[workspace.lints.rust]-unexpected_cfgs = { level = "warn", check-cfg = ['cfg(frb_expand)'] }+unexpected_cfgs = { level = "deny", check-cfg = ['cfg(frb_expand)'] }+unsafe_code = "deny"[workspace.lints.clippy]allow_attributes = "deny"diff --git a/rust/apps/ensu/src-tauri/src/commands/notes/source.rs b/rust/apps/ensu/src-tauri/src/commands/notes/source.rsindex 31bc50eb68..f750ab2d39 100644--- a/rust/apps/ensu/src-tauri/src/commands/notes/source.rs+++ b/rust/apps/ensu/src-tauri/src/commands/notes/source.rs@@ -245,6 +245,10 @@ fn open_collection_source(root: &Path, _document_id: &str, path: &Path) -> Resul}#[cfg(windows)]+#[expect(+ unsafe_code,+ reason = "Resolve the opened Windows handle to reject path escapes"+)]fn final_windows_handle_path(file: &File) -> Result<PathBuf, ApiError> {use std::ffi::OsString;use windows_sys::Win32::Foundation::HANDLE;diff --git a/rust/apps/ensu/src-tauri/src/commands/system.rs b/rust/apps/ensu/src-tauri/src/commands/system.rsindex afc4da001f..10997743f4 100644--- a/rust/apps/ensu/src-tauri/src/commands/system.rs+++ b/rust/apps/ensu/src-tauri/src/commands/system.rs@@ -8,6 +8,10 @@ pub struct SystemInfo {}#[cfg(unix)]+#[expect(+ unsafe_code,+ reason = "Read physical memory through the system's sysconf API"+)]fn total_memory_bytes() -> Option<u64> {let pages = unsafe { libc::sysconf(libc::_SC_PHYS_PAGES) };let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) };@@ -21,6 +25,7 @@ fn total_memory_bytes() -> Option<u64> {}#[cfg(target_os = "windows")]+#[expect(unsafe_code, reason = "Read physical memory through the Windows API")]fn total_memory_bytes() -> Option<u64> {use windows_sys::Win32::System::SystemInformation::{GlobalMemoryStatusEx, MEMORYSTATUSEX};diff --git a/rust/bindings/frb/locker/src/lib.rs b/rust/bindings/frb/locker/src/lib.rsindex 7feeb51d78..9f94577e8e 100644--- a/rust/bindings/frb/locker/src/lib.rs+++ b/rust/bindings/frb/locker/src/lib.rs@@ -2,9 +2,10 @@ pub mod api;#[cfg(feature = "flutter")]#[expect(+ unsafe_code,clippy::allow_attributes,clippy::allow_attributes_without_reason,- reason = "Flutter Rust Bridge generates allow attributes without reasons"+ reason = "Flutter Rust Bridge generates unsafe bindings and allow attributes without reasons")]#[rustfmt::skip]mod frb_generated;diff --git a/rust/bindings/frb/photos/src/api/usearch_api.rs b/rust/bindings/frb/photos/src/api/usearch_api.rsindex cecfcfe16b..68f6a337c9 100644--- a/rust/bindings/frb/photos/src/api/usearch_api.rs+++ b/rust/bindings/frb/photos/src/api/usearch_api.rs@@ -7,10 +7,18 @@ type BulkSearchByKeyMatch = (Vec<u64>, Vec<Vec<u64>>, Vec<Vec<f32>>);// USearch already vendors and links SimSIMD. Call that single exported symbol// directly to avoid pulling a second copy of SimSIMD into the final app binary.+#[expect(+ unsafe_code,+ reason = "Declare the SimSIMD symbol already linked by USearch"+)]unsafe extern "C" {fn simsimd_dot_f32(a: *const f32, b: *const f32, n: u64, d: *mut f64);}+#[expect(+ unsafe_code,+ reason = "Call the SimSIMD symbol already linked by USearch"+)]fn simsimd_dot_product(a: &[f32], b: &[f32]) -> f64 {debug_assert_eq!(a.len(), b.len());let mut score = 0.0_f64;diff --git a/rust/bindings/frb/photos/src/lib.rs b/rust/bindings/frb/photos/src/lib.rsindex 7feeb51d78..9f94577e8e 100644--- a/rust/bindings/frb/photos/src/lib.rs+++ b/rust/bindings/frb/photos/src/lib.rs@@ -2,9 +2,10 @@ pub mod api;#[cfg(feature = "flutter")]#[expect(+ unsafe_code,clippy::allow_attributes,clippy::allow_attributes_without_reason,- reason = "Flutter Rust Bridge generates allow attributes without reasons"+ reason = "Flutter Rust Bridge generates unsafe bindings and allow attributes without reasons")]#[rustfmt::skip]mod frb_generated;diff --git a/rust/crates/ensu/src/llm/context.rs b/rust/crates/ensu/src/llm/context.rsindex eaace22f11..fde01da809 100644--- a/rust/crates/ensu/src/llm/context.rs+++ b/rust/crates/ensu/src/llm/context.rs@@ -67,7 +67,15 @@ pub struct Context {pub type ContextRef = Arc<Context>;+#[expect(+ unsafe_code,+ reason = "Retained LLM threading exception; review deferred"+)]unsafe impl Send for Context {}+#[expect(+ unsafe_code,+ reason = "Retained LLM threading exception; review deferred"+)]unsafe impl Sync for Context {}impl Context {diff --git a/rust/crates/ensu/src/retrieval/index.rs b/rust/crates/ensu/src/retrieval/index.rsindex 8506930da6..b7b91074da 100644--- a/rust/crates/ensu/src/retrieval/index.rs+++ b/rust/crates/ensu/src/retrieval/index.rs@@ -162,7 +162,15 @@ impl RetrievalIndex {validate_offsets(&offsets, metadata_len)?;// Native callers prevent file swaps or deletion while mapped.+ #[expect(+ unsafe_code,+ reason = "File-backed memory mapping requires an unsafe call"+ )]let vectors = unsafe { MmapOptions::new().map(&vector_file)? };+ #[expect(+ unsafe_code,+ reason = "File-backed memory mapping requires an unsafe call"+ )]let metadata = unsafe { MmapOptions::new().map(&metadata_file)? };Ok(Self {diff --git a/rust/crates/ml/src/onnx/webgpu.rs b/rust/crates/ml/src/onnx/webgpu.rsindex 7142fe1296..67c46cfe88 100644--- a/rust/crates/ml/src/onnx/webgpu.rs+++ b/rust/crates/ml/src/onnx/webgpu.rs@@ -110,6 +110,10 @@ pub(super) fn check_adapter() -> AdapterCheck {}#[cfg(target_os = "android")]+#[expect(+ unsafe_code,+ reason = "Probe Vulkan device vendors through ash's unsafe API"+)]fn probe_vulkan_vendor_ids() -> Result<Vec<u32>, String> {use ash::vk;