Skip to content
Prev Previous commit
Next Next commit
fix(visibility): gate withheld-paths and handle nested/exact globs
Three fixes from the PR #33 review:

- withheld_paths now applies the whole-repo "/" read gate (returns
  repo-not-found when the caller cannot read the root), matching the git read
  endpoints. Without it the endpoint disclosed a private repo's existence and
  path layout to unauthorized callers. The withheld_globs doc already assumed
  this gate existed; now it does.

- A nested allow under a denied parent (e.g. "/secret/public/**" allowed,
  "/secret/**" denied) was over-withheld: the client sparse-excluded the whole
  parent and hid paths the caller may read. The endpoint now also returns a
  "reinclude" list (allowed globs strictly under a denied one) and gl clone
  re-includes them in the sparse spec after the excludes.

- Wildcard-free globs like "/docs/private" match both the exact path and a
  subtree (per glob_matches), but the client only emitted the subtree exclude.
  sparse_patterns now emits both "/docs/private" and "/docs/private/".

Verified the exclude-then-reinclude sparse ordering checks out cleanly with
real git, plus unit tests for reincluded_globs, the nested re-include, the
exact-path exclude, and sparse_patterns.
  • Loading branch information
beardthelion committed Jun 7, 2026
commit af972499a94a8a644389ceef92fac34047866224
21 changes: 18 additions & 3 deletions crates/gitlawb-node/src/api/visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,11 @@ pub async fn list_visibility(

/// GET /api/v1/repos/{owner}/{repo}/withheld-paths
///
/// Returns only the path globs the (optionally authenticated) caller is denied,
/// so a clean-clone client can sparse-exclude them. Unlike `list_visibility`
/// this is not owner-gated and never exposes reader_dids.
/// Returns the path globs the (optionally authenticated) caller is denied
/// (`withheld`) plus any more-specific globs that are allowed underneath a
/// denied one (`reinclude`), so a clean-clone client can sparse-exclude the
/// denied subtrees while re-including the allowed nested paths. Unlike
/// `list_visibility` this is not owner-gated and never exposes reader_dids.
pub async fn withheld_paths(
State(state): State<AppState>,
auth: Option<Extension<AuthenticatedDid>>,
Expand All @@ -203,12 +205,25 @@ pub async fn withheld_paths(

let rules = state.db.list_visibility_rules(&record.id).await?;
let caller = auth.as_ref().map(|e| e.0 .0.as_str());

// Whole-repo read gate: a caller who cannot read "/" gets repo-not-found,
// matching the git read endpoints, so this never discloses a private repo's
// existence or its path layout to an unauthorized caller.
if crate::visibility::visibility_check(&rules, record.is_public, &record.owner_did, caller, "/")
== crate::visibility::Decision::Deny
{
return Err(AppError::RepoNotFound(format!("{owner}/{repo}")));
}

let withheld =
crate::visibility::withheld_globs(&rules, record.is_public, &record.owner_did, caller);
let reinclude =
crate::visibility::reincluded_globs(&rules, record.is_public, &record.owner_did, caller);

Ok(Json(serde_json::json!({
"repo": format!("{owner}/{repo}"),
"withheld": withheld,
"reinclude": reinclude,
})))
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

Expand Down
68 changes: 68 additions & 0 deletions crates/gitlawb-node/src/visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,55 @@ pub fn withheld_globs(
.collect()
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

/// The allowed globs that sit strictly underneath a denied glob. A clean-clone
/// client sparse-excludes everything in `withheld_globs`, which would also hide
/// these nested allowed paths; re-including them restores the caller's access.
/// Example: with `/secret/**` denied and `/secret/public/**` allowed for the
/// same caller, `/secret/public/**` is returned here so the client re-includes
/// it after excluding `/secret/`.
pub fn reincluded_globs(
rules: &[VisibilityRule],
is_public: bool,
owner_did: &str,
caller: Option<&str>,
) -> Vec<String> {
let denied: Vec<&str> = rules
.iter()
.filter(|r| r.path_glob != "/")
.filter(|r| {
visibility_check(
rules,
is_public,
owner_did,
caller,
glob_prefix(&r.path_glob),
) == Decision::Deny
})
.map(|r| glob_prefix(&r.path_glob))
.collect();

rules
.iter()
.filter(|r| r.path_glob != "/")
.filter(|r| {
visibility_check(
rules,
is_public,
owner_did,
caller,
glob_prefix(&r.path_glob),
) == Decision::Allow
})
.filter(|r| {
let p = glob_prefix(&r.path_glob);
denied
.iter()
.any(|d| *d != p && p.starts_with(&format!("{d}/")))
})
.map(|r| r.path_glob.clone())
.collect()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -157,6 +206,25 @@ mod tests {
assert_eq!(anon, vec!["/docs/**".to_string(), "/secret/**".to_string()]);
}

#[test]
fn reincluded_globs_restores_allowed_nested_path() {
let rules = [
rule("/secret/**", VisibilityMode::B, &["did:key:z6MkFriend"]),
rule(
"/secret/public/**",
VisibilityMode::B,
&["did:key:z6MkFriend", "did:key:z6MkStranger"],
),
];
// Stranger is denied /secret/** but allowed the nested /secret/public/**.
let withheld = withheld_globs(&rules, true, OWNER, Some("did:key:z6MkStranger"));
assert_eq!(withheld, vec!["/secret/**".to_string()]);
let reinc = reincluded_globs(&rules, true, OWNER, Some("did:key:z6MkStranger"));
assert_eq!(reinc, vec!["/secret/public/**".to_string()]);
// Owner is denied nothing, so there is nothing to re-include.
assert!(reincluded_globs(&rules, true, OWNER, Some(OWNER)).is_empty());
}

#[test]
fn no_rules_public_allows_anonymous() {
assert_eq!(
Expand Down
148 changes: 128 additions & 20 deletions crates/gl/src/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ fn git_global(args: &[&str]) -> Result<()> {
Ok(())
}

/// Sparse-checkout pattern(s) for a visibility glob. A subtree glob
/// (`/secret/**`) maps to the directory `/secret/`. A wildcard-free glob
/// (`/docs/private`) matches both the exact path and a subtree at that path
/// (mirroring the node's `glob_matches`), so it maps to both `/docs/private`
/// and `/docs/private/`. Callers prefix these with `!` to exclude.
fn sparse_patterns(glob: &str) -> Vec<String> {
match glob.strip_suffix("/**") {
Some(base) => vec![format!("{base}/")],
None => vec![glob.to_string(), format!("{glob}/")],
}
}

/// Clone `remote_url` into `dest`, excluding `withheld_globs` from checkout.
/// `dest` must not already exist. With nothing withheld this is a plain full
/// clone. With globs withheld it clones as a promisor (`--filter=blob:none`,
Expand All @@ -72,6 +84,7 @@ pub fn setup_partial_clone(
dest: &Path,
remote_url: &str,
withheld_globs: &[String],
reinclude_globs: &[String],
branch: Option<&str>,
) -> Result<()> {
let dest_str = dest
Expand All @@ -95,13 +108,22 @@ pub fn setup_partial_clone(
dest_str,
])?;
git(dest, &["sparse-checkout", "init", "--no-cone"])?;
// Non-cone sparse-checkout, gitignore-style and order-sensitive: include
// everything, exclude the withheld globs, then re-include any allowed globs
// nested under an excluded one (later patterns win).
let mut spec = String::from("/*\n");
for g in withheld_globs {
// "/secret/**" -> "!/secret/"
let dir = g.trim_end_matches("**").trim_end_matches('/');
spec.push('!');
spec.push_str(dir);
spec.push_str("/\n");
for pat in sparse_patterns(g) {
spec.push('!');
spec.push_str(&pat);
spec.push('\n');
}
}
for g in reinclude_globs {
for pat in sparse_patterns(g) {
spec.push_str(&pat);
spec.push('\n');
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
std::fs::write(dest.join(".git/info/sparse-checkout"), spec)
.context("writing sparse-checkout spec")?;
Expand Down Expand Up @@ -144,9 +166,11 @@ fn parse_repo(repo: &str) -> Result<(String, String, String)> {
))
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

/// Ask the node which globs are withheld for this caller. Any error or non-2xx
/// is treated as "nothing withheld" so public repos clone normally.
async fn fetch_withheld(node: &str, owner: &str, name: &str) -> Vec<String> {
/// Ask the node which globs are withheld for this caller and which allowed globs
/// nested under them must be re-included. Returns `(withheld, reinclude)`. Any
/// error or non-2xx is treated as "nothing withheld" so public repos clone
/// normally.
async fn fetch_withheld(node: &str, owner: &str, name: &str) -> (Vec<String>, Vec<String>) {
let kp = load_keypair_from_dir(None).ok();
let signed = kp.is_some();
let client = NodeClient::new(node, kp);
Expand All @@ -158,17 +182,20 @@ async fn fetch_withheld(node: &str, owner: &str, name: &str) -> Vec<String> {
};
let resp = match resp {
Ok(r) if r.status().is_success() => r,
_ => return Vec::new(),
_ => return (Vec::new(), Vec::new()),
};
let body: Value = resp.json().await.unwrap_or_default();
body.get("withheld")
.and_then(|w| w.as_array())
.map(|a| {
a.iter()
.filter_map(|x| x.as_str().map(String::from))
.collect()
})
.unwrap_or_default()
let globs = |field: &str| -> Vec<String> {
body.get(field)
.and_then(|w| w.as_array())
.map(|a| {
a.iter()
.filter_map(|x| x.as_str().map(String::from))
.collect()
})
.unwrap_or_default()
};
(globs("withheld"), globs("reinclude"))
}

pub async fn run(args: CloneArgs) -> Result<()> {
Expand All @@ -179,7 +206,7 @@ pub async fn run(args: CloneArgs) -> Result<()> {
bail!("destination '{dest_name}' already exists");
}

let withheld = fetch_withheld(&args.node, &owner, &name).await;
let (withheld, reinclude) = fetch_withheld(&args.node, &owner, &name).await;
if withheld.is_empty() {
println!("Cloning {url} into {dest_name}");
} else {
Expand All @@ -189,7 +216,7 @@ pub async fn run(args: CloneArgs) -> Result<()> {
);
}

setup_partial_clone(&dest, &url, &withheld, args.branch.as_deref())?;
setup_partial_clone(&dest, &url, &withheld, &reinclude, args.branch.as_deref())?;
println!("Done. Cloned into {dest_name}");
Ok(())
}
Expand Down Expand Up @@ -237,7 +264,7 @@ mod tests {
// file:// so --filter is honored (local-path clones ignore it).
let dest = td.path().join("dest");
let url = format!("file://{}", bare.display());
setup_partial_clone(&dest, &url, &["/secret/**".to_string()], None).unwrap();
setup_partial_clone(&dest, &url, &["/secret/**".to_string()], &[], None).unwrap();

assert!(dest.join("public/a.txt").exists(), "public file present");
assert!(
Expand All @@ -246,6 +273,87 @@ mod tests {
);
}

/// Build a bare remote with the given files (relative path -> contents),
/// committed on one branch. Returns (tempdir, file:// url).
fn bare_remote(files: &[(&str, &[u8])]) -> (TempDir, String) {
let td = TempDir::new().unwrap();
let origin = td.path().join("origin");
let bare = td.path().join("bare.git");
for (path, contents) in files {
let full = origin.join(path);
std::fs::create_dir_all(full.parent().unwrap()).unwrap();
std::fs::write(full, contents).unwrap();
}
g(&["init", "-q"], &origin);
g(&["config", "user.email", "t@t"], &origin);
g(&["config", "user.name", "t"], &origin);
g(&["add", "."], &origin);
g(&["commit", "-qm", "init"], &origin);
g(
&[
"clone",
"-q",
"--bare",
origin.to_str().unwrap(),
bare.to_str().unwrap(),
],
td.path(),
);
let url = format!("file://{}", bare.display());
(td, url)
}

#[test]
fn reinclude_restores_allowed_nested_path() {
let (td, url) = bare_remote(&[
("public/a.txt", b"pub\n"),
("secret/private/p.txt", b"PRIV\n"),
("secret/public/s.txt", b"SHARED\n"),
]);
let dest = td.path().join("dest");
setup_partial_clone(
&dest,
&url,
&["/secret/**".to_string()],
&["/secret/public/**".to_string()],
None,
)
.unwrap();

assert!(dest.join("public/a.txt").exists(), "public present");
assert!(
dest.join("secret/public/s.txt").exists(),
"allowed nested path must be re-included"
);
assert!(
!dest.join("secret/private/p.txt").exists(),
"denied nested path must stay excluded"
);
}

#[test]
fn exact_path_glob_is_excluded() {
// A wildcard-free glob must exclude the exact file, not just a subtree.
let (td, url) = bare_remote(&[("public/a.txt", b"pub\n"), ("docs/private", b"SECRET\n")]);
let dest = td.path().join("dest");
setup_partial_clone(&dest, &url, &["/docs/private".to_string()], &[], None).unwrap();

assert!(dest.join("public/a.txt").exists(), "public present");
assert!(
!dest.join("docs/private").exists(),
"exact-path withheld file must be excluded"
);
}

#[test]
fn sparse_patterns_subtree_and_exact() {
assert_eq!(sparse_patterns("/secret/**"), vec!["/secret/".to_string()]);
assert_eq!(
sparse_patterns("/docs/private"),
vec!["/docs/private".to_string(), "/docs/private/".to_string()]
);
}

#[test]
fn parse_repo_accepts_url_and_bare() {
let (url, o, n) = parse_repo("gitlawb://did:key:zAbc/myrepo").unwrap();
Expand Down
Loading