Skip to content

Commit

Permalink
Merge pull request #7880 from Turbo87/payload-too-large
Browse files Browse the repository at this point in the history
Respond with "413 Payload too large" when max upload size is exceeded
  • Loading branch information
Turbo87 authored Jan 5, 2024
2 parents 1155131 + 97dcf53 commit 7dd274b
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/controllers/krate/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::models::token::EndpointScope;
use crate::rate_limiter::LimitedAction;
use crate::schema::*;
use crate::sql::canon_crate_name;
use crate::util::errors::{cargo_err, internal, AppResult};
use crate::util::errors::{cargo_err, custom, internal, AppResult};
use crate::util::Maximums;
use crate::views::{
EncodableCrate, EncodableCrateDependency, GoodCrate, PublishMetadata, PublishWarnings,
Expand Down Expand Up @@ -120,7 +120,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
);

if content_length > maximums.max_upload_size {
return Err(cargo_err(format_args!(
return Err(custom(StatusCode::PAYLOAD_TOO_LARGE, format!(
"max upload size is: {}",
maximums.max_upload_size
)));
Expand Down
2 changes: 1 addition & 1 deletion src/tests/krate/publish/max_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn tarball_bigger_than_max_upload_size() {
let body = PublishBuilder::create_publish_body(&json, &tarball);

let response = token.publish_crate(body);
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(response.status(), StatusCode::PAYLOAD_TOO_LARGE);
assert_json_snapshot!(response.json());
assert_that!(app.stored_files(), empty());
}
Expand Down
4 changes: 2 additions & 2 deletions src/util/bytes_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ where
let collected = body.collect().await.map_err(|err| {
let box_error = err.into_inner();
match box_error.downcast::<LengthLimitError>() {
Ok(_) => StatusCode::BAD_REQUEST.into_response(),
Ok(_) => StatusCode::PAYLOAD_TOO_LARGE.into_response(),
Err(err) => server_error_response(&*err),
}
})?;
Expand Down Expand Up @@ -91,7 +91,7 @@ mod tests {
let request = Request::get("/").body(body).unwrap();
let response = app().oneshot(request).await.unwrap();

assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_eq!(response.status(), StatusCode::PAYLOAD_TOO_LARGE);

let body = vec![0; BODY_SIZE_LIMIT];
let body = axum::body::Body::from(body);
Expand Down

0 comments on commit 7dd274b

Please sign in to comment.