diff --git a/crates/forge-core/src/cargo.rs b/crates/forge-core/src/cargo.rs index 5922c75..8a098ed 100644 --- a/crates/forge-core/src/cargo.rs +++ b/crates/forge-core/src/cargo.rs @@ -184,7 +184,10 @@ pub fn repo_link(repository: &str, base_url: &str) -> Option<(String, String)> { /// (`-/README.md`). Read at most [`MAX_README_BYTES`] bytes. Returns `None` when /// the archive is not a gzipped tarball or has no README. pub fn crate_readme(crate_file: &[u8]) -> Option { - let mut archive = tar::Archive::new(GzDecoder::new(crate_file)); + const MAX_INFLATED_SCAN_BYTES: u64 = 256 * 1024 * 1024; + // A small upload can inflate without end; past this much the README search gives up. + let inflated = GzDecoder::new(crate_file).take(MAX_INFLATED_SCAN_BYTES); + let mut archive = tar::Archive::new(inflated); let entries = archive.entries().ok()?; for entry in entries { let entry = entry.ok()?; diff --git a/crates/forge-web/src/mail/account.rs b/crates/forge-web/src/mail/account.rs index 0de7921..470677b 100644 --- a/crates/forge-web/src/mail/account.rs +++ b/crates/forge-web/src/mail/account.rs @@ -36,8 +36,31 @@ pub async fn send_reset(state: &AppState, user: &User) -> forge_core::Result<()> Ok(()) } -/// Queues a verification link for `address`, which is the user's current address. -pub async fn send_verify(state: &AppState, user: &User, address: &str) -> forge_core::Result<()> { +/// Verification mails one address may get inside the throttle window. +const VERIFY_ADDRESS_LIMIT: usize = 3; +/// Verification mails one account may send inside the throttle window, to any addresses. +const VERIFY_USER_LIMIT: usize = 5; + +/// Queues a verification link for `address`, which is the user's current address. Returns false, +/// with nothing queued, when the address or the account had too many of them lately: an account +/// may set any address as its own, so resends would otherwise mail a stranger without end. +pub async fn send_verify(state: &AppState, user: &User, address: &str) -> forge_core::Result { + let now = std::time::Instant::now(); + let address_key = format!("verify-address:{}", address.to_ascii_lowercase()); + let user_key = format!("verify-user:{}", user.id); + if state + .throttle + .check(&address_key, VERIFY_ADDRESS_LIMIT, now) + .is_some() + || state + .throttle + .check(&user_key, VERIFY_USER_LIMIT, now) + .is_some() + { + return Ok(false); + } + state.throttle.record_failure(&address_key, now); + state.throttle.record_failure(&user_key, now); let token = state.store.create_verify_token(&user.id, address).await?; let link = format!("{}/verify/{token}", base(state)); 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_ &body, ) .await?; - Ok(()) + Ok(true) } /// Creates an invite for `address` and queues its link. Returns the store's refusal, such as an diff --git a/crates/forge-web/src/oci/auth.rs b/crates/forge-web/src/oci/auth.rs index d5bcffc..b69a3bf 100644 --- a/crates/forge-web/src/oci/auth.rs +++ b/crates/forge-web/src/oci/auth.rs @@ -145,18 +145,24 @@ pub async fn authorize( } else if anonymous { Err(Box::new(challenge(state, Some(name)))) } else { - Err(Box::new(denied())) + // The same answer as a missing image, so a signed-in stranger cannot learn which + // private image names exist. + Err(Box::new(name_unknown())) } } None if anonymous => Err(Box::new(challenge(state, Some(name)))), - None => Err(Box::new(oci_error( - StatusCode::NOT_FOUND, - "NAME_UNKNOWN", - "repository name not known to registry", - ))), + None => Err(Box::new(name_unknown())), } } +fn name_unknown() -> Response { + oci_error( + StatusCode::NOT_FOUND, + "NAME_UNKNOWN", + "repository name not known to registry", + ) +} + /// The 401 that asks for credentials. It names the token endpoint, and the repository's scope when /// the request names one. pub fn challenge(state: &AppState, name: Option<&str>) -> Response { diff --git a/crates/forge-web/src/pages/register.rs b/crates/forge-web/src/pages/register.rs index 7d28dd3..c0c060c 100644 --- a/crates/forge-web/src/pages/register.rs +++ b/crates/forge-web/src/pages/register.rs @@ -326,6 +326,7 @@ async fn register_submit(cx: &Cx, Form(form): Form) -> Result) -> Result) -> Option<&'static str> { Some("password") => Some(t(lang, Key::OkPasswordChanged)), Some("email") => Some(t(lang, Key::OkEmailSaved)), Some("verify_sent") => Some(t(lang, Key::OkVerifySent)), + Some("verify_wait") => Some(s( + lang, + "Too many verification emails went to this address lately, so none was sent now. Wait a few minutes, then use Resend.", + "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.", + )), Some("2fa-off") => Some(t(lang, Key::OkTwoFactorOff)), Some("passkey-added") => Some(s(lang, "Passkey added.", "Geçiş anahtarı eklendi.")), Some("passkey-renamed") => Some(s( @@ -769,7 +774,7 @@ async fn email_change(cx: &Cx, Form(form): Form) -> Result Ok(()) => { // A new address is unverified until its link is opened. state.store.clear_email_verified(&viewer.0.id).await?; - crate::mail::account::send_verify(state, &viewer.0, &email).await?; + let sent = crate::mail::account::send_verify(state, &viewer.0, &email).await?; // The allowed-signers lines and cached verdicts carry the old address. crate::signatures::on_keys_changed(state, &viewer.0.id).await; audit::record( @@ -781,7 +786,8 @@ async fn email_change(cx: &Cx, Form(form): Form) -> Result serde_json::Value::Null, ) .await; - Err(see_other("/settings/security?saved=email").into()) + let saved = if sent { "email" } else { "verify_wait" }; + Err(see_other(format!("/settings/security?saved={saved}")).into()) } Err(e) => match user_facing_message(&e) { Some(message) => {