diff --git a/crates/handlers/src/admin/v1/user_registration_tokens/add.rs b/crates/handlers/src/admin/v1/user_registration_tokens/add.rs index bbcdbd2ff..9dec1d3f1 100644 --- a/crates/handlers/src/admin/v1/user_registration_tokens/add.rs +++ b/crates/handlers/src/admin/v1/user_registration_tokens/add.rs @@ -131,6 +131,9 @@ pub enum AllowedEmailsError { #[serde(rename = "AddUserRegistrationTokenRequest")] pub struct Request { /// The token string. If not provided, a random token will be generated. + /// Anyone holding the token can open its invite URL, which shows the + /// username and email address it imposes, so a token provided here must be + /// as hard to guess as a generated one. token: Option, /// Maximum number of times this token can be used. If not provided, the diff --git a/crates/handlers/src/views/register/mod.rs b/crates/handlers/src/views/register/mod.rs index eb0eabb58..f78a1388c 100644 --- a/crates/handlers/src/views/register/mod.rs +++ b/crates/handlers/src/views/register/mod.rs @@ -1417,6 +1417,28 @@ mod tests { assert!(upstream_sessions(&state, &response).is_none()); } + /// The fresh page a stale CSRF token sends the user back to keeps the + /// invite code the link carried + #[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")] + async fn test_post_invalid_csrf_keeps_token(pool: PgPool) { + setup(); + let state = TestState::from_pool(pool).await.unwrap(); + let cookies = CookieHelper::new(); + + let (csrf_token, _body) = render_page(&state, &cookies).await; + + let request = cookies.with_cookies(Request::post("/register?token=invite").form( + serde_json::json!({ + "csrf": format!("{csrf_token}invalid"), + "username": "alice", + "token": "invite", + }), + )); + let response = state.request(request).await; + response.assert_status(StatusCode::SEE_OTHER); + response.assert_header_value(LOCATION, "/register?token=invite"); + } + /// The continue URL of the tokens [`add_continue_token`] adds, which no /// page of MAS and no redirect to one should show const CONTINUE_URL: &str = "https://app.example.com/#/room/secretroom"; diff --git a/crates/handlers/src/views/register/password.rs b/crates/handlers/src/views/register/password.rs index ac6bec7fe..2ef1fe236 100644 --- a/crates/handlers/src/views/register/password.rs +++ b/crates/handlers/src/views/register/password.rs @@ -301,7 +301,10 @@ pub(super) async fn register( // Check the rate limit if we are about to process the form. A // passwordless code pinning or listing email addresses skips the // registration limit, so that people behind one address can accept - // their invites together. + // their invites together. This can't register in bulk: such a code + // is minted by an admin client for the addresses it names, each + // address can register only one account, and the email + // authentication limits below still apply. let skips_limit = registration_token.as_ref().is_some_and(|token| { token.passwordless && (token.email.is_some() || token.allowed_emails.is_some()) }); diff --git a/docs/api/spec.json b/docs/api/spec.json index e92aeccf8..908e87ecc 100644 --- a/docs/api/spec.json +++ b/docs/api/spec.json @@ -7571,7 +7571,7 @@ "type": "object", "properties": { "token": { - "description": "The token string. If not provided, a random token will be generated.", + "description": "The token string. If not provided, a random token will be generated.\nAnyone holding the token can open its invite URL, which shows the\nusername and email address it imposes, so a token provided here must be\nas hard to guess as a generated one.", "type": [ "string", "null"