diff --git a/.agents/features/join-page.md b/.agents/features/join-page.md new file mode 100644 index 0000000..5284758 --- /dev/null +++ b/.agents/features/join-page.md @@ -0,0 +1,39 @@ +# The join page — an invite link a messenger can open + +## 1. Why and what +Amicitia (the app this relay serves) makes connecting seamless: *Invite Petr* opens the phone's +share sheet with a message holding the invite link, and Petr taps it (Amicitia's +`.agents/features/seamless-invite.md`). Messengers make `https://` links tappable, not +`amicitia://`. So the link is `https:///join#.`, and the relay serves the +page it opens: one button, **Open Amicitia**, that hands the invite to the app. + +## 2. Design +```mermaid +sequenceDiagram + participant B as Browser + participant R as Relay + participant A as App + B->>R: GET /join (the fragment #id.secret is never sent) + R-->>B: static page: CSP default-src 'none' + hashes, no-store, no-referrer + B->>A: tap → amicitia://invite?relay=#id.secret +``` +- `crates/relay/assets/join.html`, served by `crates/relay/src/join.rs`. The CSP names the page's + only script and style by SHA-256, computed from the page itself at start-up, so an edit to the + page cannot run without its hash. +- The script checks the fragment's shape (22 + 43 base64url characters) and only then sets the + button's target; anything else shows *This invite link is incomplete*. +- No automatic redirect: Chrome refuses a custom scheme opened without a tap. + +## 3. Risks +- **Privacy:** the relay learns that an address opened `/join` — no more than any client tells it. + It stores nothing and prints no request. The secret stays in the fragment. A messenger's link + preview fetches `/join` only. +- **The page as an attack surface:** static, no input read on the server, no cookie, framing + refused, nothing loaded from elsewhere. + +## 4. Verification +| Case | How | +|:--|:--| +| Static, strict headers, nothing external, no cookie | `the_join_page_is_static_and_loads_nothing_from_elsewhere` | +| The policy's hashes match the page's script and style | `the_join_page_policy_hashes_its_own_script_and_style` | +| The script builds the app link from a good fragment and refuses a bad one | Node, by hand (in the PR); Amicitia's phone test end to end | diff --git a/Cargo.lock b/Cargo.lock index 67f29cf..37ab854 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2276,11 +2276,13 @@ name = "tabellarius-relay" version = "0.0.0" dependencies = [ "axum", + "base64", "getrandom 0.3.4", "http-body-util", "rusqlite", "serde", "serde_json", + "sha2 0.10.9", "subtle", "tabellarius-protocol", "tempfile", diff --git a/crates/relay/Cargo.toml b/crates/relay/Cargo.toml index edcaf0a..38e47ec 100644 --- a/crates/relay/Cargo.toml +++ b/crates/relay/Cargo.toml @@ -15,6 +15,9 @@ rusqlite = { version = "0.41", features = ["bundled"] } serde = "1" serde_json = "1" subtle = "2" +# The join page's Content-Security-Policy names its inline script and style by SHA-256 (base64). +sha2 = "0.10" +base64 = "0.22" [dev-dependencies] getrandom = "0.3" diff --git a/crates/relay/assets/join.html b/crates/relay/assets/join.html new file mode 100644 index 0000000..a81ed39 --- /dev/null +++ b/crates/relay/assets/join.html @@ -0,0 +1,45 @@ + + + + + + + +An invite to a private chat + + + +
+

You're invited to a private chat

+

Someone who knows you invited you to chat in Amicitia, end to end encrypted.

+Open Amicitia +

Don't have Amicitia yet? Ask the person who invited you how to get it, then tap their message again.

