Skip to content

Commit c6f57da

Browse files
authored
Introduce length limit on the description field (#8746)
1 parent 4602107 commit c6f57da

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/controllers/krate/publish.rs

+8
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const MISSING_RIGHTS_ERROR_MESSAGE: &str = "this crate exists but you don't seem
3838
to accept an invitation to be an owner before \
3939
publishing.";
4040

41+
const MAX_DESCRIPTION_LENGTH: usize = 1000;
42+
4143
/// Handles the `PUT /crates/new` route.
4244
/// Used by `cargo publish` to publish a new crate or to publish a new version of an
4345
/// existing crate.
@@ -160,6 +162,12 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
160162
return Err(bad_request(&message));
161163
}
162164

165+
if let Some(description) = &description {
166+
if description.len() > MAX_DESCRIPTION_LENGTH {
167+
return Err(bad_request(format!("The `description` is too long. A maximum of {MAX_DESCRIPTION_LENGTH} characters are currently allowed.")));
168+
}
169+
}
170+
163171
if let Some(ref license) = license {
164172
parse_license_expr(license).map_err(|e| bad_request(format_args!(
165173
"unknown or invalid license expression; \

src/tests/krate/publish/validation.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::util::{RequestHelper, TestApp};
33
use crates_io::models::krate::MAX_NAME_LENGTH;
44
use googletest::prelude::*;
55
use http::StatusCode;
6-
use insta::assert_json_snapshot;
6+
use insta::{assert_json_snapshot, assert_snapshot};
77

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

92+
#[tokio::test(flavor = "multi_thread")]
93+
async fn long_description() {
94+
let (app, _, _, token) = TestApp::full().with_token();
95+
96+
let description = "a".repeat(2000);
97+
let crate_to_publish = PublishBuilder::new("foo_metadata", "1.1.0").description(&description);
98+
99+
let response = token.publish_crate(crate_to_publish).await;
100+
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
101+
assert_snapshot!(response.text(), @r###"{"errors":[{"detail":"The `description` is too long. A maximum of 1000 characters are currently allowed."}]}"###);
102+
103+
assert_that!(app.stored_files().await, empty());
104+
}
105+
92106
#[tokio::test(flavor = "multi_thread")]
93107
async fn invalid_license() {
94108
let (app, _, _, token) = TestApp::full().with_token();

0 commit comments

Comments
 (0)