Skip to content

Commit 9cae1fe

Browse files
Kaalclaude
andcommitted
fix(relay): decompress gzip-encoded git smart-HTTP request bodies
git's smart-HTTP client gzip-compresses the git-upload-pack / git-receive-pack request body once it exceeds an internal size threshold. That threshold is driven by the number of want/have lines, so it fires reliably on clones/fetches of repositories with many refs (hundreds+ of branches/tags), while small repositories stay under it. The relay pipes the request body straight into the git subprocess's stdin, so a still-compressed body reaches `git upload-pack` as raw gzip. git then fails parsing pkt-lines with: fatal: protocol error: bad line length character: <binary> Because the subprocess stderr is discarded, this surfaced to clients only as a mid-stream "the remote end hung up unexpectedly" — with no server-side signal — making it hard to diagnose. The symptom is strongly ref-count-correlated (small repos clone fine, many-ref repos always fail), which is exactly the gzip threshold crossing. Fix: add `decode_git_request_body`, which transparently inflates the body when `Content-Encoding: gzip` (or `x-gzip`) is present, and apply it in both `upload_pack` and `receive_pack` before the body is streamed to the subprocess. Identity-encoded bodies pass through byte-for-byte, so the common small-clone path is unchanged. Only gzip is decoded (the sole encoding git emits); an unknown non-identity encoding is passed through rather than rejected, letting the subprocess surface any real mismatch as an in-band protocol error. Adds unit tests for both the gzip-inflate and identity-passthrough paths. Co-Authored-By: Claude Opus 4.8 <[email protected]>
1 parent acfbb1b commit 9cae1fe

3 files changed

Lines changed: 79 additions & 0 deletions

File tree

‎Cargo.lock‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/buzz-relay/Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ metrics = { workspace = true }
7878
metrics-exporter-prometheus = { workspace = true }
7979
metrics-util = { workspace = true }
8080
pulldown-cmark = { version = "0.13.4", default-features = false, features = ["html"] }
81+
async-compression = { version = "0.4.42", features = ["tokio", "gzip"] }
8182

8283
[features]
8384
dev = ["buzz-auth/dev"]
@@ -90,3 +91,4 @@ buzz-auth = { workspace = true, features = ["dev"] }
9091
reqwest = { workspace = true }
9192
tokio-tungstenite = { workspace = true }
9293
futures = "0.3"
94+
flate2 = "1.1.9"

‎crates/buzz-relay/src/api/git/transport.rs‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,40 @@ async fn info_refs_subprocess(
714714
.unwrap())
715715
}
716716

