From b036c8e8cc55568b1f9b9251dace0abe59134540 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 16 May 2025 08:11:17 +0200 Subject: [PATCH 1/3] Replace `Error::new(ErrorKind::Other, ...)` with `Error::other(...)` --- crates/crates_io_tarball/src/limit_reader.rs | 7 +++---- src/bin/crates-admin/render_readmes.rs | 4 +--- src/controllers/krate/publish.rs | 2 +- src/sentry/mod.rs | 2 +- src/util/errors.rs | 2 +- src/util/io_util.rs | 2 +- 6 files changed, 8 insertions(+), 11 deletions(-) diff --git a/crates/crates_io_tarball/src/limit_reader.rs b/crates/crates_io_tarball/src/limit_reader.rs index b77070b7ce8..aa43a4942c3 100644 --- a/crates/crates_io_tarball/src/limit_reader.rs +++ b/crates/crates_io_tarball/src/limit_reader.rs @@ -25,10 +25,9 @@ impl AsyncRead for LimitErrorReader { ) -> Poll> { let reader = Pin::new(&mut self.inner); match reader.poll_read(cx, buf) { - Poll::Ready(Ok(())) if self.inner.limit() == 0 => Poll::Ready(Err(io::Error::new( - io::ErrorKind::Other, - "maximum limit reached when reading", - ))), + Poll::Ready(Ok(())) if self.inner.limit() == 0 => { + Poll::Ready(Err(io::Error::other("maximum limit reached when reading"))) + } e => e, } } diff --git a/src/bin/crates-admin/render_readmes.rs b/src/bin/crates-admin/render_readmes.rs index 0d586cec96f..0ede1c9a174 100644 --- a/src/bin/crates-admin/render_readmes.rs +++ b/src/bin/crates-admin/render_readmes.rs @@ -169,9 +169,7 @@ async fn get_readme( )); } - let reader = response - .bytes_stream() - .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e)); + let reader = response.bytes_stream().map_err(std::io::Error::other); let reader = StreamReader::new(reader); let reader = GzipDecoder::new(reader); let archive = Archive::new(reader); diff --git a/src/controllers/krate/publish.rs b/src/controllers/krate/publish.rs index 0e5a3b78cb1..c3456ac9b2d 100644 --- a/src/controllers/krate/publish.rs +++ b/src/controllers/krate/publish.rs @@ -67,7 +67,7 @@ const MAX_DESCRIPTION_LENGTH: usize = 1000; )] pub async fn publish(app: AppState, req: Parts, body: Body) -> AppResult> { let stream = body.into_data_stream(); - let stream = stream.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err)); + let stream = stream.map_err(std::io::Error::other); let mut reader = StreamReader::new(stream); // The format of the req.body() of a publish request is as follows: diff --git a/src/sentry/mod.rs b/src/sentry/mod.rs index 365c68d5fd5..451a417a590 100644 --- a/src/sentry/mod.rs +++ b/src/sentry/mod.rs @@ -113,7 +113,7 @@ mod tests { query_string: None, env: Default::default(), }; - let err = std::io::Error::new(std::io::ErrorKind::Other, "error"); + let err = std::io::Error::other("error"); let opts = options(SentryConfig::default()); let event_req = req.clone(); diff --git a/src/util/errors.rs b/src/util/errors.rs index cdc357c18bc..c824fb26ddb 100644 --- a/src/util/errors.rs +++ b/src/util/errors.rs @@ -311,7 +311,7 @@ mod tests { StatusCode::INTERNAL_SERVER_ERROR ); assert_eq!( - BoxedAppError::from(::std::io::Error::new(::std::io::ErrorKind::Other, "")) + BoxedAppError::from(::std::io::Error::other("")) .response() .status(), StatusCode::INTERNAL_SERVER_ERROR diff --git a/src/util/io_util.rs b/src/util/io_util.rs index b8f6ec8807a..7068bfca21c 100644 --- a/src/util/io_util.rs +++ b/src/util/io_util.rs @@ -17,7 +17,7 @@ pub fn read_fill(r: &mut R, mut slice: &mut [u8]) -> io::Resul while !slice.is_empty() { let n = r.read(slice)?; if n == 0 { - return Err(io::Error::new(io::ErrorKind::Other, "end of file reached")); + return Err(io::Error::other("end of file reached")); } slice = &mut mem::take(&mut slice)[n..]; } From e297454de274f2abe3ef1c4590287174aeb9dc17 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 16 May 2025 08:18:05 +0200 Subject: [PATCH 2/3] middleware/block_traffic: Fix "large Err variant" warnings --- src/middleware/block_traffic.rs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/middleware/block_traffic.rs b/src/middleware/block_traffic.rs index 2a80969a5a7..7e1bd9deb4f 100644 --- a/src/middleware/block_traffic.rs +++ b/src/middleware/block_traffic.rs @@ -1,7 +1,7 @@ use crate::app::AppState; use crate::middleware::log_request::RequestLogExt; use crate::middleware::real_ip::RealIp; -use crate::util::errors::custom; +use crate::util::errors::{BoxedAppError, custom}; use axum::extract::{Extension, MatchedPath, Request}; use axum::middleware::Next; use axum::response::{IntoResponse, Response}; @@ -14,9 +14,9 @@ pub async fn middleware( req: Request, next: Next, ) -> Result { - block_by_ip(&real_ip, &state, req.headers())?; - block_by_header(&state, &req)?; - block_routes(matched_path.as_ref(), &state)?; + block_by_ip(&real_ip, &state, req.headers()).map_err(IntoResponse::into_response)?; + block_by_header(&state, &req).map_err(IntoResponse::into_response)?; + block_routes(matched_path.as_ref(), &state).map_err(IntoResponse::into_response)?; Ok(next.run(req).await) } @@ -29,7 +29,7 @@ pub async fn middleware( /// to `User-Agent=BLOCKED_UAS` and `BLOCKED_UAS` to `curl/7.54.0,cargo 1.36.0 (c4fcfb725 2019-05-15)` /// to block requests from the versions of curl or Cargo specified (values are nonsensical examples). /// Values of the headers must match exactly. -pub fn block_by_header(state: &AppState, req: &Request) -> Result<(), Response> { +pub fn block_by_header(state: &AppState, req: &Request) -> Result<(), impl IntoResponse> { let blocked_traffic = &state.config.blocked_traffic; for (header_name, blocked_values) in blocked_traffic { @@ -53,7 +53,7 @@ pub fn block_by_ip( real_ip: &RealIp, state: &AppState, headers: &HeaderMap, -) -> Result<(), Response> { +) -> Result<(), impl IntoResponse> { if state.config.blocked_ips.contains(real_ip) { return Err(rejection_response_from(state, headers)); } @@ -61,7 +61,7 @@ pub fn block_by_ip( Ok(()) } -fn rejection_response_from(state: &AppState, headers: &HeaderMap) -> Response { +fn rejection_response_from(state: &AppState, headers: &HeaderMap) -> impl IntoResponse { let domain_name = &state.config.domain_name; // Heroku should always set this header @@ -77,17 +77,19 @@ fn rejection_response_from(state: &AppState, headers: &HeaderMap) -> Response { Please email help@crates.io and provide the request id {request_id}" ); - (StatusCode::FORBIDDEN, body).into_response() + (StatusCode::FORBIDDEN, body) } /// Allow blocking individual routes by their pattern through the `BLOCKED_ROUTES` /// environment variable. -pub fn block_routes(matched_path: Option<&MatchedPath>, state: &AppState) -> Result<(), Response> { +pub fn block_routes( + matched_path: Option<&MatchedPath>, + state: &AppState, +) -> Result<(), BoxedAppError> { if let Some(matched_path) = matched_path { if state.config.blocked_routes.contains(matched_path.as_str()) { let body = "This route is temporarily blocked. See https://status.crates.io."; - let error = custom(StatusCode::SERVICE_UNAVAILABLE, body); - return Err(error.into_response()); + return Err(custom(StatusCode::SERVICE_UNAVAILABLE, body)); } } From 7978b29e64dc0ce36860d1b380e43b2cafea005d Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Thu, 15 May 2025 22:53:33 +0000 Subject: [PATCH 3/3] Update Rust to v1.87.0 --- backend.Dockerfile | 2 +- rust-toolchain.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/backend.Dockerfile b/backend.Dockerfile index 201afd6c81b..78c5922b3f0 100644 --- a/backend.Dockerfile +++ b/backend.Dockerfile @@ -1,5 +1,5 @@ # renovate: datasource=github-tags depName=rust lookupName=rust-lang/rust -ARG RUST_VERSION=1.86.0 +ARG RUST_VERSION=1.87.0 FROM rust:$RUST_VERSION diff --git a/rust-toolchain.toml b/rust-toolchain.toml index cf6d0f556a3..b8889a3bb3b 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,2 +1,2 @@ [toolchain] -channel = "1.86.0" +channel = "1.87.0"