🔒️ Protección CSRF y límite de logins por IP

- CSRF sin tokens en el núcleo: se rechazan con 403 las peticiones que
  cambian estado si el navegador indica otro origen (`Sec-Fetch-Site`, y
  `Origin` frente a `Host`), con el criterio de Go. Sin interruptor.
- Nuevo `[server] trusted_origins`, validado y normalizado al arrancar.
- (user): `max_ip_failed_logins` limita por IP los logins fallidos; el
  intento se anota antes de Argon2 para que no se salte en paralelo.
- (user): avisos para la página siguiente tras registro, verificación,
  restablecimiento de contraseña y cambio de email.
- `flows.rs` ignora las marcas de componentes aplazados al comparar.
This commit is contained in:
Manuel Cillero 2026-11-02 14:34:05 +01:00
parent c9fdc7afc7
commit 574010374d
19 changed files with 639 additions and 35 deletions

View file

@ -87,7 +87,16 @@ pub async fn login_post(
) -> Response {
let cx = Context::new(request.clone());
let next = waypoint.or(cx.route("/"));
let result = auth::login(&form.username, &form.password, form.remember).await;
// Una dirección que ya ha fallado demasiadas veces no llega a comprobar la contraseña (Argon2).
let result = if throttle::allow_login(&request) {
let result = auth::login(&form.username, &form.password, form.remember).await;
if !matches!(result, Err(AuthError::InvalidCredentials)) {
throttle::forget_login(&request);
}
result
} else {
Err(AuthError::TooManyRequests)
};
match result {
Ok(sid) => {
let cookie = session::build_cookie(&sid, form.remember);
@ -98,6 +107,7 @@ pub async fn login_post(
AuthError::AccountBlocked => "error-account-blocked",
AuthError::AccountPending => "error-account-pending",
AuthError::AccountLocked => "error-account-locked",
AuthError::TooManyRequests => "error-too-many-requests",
_ => "error-invalid-credentials",
};
Page::new(request)
@ -190,7 +200,11 @@ pub async fn register_post(
)
.await
}
Ok(_) => Redirect::see_other(cx.route(LOGIN_PATH)),
Ok(_) => Redirect::see_other(cx.route(LOGIN_PATH)).with_message(
&cx,
MessageLevel::Info,
Lc::t("msg-account-created", &LOCALES_USER),
),
Err(err) => Page::new(request)
.with_title(Lc::t("title-register", &LOCALES_USER))
.with_child(
@ -326,7 +340,11 @@ pub async fn password_reset_confirm_post(
}
.await;
match result {
Ok(()) => Redirect::see_other(cx.route(LOGIN_PATH)),
Ok(()) => Redirect::see_other(cx.route(LOGIN_PATH)).with_message(
&cx,
MessageLevel::Info,
Lc::t("msg-password-reset-done", &LOCALES_USER),
),
Err(err) => {
let error = match err {
AuthError::InvalidToken => Lc::t("error-token-invalid", &LOCALES_USER),
@ -407,7 +425,11 @@ pub async fn verify_email_post(
}
.await;
match result {
Ok(()) => Redirect::see_other(cx.route(LOGIN_PATH)),
Ok(()) => Redirect::see_other(cx.route(LOGIN_PATH)).with_message(
&cx,
MessageLevel::Info,
Lc::t("msg-email-verified", &LOCALES_USER),
),
Err(_) => ErrorPage::NotFound(Some(request)).into_response(),
}
}