Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(node): serve filtered pack when caller has withheld subtree blobs
  • Loading branch information
beardthelion committed Jun 6, 2026
commit e292b79fc0014aa849332129559267ffc2f5ea26
40 changes: 27 additions & 13 deletions crates/gitlawb-node/src/api/repos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use uuid::Uuid;

use crate::cert;
use crate::error::{AppError, Result};
use crate::git::{smart_http, store};
use crate::git::{smart_http, store, visibility_pack};
use crate::state::AppState;
use crate::visibility::{visibility_check, Decision};
use crate::webhooks;
Expand Down Expand Up @@ -392,18 +392,32 @@ pub async fn git_upload_pack(
.await
.map_err(|e| AppError::Git(e.to_string()))?;
let body_len = body.len();
let resp = smart_http::upload_pack(&disk_path, body)
.await
.map_err(|e| {
let msg = e.to_string();
if msg.contains("bad line length") || msg.contains("protocol error") {
tracing::warn!(repo = %name, err = %msg, "git-upload-pack: bad client request");
AppError::BadRequest(msg)
} else {
tracing::error!(repo = %name, err = %msg, "git-upload-pack failed");
AppError::Git(msg)
}
})?;

let withheld = visibility_pack::withheld_blob_oids(
&disk_path,
&rules,
record.is_public,
&record.owner_did,
caller,
)
.map_err(|e| AppError::Git(e.to_string()))?;

let resp = if withheld.is_empty() {
smart_http::upload_pack(&disk_path, body).await
} else {
tracing::info!(repo = %name, caller = ?caller, withheld = withheld.len(), "serving filtered pack");
smart_http::upload_pack_excluding(&disk_path, body, &withheld).await
}
.map_err(|e| {
let msg = e.to_string();
if msg.contains("bad line length") || msg.contains("protocol error") {
tracing::warn!(repo = %name, err = %msg, "git-upload-pack: bad client request");
AppError::BadRequest(msg)
} else {
tracing::error!(repo = %name, err = %msg, "git-upload-pack failed");
AppError::Git(msg)
}
})?;
crate::metrics::record_fetch(&format!("{owner}/{name}"));
crate::metrics::observe_pack_size(body_len as f64);
Ok(resp)
Expand Down
4 changes: 0 additions & 4 deletions crates/gitlawb-node/src/git/smart_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ fn pkt_line(data: &str) -> Vec<u8> {
/// Build a packfile containing every object reachable from all refs EXCEPT the
/// given blob OIDs. Commits and trees are always included, so SHAs stay intact;
/// only the named blobs are dropped.
// #[allow(dead_code)] removed when wired into the upload-pack handler in the next task.
#[allow(dead_code)]
pub fn build_filtered_pack(repo_path: &Path, withheld: &HashSet<String>) -> Result<Vec<u8>> {
// All reachable objects as "oid [path]" lines.
let rev = std::process::Command::new("git")
Expand Down Expand Up @@ -173,8 +171,6 @@ pub fn build_filtered_pack(repo_path: &Path, withheld: &HashSet<String>) -> Resu
/// Serve a clone/fetch with the withheld blobs removed from the response pack.
/// Framing: the body wraps `build_filtered_pack` output in the upload-pack
/// `packfile` section with sideband-64k band 1, terminated by flush.
// #[allow(dead_code)] removed when wired into the upload-pack handler in the next task.
#[allow(dead_code)]
pub async fn upload_pack_excluding(
repo_path: &Path,
_request_body: Bytes,
Expand Down
2 changes: 0 additions & 2 deletions crates/gitlawb-node/src/git/visibility_pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use anyhow::{Context, Result};
use std::collections::HashSet;
use std::path::Path;

#[allow(dead_code)]
/// List every (blob_oid, "/repo/relative/path") pair reachable from any branch
/// ref in `repo_path`. Uses `git ls-tree -r` per ref so each path a blob lives
/// at is represented (the same blob content can appear at several paths). Paths
Expand Down Expand Up @@ -50,7 +49,6 @@ fn blob_paths(repo_path: &Path) -> Result<Vec<(String, String)>> {
Ok(out)
}

#[allow(dead_code)]
/// Blob OIDs the caller may not read. A blob is withheld only if visibility
/// denies the caller at *every* path the blob appears at; a blob that is also
/// reachable through an allowed path is sent (its content is public elsewhere).
Expand Down