From c6d1eb10d3d17fd06301f2ae250e0a3005941f84 Mon Sep 17 00:00:00 2001 From: Devon Hudson Date: Fri, 13 Nov 2026 21:10:23 +0000 Subject: [PATCH 1/3] Warn that a custom registration token must be hard to guess Its invite URL shows whoever holds it the username and email address the token imposes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FqDpJcPorK3QoHp6G6hsYt --- crates/handlers/src/admin/v1/user_registration_tokens/add.rs | 3 +++ docs/api/spec.json | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) 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 48c175063..d362de45b 100644 --- a/crates/handlers/src/admin/v1/user_registration_tokens/add.rs +++ b/crates/handlers/src/admin/v1/user_registration_tokens/add.rs @@ -90,6 +90,9 @@ impl NotAllowed { #[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/docs/api/spec.json b/docs/api/spec.json index 3939b2c22..21d027002 100644 --- a/docs/api/spec.json +++ b/docs/api/spec.json @@ -7454,7 +7454,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" From aac1981163564ccba35e957f26f0831bff3478c2 Mon Sep 17 00:00:00 2001 From: Devon Hudson Date: Fri, 13 Nov 2026 21:10:23 +0000 Subject: [PATCH 2/3] Say why skipping the registration limit for invites is safe Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FqDpJcPorK3QoHp6G6hsYt --- crates/handlers/src/views/register/password.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/handlers/src/views/register/password.rs b/crates/handlers/src/views/register/password.rs index 721b94f49..f2219c220 100644 --- a/crates/handlers/src/views/register/password.rs +++ b/crates/handlers/src/views/register/password.rs @@ -259,7 +259,10 @@ pub(super) async fn register( // Check the rate limit if we are about to process the form. A // passwordless code pinning an email address 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 one address, only one account + // can register with that address, 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()); From 6368de2dff98efafab8a4aec66e994b714d4388f Mon Sep 17 00:00:00 2001 From: Devon Hudson Date: Fri, 13 Nov 2026 21:10:23 +0000 Subject: [PATCH 3/3] Test that a stale CSRF token keeps the invite code Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FqDpJcPorK3QoHp6G6hsYt --- crates/handlers/src/views/register/mod.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/crates/handlers/src/views/register/mod.rs b/crates/handlers/src/views/register/mod.rs index 477832182..0457c8df2 100644 --- a/crates/handlers/src/views/register/mod.rs +++ b/crates/handlers/src/views/register/mod.rs @@ -1312,6 +1312,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"); + } + /// A username too long to ever become an MXID is dropped instead of being /// carried in the cookie #[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")]