+ +
+ + + diff --git a/crates/relay/src/http.rs b/crates/relay/src/http.rs index bba8c31..53348a1 100644 --- a/crates/relay/src/http.rs +++ b/crates/relay/src/http.rs @@ -38,6 +38,7 @@ pub fn router(state: AppState) -> Router { .route("/v0/invites", post(create_invite)) .route("/v0/invites/{id}", get(claim_invite)) .route("/version", get(version)) + .route("/join", get(crate::join::page)) .layer(DefaultBodyLimit::max(MAX_JSON_BYTES)) .merge(blob_routes) .with_state(state) diff --git a/crates/relay/src/join.rs b/crates/relay/src/join.rs new file mode 100644 index 0000000..93600fd --- /dev/null +++ b/crates/relay/src/join.rs @@ -0,0 +1,57 @@ +//! `GET /join` (PROTOCOL.md §8, added 2026-11-18): the page an invite link opens in a browser. It +//! hands the invite, which sits in the URL's fragment and so never reaches this server, to the app +//! as `amicitia://invite?relay=#.`. Static: it reads nothing, stores +//! nothing, sets no cookie and loads nothing from anywhere else. + +use std::sync::LazyLock; + +use axum::http::header; +use axum::response::{IntoResponse, Response}; +use base64::Engine; +use sha2::{Digest, Sha256}; + +pub const PAGE: &str = include_str!("../assets/join.html"); + +/// The text between `` and `` in the page (one of each), exactly as the browser hashes it. +fn inline(tag: &str) -> &'static str { + let open = format!("<{tag}>"); + let close = format!(""); + let start = PAGE.find(&open).expect("the page has the tag") + open.len(); + let end = PAGE[start..].find(&close).expect("the tag is closed") + start; + &PAGE[start..end] +} + +fn hash(text: &str) -> String { + let digest = Sha256::digest(text.as_bytes()); + format!( + "'sha256-{}'", + base64::engine::general_purpose::STANDARD.encode(digest) + ) +} + +/// Nothing but the page's own script and style, each by its hash: no other script, no image, no +/// font, no connection, no frame, no form. +pub static CONTENT_SECURITY_POLICY: LazyLock = LazyLock::new(|| { + format!( + "default-src 'none'; script-src {}; style-src {}; base-uri 'none'; form-action 'none'; frame-ancestors 'none'", + hash(inline("script")), + hash(inline("style")) + ) +}); + +pub async fn page() -> Response { + ( + [ + (header::CONTENT_TYPE, "text/html; charset=utf-8"), + ( + header::CONTENT_SECURITY_POLICY, + CONTENT_SECURITY_POLICY.as_str(), + ), + (header::CACHE_CONTROL, "no-store"), + (header::REFERRER_POLICY, "no-referrer"), + (header::X_CONTENT_TYPE_OPTIONS, "nosniff"), + ], + PAGE, + ) + .into_response() +} diff --git a/crates/relay/src/lib.rs b/crates/relay/src/lib.rs index 3c9c6e1..e17bf8c 100644 --- a/crates/relay/src/lib.rs +++ b/crates/relay/src/lib.rs @@ -2,6 +2,7 @@ //! acknowledges it. Stores exactly `docs/PROTOCOL.md` §5 and logs no request. pub mod http; +pub mod join; pub mod store; use std::net::SocketAddr; diff --git a/crates/relay/tests/relay.rs b/crates/relay/tests/relay.rs index 0558d53..f8fbedb 100644 --- a/crates/relay/tests/relay.rs +++ b/crates/relay/tests/relay.rs @@ -741,3 +741,98 @@ async fn no_random_request_breaks_the_relay() { assert_eq!(status, StatusCode::NOT_FOUND); } } + +/// `GET /join` (PROTOCOL.md §8, 2026-11-18): a static page that hands the fragment to the app. It +/// must load nothing from elsewhere, run nothing but its own script, and keep nothing. +#[tokio::test] +async fn the_join_page_is_static_and_loads_nothing_from_elsewhere() { + use tower::ServiceExt; + let relay = Relay::in_memory(); + let response = relay + .app + .clone() + .oneshot(Request::get("/join").body(Body::empty()).expect("request")) + .await + .expect("response"); + assert_eq!(response.status(), StatusCode::OK); + let h = response.headers().clone(); + let get = |name: &str| { + h.get(name) + .and_then(|v| v.to_str().ok()) + .unwrap_or("") + .to_string() + }; + assert_eq!(get("content-type"), "text/html; charset=utf-8"); + assert_eq!(get("cache-control"), "no-store"); + assert_eq!(get("referrer-policy"), "no-referrer"); + assert_eq!(get("x-content-type-options"), "nosniff"); + assert!(h.get("set-cookie").is_none()); + let csp = get("content-security-policy"); + assert!( + csp.starts_with("default-src 'none'; script-src 'sha256-"), + "{csp}" + ); + for part in [ + "base-uri 'none'", + "form-action 'none'", + "frame-ancestors 'none'", + ] { + assert!(csp.contains(part), "{csp} lacks {part}"); + } + let body = String::from_utf8( + response + .into_body() + .collect() + .await + .expect("body") + .to_bytes() + .to_vec(), + ) + .expect("utf-8"); + // Nothing loaded from anywhere, and nothing the script could send the fragment with or leave + // the page by on its own (a custom scheme must wait for the person's tap). + for banned in [ + "http://", + "https://", + " src=", + "").count(), 1); + assert_eq!(body.matches(""), + ] { + let b64 = base64::engine::general_purpose::STANDARD.encode(Sha256::digest(text.as_bytes())); + assert!(csp.contains(&format!("'sha256-{b64}'")), "{csp}"); + } +} diff --git a/docs/PROTOCOL.md b/docs/PROTOCOL.md index 70bff33..36bf066 100644 --- a/docs/PROTOCOL.md +++ b/docs/PROTOCOL.md @@ -89,7 +89,7 @@ The platform facts above are Claude's understanding, to be verified in `.agents/ - Parsers are fuzzed (`cargo fuzz`) for every message type. - Every check in `VERIFY.md` §1 has a CI job or a documented manual procedure. -## 8. The wire (v0) — approved 2026-11-08, routes added 2026-11-14, quotas 2026-11-16 +## 8. The wire (v0) — approved 2026-11-08, routes added 2026-11-14, quotas 2026-11-16, join page 2026-11-18 HTTP/1.1 behind TLS (the reverse proxy, F23). Ids and tokens are unpadded base64url of a fixed length with one canonical spelling: ids are 16 bytes, and tokens and hashes are 32. JSON bodies refuse unknown fields. A blob is at most 256 KiB, and a KeyPackage at most 16 KiB. @@ -104,6 +104,7 @@ unknown fields. A blob is at most 256 KiB, and a KeyPackage at most 16 KiB. | `POST /v0/invites` `{id, secretHash, keyPackage}` | none | stores an invite. It expires after 7 days, rounded up to the hour | | `GET /v0/invites/{id}` | invite secret | returns the KeyPackage once, then deletes the invite | | `GET /version` | none | the protocol and relay versions | +| `GET /join` | none | a static page for an invite link opened in a browser *(added 2026-11-18)* | - A wrong token, an unknown id and a malformed id all give the same bare **404**, so nobody can probe what exists. @@ -113,6 +114,13 @@ unknown fields. A blob is at most 256 KiB, and a KeyPackage at most 16 KiB. limit the answer is a bare **507**. The per-mailbox caps are checked only after the post token, so a 507 tells a stranger nothing about a mailbox. Nothing new is stored to count: the counts come from §5's own rows and `size`. A client treats 507 like any failure and tries again later. +- **The join page** *(added 2026-11-18; `.agents/features/join-page.md`)*: an invite can travel as + `https:///join#.`, which messengers make tappable. The browser sends + only `GET /join`: the fragment, and so the secret, never reaches the relay, a link preview or a + proxy. The page is static (one inline script and style, named by hash in a + `Content-Security-Policy` of `default-src 'none'`; `no-store`; `no-referrer`; no cookie) and + points its one button at `amicitia://invite?relay=#.`. The relay stores + nothing for it and learns only that some address opened the page. - A refusal has no body. - The relay prints no request. - Tests: `crates/relay/tests/relay.rs`, `crates/protocol/tests/wire.rs`.