-
Notifications
You must be signed in to change notification settings - Fork 15
feat: path-scoped repository visibility (Phase 1) for #18 #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kevincodex1
merged 2 commits into
Gitlawb:main
from
beardthelion:feat/path-scoped-visibility
Jun 5, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,4 +17,5 @@ pub mod repos; | |
| pub mod resolve; | ||
| pub mod stars; | ||
| pub mod tasks; | ||
| pub mod visibility; | ||
| pub mod webhooks; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| //! Path-scoped visibility management API. Owner-only, mirrors `api/protect.rs`. | ||
|
|
||
| use axum::extract::{Extension, Path, State}; | ||
| use axum::http::StatusCode; | ||
| use axum::Json; | ||
| use serde::Deserialize; | ||
|
|
||
| use crate::auth::AuthenticatedDid; | ||
| use crate::db::VisibilityMode; | ||
| use crate::error::{AppError, Result}; | ||
| use crate::state::AppState; | ||
|
|
||
| #[derive(Deserialize)] | ||
| pub struct SetVisibilityRequest { | ||
| pub path_glob: String, | ||
| /// "a" or "b"; defaults to "b" if absent or unrecognized. | ||
| #[serde(default)] | ||
| pub mode: Option<String>, | ||
| #[serde(default)] | ||
| pub reader_dids: Vec<String>, | ||
| } | ||
|
|
||
| #[derive(Deserialize)] | ||
| pub struct RemoveVisibilityRequest { | ||
| pub path_glob: String, | ||
| } | ||
|
|
||
| fn require_owner(record: &crate::db::RepoRecord, caller: &str) -> Result<()> { | ||
| let owner_short = record | ||
| .owner_did | ||
| .split(':') | ||
| .next_back() | ||
| .unwrap_or(&record.owner_did); | ||
| if caller != record.owner_did && caller != owner_short { | ||
| return Err(AppError::BadRequest( | ||
| "only the repo owner can manage visibility".into(), | ||
| )); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Reject malformed globs before they reach the store, where they would | ||
| /// silently misconfigure access (an empty glob behaves like "/", and a glob | ||
| /// without a leading "/" never matches a real repo path). The accepted forms | ||
| /// match what `visibility_check` understands: "/", "/prefix", or "/prefix/**". | ||
| fn validate_path_glob(path_glob: &str) -> Result<()> { | ||
| if !path_glob.starts_with('/') { | ||
| return Err(AppError::BadRequest("path_glob must start with '/'".into())); | ||
| } | ||
| if path_glob == "/**" { | ||
| return Err(AppError::BadRequest( | ||
| "use '/' for whole-repo scope, not '/**'".into(), | ||
| )); | ||
| } | ||
| if path_glob != "/" && path_glob.ends_with('/') { | ||
| return Err(AppError::BadRequest( | ||
| "path_glob must not end with '/'".into(), | ||
| )); | ||
| } | ||
| if path_glob.contains('*') && !path_glob.ends_with("/**") { | ||
| return Err(AppError::BadRequest( | ||
| "the only supported wildcard is a trailing '/**'".into(), | ||
| )); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// PUT /api/v1/repos/{owner}/{repo}/visibility | ||
| pub async fn set_visibility( | ||
| State(state): State<AppState>, | ||
| Extension(auth): Extension<AuthenticatedDid>, | ||
| Path((owner, repo)): Path<(String, String)>, | ||
| Json(req): Json<SetVisibilityRequest>, | ||
| ) -> Result<(StatusCode, Json<serde_json::Value>)> { | ||
| let record = state | ||
| .db | ||
| .get_repo(&owner, &repo) | ||
| .await? | ||
| .ok_or_else(|| AppError::RepoNotFound(format!("{owner}/{repo}")))?; | ||
| require_owner(&record, &auth.0)?; | ||
| validate_path_glob(&req.path_glob)?; | ||
|
|
||
| let mode = match req.mode.as_deref() { | ||
| Some("a") => VisibilityMode::A, | ||
| _ => VisibilityMode::B, | ||
| }; | ||
|
|
||
| // Mode A hides existence and is only coherent for the whole repo; a subtree | ||
| // cannot hide its existence without rewriting git history (see spec). | ||
| if mode == VisibilityMode::A && req.path_glob != "/" { | ||
| return Err(AppError::BadRequest( | ||
| "mode 'a' (hide) is only allowed for the whole repo (path_glob '/'); use mode 'b' for subtrees".into(), | ||
| )); | ||
| } | ||
|
|
||
| // An empty reader_dids list is valid and intentional: the owner is always | ||
| // allowed by visibility_check, so a "/" rule with no readers is exactly the | ||
| // whole-repo "private to owner only" case. | ||
| state | ||
| .db | ||
| .set_visibility_rule(&record.id, &req.path_glob, mode, &req.reader_dids, &auth.0) | ||
| .await?; | ||
|
|
||
| let subtree_warning = req.path_glob != "/"; | ||
| tracing::info!( | ||
| repo = %repo, caller = %auth.0, path_glob = %req.path_glob, mode = %mode.as_str(), | ||
| subtree_pending = subtree_warning, | ||
| "visibility rule set" | ||
| ); | ||
|
|
||
| Ok(( | ||
| StatusCode::CREATED, | ||
| Json(serde_json::json!({ | ||
| "status": "set", | ||
| "repo": format!("{owner}/{repo}"), | ||
| "path_glob": req.path_glob, | ||
| "mode": mode.as_str(), | ||
| "reader_dids": req.reader_dids, | ||
| "subtree_clone_enforcement_pending": subtree_warning, | ||
| })), | ||
| )) | ||
| } | ||
|
|
||
| /// DELETE /api/v1/repos/{owner}/{repo}/visibility | ||
| pub async fn remove_visibility( | ||
| State(state): State<AppState>, | ||
| Extension(auth): Extension<AuthenticatedDid>, | ||
| Path((owner, repo)): Path<(String, String)>, | ||
| Json(req): Json<RemoveVisibilityRequest>, | ||
| ) -> Result<Json<serde_json::Value>> { | ||
| let record = state | ||
| .db | ||
| .get_repo(&owner, &repo) | ||
| .await? | ||
| .ok_or_else(|| AppError::RepoNotFound(format!("{owner}/{repo}")))?; | ||
| require_owner(&record, &auth.0)?; | ||
|
|
||
| state | ||
| .db | ||
| .remove_visibility_rule(&record.id, &req.path_glob) | ||
| .await?; | ||
|
|
||
| tracing::info!( | ||
| repo = %repo, caller = %auth.0, path_glob = %req.path_glob, | ||
| "visibility rule removed" | ||
| ); | ||
|
|
||
| Ok(Json(serde_json::json!({ | ||
| "status": "removed", | ||
| "repo": format!("{owner}/{repo}"), | ||
| "path_glob": req.path_glob, | ||
| }))) | ||
| } | ||
|
|
||
| /// GET /api/v1/repos/{owner}/{repo}/visibility | ||
| pub async fn list_visibility( | ||
| State(state): State<AppState>, | ||
| Extension(auth): Extension<AuthenticatedDid>, | ||
| Path((owner, repo)): Path<(String, String)>, | ||
| ) -> Result<Json<serde_json::Value>> { | ||
| let record = state | ||
| .db | ||
| .get_repo(&owner, &repo) | ||
| .await? | ||
| .ok_or_else(|| AppError::RepoNotFound(format!("{owner}/{repo}")))?; | ||
| require_owner(&record, &auth.0)?; | ||
|
|
||
| let rules = state.db.list_visibility_rules(&record.id).await?; | ||
| let rules_json: Vec<_> = rules | ||
| .into_iter() | ||
| .map(|r| { | ||
| serde_json::json!({ | ||
| "path_glob": r.path_glob, | ||
| "mode": r.mode.as_str(), | ||
| "reader_dids": r.reader_dids, | ||
| "created_by": r.created_by, | ||
| }) | ||
| }) | ||
| .collect(); | ||
|
|
||
| Ok(Json(serde_json::json!({ | ||
| "repo": format!("{owner}/{repo}"), | ||
| "count": rules_json.len(), | ||
| "rules": rules_json, | ||
| }))) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::validate_path_glob; | ||
|
|
||
| #[test] | ||
| fn accepts_supported_globs() { | ||
| for g in ["/", "/secret", "/secret/**", "/a/b/c", "/a/b/**"] { | ||
| assert!(validate_path_glob(g).is_ok(), "{g} should be valid"); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn rejects_malformed_globs() { | ||
| // empty, no leading slash, whole-repo via "/**", trailing slash, and | ||
| // non-trailing wildcards are all rejected. | ||
| for g in ["", "secret/**", "/**", "/secret/", "/a*b", "/*/x"] { | ||
| assert!(validate_path_glob(g).is_err(), "{g} should be rejected"); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.