diff --git a/crates/in-core/src/store/sessions.rs b/crates/in-core/src/store/sessions.rs index f443c11..364f757 100644 --- a/crates/in-core/src/store/sessions.rs +++ b/crates/in-core/src/store/sessions.rs @@ -202,6 +202,19 @@ impl Store { Ok(inserted == 1) } + /// Whether a back-channel logout token id is already recorded. + pub async fn jti_recorded(&self, jti: &str) -> Result { + let conn = self.read_conn().await; + let row = fetch_opt( + &conn, + "SELECT 1 FROM seen_logout_jti WHERE jti = ?1", + (jti.to_string(),), + |_| Ok(()), + ) + .await?; + Ok(row.is_some()) + } + /// Removes logout token ids that expired at or before `now`. pub async fn sweep_jti(&self, now: &str) -> Result { let conn = self.inner.conn.lock().await; diff --git a/crates/in-web/src/boot.rs b/crates/in-web/src/boot.rs index 6f4029a..0ca7c90 100644 --- a/crates/in-web/src/boot.rs +++ b/crates/in-web/src/boot.rs @@ -21,7 +21,7 @@ use tokio::net::TcpListener; use tokio::signal::unix::{SignalKind, signal}; use tokio::sync::watch; use topcoat::cookie::RouterBuilderCookieExt; -use topcoat::router::{BodyLimit, Router, RouterBuilderDiscoverExt, RouterService}; +use topcoat::router::{BodyLimit, Router, RouterBuilderDiscoverExt, RouterService, TrustedProxies}; use crate::backup; use crate::directory; @@ -235,7 +235,11 @@ fn read_secret(path: &Path) -> Result { /// headers on every response, and the styled 404 for browsers. pub fn router(state: AppState) -> Router { let security = SecurityHeaders::new(security::headers_for(&state.config.base_url)); + // Behind a reverse proxy, client_ip reads X-Forwarded-For only from these peers; without them + // every client would share the proxy's address and per-IP throttles would become global. + let proxies = TrustedProxies::new().networks(state.config.trusted_proxies.clone()); Router::builder() + .trusted_proxies(proxies) .discover() .app_context(state) .cookies() diff --git a/crates/in-web/src/pages/backchannel.rs b/crates/in-web/src/pages/backchannel.rs index fdfd171..6396cd6 100644 --- a/crates/in-web/src/pages/backchannel.rs +++ b/crates/in-web/src/pages/backchannel.rs @@ -50,16 +50,14 @@ pub enum Outcome { Replayed, } -/// Ends the sessions a verified logout token names, after recording its `jti`. +/// Ends the sessions a verified logout token names, then records its `jti`. /// -/// The `jti` is recorded first, and the insert decides: two deliveries of one token cannot both be -/// acted on, and a replay ends nothing. The `sid` names one `im` session; a token with only `sub` ends -/// every session of the person. +/// A recorded `jti` is a replay and ends nothing. The `jti` is recorded only after the sessions are +/// gone: if ending them fails, `im` retries the same token and it is acted on again, so a transient +/// failure never leaves sessions alive. Two parallel deliveries may both delete, which is harmless. +/// The `sid` names one `im` session; a token with only `sub` ends every session of the person. pub async fn end_sessions(store: &Store, claims: &LogoutClaims) -> CoreResult { - if !store - .seen_jti(&claims.jti, &stamp_after(REPLAY_WINDOW)) - .await? - { + if store.jti_recorded(&claims.jti).await? { return Ok(Outcome::Replayed); } let ended = match (&claims.sid, &claims.sub) { @@ -67,6 +65,9 @@ pub async fn end_sessions(store: &Store, claims: &LogoutClaims) -> CoreResult store.delete_sessions_by_sub(sub).await?, (None, None) => 0, }; + store + .seen_jti(&claims.jti, &stamp_after(REPLAY_WINDOW)) + .await?; Ok(Outcome::Ended(ended)) } diff --git a/crates/in-web/src/pages/photo.rs b/crates/in-web/src/pages/photo.rs index cde51a7..df4a47b 100644 --- a/crates/in-web/src/pages/photo.rs +++ b/crates/in-web/src/pages/photo.rs @@ -182,7 +182,7 @@ async fn photo(cx: &Cx) -> Result { let state = app_context::(cx); let segments: Vec<&str> = uri(cx).path().split('/').collect(); let sub = match segments.as_slice() { - ["", "people", sub, "photo"] if valid_sub(sub) => *sub, + ["", "avatar", sub] if valid_sub(sub) => *sub, _ => return Err(not_found().into()), }; if current_member(cx).await?.is_none() {