Speak Git protocol v2 to HTTPS clients that ask for it
Browse filesThe Git-Protocol request header was never passed to upload-pack, so every HTTPS clone or fetch fell back to protocol v0 and heard every ref before negotiating. A well-formed header now becomes GIT_PROTOCOL, as it already does over SSH, and a v2 ref advertisement omits the service line, as git http-backend does. The clone test covers v0 and v2. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Author
- Um9i vulgarmessiah@gmail.com
- Date
- 22 Nov 2026, 16:16
- Commit
- 0eac27bd3235
- Parent
- 511be5f
2 files changed +47−9
| @@ -5,7 +5,7 @@ use super::throttle::Client; | ||
| use axum::{ | ||
| body::Bytes, | ||
| extract::{Path, RawQuery, Request}, | ||
| http::{StatusCode, header}, | ||
| http::{HeaderMap, StatusCode, header}, | ||
| response::{IntoResponse, Response}, | ||
| }; | ||
| use gitv::{git_command, repo_dir}; | ||
| @@ -17,11 +17,24 @@ use tokio::process::Command; | ||
| /// anonymous; each client may run two, so no single address can take them all. | ||
| static TRANSFERS: LazyLock<Pool> = LazyLock::new(|| Pool::new(4, 0, 2)); | ||
| pub async fn refs(Path(path): Path<(String, String)>, RawQuery(query): RawQuery, client: Client) -> Response { | ||
| pub async fn refs( | ||
| Path(path): Path<(String, String)>, | ||
| RawQuery(query): RawQuery, | ||
| client: Client, | ||
| headers: HeaderMap, | ||
| ) -> Response { | ||
| if query.as_deref() != Some("service=git-upload-pack") { | ||
| return StatusCode::NOT_FOUND.into_response(); | ||
| } | ||
| transfer(path, client, None).await | ||
| transfer(path, client, protocol(&headers), None).await | ||
| } | ||
| /// The protocol the client asked for in its `Git-Protocol` header (`version=2`, say), when it | ||
| /// is well formed, to hand to upload-pack as `GIT_PROTOCOL`, as `git http-backend` does. | ||
| fn protocol(headers: &HeaderMap) -> Option<String> { | ||
| let value = headers.get("git-protocol")?.to_str().ok()?; | ||
| (value.len() <= 64 && value.bytes().all(|b| b.is_ascii_alphanumeric() || b"=:.".contains(&b))) | ||
| .then(|| value.to_string()) | ||
| } | ||
| pub async fn pack(Path(path): Path<(String, String)>, client: Client, request: Request) -> Response { | ||
| @@ -30,10 +43,16 @@ pub async fn pack(Path(path): Path<(String, String)>, client: Client, request: R | ||
| { | ||
| return StatusCode::UNSUPPORTED_MEDIA_TYPE.into_response(); | ||
| } | ||
| transfer(path, client, Some(request)).await | ||
| let protocol = protocol(request.headers()); | ||
| transfer(path, client, protocol, Some(request)).await | ||
| } | ||
| async fn transfer((owner, name): (String, String), client: Client, request: Option<Request>) -> Response { | ||
| async fn transfer( | ||
| (owner, name): (String, String), | ||
| client: Client, | ||
| protocol: Option<String>, | ||
| request: Option<Request>, | ||
| ) -> Response { | ||
| let Some(dir) = repo_dir(&owner, &name).filter(|dir| dir.is_public()) else { | ||
| return StatusCode::NOT_FOUND.into_response(); | ||
| }; | ||
| @@ -87,8 +106,16 @@ async fn transfer((owner, name): (String, String), client: Client, request: Opti | ||
| cmd.arg("--advertise-refs"); | ||
| } | ||
| cmd.arg(".").stdin(Stdio::piped()); | ||
| // Protocol v2 lets a client ask only for the refs it wants instead of hearing every one. | ||
| let v2 = protocol.as_deref().is_some_and(|p| p.split(':').any(|item| item == "version=2")); | ||
| if let Some(protocol) = &protocol { | ||
| cmd.env("GIT_PROTOCOL", protocol); | ||
| } | ||
| let Ok(output) = Output::start(cmd) else { return StatusCode::INTERNAL_SERVER_ERROR.into_response() }; | ||
| let prefix = if advertise { Bytes::from_static(b"001e# service=git-upload-pack\n0000") } else { Bytes::new() }; | ||
| // Version 2's capability advertisement stands alone; earlier versions start with the | ||
| // service line (git http-backend does the same). | ||
| let prefix = | ||
| if advertise && !v2 { Bytes::from_static(b"001e# service=git-upload-pack\n0000") } else { Bytes::new() }; | ||
| let body = output.body(prefix, input, permit, Duration::from_secs(300)); | ||
| let kind = | ||
| if advertise { "application/x-git-upload-pack-advertisement" } else { "application/x-git-upload-pack-result" }; | ||
+14−3
| @@ -831,9 +831,20 @@ fn public_web_clone_and_visibility_changes() { | ||
| let page = server.get_signed_out("/ada/sharing/tree").text(); | ||
| assert!(page.contains("https://git.example.test/ada/sharing")); | ||
| assert_eq!(server.get_signed_out("/ada/sharing/raw?path=hello").text(), "public content"); | ||
| git(&work, &["clone", &url, "copy"]); | ||
| let copy = work.join("copy"); | ||
| assert_eq!(git(©, &["rev-parse", "HEAD"]), git(&work, &["rev-parse", "HEAD"])); | ||
| // Both protocol versions clone; version 2 is what the server speaks when asked. | ||
| for version in ["0", "2"] { | ||
| let out = common::git_cmd(&work) | ||
| .args(["-c", &format!("protocol.version={version}"), "clone", "-q", &url, &format!("copy-v{version}")]) | ||
| .env("GIT_TRACE_PACKET", "1") | ||
| .output() | ||
| .unwrap(); | ||
| assert!(out.status.success(), "v{version}: {}", String::from_utf8_lossy(&out.stderr)); | ||
| let trace = String::from_utf8_lossy(&out.stderr); | ||
| assert_eq!(trace.contains("< version 2"), version == "2", "v{version}: {trace}"); | ||
| let copy = work.join(format!("copy-v{version}")); | ||
| assert_eq!(git(©, &["rev-parse", "HEAD"]), git(&work, &["rev-parse", "HEAD"])); | ||
| } | ||
| let copy = work.join("copy-v2"); | ||
| assert!(!host.git(©, &["push", "origin", "HEAD:forbidden"]).status.success()); | ||
| assert!(host.admin(&["visibility", "ada", "sharing", "private"]).status.success()); | ||
| assert_eq!(server.get_signed_out(refs).status, 404); | ||