diff options
| author | aur-build <aur-build@localhost> | 2026-11-18 14:41:45 +0000 |
|---|---|---|
| committer | aur-build <aur-build@localhost> | 2026-11-18 14:41:45 +0000 |
| commit | 3370a487d6e1100623ebfd982967f1223d251607 (patch) | |
| tree | 124db2f1fe41c3835b4aa4f39a980200804202ea /src/http/routes.rs | |
| parent | 157c078e52fee6c37ff20093189ae7ad7cd223cb (diff) | |
| download | aur-build-3370a487d6e1100623ebfd982967f1223d251607.tar.gz aur-build-3370a487d6e1100623ebfd982967f1223d251607.zip | |
http: the session id is the credential, not a cookie a client can type
The gate asked whether the request carried `Cookie: auth=1`, and login did
nothing but set that literal value, so the password protected the login form
and nothing else: anyone could read the reviews, the audit log, the settings
and the build logs by typing the cookie themselves, and the CSRF step then
happily minted them a session to POST with. The random sid machinery was
already there and already unguessable; it was just never bound to the flag
that said "someone authenticated".
A session now carries that flag. `auth.rs` holds a map of live sids to
whether they passed the password gate; `authenticate` can only flip a sid this
process minted, so a login attempt cannot mint a session as a side effect.
`gate` asks `is_authenticated(sid)` and nothing else, and the `auth` cookie is
gone from both the login response and the logout one. Login rotates the sid
rather than promoting the anonymous one, so the session that existed before
the password was typed is never the one that carries the privilege after it;
logout drops the sid, which invalidates its derived CSRF token with it.
Two tests, both over a bare socket because a client that follows redirects
turns "you are not signed in" into the login page's 200 and hides the whole
point: `a_forged_auth_cookie_is_not_a_session` (the exact forgery, on every
sensitive surface, GET and POST) and `login_rotates_the_session_and_logout_ends_it`
(rotation, dead pre-login sid, dead logged-out sid). The audit actor is
honest now too: with a password set, only an authenticated session is "web".
Diffstat (limited to 'src/http/routes.rs')
| -rw-r--r-- | src/http/routes.rs | 39 |
1 files changed, 26 insertions, 13 deletions
diff --git a/src/http/routes.rs b/src/http/routes.rs index 9efb90b..abd7345 100644 --- a/src/http/routes.rs +++ b/src/http/routes.rs @@ -12,13 +12,14 @@ use super::auth; use super::render::*; use super::{sse, Req, Res, State}; -/// Redirect an unauthenticated visitor to the login page. -pub fn gate(state: &State, req: &Req) -> Option<Res> { +/// Redirect an unauthenticated visitor to the login page. The session id is +/// the credential: a cookie a client typed by hand proves nothing, only a sid +/// this process minted and marked authenticated at a successful login does. +pub fn gate(state: &State, req: &Req, sid: &str) -> Option<Res> { if !auth::auth_required(&state.cfg) { return None; } - let signed_in = req.cookie("auth").as_deref() == Some("1"); - if signed_in || req.path == "/login" { + if auth::is_authenticated(sid) || req.path == "/login" { return None; } Some(Res::redirect("/login")) @@ -29,7 +30,7 @@ pub fn login_form(state: &State, req: &Req) -> Res { page_to_res(Login { page, error: String::new() }, 200) } -/// Forget the session server-side and clear both cookies. The CSRF token is +/// Forget the session server-side and clear the cookie. The CSRF token is /// derived from the sid, so dropping the sid invalidates every form a browser /// still has open — which is exactly what logout should do. pub fn logout(req: &Req) -> Res { @@ -37,7 +38,6 @@ pub fn logout(req: &Req) -> Res { auth::drop_session(&sid); } let mut res = Res::redirect("/login"); - res.header("Set-Cookie", "auth=; Path=/; Max-Age=0"); res.header("Set-Cookie", "sid=; Path=/; Max-Age=0"); res } @@ -46,8 +46,17 @@ pub fn login(state: &State, req: &Req) -> Res { let given = req.form_get("password").unwrap_or_default(); let page = Page::new("Sign in", csrf(state, req)); if auth::password_ok(&state.cfg, &given) { + // Rotate rather than promote: the sid the visitor arrived with (if any) + // is dropped, so the session that existed before the password was typed + // is never the one that carries the privilege afterwards. + if let Some(old) = req.cookie("sid") { + auth::drop_session(&old); + } + let sid = auth::new_session(); + auth::authenticate(&sid); let mut res = Res::redirect("/"); - res.header("Set-Cookie", "auth=1; Path=/; HttpOnly; SameSite=Strict"); + // `serve` turns this into the `Set-Cookie: sid=…` header. + res.header("X-New-Session", &sid); res } else { page_to_res(Login { page, error: "wrong password".into() }, 401) @@ -335,7 +344,7 @@ fn review_approve(state: &State, req: &Req, id: &str) -> Res { let identity = req.form_get("identity_hash").unwrap_or_default(); let digest = req.form_get("diff_digest").unwrap_or_default(); let note = req.form_get("note").filter(|s| !s.trim().is_empty()); - match ap::approve(db, actor(req), id, &identity, &digest, note.as_deref()) { + match ap::approve(db, actor(state, req), id, &identity, &digest, note.as_deref()) { Ok(a) => { let mut page = chrome(state, req, "Reviews", "nav_reviews"); page.flash = format!("approved {} (approval {}); build queued", c.pkgbase, a.id); @@ -400,7 +409,7 @@ fn review_reject(state: &State, req: &Req, id: &str) -> Res { page.flash_err = true; return page_to_res(ReviewsPage { page, rows: review_rows(state) }, 400); }; - match ap::reject(db, actor(req), id, ¬e) { + match ap::reject(db, actor(state, req), id, ¬e) { Ok(()) => { let mut page = chrome(state, req, "Reviews", "nav_reviews"); page.flash = format!("rejected #{id}; it can be re-opened from the package page"); @@ -453,12 +462,12 @@ fn reviews_bulk(state: &State, req: &Req) -> Res { continue; } let res = if decision == "approve" { - ap::approve(db, actor(req), id, &c.identity_hash, &c.diff_digest, Some("bulk")) + ap::approve(db, actor(state, req), id, &c.identity_hash, &c.diff_digest, Some("bulk")) .map(|_| ()) .map_err(|e| e.to_string()) } else { let note = req.form_get("note").unwrap_or_else(|| "bulk reject".into()); - ap::reject(db, actor(req), id, ¬e).map_err(|e| e.to_string()) + ap::reject(db, actor(state, req), id, ¬e).map_err(|e| e.to_string()) }; match res { Ok(()) => { @@ -482,8 +491,12 @@ fn reviews_bulk(state: &State, req: &Req) -> Res { page_to_res(ReviewsPage { page, rows: review_rows(state) }, 200) } -fn actor(req: &Req) -> &str { - if req.cookie("auth").is_some() { +/// Who the audit trail says made a decision. With no password configured the +/// UI is open on loopback and every session is the operator's; with one, only +/// a session that passed the gate gets a name. +fn actor(state: &State, req: &Req) -> &'static str { + let sid = req.cookie("sid").unwrap_or_default(); + if !auth::auth_required(&state.cfg) || auth::is_authenticated(&sid) { "web" } else { "web-anon" |
