Cotton code
178 lines · 5.8 KB · RustRaw
//! The port of `apps/cotton/__tests__/replica/serverAddress.test.ts` and the//! address half of `serverAddressSeed.security.test.ts`.use super::*;#[test]fn an_https_origin_is_admitted_and_comes_back_without_its_trailing_slash() {    assert_eq!(        normalize_server_url("https://studio.relay.cottondb.com/"),        Some("https://studio.relay.cottondb.com".to_string())    );    assert_eq!(        normalize_server_url("  https://api.cottondb.com  "),        Some("https://api.cottondb.com".to_string())    );}#[test]fn plain_http_is_this_machine_or_this_house_and_nowhere_else() {    for admitted in [        // Loopback: a development server on this desk.        "http://localhost:8790",        "http://127.0.0.1:8790",        "http://[::1]:8790",        // The home network: a Mac the phone is standing next to. All three        // private ranges, because the Mac offers whichever interface it is on.        "http://192.168.1.10:8790",        "http://10.0.0.4:8790",        "http://172.16.5.9:8790",        "http://172.31.255.255:8790",        // What Bonjour resolves that Mac to.        "http://studio.local:8790",        "http://Studio.local:8790",    ] {        assert!(            normalize_server_url(admitted).is_some(),            "{admitted} is a server on this desk or in this house"        );    }    for refused in [        "http://api.cottondb.com",        "http://0.0.0.0:8790",        "http://example.invalid",        // Just outside the three ranges, each side.        "http://172.15.0.1:8790",        "http://172.32.0.1:8790",        "http://11.0.0.1:8790",        "http://192.169.1.1:8790",        // A public host that merely ENDS in `.local`, and a multi-label name        // mDNS never mints: a suffix test alone would admit both.        "http://mac.evil.example.local:8790",        "http://a.b.local:8790",        "http://.local:8790",    ] {        assert_eq!(            normalize_server_url(refused),            None,            "{refused} would send the device signature in the clear"        );    }}#[test]fn https_is_admitted_on_a_home_address_too_because_the_rule_widened_only_http() {    // Widening http did not narrow https: a Mac holding a certificate for its    // own LAN address is still an address, and the trade the http rule makes is    // about the ones that cannot.    assert_eq!(        normalize_server_url("https://192.168.1.10:8790"),        Some("https://192.168.1.10:8790".to_string())    );}#[test]fn the_port_survives_because_an_origin_is_scheme_host_and_port() {    assert_eq!(        normalize_server_url("http://localhost:8790"),        Some("http://localhost:8790".to_string())    );}#[test]fn credentials_a_query_a_fragment_and_a_path_are_all_refused() {    for refused in [        "https://user:pass@api.cottondb.com",        "https://user@api.cottondb.com",        "https://api.cottondb.com?x=1",        "https://api.cottondb.com#x",        "https://api.cottondb.com/v1",        "https://api.cottondb.com/some/page",    ] {        assert_eq!(normalize_server_url(refused), None, "{refused}");    }}#[test]fn anything_that_is_not_a_url_at_all_is_not_an_address() {    for refused in [        "",        "   ",        "studio",        "studio.relay.cottondb.com",        "ftp://api.cottondb.com",        "cotton-pair:{}",    ] {        assert_eq!(normalize_server_url(refused), None, "{refused:?}");    }}#[test]fn the_live_address_is_the_base_with_its_scheme_swapped() {    assert_eq!(        live_socket_url("https://studio.relay.cottondb.com"),        Some("wss://studio.relay.cottondb.com/v1/live".to_string())    );    assert_eq!(        live_socket_url("http://localhost:8790/"),        Some("ws://localhost:8790/v1/live".to_string())    );    assert_eq!(live_socket_url(""), None);}#[test]fn the_pairing_routes_are_the_ones_a_refused_device_may_still_call() {    for pairing in [        "/v1/devices/pair",        "/v1/devices/pair/abc-123",        "/v1/devices/pair?x=1",    ] {        assert!(is_pairing_route(pairing), "{pairing}");    }    // The long-poll carries a query, and a query is part of the SIGNED path —    // so the exemption has to recognise it there too.    assert!(is_pairing_route("/v1/devices/pair/prg_01JAAA?wait=25"));    // An invite's claim is the way into a shared database at another Mac,    // with or without its signed query.    assert!(is_pairing_route("/v1/invites/inv-1/claim"));    assert!(is_pairing_route("/v1/invites/inv-1/claim?database=garden"));    for owner in [        "/v1/invites",        "/v1/invites/inv-1",        "/v1/invites/inv-1/claims",        "/v1/devices/pending",        "/v1/devices/approved",        "/v1/devices/pairs",        "/v1/sync/pull",        "/v1/me",        "/v1/live",    ] {        assert!(            !is_pairing_route(owner),            "{owner} is an owner route and a standing refusal stops it"        );    }}#[test]fn an_attach_address_carries_the_session_in_the_query_the_signature_covers() {    assert_eq!(        attach_socket_url("https://me.relay.cottondb.com", "5f0c9a1e-0000-4000-8000-000000000000")            .as_deref(),        Some("wss://me.relay.cottondb.com/v1/sessions/attach?session=5f0c9a1e-0000-4000-8000-000000000000")    );    assert_eq!(        attach_socket_url("http://127.0.0.1:8080/", "s1").as_deref(),        Some("ws://127.0.0.1:8080/v1/sessions/attach?session=s1")    );}#[test]fn a_session_id_outside_the_alphabet_is_refused_rather_than_escaped() {    // What a device signs is the path AND the query as the server reads them    // back; a character one side encoded and the other did not is a signature    // over an address nobody asked for.    for bad in ["", "a b", "a/b", "a?b", "a&b=1", "a%2Fb", "café"] {        assert_eq!(attach_socket_url("https://me.relay.cottondb.com", bad), None, "{bad}");    }}