Skip to content

Commit

Permalink
Merge pull request #10096 from Turbo87/maximums
Browse files Browse the repository at this point in the history
Inline `Maximums` struct
  • Loading branch information
Turbo87 authored Nov 28, 2024
2 parents ec834ae + 9fdeaa6 commit a27a914
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/config/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 7 additions & 9 deletions src/controllers/krate/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -142,18 +141,17 @@ pub async fn publish(app: AppState, req: Parts, body: Body) -> AppResult<Json<Go
.check_rate_limit(auth.user().id, rate_limit_action, &mut conn)
.await?;

let maximums = Maximums::new(
existing_crate.as_ref().and_then(|c| c.max_upload_size),
app.config.max_upload_size,
app.config.max_unpack_size,
);
let max_upload_size = existing_crate
.as_ref()
.and_then(|c| c.max_upload_size())
.unwrap_or(app.config.max_upload_size);

let tarball_bytes = read_tarball_bytes(&mut reader, maximums.max_upload_size as u32).await?;
let tarball_bytes = read_tarball_bytes(&mut reader, max_upload_size).await?;
let content_length = tarball_bytes.len() as u64;

let pkg_name = format!("{}-{}", &*metadata.name, &version_string);
let tarball_info =
process_tarball(&pkg_name, &*tarball_bytes, maximums.max_unpack_size).await?;
let max_unpack_size = std::cmp::max(app.config.max_unpack_size, max_upload_size as u64);
let tarball_info = process_tarball(&pkg_name, &*tarball_bytes, max_unpack_size).await?;

// `unwrap()` is safe here since `process_tarball()` validates that
// we only accept manifests with a `package` section and without
Expand Down
7 changes: 6 additions & 1 deletion src/models/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub struct Crate {
pub homepage: Option<String>,
pub documentation: Option<String>,
pub repository: Option<String>,
pub max_upload_size: Option<i32>,
max_upload_size: Option<i32>,
pub max_features: Option<i16>,
}

Expand Down Expand Up @@ -156,6 +156,11 @@ impl<'a> NewCrate<'a> {
}

impl Crate {
pub fn max_upload_size(&self) -> Option<u32> {
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.
///
Expand Down
4 changes: 2 additions & 2 deletions src/tests/krate/publish/max_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async fn tarball_between_default_axum_limit_and_max_upload_size() {
let (app, _, _, token) = TestApp::full()
.with_config(|config| {
config.max_upload_size = max_upload_size;
config.max_unpack_size = max_upload_size;
config.max_unpack_size = max_upload_size as u64;
})
.with_token()
.await;
Expand Down Expand Up @@ -65,7 +65,7 @@ async fn tarball_bigger_than_max_upload_size() {
let (app, _, _, token) = TestApp::full()
.with_config(|config| {
config.max_upload_size = max_upload_size;
config.max_unpack_size = max_upload_size;
config.max_unpack_size = max_upload_size as u64;
})
.with_token()
.await;
Expand Down
23 changes: 0 additions & 23 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::cmp;

pub use self::bytes_request::BytesRequest;
pub use self::io_util::{read_fill, read_le_u32};
pub use self::request_helpers::*;
Expand All @@ -12,24 +10,3 @@ mod request_helpers;
pub mod rfc3339;
pub mod token;
pub mod tracing;

#[derive(Debug, Copy, Clone)]
pub struct Maximums {
pub max_upload_size: u64,
pub max_unpack_size: u64,
}

impl Maximums {
pub fn new(
krate_max_upload: Option<i32>,
app_max_upload: u64,
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);
Maximums {
max_upload_size,
max_unpack_size,
}
}
}

0 comments on commit a27a914

Please sign in to comment.