Cotton code
217 lines · 8.4 KB · RustRaw
//! The one rule the CLIENT build exists to keep: an iPhone links no server.//!//! Design P2. The server and its Mac host build are gone; the one build left//! (`--no-default-features`, the one the XCFramework's iOS slices are built//! from) runs no server, and a build that quietly picked a server stack up//! would be a TLS front end inside a process that will never terminate a//! connection for anybody — invisible until somebody measured the binary.//!//! **This reads the RESOLVED DEPENDENCY GRAPH, not the source.** A grep over//! `use` lines answers a different question: a crate can arrive through a//! feature nobody named, through a dependency's dependency, or through a//! `default-features` somebody forgot to turn off, and in none of those cases//! does this crate's source mention it. `cargo tree` with exactly the flags the//! XCFramework build uses is the only reading that cannot be wrong about what//! ships.//!//! The runnable proof a person can see for themselves://!//!   cd crates//!   cargo tree -p cotton-ffi -e normal --no-default-features \//!     | grep -E "axum|hyper|tower|rustls|mupdf|cotton-server"//!//! must print nothing on an Apple target. Non-Apple clients use rustls for//! outbound TLS, with no server/ACME stack. `cotton-tools`' own boundary (`dependency_boundary.rs`)//! is the sibling of this one, a layer down.use std::collections::BTreeSet;/// What may never appear in the client build, however it arrives — named so a red/// run says which, rather than saying "something is bigger than it was".////// Every one of these is a SERVER's dependency. They are not banned from the/// repository; they are banned from this graph.////// **`x25519-dalek` was on this list and is deliberately off it** (schema v6,/// the vault). It was banned as the Mac backup's age identity — a server's/// concern. It is now the phone's own: a device mints an X25519 vault key,/// keeps the private half in its enclave-backed keychain, and the workspace's/// master key reaches it as a wrap made to that public half/// (`cotton-core/src/vault/wrap.rs`). A phone that could not do X25519 could/// not open a single secret of its owner's, which is the whole point of/// holding them in the database at all. The line this file draws is "an/// iPhone links no SERVER", not "an iPhone does no cryptography".const BANNED: &[&str] = &[    // The server itself, and the crate that would drag every one of the rest in.    "cotton-server",    // The HTTP server stack.    "axum",    "hyper",    "hyper-util",    "tower",    "tower-http",    // The document pipeline's vendored C tree. This one alone is most of the    // 100.8 MiB the Mac's archive measures.    "mupdf",    "mupdf-sys",    // TLS front-end, ACME and certificate generation. rustls itself also    // serves outbound TLS on Android/Linux; the target-aware test holds that    // exception while keeping it out of the iPhone graph.    "tokio-rustls",    "rustls-pemfile",    "rcgen",    "x509-parser",    "instant-acme",    // An async WebSocket client with rustls under it.    "tokio-tungstenite",    // The HTTP CLIENT, which is here only because APNs needs http2 — a push    // rail is the server's, and a phone RECEIVES pushes rather than sending    // them.    "reqwest",    // The model lane.    "cotton-ai",    // Not `zip`: a phone writes and reads a `.cottonpkg` itself    // (`package.export`, `package.import`), and the workspace builds it with    // pure-Rust deflate only — no C, nothing a server alone would want.];/// The resolved normal-dependency graph of one feature set, as crate names.////// `-e normal` on purpose: a dev-dependency is a test's, never a shipped one,/// and `cotton-client`'s own suites legitimately link a whole server to prove/// an import lands on the far side.fn graph_for_package(package: &str, target: Option<&str>, features: &[&str], default_features: bool) -> BTreeSet<String> {    let mut command = std::process::Command::new(env!("CARGO"));    command        .current_dir(cotton_store::manifest_dir().join(".."))        .args([            "tree",            "-p",            package,            "-e",            "normal",            "--prefix",            "none",        ]);    if let Some(target) = target {        command.args(["--target", target]);    }    if !default_features {        command.arg("--no-default-features");    }    if !features.is_empty() {        command.args(["--features", &features.join(",")]);    }    let output = command        .output()        .expect("cargo is on PATH — this guard reads the resolved graph and cannot run without it");    assert!(        output.status.success(),        "cargo tree failed, so this guard read NOTHING rather than reading a clean graph:\n{}",        String::from_utf8_lossy(&output.stderr)    );    let text = String::from_utf8_lossy(&output.stdout);    let names: BTreeSet<String> = text        .lines()        .filter_map(|line| line.split_whitespace().next())        .filter(|name| !name.is_empty() && *name != "[build-dependencies]")        .map(str::to_string)        .collect();    // A LOUD null read (the repo's own rule for a guard that reads something    // outside itself): an empty or implausibly small graph means the command    // moved, not that the boundary holds.    assert!(        names.contains("cotton-ffi") && names.contains("cotton-store") && names.len() > 20,        "cargo tree answered {} crate names, which is not a real graph — this guard would have \         gone quiet rather than red",        names.len()    );    names}fn graph_for(target: Option<&str>, features: &[&str], default_features: bool) -> BTreeSet<String> {    graph_for_package("cotton-ffi", target, features, default_features)}fn graph(features: &[&str], default_features: bool) -> BTreeSet<String> {    graph_for(None, features, default_features)}#[test]fn each_phone_uses_only_its_client_tls_stack() {    for (target, apple) in [        ("aarch64-apple-ios", true),        ("aarch64-linux-android", false),        ("armv7-linux-androideabi", false),        ("i686-linux-android", false),        ("x86_64-linux-android", false),    ] {        let phone = graph_for(Some(target), &[], false);        for banned in BANNED {            assert!(                !phone.contains(*banned),                "{target} links server dependency {banned}"            );        }        assert_eq!(            phone.contains("native-tls"),            apple,            "{target}: platform TLS"        );        // RUSTLS OFF APPLE ONLY: the iPhone's every origin is the system        // stack's to verify; other targets take rustls and its roots.        assert_eq!(phone.contains("rustls"), !apple, "{target}: client TLS");        assert_eq!(phone.contains("webpki-roots"), !apple, "{target}: trust roots");        assert!(            !phone.contains("openssl-sys"),            "{target} needs system OpenSSL"        );    }}#[test]fn the_client_build_links_no_server() {    let phone = graph(&[], false);    let found: Vec<&&str> = BANNED        .iter()        .filter(|banned| phone.contains(**banned))        .collect();    assert!(        found.is_empty(),        "the client build links {found:?}. Something turned a server dependency on: check \         that it is `optional = true` in the crate that declares it."    );}#[test]fn the_client_half_the_phone_keeps_is_still_all_there() {    // The other direction, and the reason this file is not just a ban list: a    // "smaller build" that had lost the store, the device key or the runtime    // would pass the test above and be useless.    let phone = graph(&[], false);    for needed in [        "cotton-store",        "cotton-core",        "cotton-client",        "cotton-tools",        "rusqlite",        "ed25519-dalek",        "tokio",        "serde_json",    ] {        assert!(phone.contains(needed), "the client build has no {needed}");    }}#[test]fn the_android_jni_adapter_links_the_client_graph() {    for target in ["aarch64-linux-android", "armv7-linux-androideabi", "i686-linux-android", "x86_64-linux-android"] {        let graph = graph_for_package("cotton-android-jni", Some(target), &[], false);        assert!(graph.contains("cotton-android-jni"));        assert!(graph.contains("jni"));        assert!(graph.contains("rustls"));        for banned in BANNED {            assert!(!graph.contains(*banned), "{target}: JNI links {banned}");        }    }}