Skip to content
Prev Previous commit
Next Next commit
fix(node): path-scope the get_tree visibility gate
get_tree gated on the repo root ("/") regardless of the requested subtree, so
a caller denied a withheld subtree could still enumerate its filenames and blob
SHAs (N3). Gate on the requested path instead, mirroring get_blob, and reject
traversal segments. A cross-caller test shows the rejection is path-scoped: a
non-reader is denied the withheld subtree but passes the gate on a non-withheld
path.
  • Loading branch information
beardthelion committed Jun 22, 2026
commit c9dab387531044ce4d0c57bf7530a3aa4f7665a0
13 changes: 12 additions & 1 deletion crates/gitlawb-node/src/api/repos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,19 @@ pub async fn get_tree(
auth: Option<Extension<AuthenticatedDid>>,
) -> Result<Json<serde_json::Value>> {
let caller = auth.as_ref().map(|e| e.0 .0.as_str());
// Gate on the REQUESTED subtree, not the repo root (N3) — otherwise a caller
// denied a withheld subtree can still enumerate its names/SHAs. Mirrors get_blob.
let normalized = tree_path.trim_matches('/');
if normalized.split('/').any(|seg| seg == "." || seg == "..") {
return Err(AppError::BadRequest("invalid tree path".into()));
}
let gate_path = if normalized.is_empty() {
"/".to_string()
} else {
format!("/{normalized}")
};
let (record, _rules) =
crate::api::authorize_repo_read(&state, &owner, &name, caller, "/").await?;
crate::api::authorize_repo_read(&state, &owner, &name, caller, &gate_path).await?;

let disk_path = state
.repo_store
Expand Down
61 changes: 61 additions & 0 deletions crates/gitlawb-node/src/test_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,65 @@ mod tests {
"delegator_did must be bound to the signer"
);
}

/// N3: get_tree gates on the REQUESTED subtree, not the repo root. A caller
/// denied a withheld subtree is rejected there (404) but passes the gate on a
/// non-withheld path (so the rejection is path-scoped, not repo-wide).
#[sqlx::test]
async fn get_tree_gate_is_path_scoped(pool: PgPool) {
use crate::db::VisibilityMode;
let owner = "did:key:zTREEOWNERAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
let stranger = "did:key:zTREESTRANGERBBBBBBBBBBBBBBBBBBBBBBBBBBBB";
let state = test_state(pool).await;
let repo = seed_repo(owner, "tree-repo");
state.db.create_repo(&repo).await.expect("seed repo");
// Withhold /secret/** from everyone but the owner.
state
.db
.set_visibility_rule(&repo.id, "/secret/**", VisibilityMode::B, &[], owner)
.await
.expect("set rule");

let router = || {
Router::new()
.route(
"/api/v1/repos/{owner}/{repo}/tree/{*path}",
axum::routing::get(crate::api::repos::get_tree),
)
.with_state(state.clone())
};

// Withheld subtree → denied at the gate (opaque 404), before any disk access.
let resp = router()
.oneshot(signed_request_as(
stranger,
Method::GET,
&format!("/api/v1/repos/{owner}/tree-repo/tree/secret"),
Body::empty(),
))
.await
.unwrap();
assert_eq!(
resp.status(),
StatusCode::NOT_FOUND,
"withheld subtree must be denied"
);

// Non-withheld path → passes the gate (whatever the disk layer then returns,
// it is NOT the gate's 404). Proves the gate keyed off the path, not the repo.
let resp = router()
.oneshot(signed_request_as(
stranger,
Method::GET,
&format!("/api/v1/repos/{owner}/tree-repo/tree/public"),
Body::empty(),
))
.await
.unwrap();
assert_ne!(
resp.status(),
StatusCode::NOT_FOUND,
"a non-withheld path must pass the path-scoped gate"
);
}
}