diff options
Diffstat (limited to 'tests/http.rs')
| -rw-r--r-- | tests/http.rs | 52 |
1 files changed, 51 insertions, 1 deletions
diff --git a/tests/http.rs b/tests/http.rs index d5387dc..0ca8a6b 100644 --- a/tests/http.rs +++ b/tests/http.rs @@ -203,7 +203,7 @@ fn bulk_approve_refuses_high_risk_without_individual_confirmation() { #[test] fn a_password_gate_redirects_anonymous_visitors() { - let web = Web::with_password("plain-make", "s3cret"); + let mut web = Web::with_password("plain-make", "s3cret"); let page = web.get("/reviews"); assert_eq!(page.status, 200); // The gate redirects; ureq follows, so we land on the login page. @@ -212,6 +212,56 @@ fn a_password_gate_redirects_anonymous_visitors() { let bad = web.post("/login", &[("csrf", &web.csrf), ("password", "nope")]); assert_eq!(bad.status, 401); assert!(web.get("/packages").body.contains("Sign in"), "still not gated"); + let ok = web.login("s3cret"); + assert_eq!(ok.status, 303, "a correct password did not redirect"); + let page = web.get("/packages"); + assert_eq!(page.status, 200); + assert!(!page.body.contains("Sign in"), "still gated after the right password"); +} + +#[test] +fn a_forged_auth_cookie_is_not_a_session() { + // What this pins: the gate used to read `Cookie: auth=1`, a value login + // merely echoed, so any client that typed the cookie itself could read the + // reviews, the audit log and the build output without ever supplying the + // password. The session id is the credential now, and only this process + // can mint one. + let web = Web::with_password("plain-make", "s3cret"); + for path in ["/", "/reviews", "/audit", "/settings", "/builds", "/packages"] { + let res = web.raw_request("GET", path, &[("Cookie", "auth=1".into())], None); + assert_eq!(res.status, 303, "{path} let a forged cookie through ({}): {}", res.status, &res.body[..120.min(res.body.len())]); + assert_eq!(res.header("location"), Some("/login"), "{path} did not redirect to the login page"); + } + // A POST with the forged cookie is turned away at the gate too, before the + // CSRF step ever gets a chance to mint the attacker a session of their own. + let res = web.raw_request("POST", "/logout", &[("Cookie", "auth=1".into())], Some("")); + assert_eq!(res.status, 303, "a forged cookie reached a mutating route"); + // And the word "auth" is not a cookie the server recognises anywhere. + let res = web.raw_request("GET", "/reviews", &[("Cookie", "auth=yes".into())], None); + assert_eq!(res.status, 303); +} + +#[test] +fn login_rotates_the_session_and_logout_ends_it() { + let mut web = Web::with_password("plain-make", "s3cret"); + // Arrive with an anonymous session (the CSRF token needs one), then log in. + let anon = web.raw_request("GET", "/", &[], None).cookie().unwrap_or_default(); + let res = web.login("s3cret"); + assert_eq!(res.status, 303); + assert_eq!(res.header("location"), Some("/")); + let session = res.cookie().unwrap_or_default(); + assert!(!session.is_empty(), "a successful login issued no session cookie"); + assert_ne!(session, anon, "login promoted the anonymous session instead of rotating it"); + // The new session sees the surface; the sid it replaced is dead. + assert_eq!(web.raw_request("GET", "/reviews", &[("Cookie", session.clone())], None).status, 200); + if !anon.is_empty() { + assert_eq!(web.raw_request("GET", "/reviews", &[("Cookie", anon)], None).status, 303, "the pre-login sid still worked"); + } + // Logout drops the sid server-side, which invalidates its CSRF token with + // it, so the same cookie is an anonymous visitor again. + let out = web.raw_request("POST", "/logout", &[("Cookie", session.clone()), ("Content-Type", "application/x-www-form-urlencoded".into())], Some(&format!("csrf={}", web.csrf))); + assert_eq!(out.status, 303, "logout did not redirect: {}", out.status); + assert_eq!(web.raw_request("GET", "/reviews", &[("Cookie", session)], None).status, 303, "the logged-out sid still worked"); } #[test] |
