| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use std::time::Duration; |
| 6 | |
| 7 | use http::header::{CONTENT_TYPE, COOKIE, LOCATION}; |
| 8 | use im_core::password::hash_password; |
| 9 | use im_core::store::sessions::{NewSession, SESSION_IDLE, SignInState}; |
| 10 | use im_core::token::{generate_token, hash_token}; |
| 11 | use topcoat::router::{Body, to_bytes}; |
| 12 | |
| 13 | use crate::boot::{router, test_state}; |
| 14 | use crate::state::AppState; |
| 15 | |
| 16 | |
| 17 | const COOKIE_NAME: &str = "__Host-im_session"; |
| 18 | |
| 19 | |
| 20 | struct Reply { |
| 21 | status: u16, |
| 22 | location: Option<String>, |
| 23 | body: String, |
| 24 | } |
| 25 | |
| 26 | |
| 27 | async fn send(state: AppState, request: http::request::Builder, body: Body) -> Reply { |
| 28 | let request = request.body(body).expect("request"); |
| 29 | let response = router(state).handle(request).await; |
| 30 | let status = response.status().as_u16(); |
| 31 | let location = response |
| 32 | .headers() |
| 33 | .get(LOCATION) |
| 34 | .and_then(|value| value.to_str().ok()) |
| 35 | .map(str::to_owned); |
| 36 | let bytes = to_bytes(response.into_body(), 1024 * 1024) |
| 37 | .await |
| 38 | .expect("body"); |
| 39 | Reply { |
| 40 | status, |
| 41 | location, |
| 42 | body: String::from_utf8_lossy(&bytes).into_owned(), |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | |
| 47 | async fn get(state: AppState, path: &str, cookie: &str) -> Reply { |
| 48 | let request = http::Request::get(path).header(COOKIE, format!("{COOKIE_NAME}={cookie}")); |
| 49 | send(state, request, Body::empty()).await |
| 50 | } |
| 51 | |
| 52 | |
| 53 | async fn post(state: AppState, path: &str, cookie: &str, form: &str) -> Reply { |
| 54 | let request = http::Request::post(path) |
| 55 | .header(CONTENT_TYPE, "application/x-www-form-urlencoded") |
| 56 | .header(COOKIE, format!("{COOKIE_NAME}={cookie}")); |
| 57 | send(state, request, Body::from(form.to_owned())).await |
| 58 | } |
| 59 | |
| 60 | |
| 61 | async fn session_for(state: &AppState, user_id: &str) -> (String, String) { |
| 62 | let token = generate_token(); |
| 63 | let csrf = generate_token(); |
| 64 | let user = state |
| 65 | .store |
| 66 | .user_by_id(user_id) |
| 67 | .await |
| 68 | .expect("user") |
| 69 | .expect("a user"); |
| 70 | state |
| 71 | .store |
| 72 | .create_session( |
| 73 | &NewSession { |
| 74 | user_id, |
| 75 | decided_on: SignInState::of(&user), |
| 76 | token_hash: &hash_token(&token), |
| 77 | csrf_token: &csrf, |
| 78 | amr: "pwd", |
| 79 | ip: None, |
| 80 | user_agent: None, |
| 81 | idle: SESSION_IDLE, |
| 82 | max_age: Duration::from_secs(3600), |
| 83 | }, |
| 84 | 10, |
| 85 | ) |
| 86 | .await |
| 87 | .expect("session") |
| 88 | .expect("not refused"); |
| 89 | (token, csrf) |
| 90 | } |
| 91 | |
| 92 | |
| 93 | async fn state_without_admin_two_factor() -> (tempfile::TempDir, AppState) { |
| 94 | let (dir, state) = test_state().await; |
| 95 | let mut policy = state.policy(); |
| 96 | policy.require_2fa_for_admins = false; |
| 97 | state.set_policy(policy); |
| 98 | (dir, state) |
| 99 | } |
| 100 | |
| 101 | #[tokio::test] |
| 102 | async fn a_member_gets_not_found_on_the_admin_users_page() { |
| 103 | let (_dir, state) = state_without_admin_two_factor().await; |
| 104 | let hash = hash_password("the current password").expect("hash"); |
| 105 | let member = state |
| 106 | .store |
| 107 | .create_user("member@example.com", "Mem", &hash, false) |
| 108 | .await |
| 109 | .expect("member"); |
| 110 | let (cookie, _) = session_for(&state, &member.id).await; |
| 111 | let reply = get(state, "/admin/users", &cookie).await; |
| 112 | assert_eq!(reply.status, 404); |
| 113 | } |
| 114 | |
| 115 | #[tokio::test] |
| 116 | async fn an_admin_form_without_the_csrf_token_is_forbidden_and_changes_nothing() { |
| 117 | let (_dir, state) = state_without_admin_two_factor().await; |
| 118 | let hash = hash_password("the current password").expect("hash"); |
| 119 | let admin = state |
| 120 | .store |
| 121 | .create_user("admin@example.com", "Ada", &hash, true) |
| 122 | .await |
| 123 | .expect("admin"); |
| 124 | let other = state |
| 125 | .store |
| 126 | .create_user("other@example.com", "Oto", &hash, false) |
| 127 | .await |
| 128 | .expect("other"); |
| 129 | let (cookie, _) = session_for(&state, &admin.id).await; |
| 130 | let reply = post( |
| 131 | state.clone(), |
| 132 | &format!("/admin/users/{}/disable", other.id), |
| 133 | &cookie, |
| 134 | "csrf=not-the-token&on=", |
| 135 | ) |
| 136 | .await; |
| 137 | assert_eq!(reply.status, 403); |
| 138 | let still = state |
| 139 | .store |
| 140 | .user_by_id(&other.id) |
| 141 | .await |
| 142 | .expect("query") |
| 143 | .expect("user"); |
| 144 | assert!(still.disabled_at.is_none()); |
| 145 | } |
| 146 | |
| 147 | #[tokio::test] |
| 148 | async fn an_administrator_cannot_disable_themselves() { |
| 149 | let (_dir, state) = state_without_admin_two_factor().await; |
| 150 | let hash = hash_password("the current password").expect("hash"); |
| 151 | let admin = state |
| 152 | .store |
| 153 | .create_user("admin@example.com", "Ada", &hash, true) |
| 154 | .await |
| 155 | .expect("admin"); |
| 156 | let other = state |
| 157 | .store |
| 158 | .create_user("other@example.com", "Oto", &hash, true) |
| 159 | .await |
| 160 | .expect("other"); |
| 161 | let (cookie, csrf) = session_for(&state, &admin.id).await; |
| 162 | let reply = post( |
| 163 | state.clone(), |
| 164 | &format!("/admin/users/{}/disable", admin.id), |
| 165 | &cookie, |
| 166 | &format!("csrf={csrf}&on="), |
| 167 | ) |
| 168 | .await; |
| 169 | assert_eq!(reply.status, 303); |
| 170 | assert!( |
| 171 | reply |
| 172 | .location |
| 173 | .as_deref() |
| 174 | .is_some_and(|location| location.contains("error=self-disable")), |
| 175 | "refused with the self-disable code, got {:?}", |
| 176 | reply.location |
| 177 | ); |
| 178 | let still = state |
| 179 | .store |
| 180 | .user_by_id(&admin.id) |
| 181 | .await |
| 182 | .expect("query") |
| 183 | .expect("user"); |
| 184 | assert!(still.disabled_at.is_none()); |
| 185 | assert!( |
| 186 | state |
| 187 | .store |
| 188 | .user_by_id(&other.id) |
| 189 | .await |
| 190 | .expect("query") |
| 191 | .is_some() |
| 192 | ); |
| 193 | } |
| 194 | |
| 195 | #[tokio::test] |
| 196 | async fn a_hostile_user_name_is_escaped_on_the_admin_users_page() { |
| 197 | let (_dir, state) = state_without_admin_two_factor().await; |
| 198 | let hash = hash_password("the current password").expect("hash"); |
| 199 | let admin = state |
| 200 | .store |
| 201 | .create_user("admin@example.com", "Ada", &hash, true) |
| 202 | .await |
| 203 | .expect("admin"); |
| 204 | state |
| 205 | .store |
| 206 | .create_user( |
| 207 | "mallory@example.com", |
| 208 | "<img src=x onerror=alert(1)>", |
| 209 | &hash, |
| 210 | false, |
| 211 | ) |
| 212 | .await |
| 213 | .expect("hostile user"); |
| 214 | let (cookie, _) = session_for(&state, &admin.id).await; |
| 215 | let reply = get(state, "/admin/users", &cookie).await; |
| 216 | assert_eq!(reply.status, 200); |
| 217 | assert!(!reply.body.contains("<img src=x onerror")); |
| 218 | assert!(reply.body.contains("<img src=x onerror=alert(1)>")); |
| 219 | } |