@@ -5,7 +5,7 @@ use crate::auth::AuthCheck;
55use crate :: worker:: jobs:: {
66 self , CheckTyposquat , SendPublishNotificationsJob , UpdateDefaultVersion ,
77} ;
8- use axum:: body:: Bytes ;
8+ use axum:: body:: { Body , Bytes } ;
99use axum:: Json ;
1010use cargo_manifest:: { Dependency , DepsSet , TargetDepsSet } ;
1111use chrono:: { DateTime , SecondsFormat , Utc } ;
@@ -17,10 +17,12 @@ use diesel_async::scoped_futures::ScopedFutureExt;
1717use diesel_async:: { AsyncConnection , AsyncPgConnection , RunQueryDsl } ;
1818use futures_util:: TryStreamExt ;
1919use hex:: ToHex ;
20+ use http:: request:: Parts ;
2021use http:: StatusCode ;
21- use hyper:: body:: Buf ;
2222use sha2:: { Digest , Sha256 } ;
2323use std:: collections:: HashMap ;
24+ use tokio:: io:: { AsyncRead , AsyncReadExt } ;
25+ use tokio_util:: io:: StreamReader ;
2426use url:: Url ;
2527
2628use crate :: models:: {
@@ -35,7 +37,7 @@ use crate::rate_limiter::LimitedAction;
3537use crate :: schema:: * ;
3638use crate :: sql:: canon_crate_name;
3739use crate :: util:: errors:: { bad_request, custom, internal, AppResult , BoxedAppError } ;
38- use crate :: util:: { BytesRequest , Maximums } ;
40+ use crate :: util:: Maximums ;
3941use crate :: views:: {
4042 EncodableCrate , EncodableCrateDependency , GoodCrate , PublishMetadata , PublishWarnings ,
4143} ;
@@ -54,12 +56,20 @@ const MAX_DESCRIPTION_LENGTH: usize = 1000;
5456/// Currently blocks the HTTP thread, perhaps some function calls can spawn new
5557/// threads and return completion or error through other methods a `cargo publish
5658/// --status` command, via crates.io's front end, or email.
57- pub async fn publish ( app : AppState , req : BytesRequest ) -> AppResult < Json < GoodCrate > > {
58- let ( req, bytes) = req. 0 . into_parts ( ) ;
59- let ( json_bytes, tarball_bytes) = split_body ( bytes) ?;
59+ pub async fn publish ( app : AppState , req : Parts , body : Body ) -> AppResult < Json < GoodCrate > > {
60+ let stream = body. into_data_stream ( ) ;
61+ let stream = stream. map_err ( |err| std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , err) ) ;
62+ let mut reader = StreamReader :: new ( stream) ;
6063
61- let metadata: PublishMetadata = serde_json:: from_slice ( & json_bytes)
62- . map_err ( |e| bad_request ( format_args ! ( "invalid upload request: {e}" ) ) ) ?;
64+ // The format of the req.body() of a publish request is as follows:
65+ //
66+ // metadata length
67+ // metadata in JSON about the crate being published
68+ // .crate tarball length
69+ // .crate tarball file
70+
71+ const MAX_JSON_LENGTH : u32 = 1024 * 1024 ; // 1 MB
72+ let metadata = read_json_metadata ( & mut reader, MAX_JSON_LENGTH ) . await ?;
6373
6474 Crate :: validate_crate_name ( "crate" , & metadata. name ) . map_err ( bad_request) ?;
6575
@@ -136,20 +146,14 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
136146 . check_rate_limit ( auth. user ( ) . id , rate_limit_action, & mut conn)
137147 . await ?;
138148
139- let content_length = tarball_bytes. len ( ) as u64 ;
140-
141149 let maximums = Maximums :: new (
142150 existing_crate. as_ref ( ) . and_then ( |c| c. max_upload_size ) ,
143151 app. config . max_upload_size ,
144152 app. config . max_unpack_size ,
145153 ) ;
146154
147- if content_length > maximums. max_upload_size {
148- return Err ( custom (
149- StatusCode :: PAYLOAD_TOO_LARGE ,
150- format ! ( "max upload size is: {}" , maximums. max_upload_size) ,
151- ) ) ;
152- }
155+ let tarball_bytes = read_tarball_bytes ( & mut reader, maximums. max_upload_size as u32 ) . await ?;
156+ let content_length = tarball_bytes. len ( ) as u64 ;
153157
154158 let pkg_name = format ! ( "{}-{}" , & * metadata. name, & version_string) ;
155159 let tarball_info =
@@ -573,44 +577,65 @@ async fn count_versions_published_today(
573577 . await
574578}
575579
576- #[ instrument( skip_all) ]
577- fn split_body ( mut bytes : Bytes ) -> AppResult < ( Bytes , Bytes ) > {
578- // The format of the req.body() of a publish request is as follows:
579- //
580- // metadata length
581- // metadata in JSON about the crate being published
582- // .crate tarball length
583- // .crate tarball file
580+ async fn read_json_metadata < R : AsyncRead + Unpin > (
581+ reader : & mut R ,
582+ max_length : u32 ,
583+ ) -> Result < PublishMetadata , BoxedAppError > {
584+ let json_len = reader. read_u32_le ( ) . await . map_err ( |e| {
585+ if e. kind ( ) == std:: io:: ErrorKind :: UnexpectedEof {
586+ bad_request ( "invalid metadata length" )
587+ } else {
588+ e. into ( )
589+ }
590+ } ) ?;
584591
585- if bytes . len ( ) < 4 {
586- // Avoid panic in `get_u32_le()` if there is not enough remaining data
587- return Err ( bad_request ( "invalid metadata length" ) ) ;
592+ if json_len > max_length {
593+ let message = "JSON metadata blob too large" ;
594+ return Err ( custom ( StatusCode :: PAYLOAD_TOO_LARGE , message ) ) ;
588595 }
589596
590- let json_len = bytes. get_u32_le ( ) as usize ;
591- if json_len > bytes. len ( ) {
592- return Err ( bad_request ( format ! (
593- "invalid metadata length for remaining payload: {json_len}"
594- ) ) ) ;
595- }
597+ let mut json_bytes = vec ! [ 0 ; json_len as usize ] ;
598+ reader. read_exact ( & mut json_bytes) . await . map_err ( |e| {
599+ if e. kind ( ) == std:: io:: ErrorKind :: UnexpectedEof {
600+ let message = format ! ( "invalid metadata length for remaining payload: {json_len}" ) ;
601+ bad_request ( message)
602+ } else {
603+ e. into ( )
604+ }
605+ } ) ?;
596606
597- let json_bytes = bytes. split_to ( json_len) ;
607+ serde_json:: from_slice ( & json_bytes)
608+ . map_err ( |e| bad_request ( format_args ! ( "invalid upload request: {e}" ) ) )
609+ }
598610
599- if bytes. len ( ) < 4 {
600- // Avoid panic in `get_u32_le()` if there is not enough remaining data
601- return Err ( bad_request ( "invalid tarball length" ) ) ;
602- }
611+ async fn read_tarball_bytes < R : AsyncRead + Unpin > (
612+ reader : & mut R ,
613+ max_length : u32 ,
614+ ) -> Result < Bytes , BoxedAppError > {
615+ let tarball_len = reader. read_u32_le ( ) . await . map_err ( |e| {
616+ if e. kind ( ) == std:: io:: ErrorKind :: UnexpectedEof {
617+ bad_request ( "invalid tarball length" )
618+ } else {
619+ e. into ( )
620+ }
621+ } ) ?;
603622
604- let tarball_len = bytes. get_u32_le ( ) as usize ;
605- if tarball_len > bytes. len ( ) {
606- return Err ( bad_request ( format ! (
607- "invalid tarball length for remaining payload: {tarball_len}"
608- ) ) ) ;
623+ if tarball_len > max_length {
624+ let message = format ! ( "max upload size is: {}" , max_length) ;
625+ return Err ( custom ( StatusCode :: PAYLOAD_TOO_LARGE , message) ) ;
609626 }
610627
611- let tarball_bytes = bytes. split_to ( tarball_len) ;
628+ let mut tarball_bytes = vec ! [ 0 ; tarball_len as usize ] ;
629+ reader. read_exact ( & mut tarball_bytes) . await . map_err ( |e| {
630+ if e. kind ( ) == std:: io:: ErrorKind :: UnexpectedEof {
631+ let message = format ! ( "invalid tarball length for remaining payload: {tarball_len}" ) ;
632+ bad_request ( message)
633+ } else {
634+ e. into ( )
635+ }
636+ } ) ?;
612637
613- Ok ( ( json_bytes , tarball_bytes) )
638+ Ok ( Bytes :: from ( tarball_bytes) )
614639}
615640
616641async fn is_reserved_name ( name : & str , conn : & mut AsyncPgConnection ) -> QueryResult < bool > {
0 commit comments