Skip to content

Commit f6e5a86

Browse files
committed
controllers/krate/publish: Use existing_crate for upload size check
There are a couple of cases to consider here: - if a krate with the same name exists, `existing_crate` will be `Some(_)` and the `max_upload_size` value is used. - if no krate with the same name exists yet, `existing_crate` will be `None` and the default size limits are used. - if no krate with the same name exists yet and there are two concurrent publish requests, the default size limits are used. this should be fine since there is no way to increase the size limits during publish or via the API in general. tl;dr there is a potential race condition here, but it doesn't matter for this specific case
1 parent c2946d9 commit f6e5a86

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

src/controllers/krate/publish.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,21 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
110110
app.rate_limiter
111111
.check_rate_limit(user.id, rate_limit_action, conn)?;
112112

113+
let content_length = tarball_bytes.len() as u64;
114+
115+
let maximums = Maximums::new(
116+
existing_crate.as_ref().and_then(|c| c.max_upload_size),
117+
app.config.max_upload_size,
118+
app.config.max_unpack_size,
119+
);
120+
121+
if content_length > maximums.max_upload_size {
122+
return Err(cargo_err(&format_args!(
123+
"max upload size is: {}",
124+
maximums.max_upload_size
125+
)));
126+
}
127+
113128
// Create a transaction on the database, if there are no errors,
114129
// commit the transactions to record a new or updated crate.
115130
conn.transaction(|conn| {
@@ -177,21 +192,6 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
177192
}
178193
}
179194

180-
let content_length = tarball_bytes.len() as u64;
181-
182-
let maximums = Maximums::new(
183-
krate.max_upload_size,
184-
app.config.max_upload_size,
185-
app.config.max_unpack_size,
186-
);
187-
188-
if content_length > maximums.max_upload_size {
189-
return Err(cargo_err(&format_args!(
190-
"max upload size is: {}",
191-
maximums.max_upload_size
192-
)));
193-
}
194-
195195
// Read tarball from request
196196
let hex_cksum: String = Sha256::digest(&tarball_bytes).encode_hex();
197197

0 commit comments

Comments
 (0)