Cloudflare Turnstile on e-mail sign-up and password reset, and a limit per address

- The sign-up and "forgot password" forms carry a token from Cloudflare
  Turnstile's widget (managed, shown only when Cloudflare asks for a
  click); the backend checks it before an e-mail goes out. Off without
  TURNSTILE_SITE_KEY and TURNSTILE_SECRET_KEY (development); the site key
  comes to the page through /v1/config.
- At most ten such e-mail requests an hour per visitor address, so a
  script cannot make the site mail thousands of strangers.
- The privacy notice (HU, EN) mentions Turnstile with Cloudflare.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
feco
2026-11-26 16:43:07 +00:00
co-authored by Claude Opus 5.5
parent fa46dcb757
commit 469f321ac0
14 changed files with 285 additions and 11 deletions
+47
View File
@@ -226,12 +226,20 @@ async fn fake_s3(
}
}
/// Cloudflare Turnstile: the token "ok" with the test secret passes.
async fn fake_turnstile(axum::Form(form): axum::Form<std::collections::HashMap<String, String>>) -> Json<Value> {
let ok = form.get("secret").map(String::as_str) == Some("test-secret")
&& form.get("response").map(String::as_str) == Some("ok");
Json(json!({ "success": ok, "error-codes": if ok { json!([]) } else { json!(["invalid-input-response"]) } }))
}
async fn fake_services_with(s3: FakeS3) -> String {
let objects = Router::new().route("/s3/photos/{*key}", get(fake_s3).put(fake_s3).delete(fake_s3)).with_state(s3);
let app = Router::new()
.merge(objects)
.route("/billingo/documents/{id}/download", get(fake_billingo_pdf))
.route("/openrouter/models", get(fake_openrouter_models))
.route("/turnstile/siteverify", post(fake_turnstile))
.route("/v1/checkout/sessions", post(fake_stripe_create))
.route("/v1/checkout/sessions/{id}", get(fake_stripe_get))
.route("/v1/refunds", post(fake_stripe_refund))
@@ -4031,3 +4039,42 @@ async fn a_gift_can_be_e_mailed_at_a_chosen_time(pool: PgPool) {
assert_eq!(r.status, StatusCode::OK, "{}", r.body);
assert_eq!(app.outbox_count("cili@example.com", "gift_package").await, 0);
}
#[sqlx::test(migrator = "db::MIGRATOR")]
async fn email_sign_up_checks_turnstile_and_limits_each_address(pool: PgPool) {
let app = TestApp::with_config(pool, |c| {
c.turnstile = Some(crate::turnstile::TurnstileConfig {
site_key: "site-key".into(),
secret: "test-secret".into(),
verify_url: format!("{}/turnstile/siteverify", c.horoscope_api_url),
});
})
.await;
assert_eq!(app.get("/v1/config", None).await.body["turnstile_site_key"], "site-key");
let ip = [("x-real-ip", "203.0.113.50")];
let sign_up = |token: Option<&str>| json!({ "email": "anna@example.com", "turnstile_token": token });
// No token or a wrong one: no account, no e-mail.
for token in [None, Some("rossz")] {
let r = app.call_with("POST", "/v1/auth/register", None, Some(sign_up(token)), &ip).await;
assert_eq!(r.status, StatusCode::BAD_REQUEST, "{}", r.body);
}
let users: i64 = sqlx::query_scalar("SELECT count(*) FROM users").fetch_one(&app.pool).await.unwrap();
assert_eq!(users, 0);
let r = app.call_with("POST", "/v1/auth/register", None, Some(sign_up(Some("ok"))), &ip).await;
assert_eq!(r.status, StatusCode::ACCEPTED, "{}", r.body);
let reset = |token: Option<&str>| json!({ "email": "anna@example.com", "turnstile_token": token });
let r = app.call_with("POST", "/v1/auth/password-reset", None, Some(reset(None)), &ip).await;
assert_eq!(r.status, StatusCode::BAD_REQUEST);
// Ten e-mail requests an hour from one address (the four above count too).
for _ in 0..6 {
let r = app.call_with("POST", "/v1/auth/password-reset", None, Some(reset(Some("ok"))), &ip).await;
assert_eq!(r.status, StatusCode::ACCEPTED);
}
let r = app.call_with("POST", "/v1/auth/password-reset", None, Some(reset(Some("ok"))), &ip).await;
assert_eq!(r.status, StatusCode::TOO_MANY_REQUESTS);
let elsewhere = [("x-real-ip", "198.51.100.60")];
let r = app.call_with("POST", "/v1/auth/password-reset", None, Some(reset(Some("ok"))), &elsewhere).await;
assert_eq!(r.status, StatusCode::ACCEPTED);
}