@@ -50,6 +50,13 @@ const PACK_OPS_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(300
5050const RECEIVE_PACK_MAX_OUTPUT_BYTES : u64 = 1024 * 1024 ;
5151/// Maximum ref advertisement output after manifest ref-count validation.
5252const INFO_REFS_MAX_OUTPUT_BYTES : u64 = 4 * 1024 * 1024 ;
53+ /// Maximum *decoded* upload-pack request body (want/have negotiation).
54+ ///
55+ /// Bounds the output of [`decode_git_request_body`] so a small gzip bomb
56+ /// cannot bypass the compressed-body `RequestBodyLimitLayer`. Negotiation
57+ /// bodies are pkt-lines of wants/haves (~50 bytes per ref); even a repo
58+ /// with a million refs stays far under this.
59+ const UPLOAD_PACK_MAX_DECODED_BYTES : u64 = 64 * 1024 * 1024 ;
5360
5461/// NIP-98 auth extractor for git routes.
5562///
@@ -728,7 +735,18 @@ async fn info_refs_subprocess(
728735/// Only `gzip` is decoded (the sole encoding git emits). An unknown
729736/// non-identity encoding is passed through unchanged rather than rejected;
730737/// the subprocess surfaces any real mismatch as an in-band protocol error.
731- fn decode_git_request_body ( headers : & axum:: http:: HeaderMap , body : Body ) -> Body {
738+ ///
739+ /// `max_decoded_bytes` bounds the *inflated* size: the router's
740+ /// `RequestBodyLimitLayer` only caps compressed bytes, so without this a
741+ /// small gzip bomb (ratios up to ~1000:1) could feed an effectively
742+ /// unbounded stream to the subprocess — for receive-pack that means
743+ /// unbounded scratch-disk writes. Exceeding the cap errors the stream,
744+ /// which the stdin pumps surface as a logged early-EOF to git.
745+ fn decode_git_request_body (
746+ headers : & axum:: http:: HeaderMap ,
747+ body : Body ,
748+ max_decoded_bytes : u64 ,
749+ ) -> Body {
732750 let is_gzip = headers
733751 . get ( header:: CONTENT_ENCODING )
734752 . and_then ( |v| v. to_str ( ) . ok ( ) )
@@ -741,11 +759,22 @@ fn decode_git_request_body(headers: &axum::http::HeaderMap, body: Body) -> Body
741759 use tokio_util:: io:: { ReaderStream , StreamReader } ;
742760 let byte_stream = body
743761 . into_data_stream ( )
744- . map ( |res| res. map_err ( |e| std:: io:: Error :: new ( std :: io :: ErrorKind :: Other , e ) ) )
762+ . map ( |res| res. map_err ( std:: io:: Error :: other ) )
745763 . boxed ( ) ;
746764 let decoder =
747765 async_compression:: tokio:: bufread:: GzipDecoder :: new ( StreamReader :: new ( byte_stream) ) ;
748- Body :: from_stream ( ReaderStream :: new ( decoder) )
766+ let mut decoded: u64 = 0 ;
767+ let capped = ReaderStream :: new ( decoder) . map ( move |chunk| {
768+ let chunk = chunk?;
769+ decoded = decoded. saturating_add ( chunk. len ( ) as u64 ) ;
770+ if decoded > max_decoded_bytes {
771+ return Err ( std:: io:: Error :: other ( format ! (
772+ "gzip git request body exceeded {max_decoded_bytes} decoded bytes"
773+ ) ) ) ;
774+ }
775+ Ok ( chunk)
776+ } ) ;
777+ Body :: from_stream ( capped)
749778}
750779
751780/// `POST /git/{owner}/{repo}/git-upload-pack`
@@ -762,7 +791,7 @@ pub async fn upload_pack(
762791 body : Body ,
763792) -> Result < Response , Response > {
764793 let _ = validate_repo_id ( & params. owner , & params. repo ) ?;
765- let body = decode_git_request_body ( & headers, body) ;
794+ let body = decode_git_request_body ( & headers, body, UPLOAD_PACK_MAX_DECODED_BYTES ) ;
766795 let permit = acquire_git_permit ( & state, "upload_pack" ) ?;
767796
768797 let repo = match hydrate_for_read (
@@ -834,7 +863,7 @@ pub async fn receive_pack(
834863 body : Body ,
835864) -> Result < Response , Response > {
836865 let repo_name = validate_repo_id ( & params. owner , & params. repo ) ?;
837- let body = decode_git_request_body ( & headers, body) ;
866+ let body = decode_git_request_body ( & headers, body, state . config . git_max_pack_bytes ) ;
838867 let pusher_hex = hex:: encode ( auth. pubkey . to_bytes ( ) ) ;
839868 let _permit = acquire_git_permit ( & state, "receive_pack" ) ?;
840869
@@ -1006,6 +1035,7 @@ async fn run_git_at(
10061035
10071036 // Stream request body to git stdin.
10081037 let mut stdin = child. stdin . take ( ) . unwrap ( ) ;
1038+ let pump_service = service. to_string ( ) ;
10091039 let body_task = tokio:: spawn ( async move {
10101040 use futures_util:: StreamExt ;
10111041 let mut stream = body. into_data_stream ( ) ;
@@ -1019,7 +1049,14 @@ async fn run_git_at(
10191049 break ;
10201050 }
10211051 }
1022- Err ( _) => break ,
1052+ Err ( e) => {
1053+ // Body/decode errors (client abort, malformed gzip,
1054+ // decoded-size cap) surface to git as early EOF; log so
1055+ // there is a server-side signal, not just an opaque
1056+ // client-side hangup.
1057+ warn ! ( error = %e, service = %pump_service, "git request body stream failed" ) ;
1058+ break ;
1059+ }
10231060 }
10241061 }
10251062 drop ( stdin) ; // close stdin → EOF for git
@@ -1416,7 +1453,14 @@ fn stream_git_read(
14161453 break ;
14171454 }
14181455 }
1419- Err ( _) => break ,
1456+ Err ( e) => {
1457+ // Body/decode errors (client abort, malformed gzip,
1458+ // decoded-size cap) surface to git as early EOF; log so
1459+ // there is a server-side signal, not just an opaque
1460+ // client-side hangup.
1461+ warn ! ( error = %e, service = %service, "git request body stream failed" ) ;
1462+ break ;
1463+ }
14201464 }
14211465 }
14221466 drop ( stdin) ; // close stdin → EOF for git
@@ -1753,7 +1797,7 @@ mod track_c_tests {
17531797
17541798 let mut headers = HeaderMap :: new ( ) ;
17551799 headers. insert ( header:: CONTENT_ENCODING , "gzip" . parse ( ) . unwrap ( ) ) ;
1756- let decoded = decode_git_request_body ( & headers, Body :: from ( gzipped) ) ;
1800+ let decoded = decode_git_request_body ( & headers, Body :: from ( gzipped) , u64 :: MAX ) ;
17571801 let bytes = axum:: body:: to_bytes ( decoded, usize:: MAX ) . await . unwrap ( ) ;
17581802 assert_eq ! ( bytes. as_ref( ) , plaintext) ;
17591803 }
@@ -1764,11 +1808,84 @@ mod track_c_tests {
17641808 async fn identity_request_body_is_passthrough ( ) {
17651809 use axum:: http:: HeaderMap ;
17661810 let plaintext = b"0032want cb09a769da1c01f458fa6959d4e8eded38fac8d3\n 0000" ;
1767- let decoded = decode_git_request_body ( & HeaderMap :: new ( ) , Body :: from ( plaintext. to_vec ( ) ) ) ;
1811+ let decoded =
1812+ decode_git_request_body ( & HeaderMap :: new ( ) , Body :: from ( plaintext. to_vec ( ) ) , u64:: MAX ) ;
17681813 let bytes = axum:: body:: to_bytes ( decoded, usize:: MAX ) . await . unwrap ( ) ;
17691814 assert_eq ! ( bytes. as_ref( ) , plaintext) ;
17701815 }
17711816
1817+ /// A gzip bomb is cut off at the decoded-byte cap: the router's
1818+ /// `RequestBodyLimitLayer` only bounds *compressed* bytes, so the
1819+ /// decode seam must enforce the inflated bound itself. Highly
1820+ /// compressible input (1 MiB of zeros → ~1 KiB gzip) must error once
1821+ /// the decoded stream crosses the cap.
1822+ #[ tokio:: test]
1823+ async fn gzip_request_body_over_decoded_cap_errors ( ) {
1824+ use axum:: http:: HeaderMap ;
1825+ use std:: io:: Write ;
1826+
1827+ let plaintext = vec ! [ 0u8 ; 1024 * 1024 ] ; // inflates 1 MiB from ~1 KiB wire
1828+ let mut encoder = flate2:: write:: GzEncoder :: new ( Vec :: new ( ) , flate2:: Compression :: default ( ) ) ;
1829+ encoder. write_all ( & plaintext) . unwrap ( ) ;
1830+ let gzipped = encoder. finish ( ) . unwrap ( ) ;
1831+ assert ! (
1832+ gzipped. len( ) < 64 * 1024 ,
1833+ "precondition: bomb is small on the wire (got {} bytes)" ,
1834+ gzipped. len( )
1835+ ) ;
1836+
1837+ let mut headers = HeaderMap :: new ( ) ;
1838+ headers. insert ( header:: CONTENT_ENCODING , "gzip" . parse ( ) . unwrap ( ) ) ;
1839+ let cap: u64 = 64 * 1024 ;
1840+ let decoded = decode_git_request_body ( & headers, Body :: from ( gzipped) , cap) ;
1841+ let err = axum:: body:: to_bytes ( decoded, usize:: MAX )
1842+ . await
1843+ . expect_err ( "decoded stream must error past the cap" ) ;
1844+ assert ! (
1845+ err. to_string( ) . contains( "decoded bytes" ) ,
1846+ "error should name the decoded-size cap, got: {err}"
1847+ ) ;
1848+ }
1849+
1850+ /// The decoded cap does not truncate bodies at or under the limit —
1851+ /// exactly-at-cap input passes through complete.
1852+ #[ tokio:: test]
1853+ async fn gzip_request_body_at_decoded_cap_passes ( ) {
1854+ use axum:: http:: HeaderMap ;
1855+ use std:: io:: Write ;
1856+
1857+ let plaintext = vec ! [ 7u8 ; 8 * 1024 ] ;
1858+ let mut encoder = flate2:: write:: GzEncoder :: new ( Vec :: new ( ) , flate2:: Compression :: default ( ) ) ;
1859+ encoder. write_all ( & plaintext) . unwrap ( ) ;
1860+ let gzipped = encoder. finish ( ) . unwrap ( ) ;
1861+
1862+ let mut headers = HeaderMap :: new ( ) ;
1863+ headers. insert ( header:: CONTENT_ENCODING , "gzip" . parse ( ) . unwrap ( ) ) ;
1864+ let decoded =
1865+ decode_git_request_body ( & headers, Body :: from ( gzipped) , plaintext. len ( ) as u64 ) ;
1866+ let bytes = axum:: body:: to_bytes ( decoded, usize:: MAX ) . await . unwrap ( ) ;
1867+ assert_eq ! ( bytes. as_ref( ) , plaintext) ;
1868+ }
1869+
1870+ /// Malformed gzip (valid header, corrupt deflate stream) surfaces as a
1871+ /// stream error rather than silently yielding garbage — the stdin pump
1872+ /// logs it and closes the subprocess's stdin early.
1873+ #[ tokio:: test]
1874+ async fn malformed_gzip_request_body_errors ( ) {
1875+ use axum:: http:: HeaderMap ;
1876+
1877+ // Valid 10-byte gzip header followed by garbage.
1878+ let mut bogus = vec ! [ 0x1f , 0x8b , 0x08 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0xff ] ;
1879+ bogus. extend_from_slice ( b"this is not deflate data at all" ) ;
1880+
1881+ let mut headers = HeaderMap :: new ( ) ;
1882+ headers. insert ( header:: CONTENT_ENCODING , "gzip" . parse ( ) . unwrap ( ) ) ;
1883+ let decoded = decode_git_request_body ( & headers, Body :: from ( bogus) , u64:: MAX ) ;
1884+ axum:: body:: to_bytes ( decoded, usize:: MAX )
1885+ . await
1886+ . expect_err ( "corrupt gzip must error the decoded stream" ) ;
1887+ }
1888+
17721889 fn branches_only_manifest ( ) -> Manifest {
17731890 let mut refs = BTreeMap :: new ( ) ;
17741891 refs. insert ( "refs/heads/feature" . to_string ( ) , oid_sha1 ( ) ) ;
0 commit comments