717+
/// Decode a smart-HTTP git request body according to its `Content-Encoding`.
718+
///
719+
/// Git's smart-HTTP client gzip-compresses the `git-upload-pack` /
720+
/// `git-receive-pack` request body once it exceeds an internal size
721+
/// threshold (`http.postBuffer`-independent; triggered by the number of
722+
/// want/have lines, so it fires reliably on many-ref clones). The relay
723+
/// pipes the request body straight into the git subprocess's stdin, so a
724+
/// still-compressed body reaches `git upload-pack` as raw gzip and fails
725+
/// with `fatal: protocol error: bad line length character`. Transparently
726+
/// inflate here so the subprocess always sees plain pkt-lines.
727+
///
728+
/// Only `gzip` is decoded (the sole encoding git emits). An unknown
729+
/// non-identity encoding is passed through unchanged rather than rejected;
730+
/// 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 {
732+
let is_gzip = headers
733+
.get(header::CONTENT_ENCODING)
734+
.and_then(|v| v.to_str().ok())
735+
.map(|v| v.eq_ignore_ascii_case("gzip") || v.eq_ignore_ascii_case("x-gzip"))
736+
.unwrap_or(false);
737+
if !is_gzip {
738+
return body;
739+
}
740+
use futures_util::StreamExt;
741+
use tokio_util::io::{ReaderStream, StreamReader};
742+
let byte_stream = body
743+
.into_data_stream()
744+
.map(|res| res.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e)))
745+
.boxed();
746+
let decoder =
747+
async_compression::tokio::bufread::GzipDecoder::new(StreamReader::new(byte_stream));
748+
Body::from_stream(ReaderStream::new(decoder))
749+
}
750+
717751
/// `POST /git/{owner}/{repo}/git-upload-pack`
718752
///
719753
/// Handles clone/fetch — client sends wants/haves, server sends pack data.
@@ -723,10 +757,12 @@ async fn info_refs_subprocess(
723757
pub async fn upload_pack(
724758
State(state): State<Arc<AppState>>,
725759
auth: GitAuth,
760+
headers: axum::http::HeaderMap,
726761
AxumPath(params): AxumPath<GitRepoParams>,
727762
body: Body,
728763
) -> Result<Response, Response> {
729764
let _ = validate_repo_id(&params.owner, &params.repo)?;
765+
let body = decode_git_request_body(&headers, body);
730766
let permit = acquire_git_permit(&state, "upload_pack")?;
731767

732768
let repo = match hydrate_for_read(
@@ -793,10 +829,12 @@ pub async fn upload_pack(
793829
pub async fn receive_pack(
794830
State(state): State<Arc<AppState>>,
795831
auth: GitAuth,
832+
headers: axum::http::HeaderMap,
796833
AxumPath(params): AxumPath<GitRepoParams>,
797834
body: Body,
798835
) -> Result<Response, Response> {
799836
let repo_name = validate_repo_id(&params.owner, &params.repo)?;
837+
let body = decode_git_request_body(&headers, body);
800838
let pusher_hex = hex::encode(auth.pubkey.to_bytes());
801839
let _permit = acquire_git_permit(&state, "receive_pack")?;
802840

@@ -1694,6 +1732,43 @@ mod track_c_tests {
16941732
"cb09a769da1c01f458fa6959d4e8eded38fac8d3".to_string()
16951733
}
16961734

1735+
/// A gzip-encoded request body is transparently inflated before it
1736+
/// reaches the git subprocess. Git's smart-HTTP client gzips the
1737+
/// upload-pack/receive-pack request body past a size threshold (fires
1738+
/// on many-ref clones); without this the subprocess sees raw gzip and
1739+
/// dies with `fatal: protocol error: bad line length character`.
1740+
#[tokio::test]
1741+
async fn gzip_request_body_is_inflated() {
1742+
use axum::http::HeaderMap;
1743+
use std::io::Write;
1744+
1745+
let plaintext = b"0032want cb09a769da1c01f458fa6959d4e8eded38fac8d3\n0000";
1746+
let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
1747+
encoder.write_all(plaintext).unwrap();
1748+
let gzipped = encoder.finish().unwrap();
1749+
assert_ne!(
1750+
gzipped, plaintext,
1751+
"precondition: body is actually compressed"
1752+
);
1753+
1754+
let mut headers = HeaderMap::new();
1755+
headers.insert(header::CONTENT_ENCODING, "gzip".parse().unwrap());
1756+
let decoded = decode_git_request_body(&headers, Body::from(gzipped));
1757+
let bytes = axum::body::to_bytes(decoded, usize::MAX).await.unwrap();
1758+
assert_eq!(bytes.as_ref(), plaintext);
1759+
}
1760+
1761+
/// Without a gzip `Content-Encoding`, the body is passed through byte
1762+
/// for byte (the common small-clone / already-inflated case).
1763+
#[tokio::test]
1764+
async fn identity_request_body_is_passthrough() {
1765+
use axum::http::HeaderMap;
1766+
let plaintext = b"0032want cb09a769da1c01f458fa6959d4e8eded38fac8d3\n0000";
1767+
let decoded = decode_git_request_body(&HeaderMap::new(), Body::from(plaintext.to_vec()));
1768+
let bytes = axum::body::to_bytes(decoded, usize::MAX).await.unwrap();
1769+
assert_eq!(bytes.as_ref(), plaintext);
1770+
}
1771+
16971772
fn branches_only_manifest() -> Manifest {
16981773
let mut refs = BTreeMap::new();
16991774
refs.insert("refs/heads/feature".to_string(), oid_sha1());

0 commit comments

Comments
 (0)