diff options
Diffstat (limited to 'src/api/access.rs')
| -rw-r--r-- | src/api/access.rs | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/api/access.rs b/src/api/access.rs index 9157e1d..47e24ba 100644 --- a/src/api/access.rs +++ b/src/api/access.rs @@ -340,6 +340,10 @@ pub(crate) async fn authorize( else { return error_response(StatusCode::UNAUTHORIZED, "not authenticated", None); }; + #[cfg(feature = "federation")] + if let Some(refused) = federated(&state, &request, &user) { + return refused; + } let access = { let replica = state.doc.read().await; if !registry::exists(&replica.doc) { @@ -389,6 +393,54 @@ pub(crate) async fn authorize( next.run(request).await } +/// On a node holding a federated store, what its users who aren't its operators may do: a +/// user of `--store-readers` reads the federation (`federation_work`), and nothing else. +/// Its other users are its operators, who may do everything. The answer refusing the +/// request, where it is refused. +#[cfg(feature = "federation")] +fn federated(state: &AppState, request: &Request<Body>, user: &str) -> Option<Response> { + if !state.store_readers.contains(user) { + return None; + } + if state.holding.with(|_| ()).is_none() { + return Some(error_response( + StatusCode::FORBIDDEN, + format!("{user} reads a federated store here alone, and this node holds none"), + None, + )); + } + if federation_work(request.method(), request.uri().path(), false) { + return None; + } + debug!(user = %user, method = %request.method(), path = %request.uri().path(), "Refused a reader's request that doesn't read the federation"); + Some(error_response( + StatusCode::FORBIDDEN, + format!( + "{user} reads the federation here, and does nothing else (--store-readers; \ + docs/guides/federation/running-a-node.md, Its API)" + ), + None, + )) +} + +/// Whether a request is federation work: reading the events API (`GET` and `HEAD` of +/// `/events` and below), checking a fork (`POST /events/fork`), and keeping a session +/// (`GET /auth/whoami`, `POST /auth/refresh`, `POST /auth/logout`); with `puts`, putting +/// an event (`POST /events`) too. Nothing of the document (`/store`, `/stream`), the +/// node (`/peers`, `/admin/…`), nor what a node's operator does to its store +/// (`/admin/events/…`). +#[cfg(feature = "federation")] +fn federation_work(method: &Method, path: &str, puts: bool) -> bool { + let reads = matches!(*method, Method::GET | Method::HEAD); + let posts = *method == Method::POST; + match path { + "/auth/whoami" => reads, + "/auth/refresh" | "/auth/logout" | "/events/fork" => posts, + "/events" => reads || (puts && posts), + _ => reads && path.starts_with("/events/"), + } +} + #[cfg(test)] mod tests { use super::*; @@ -402,6 +454,32 @@ mod tests { .collect() } + /// Federation work is reading the events API, checking a fork and keeping a session; + /// putting events where it puts; nothing of the document, the node, or what an + /// operator does to the store. + #[cfg(feature = "federation")] + #[test] + fn federation_work_is_the_events_api() { + let work = |method: Method, path: &str, puts: bool| federation_work(&method, path, puts); + assert!(work(Method::GET, "/events", false)); + assert!(work(Method::GET, "/events/counts", false)); + assert!(work(Method::HEAD, "/events/0c7e", false)); + assert!(work(Method::POST, "/events/fork", false)); + assert!(work(Method::POST, "/auth/refresh", false)); + assert!(work(Method::GET, "/auth/whoami", false)); + assert!(!work(Method::POST, "/events", false)); + assert!(work(Method::POST, "/events", true)); + assert!(!work(Method::POST, "/admin/events/drop", true)); + assert!(!work(Method::POST, "/admin/events/vote", true)); + assert!(!work(Method::GET, "/admin/events/fork", true)); + assert!(!work(Method::GET, "/store", true)); + assert!(!work(Method::GET, "/store/home/tv", true)); + assert!(!work(Method::GET, "/stream", true)); + assert!(!work(Method::GET, "/peers", true)); + assert!(!work(Method::GET, "/eventsx", true)); + assert!(!work(Method::PUT, "/events/0c7e", true)); + } + /// A token's limit narrows what an endpoint takes, not only what the store's handlers /// read: the dashboard's hand-over token (the member list, read) of an operator may /// look, and nothing else. |
