Skip to content

Introduce length limit on the description field #8746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/controllers/krate/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
to accept an invitation to be an owner before \
publishing.";

const MAX_DESCRIPTION_LENGTH: usize = 1000;

/// Handles the `PUT /crates/new` route.
/// Used by `cargo publish` to publish a new crate or to publish a new version of an
/// existing crate.
Expand Down Expand Up @@ -160,6 +162,12 @@
return Err(bad_request(&message));
}

if let Some(description) = &description {
if description.len() > MAX_DESCRIPTION_LENGTH {
return Err(bad_request(format!("The `description` is too long. A maximum of {MAX_DESCRIPTION_LENGTH} characters are currently allowed.")));
}
}

Check warning on line 169 in src/controllers/krate/publish.rs

View check run for this annotation

Codecov / codecov/patch

src/controllers/krate/publish.rs#L169

Added line #L169 was not covered by tests

if let Some(ref license) = license {
parse_license_expr(license).map_err(|e| bad_request(format_args!(
"unknown or invalid license expression; \
Expand Down
16 changes: 15 additions & 1 deletion src/tests/krate/publish/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::util::{RequestHelper, TestApp};
use crates_io::models::krate::MAX_NAME_LENGTH;
use googletest::prelude::*;
use http::StatusCode;
use insta::assert_json_snapshot;
use insta::{assert_json_snapshot, assert_snapshot};

#[tokio::test(flavor = "multi_thread")]
async fn empty_json() {
Expand Down Expand Up @@ -89,6 +89,20 @@ async fn license_and_description_required() {
assert_that!(app.stored_files().await, empty());
}

#[tokio::test(flavor = "multi_thread")]
async fn long_description() {
let (app, _, _, token) = TestApp::full().with_token();

let description = "a".repeat(2000);
let crate_to_publish = PublishBuilder::new("foo_metadata", "1.1.0").description(&description);

let response = token.publish_crate(crate_to_publish).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_snapshot!(response.text(), @r###"{"errors":[{"detail":"The `description` is too long. A maximum of 1000 characters are currently allowed."}]}"###);

assert_that!(app.stored_files().await, empty());
}

#[tokio::test(flavor = "multi_thread")]
async fn invalid_license() {
let (app, _, _, token) = TestApp::full().with_token();
Expand Down