//! Router tests of the administrator area: a member gets the 404, a form without the CSRF token is refused //! before anything changes, an administrator cannot disable themselves, and a hostile name is escaped on //! the users page. use std::time::Duration; use http::header::{CONTENT_TYPE, COOKIE, LOCATION}; use im_core::password::hash_password; use im_core::store::sessions::{NewSession, SESSION_IDLE, SignInState}; use im_core::token::{generate_token, hash_token}; use topcoat::router::{Body, to_bytes}; use crate::boot::{router, test_state}; use crate::state::AppState; /// The cookie name the browser sends: the `__Host-` prefix is added by the jar. const COOKIE_NAME: &str = "__Host-im_session"; /// A status, the `Location` header if any, and the body of a response. struct Reply { status: u16, location: Option, body: String, } /// Sends a request through the router and collects the reply. async fn send(state: AppState, request: http::request::Builder, body: Body) -> Reply { let request = request.body(body).expect("request"); let response = router(state).handle(request).await; let status = response.status().as_u16(); let location = response .headers() .get(LOCATION) .and_then(|value| value.to_str().ok()) .map(str::to_owned); let bytes = to_bytes(response.into_body(), 1024 * 1024) .await .expect("body"); Reply { status, location, body: String::from_utf8_lossy(&bytes).into_owned(), } } /// A GET with the session cookie. async fn get(state: AppState, path: &str, cookie: &str) -> Reply { let request = http::Request::get(path).header(COOKIE, format!("{COOKIE_NAME}={cookie}")); send(state, request, Body::empty()).await } /// A form POST with the session cookie. async fn post(state: AppState, path: &str, cookie: &str, form: &str) -> Reply { let request = http::Request::post(path) .header(CONTENT_TYPE, "application/x-www-form-urlencoded") .header(COOKIE, format!("{COOKIE_NAME}={cookie}")); send(state, request, Body::from(form.to_owned())).await } /// Creates a session for a user with a known CSRF token. Returns the cookie value and the CSRF token. async fn session_for(state: &AppState, user_id: &str) -> (String, String) { let token = generate_token(); let csrf = generate_token(); let user = state .store .user_by_id(user_id) .await .expect("user") .expect("a user"); state .store .create_session( &NewSession { user_id, decided_on: SignInState::of(&user), token_hash: &hash_token(&token), csrf_token: &csrf, amr: "pwd", ip: None, user_agent: None, idle: SESSION_IDLE, max_age: Duration::from_secs(3600), }, 10, ) .await .expect("session") .expect("not refused"); (token, csrf) } /// A state where administrators need no two-factor sign-in, so a test session reaches the admin pages. async fn state_without_admin_two_factor() -> (tempfile::TempDir, AppState) { let (dir, state) = test_state().await; let mut policy = state.policy(); policy.require_2fa_for_admins = false; state.set_policy(policy); (dir, state) } #[tokio::test] async fn a_member_gets_not_found_on_the_admin_users_page() { let (_dir, state) = state_without_admin_two_factor().await; let hash = hash_password("the current password").expect("hash"); let member = state .store .create_user("member@example.com", "Mem", &hash, false) .await .expect("member"); let (cookie, _) = session_for(&state, &member.id).await; let reply = get(state, "/admin/users", &cookie).await; assert_eq!(reply.status, 404); } #[tokio::test] async fn an_admin_form_without_the_csrf_token_is_forbidden_and_changes_nothing() { let (_dir, state) = state_without_admin_two_factor().await; let hash = hash_password("the current password").expect("hash"); let admin = state .store .create_user("admin@example.com", "Ada", &hash, true) .await .expect("admin"); let other = state .store .create_user("other@example.com", "Oto", &hash, false) .await .expect("other"); let (cookie, _) = session_for(&state, &admin.id).await; let reply = post( state.clone(), &format!("/admin/users/{}/disable", other.id), &cookie, "csrf=not-the-token&on=", ) .await; assert_eq!(reply.status, 403); let still = state .store .user_by_id(&other.id) .await .expect("query") .expect("user"); assert!(still.disabled_at.is_none()); } #[tokio::test] async fn an_administrator_cannot_disable_themselves() { let (_dir, state) = state_without_admin_two_factor().await; let hash = hash_password("the current password").expect("hash"); let admin = state .store .create_user("admin@example.com", "Ada", &hash, true) .await .expect("admin"); let other = state .store .create_user("other@example.com", "Oto", &hash, true) .await .expect("other"); let (cookie, csrf) = session_for(&state, &admin.id).await; let reply = post( state.clone(), &format!("/admin/users/{}/disable", admin.id), &cookie, &format!("csrf={csrf}&on="), ) .await; assert_eq!(reply.status, 303); assert!( reply .location .as_deref() .is_some_and(|location| location.contains("error=self-disable")), "refused with the self-disable code, got {:?}", reply.location ); let still = state .store .user_by_id(&admin.id) .await .expect("query") .expect("user"); assert!(still.disabled_at.is_none()); assert!( state .store .user_by_id(&other.id) .await .expect("query") .is_some() ); } #[tokio::test] async fn a_hostile_user_name_is_escaped_on_the_admin_users_page() { let (_dir, state) = state_without_admin_two_factor().await; let hash = hash_password("the current password").expect("hash"); let admin = state .store .create_user("admin@example.com", "Ada", &hash, true) .await .expect("admin"); state .store .create_user( "mallory@example.com", "", &hash, false, ) .await .expect("hostile user"); let (cookie, _) = session_for(&state, &admin.id).await; let reply = get(state, "/admin/users", &cookie).await; assert_eq!(reply.status, 200); assert!(!reply.body.contains("