diff --git a/backend/tests/duplicate_session_cookie.rs b/backend/tests/duplicate_session_cookie.rs new file mode 100644 index 0000000000..8122e27b6d --- /dev/null +++ b/backend/tests/duplicate_session_cookie.rs @@ -0,0 +1,38 @@ +//! A browser can send two `token` cookies: this instance's, and one a parent-domain instance set +//! with `Domain=` (app.windmill.dev on `.windmill.dev`, seen from a tenant subdomain). Safari +//! sends the host's cookie first and Chrome last, so the request must authenticate on whichever +//! is a live session here, in either order. + +use reqwest::StatusCode; +use sqlx::{Pool, Postgres}; +use windmill_test_utils::*; + +#[sqlx::test(fixtures("base"))] +async fn test_duplicate_session_cookie_uses_the_live_one(db: Pool) -> anyhow::Result<()> { + initialize_tracing().await; + + let server = ApiServer::start(db.clone()).await?; + let client = reqwest::Client::new(); + let url = format!( + "http://localhost:{}/api/workspaces/users", + server.addr.port() + ); + let status = |cookie: &'static str| { + let req = client.get(&url).header("Cookie", cookie); + async move { anyhow::Ok(req.send().await?.status()) } + }; + + assert_eq!( + status("token=FOREIGN_INSTANCE_TOKEN; token=SECRET_TOKEN").await?, + StatusCode::OK + ); + assert_eq!( + status("token=SECRET_TOKEN; token=FOREIGN_INSTANCE_TOKEN").await?, + StatusCode::OK + ); + assert_eq!( + status("token=FOREIGN_INSTANCE_TOKEN; token=OTHER_FOREIGN_TOKEN").await?, + StatusCode::UNAUTHORIZED + ); + Ok(()) +} diff --git a/backend/windmill-api-auth/src/auth.rs b/backend/windmill-api-auth/src/auth.rs index 889f1f09a0..32b31f771a 100644 --- a/backend/windmill-api-auth/src/auth.rs +++ b/backend/windmill-api-auth/src/auth.rs @@ -41,6 +41,8 @@ lazy_static::lazy_static! { pub static ref AUTH_CACHE: Cache<(String, String), ExpiringAuthCache> = Cache::new(300); // Cache for token -> email lookups (for non-workspace-member authenticated users) static ref TOKEN_EMAIL_CACHE: Cache, std::time::Instant)> = Cache::new(500); + // Joined hashes of a request's duplicate `token` cookies -> index of the live one. + static ref PICKED_SESSION_CACHE: Cache, std::time::Instant)> = Cache::new(500); } /// A token keeps its identity when a superadmin moves the account to another address, so entries @@ -956,6 +958,69 @@ async fn admit_and_record_guest_jwt(db: &DB, w_id: &str, email: &str, app_path: true } +/// Browsers send one or two; checking more would let one request try many guessed tokens +/// while `BRUTE_FORCE_COUNTER` counts a single failure. +const MAX_SESSION_COOKIES: usize = 4; + +/// The first `token` cookie values, parsed the way `Cookies` parses them, in the order sent. +fn session_cookie_values(parts: &Parts) -> Vec { + parts + .headers + .get_all(http::header::COOKIE) + .iter() + .filter_map(|h| h.to_str().ok()) + .flat_map(|h| h.split(';')) + .filter_map(|c| tower_cookies::Cookie::parse_encoded(c).ok()) + .filter(|c| c.name() == COOKIE_NAME) + .map(|c| c.value_trimmed().to_owned()) + .take(MAX_SESSION_COOKIES) + .collect() +} + +/// A browser can hold two `token` cookies for one host: this instance's, and one a parent-domain +/// instance set with `Domain=` (app.windmill.dev on `.windmill.dev`), which JS cannot clear since +/// it is HttpOnly. Browsers disagree on their order (Safari sends the host's first, Chrome last), +/// so neither position can be trusted: keep the first one that is a live session here. +async fn pick_live_session_token( + parts: &mut Parts, + state: &S, + candidates: Vec, +) -> Option { + // `Cookies::get` keeps the last duplicate: the pick when nothing can be checked. + let fallback = candidates.last().cloned(); + let Ok(Extension(cache)) = Extension::>::from_request_parts(parts, state).await + else { + return fallback; + }; + let hashes: Vec = candidates.iter().map(|t| hash_token(t)).collect(); + // A browser keeps sending the same pair on every request, so the pick is cached. A token + // revoked within the TTL can still be picked, and then fails authentication as revoked. + let key = hashes.join(","); + if let Some((picked, at)) = PICKED_SESSION_CACHE.get(&key) { + if at.elapsed().as_secs() < TOKEN_EMAIL_CACHE_TTL_SECS { + return picked.map(|i| candidates[i].clone()).or(fallback); + } + } + let live: Vec = match sqlx::query_scalar( + "SELECT h FROM unnest($1::text[]) AS h \ + WHERE EXISTS (SELECT 1 FROM token WHERE token_hash = h \ + AND (expiration > NOW() OR expiration IS NULL))", + ) + .bind(&hashes) + .fetch_all(&cache.db) + .await + { + Ok(live) => live, + Err(e) => { + tracing::error!("checking duplicate session cookies: {e:#}"); + return fallback; + } + }; + let picked = hashes.iter().position(|h| live.contains(h)); + PICKED_SESSION_CACHE.insert(key, (picked, std::time::Instant::now())); + picked.map(|i| candidates[i].clone()).or(fallback) +} + pub(crate) async fn extract_token(parts: &mut Parts, state: &S) -> Option { let auth_header = parts .headers @@ -965,14 +1030,21 @@ pub(crate) async fn extract_token(parts: &mut Parts, state: &S) let from_cookie = match auth_header { Some(x) => Some(x.to_owned()), - None => Extension::::from_request_parts(parts, state) - .await - .ok() - .and_then(|cookies| { - cookies - .get(COOKIE_NAME) - .map(|c| c.value_trimmed().to_owned()) - }), + None => { + let candidates = session_cookie_values(parts); + if candidates.len() > 1 { + pick_live_session_token(parts, state, candidates).await + } else { + Extension::::from_request_parts(parts, state) + .await + .ok() + .and_then(|cookies| { + cookies + .get(COOKIE_NAME) + .map(|c| c.value_trimmed().to_owned()) + }) + } + } }; #[derive(Deserialize)]