From f55f67ee0ad6cdb173541a93731d41a46e5cf0ba Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Wed, 27 Nov 2024 21:00:25 +0100 Subject: [PATCH 1/3] config/server: Change `max_upload_size` to `u32` A higher value isn't possible anyway due to the way the publish endpoint was designed... --- src/config/server.rs | 2 +- src/controllers/krate/publish.rs | 2 +- src/tests/krate/publish/max_size.rs | 4 ++-- src/util.rs | 9 +++++---- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/config/server.rs b/src/config/server.rs index 8f59f94aedf..0262c4ce6bf 100644 --- a/src/config/server.rs +++ b/src/config/server.rs @@ -41,7 +41,7 @@ pub struct Server { pub session_key: cookie::Key, pub gh_client_id: ClientId, pub gh_client_secret: ClientSecret, - pub max_upload_size: u64, + pub max_upload_size: u32, pub max_unpack_size: u64, pub max_dependencies: usize, pub max_features: usize, diff --git a/src/controllers/krate/publish.rs b/src/controllers/krate/publish.rs index f44a78ea6c6..ed1694309d3 100644 --- a/src/controllers/krate/publish.rs +++ b/src/controllers/krate/publish.rs @@ -152,7 +152,7 @@ pub async fn publish(app: AppState, req: Parts, body: Body) -> AppResult, - app_max_upload: u64, + app_max_upload: u32, app_max_unpack: u64, ) -> Maximums { - let max_upload_size = krate_max_upload.map(|m| m as u64).unwrap_or(app_max_upload); - let max_unpack_size = cmp::max(app_max_unpack, max_upload_size); + let krate_max_upload = krate_max_upload.and_then(|m| u32::try_from(m).ok()); + let max_upload_size = krate_max_upload.unwrap_or(app_max_upload); + let max_unpack_size = cmp::max(app_max_unpack, max_upload_size as u64); Maximums { max_upload_size, max_unpack_size, From 4312354e63d1c6b86356d92d69de79f556ea2419 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Wed, 27 Nov 2024 21:07:24 +0100 Subject: [PATCH 2/3] controllers/krate/publish: Inline `Maximums` calculation This is only used in the publish endpoint, and can apparently be written quite a bit more concise... :D --- src/controllers/krate/publish.rs | 17 ++++++++--------- src/util.rs | 24 ------------------------ 2 files changed, 8 insertions(+), 33 deletions(-) diff --git a/src/controllers/krate/publish.rs b/src/controllers/krate/publish.rs index ed1694309d3..343e728efb7 100644 --- a/src/controllers/krate/publish.rs +++ b/src/controllers/krate/publish.rs @@ -37,7 +37,6 @@ use crate::rate_limiter::LimitedAction; use crate::schema::*; use crate::sql::canon_crate_name; use crate::util::errors::{bad_request, custom, internal, AppResult, BoxedAppError}; -use crate::util::Maximums; use crate::views::{ EncodableCrate, EncodableCrateDependency, GoodCrate, PublishMetadata, PublishWarnings, }; @@ -146,18 +145,18 @@ pub async fn publish(app: AppState, req: Parts, body: Body) -> AppResult, - app_max_upload: u32, - app_max_unpack: u64, - ) -> Maximums { - let krate_max_upload = krate_max_upload.and_then(|m| u32::try_from(m).ok()); - let max_upload_size = krate_max_upload.unwrap_or(app_max_upload); - let max_unpack_size = cmp::max(app_max_unpack, max_upload_size as u64); - Maximums { - max_upload_size, - max_unpack_size, - } - } -} From 9fdeaa6dac2d5530eb877c99dc614efa9330c9fd Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Thu, 28 Nov 2024 10:29:28 +0100 Subject: [PATCH 3/3] models/krate: Extract `max_upload_size()` fn --- src/controllers/krate/publish.rs | 3 +-- src/models/krate.rs | 7 ++++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/controllers/krate/publish.rs b/src/controllers/krate/publish.rs index 343e728efb7..1a72bf650d6 100644 --- a/src/controllers/krate/publish.rs +++ b/src/controllers/krate/publish.rs @@ -147,8 +147,7 @@ pub async fn publish(app: AppState, req: Parts, body: Body) -> AppResult, pub documentation: Option, pub repository: Option, - pub max_upload_size: Option, + max_upload_size: Option, pub max_features: Option, } @@ -156,6 +156,11 @@ impl<'a> NewCrate<'a> { } impl Crate { + pub fn max_upload_size(&self) -> Option { + self.max_upload_size + .and_then(|size| u32::try_from(size).ok()) + } + /// SQL filter based on whether the crate's name loosely matches the given /// string. ///