Verification mails are throttled per address (3) and per account (5) in the throttle window, and the page says when one was held back, so an account cannot mail a stranger without end
Browse files at this commit389919aee303c414cc1e301339965752de5625abParent: a7608d1- harden
- verification mails are throttled per address (3) and per account (5) in the throttle window, and the page says when one was held back, so an account cannot mail a stranger without end
- a signed-in stranger gets the same 404 for a private container image as for a missing one
- a published crate's README search stops after 256 MiB inflated
5 changed files +54 -13
- crates/forge-core/src/cargo.rs +4 -1
- crates/forge-web/src/mail/account.rs +26 -3
- crates/forge-web/src/oci/auth.rs +12 -6
- crates/forge-web/src/pages/register.rs +4 -1
- crates/forge-web/src/pages/settings_security.rs +8 -2
crates/forge-core/src/cargo.rsmodified+4 -1
| @@ -184,7 +184,10 @@ pub fn repo_link(repository: &str, base_url: &str) -> Option<(String, String)> { | |||
| 184 | /// (`<name>-<version>/README.md`). Read at most [`MAX_README_BYTES`] bytes. Returns `None` when | 184 | /// (`<name>-<version>/README.md`). Read at most [`MAX_README_BYTES`] bytes. Returns `None` when |
| 185 | /// the archive is not a gzipped tarball or has no README. | 185 | /// the archive is not a gzipped tarball or has no README. |
| 186 | pub fn crate_readme(crate_file: &[u8]) -> Option<String> { | 186 | pub fn crate_readme(crate_file: &[u8]) -> Option<String> { |
| 187 | - let mut archive = tar::Archive::new(GzDecoder::new(crate_file)); | 187 | + const MAX_INFLATED_SCAN_BYTES: u64 = 256 * 1024 * 1024; |
| 188 | + // A small upload can inflate without end; past this much the README search gives up. | ||
| 189 | + let inflated = GzDecoder::new(crate_file).take(MAX_INFLATED_SCAN_BYTES); | ||
| 190 | + let mut archive = tar::Archive::new(inflated); | ||
| 188 | let entries = archive.entries().ok()?; | 191 | let entries = archive.entries().ok()?; |
| 189 | for entry in entries { | 192 | for entry in entries { |
| 190 | let entry = entry.ok()?; | 193 | let entry = entry.ok()?; |
crates/forge-web/src/mail/account.rsmodified+26 -3
| @@ -36,8 +36,31 @@ pub async fn send_reset(state: &AppState, user: &User) -> forge_core::Result<()> | |||
| 36 | Ok(()) | 36 | Ok(()) |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | -/// Queues a verification link for `address`, which is the user's current address. | 39 | +/// Verification mails one address may get inside the throttle window. |
| 40 | -pub async fn send_verify(state: &AppState, user: &User, address: &str) -> forge_core::Result<()> { | 40 | +const VERIFY_ADDRESS_LIMIT: usize = 3; |
| 41 | +/// Verification mails one account may send inside the throttle window, to any addresses. | ||
| 42 | +const VERIFY_USER_LIMIT: usize = 5; | ||
| 43 | + | ||
| 44 | +/// Queues a verification link for `address`, which is the user's current address. Returns false, | ||
| 45 | +/// with nothing queued, when the address or the account had too many of them lately: an account | ||
| 46 | +/// may set any address as its own, so resends would otherwise mail a stranger without end. | ||
| 47 | +pub async fn send_verify(state: &AppState, user: &User, address: &str) -> forge_core::Result<bool> { | ||
| 48 | + let now = std::time::Instant::now(); | ||
| 49 | + let address_key = format!("verify-address:{}", address.to_ascii_lowercase()); | ||
| 50 | + let user_key = format!("verify-user:{}", user.id); | ||
| 51 | + if state | ||
| 52 | + .throttle | ||
| 53 | + .check(&address_key, VERIFY_ADDRESS_LIMIT, now) | ||
| 54 | + .is_some() | ||
| 55 | + || state | ||
| 56 | + .throttle | ||
| 57 | + .check(&user_key, VERIFY_USER_LIMIT, now) | ||
| 58 | + .is_some() | ||
| 59 | + { | ||
| 60 | + return Ok(false); | ||
| 61 | + } | ||
| 62 | + state.throttle.record_failure(&address_key, now); | ||
| 63 | + state.throttle.record_failure(&user_key, now); | ||
| 41 | let token = state.store.create_verify_token(&user.id, address).await?; | 64 | let token = state.store.create_verify_token(&user.id, address).await?; |
| 42 | let link = format!("{}/verify/{token}", base(state)); | 65 | let link = format!("{}/verify/{token}", base(state)); |
| 43 | let (subject, body) = templates::email_verify(&user.username, &link); | 66 | let (subject, body) = templates::email_verify(&user.username, &link); |
| @@ -51,7 +74,7 @@ pub async fn send_verify(state: &AppState, user: &User, address: &str) -> forge_ | |||
| 51 | &body, | 74 | &body, |
| 52 | ) | 75 | ) |
| 53 | .await?; | 76 | .await?; |
| 54 | - Ok(()) | 77 | + Ok(true) |
| 55 | } | 78 | } |
| 56 | 79 | ||
| 57 | /// Creates an invite for `address` and queues its link. Returns the store's refusal, such as an | 80 | /// Creates an invite for `address` and queues its link. Returns the store's refusal, such as an |
crates/forge-web/src/oci/auth.rsmodified+12 -6
| @@ -145,18 +145,24 @@ pub async fn authorize( | |||
| 145 | } else if anonymous { | 145 | } else if anonymous { |
| 146 | Err(Box::new(challenge(state, Some(name)))) | 146 | Err(Box::new(challenge(state, Some(name)))) |
| 147 | } else { | 147 | } else { |
| 148 | - Err(Box::new(denied())) | 148 | + // The same answer as a missing image, so a signed-in stranger cannot learn which |
| 149 | + // private image names exist. | ||
| 150 | + Err(Box::new(name_unknown())) | ||
| 149 | } | 151 | } |
| 150 | } | 152 | } |
| 151 | None if anonymous => Err(Box::new(challenge(state, Some(name)))), | 153 | None if anonymous => Err(Box::new(challenge(state, Some(name)))), |
| 152 | - None => Err(Box::new(oci_error( | 154 | + None => Err(Box::new(name_unknown())), |
| 153 | - StatusCode::NOT_FOUND, | ||
| 154 | - "NAME_UNKNOWN", | ||
| 155 | - "repository name not known to registry", | ||
| 156 | - ))), | ||
| 157 | } | 155 | } |
| 158 | } | 156 | } |
| 159 | 157 | ||
| 158 | +fn name_unknown() -> Response { | ||
| 159 | + oci_error( | ||
| 160 | + StatusCode::NOT_FOUND, | ||
| 161 | + "NAME_UNKNOWN", | ||
| 162 | + "repository name not known to registry", | ||
| 163 | + ) | ||
| 164 | +} | ||
| 165 | + | ||
| 160 | /// The 401 that asks for credentials. It names the token endpoint, and the repository's scope when | 166 | /// The 401 that asks for credentials. It names the token endpoint, and the repository's scope when |
| 161 | /// the request names one. | 167 | /// the request names one. |
| 162 | pub fn challenge(state: &AppState, name: Option<&str>) -> Response { | 168 | pub fn challenge(state: &AppState, name: Option<&str>) -> Response { |
crates/forge-web/src/pages/register.rsmodified+4 -1
| @@ -326,6 +326,7 @@ async fn register_submit(cx: &Cx, Form(form): Form<RegisterForm>) -> Result<impl | |||
| 326 | ) | 326 | ) |
| 327 | .await; | 327 | .await; |
| 328 | } else if let Some(address) = user.email.clone() { | 328 | } else if let Some(address) = user.email.clone() { |
| 329 | + // A brand-new account: the throttle cannot hold this first link back. | ||
| 329 | account::send_verify(state, &user, &address).await?; | 330 | account::send_verify(state, &user, &address).await?; |
| 330 | } | 331 | } |
| 331 | login(cx, &user).await?; | 332 | login(cx, &user).await?; |
| @@ -423,7 +424,9 @@ async fn verify_resend(cx: &Cx, Form(form): Form<ResendForm>) -> Result<SeeOther | |||
| 423 | if let Some(address) = viewer.0.email.clone() | 424 | if let Some(address) = viewer.0.email.clone() |
| 424 | && !state.store.email_is_verified(&viewer.0.id).await? | 425 | && !state.store.email_is_verified(&viewer.0.id).await? |
| 425 | { | 426 | { |
| 426 | - account::send_verify(state, &viewer.0, &address).await?; | 427 | + if !account::send_verify(state, &viewer.0, &address).await? { |
| 428 | + return Ok(see_other("/settings/security?saved=verify_wait")); | ||
| 429 | + } | ||
| 427 | } | 430 | } |
| 428 | Ok(see_other("/settings/security?saved=verify_sent")) | 431 | Ok(see_other("/settings/security?saved=verify_sent")) |
| 429 | } | 432 | } |
crates/forge-web/src/pages/settings_security.rsmodified+8 -2
| @@ -497,6 +497,11 @@ fn notice_for(lang: Lang, saved: Option<&str>) -> Option<&'static str> { | |||
| 497 | Some("password") => Some(t(lang, Key::OkPasswordChanged)), | 497 | Some("password") => Some(t(lang, Key::OkPasswordChanged)), |
| 498 | Some("email") => Some(t(lang, Key::OkEmailSaved)), | 498 | Some("email") => Some(t(lang, Key::OkEmailSaved)), |
| 499 | Some("verify_sent") => Some(t(lang, Key::OkVerifySent)), | 499 | Some("verify_sent") => Some(t(lang, Key::OkVerifySent)), |
| 500 | + Some("verify_wait") => Some(s( | ||
| 501 | + lang, | ||
| 502 | + "Too many verification emails went to this address lately, so none was sent now. Wait a few minutes, then use Resend.", | ||
| 503 | + "Bu adrese son zamanlarda çok fazla doğrulama e-postası gönderildi, bu yüzden şimdi gönderilmedi. Birkaç dakika bekleyip Yeniden gönder'i kullanın.", | ||
| 504 | + )), | ||
| 500 | Some("2fa-off") => Some(t(lang, Key::OkTwoFactorOff)), | 505 | Some("2fa-off") => Some(t(lang, Key::OkTwoFactorOff)), |
| 501 | Some("passkey-added") => Some(s(lang, "Passkey added.", "Geçiş anahtarı eklendi.")), | 506 | Some("passkey-added") => Some(s(lang, "Passkey added.", "Geçiş anahtarı eklendi.")), |
| 502 | Some("passkey-renamed") => Some(s( | 507 | Some("passkey-renamed") => Some(s( |
| @@ -769,7 +774,7 @@ async fn email_change(cx: &Cx, Form(form): Form<EmailForm>) -> Result<impl View> | |||
| 769 | Ok(()) => { | 774 | Ok(()) => { |
| 770 | // A new address is unverified until its link is opened. | 775 | // A new address is unverified until its link is opened. |
| 771 | state.store.clear_email_verified(&viewer.0.id).await?; | 776 | state.store.clear_email_verified(&viewer.0.id).await?; |
| 772 | - crate::mail::account::send_verify(state, &viewer.0, &email).await?; | 777 | + let sent = crate::mail::account::send_verify(state, &viewer.0, &email).await?; |
| 773 | // The allowed-signers lines and cached verdicts carry the old address. | 778 | // The allowed-signers lines and cached verdicts carry the old address. |
| 774 | crate::signatures::on_keys_changed(state, &viewer.0.id).await; | 779 | crate::signatures::on_keys_changed(state, &viewer.0.id).await; |
| 775 | audit::record( | 780 | audit::record( |
| @@ -781,7 +786,8 @@ async fn email_change(cx: &Cx, Form(form): Form<EmailForm>) -> Result<impl View> | |||
| 781 | serde_json::Value::Null, | 786 | serde_json::Value::Null, |
| 782 | ) | 787 | ) |
| 783 | .await; | 788 | .await; |
| 784 | - Err(see_other("/settings/security?saved=email").into()) | 789 | + let saved = if sent { "email" } else { "verify_wait" }; |
| 790 | + Err(see_other(format!("/settings/security?saved={saved}")).into()) | ||
| 785 | } | 791 | } |
| 786 | Err(e) => match user_facing_message(&e) { | 792 | Err(e) => match user_facing_message(&e) { |
| 787 | Some(message) => { | 793 | Some(message) => { |