fix(hub): bind email budget reserve to a bounded resend chain
The global reserve is claimed atomically with the budget check by deleting a live attempt for the same address (under 5 failures, under 3 resends, chain under 30 minutes), so it cannot fund mail to other addresses, branch or race.
This commit is contained in:
parent
6ea14d0ec2
commit
fea876654b
5 changed files with 196 additions and 33 deletions
|
|
@ -220,23 +220,26 @@ async fn email_start(
|
|||
let lang = Lang::from_tag(body.lang.as_deref());
|
||||
let source = mail.source_mac(&client_ip(&headers));
|
||||
let previous = cookie(&headers, EMAIL_BROWSER);
|
||||
let resend = match &previous {
|
||||
Some(browser) => mail.has_pending(&auth.pool, browser).await.unwrap_or(false),
|
||||
None => false,
|
||||
};
|
||||
let reserved = mail
|
||||
.reserve_send(&auth.pool, &mail.email_mac(&address), &source, resend)
|
||||
.await;
|
||||
match reserved {
|
||||
Ok(true) => {}
|
||||
Ok(false) => return failure(StatusCode::TOO_MANY_REQUESTS, "try_later"),
|
||||
let chain = match mail
|
||||
.reserve_send(
|
||||
&auth.pool,
|
||||
&mail.email_mac(&address),
|
||||
&source,
|
||||
previous.as_deref(),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(email::Reservation::Allowed { chain }) => chain,
|
||||
Ok(email::Reservation::Denied) => {
|
||||
return failure(StatusCode::TOO_MANY_REQUESTS, "try_later");
|
||||
}
|
||||
Err(_) => return failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"),
|
||||
}
|
||||
};
|
||||
// Housekeeping failures must not block sign-in; expiry is enforced on every read.
|
||||
let _ = store::cleanup(&auth.pool).await;
|
||||
let browser = store::token();
|
||||
let Ok((attempt_id, code)) = mail
|
||||
.create_attempt(&auth.pool, &address, &browser, previous.as_deref())
|
||||
.create_attempt(&auth.pool, &address, &browser, previous.as_deref(), chain)
|
||||
.await
|
||||
else {
|
||||
return failure(StatusCode::SERVICE_UNAVAILABLE, "try_later");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue