Back-channel logout records the jti only after sessions end
Browse files at this commit0f1ee24115bf7ffe76a844377ecdae17fcc7e646Parent: 60b492c- fix
- back-channel logout records the jti only after sessions end
- honour trusted_proxies
- avatar route matches its path
4 changed files +28 -10
- crates/in-core/src/store/sessions.rs +13 -0
- crates/in-web/src/boot.rs +5 -1
- crates/in-web/src/pages/backchannel.rs +9 -8
- crates/in-web/src/pages/photo.rs +1 -1
crates/in-core/src/store/sessions.rsmodified+13 -0
| @@ -202,6 +202,19 @@ impl Store { | |||
| 202 | Ok(inserted == 1) | 202 | Ok(inserted == 1) |
| 203 | } | 203 | } |
| 204 | 204 | ||
| 205 | + /// Whether a back-channel logout token id is already recorded. | ||
| 206 | + pub async fn jti_recorded(&self, jti: &str) -> Result<bool> { | ||
| 207 | + let conn = self.read_conn().await; | ||
| 208 | + let row = fetch_opt( | ||
| 209 | + &conn, | ||
| 210 | + "SELECT 1 FROM seen_logout_jti WHERE jti = ?1", | ||
| 211 | + (jti.to_string(),), | ||
| 212 | + |_| Ok(()), | ||
| 213 | + ) | ||
| 214 | + .await?; | ||
| 215 | + Ok(row.is_some()) | ||
| 216 | + } | ||
| 217 | + | ||
| 205 | /// Removes logout token ids that expired at or before `now`. | 218 | /// Removes logout token ids that expired at or before `now`. |
| 206 | pub async fn sweep_jti(&self, now: &str) -> Result<u64> { | 219 | pub async fn sweep_jti(&self, now: &str) -> Result<u64> { |
| 207 | let conn = self.inner.conn.lock().await; | 220 | let conn = self.inner.conn.lock().await; |
crates/in-web/src/boot.rsmodified+5 -1
| @@ -21,7 +21,7 @@ use tokio::net::TcpListener; | |||
| 21 | use tokio::signal::unix::{SignalKind, signal}; | 21 | use tokio::signal::unix::{SignalKind, signal}; |
| 22 | use tokio::sync::watch; | 22 | use tokio::sync::watch; |
| 23 | use topcoat::cookie::RouterBuilderCookieExt; | 23 | use topcoat::cookie::RouterBuilderCookieExt; |
| 24 | -use topcoat::router::{BodyLimit, Router, RouterBuilderDiscoverExt, RouterService}; | 24 | +use topcoat::router::{BodyLimit, Router, RouterBuilderDiscoverExt, RouterService, TrustedProxies}; |
| 25 | 25 | ||
| 26 | use crate::backup; | 26 | use crate::backup; |
| 27 | use crate::directory; | 27 | use crate::directory; |
| @@ -235,7 +235,11 @@ fn read_secret(path: &Path) -> Result<String> { | |||
| 235 | /// headers on every response, and the styled 404 for browsers. | 235 | /// headers on every response, and the styled 404 for browsers. |
| 236 | pub fn router(state: AppState) -> Router { | 236 | pub fn router(state: AppState) -> Router { |
| 237 | let security = SecurityHeaders::new(security::headers_for(&state.config.base_url)); | 237 | let security = SecurityHeaders::new(security::headers_for(&state.config.base_url)); |
| 238 | + // Behind a reverse proxy, client_ip reads X-Forwarded-For only from these peers; without them | ||
| 239 | + // every client would share the proxy's address and per-IP throttles would become global. | ||
| 240 | + let proxies = TrustedProxies::new().networks(state.config.trusted_proxies.clone()); | ||
| 238 | Router::builder() | 241 | Router::builder() |
| 242 | + .trusted_proxies(proxies) | ||
| 239 | .discover() | 243 | .discover() |
| 240 | .app_context(state) | 244 | .app_context(state) |
| 241 | .cookies() | 245 | .cookies() |
crates/in-web/src/pages/backchannel.rsmodified+9 -8
| @@ -50,16 +50,14 @@ pub enum Outcome { | |||
| 50 | Replayed, | 50 | Replayed, |
| 51 | } | 51 | } |
| 52 | 52 | ||
| 53 | -/// Ends the sessions a verified logout token names, after recording its `jti`. | 53 | +/// Ends the sessions a verified logout token names, then records its `jti`. |
| 54 | /// | 54 | /// |
| 55 | -/// The `jti` is recorded first, and the insert decides: two deliveries of one token cannot both be | 55 | +/// A recorded `jti` is a replay and ends nothing. The `jti` is recorded only after the sessions are |
| 56 | -/// acted on, and a replay ends nothing. The `sid` names one `im` session; a token with only `sub` ends | 56 | +/// gone: if ending them fails, `im` retries the same token and it is acted on again, so a transient |
| 57 | -/// every session of the person. | 57 | +/// failure never leaves sessions alive. Two parallel deliveries may both delete, which is harmless. |
| 58 | +/// The `sid` names one `im` session; a token with only `sub` ends every session of the person. | ||
| 58 | pub async fn end_sessions(store: &Store, claims: &LogoutClaims) -> CoreResult<Outcome> { | 59 | pub async fn end_sessions(store: &Store, claims: &LogoutClaims) -> CoreResult<Outcome> { |
| 59 | - if !store | 60 | + if store.jti_recorded(&claims.jti).await? { |
| 60 | - .seen_jti(&claims.jti, &stamp_after(REPLAY_WINDOW)) | ||
| 61 | - .await? | ||
| 62 | - { | ||
| 63 | return Ok(Outcome::Replayed); | 61 | return Ok(Outcome::Replayed); |
| 64 | } | 62 | } |
| 65 | let ended = match (&claims.sid, &claims.sub) { | 63 | let ended = match (&claims.sid, &claims.sub) { |
| @@ -67,6 +65,9 @@ pub async fn end_sessions(store: &Store, claims: &LogoutClaims) -> CoreResult<Ou | |||
| 67 | (None, Some(sub)) => store.delete_sessions_by_sub(sub).await?, | 65 | (None, Some(sub)) => store.delete_sessions_by_sub(sub).await?, |
| 68 | (None, None) => 0, | 66 | (None, None) => 0, |
| 69 | }; | 67 | }; |
| 68 | + store | ||
| 69 | + .seen_jti(&claims.jti, &stamp_after(REPLAY_WINDOW)) | ||
| 70 | + .await?; | ||
| 70 | Ok(Outcome::Ended(ended)) | 71 | Ok(Outcome::Ended(ended)) |
| 71 | } | 72 | } |
| 72 | 73 | ||
crates/in-web/src/pages/photo.rsmodified+1 -1
| @@ -182,7 +182,7 @@ async fn photo(cx: &Cx) -> Result<PhotoAnswer> { | |||
| 182 | let state = app_context::<AppState>(cx); | 182 | let state = app_context::<AppState>(cx); |
| 183 | let segments: Vec<&str> = uri(cx).path().split('/').collect(); | 183 | let segments: Vec<&str> = uri(cx).path().split('/').collect(); |
| 184 | let sub = match segments.as_slice() { | 184 | let sub = match segments.as_slice() { |
| 185 | - ["", "people", sub, "photo"] if valid_sub(sub) => *sub, | 185 | + ["", "avatar", sub] if valid_sub(sub) => *sub, |
| 186 | _ => return Err(not_found().into()), | 186 | _ => return Err(not_found().into()), |
| 187 | }; | 187 | }; |
| 188 | if current_member(cx).await?.is_none() { | 188 | if current_member(cx).await?.is_none() { |