Files
ngit-grasp/src/grasp06/endpoint.rs
T
DanConwayDev 32217fe288 feat(grasp06): /prs/ endpoint reachable with empty-repo fetch
Adds the `/prs/<npub>/<identifier>.git/*` URL space behind the existing
`NGIT_GRASP06_ENABLE` flag. Fetches synthesise an empty bare repo per
request (via `git init --bare` in a tempdir) when no real repo exists
on disk; if a real repo exists at `<git_data_path>/prs/<hex>/<id>.git`
it delegates to the standard `handle_info_refs` / `handle_upload_pack`.

The receive-pack endpoint is a deliberate stub that returns HTTP 200
with an ERR pkt-line ("GRASP-06 receive-pack not yet implemented"),
matching `build_git_protocol_error_response`'s shape so git clients
print the message and exit non-zero rather than seeing a 404. The real
receive-pack handler is not yet implemented.

Module layout under `src/grasp06/`:
  paths.rs    — PRS_URL_PREFIX, PRS_DISK_PREFIX, prs_repo_path,
                prs_base_path, is_prs_repo_path (hex on disk).
  endpoint.rs — PrsUrl + parse_prs_url (npub-only, percent-decoded id).
  fetch.rs    — handle_prs_info_refs, handle_prs_upload_pack, and the
                receive-pack stub.

Routing is wired into HttpService::call ahead of `parse_git_url` so
`/prs/...` paths can't be misclassified as standard owner repos. When
the flag is off the branch is skipped and `/prs/*` falls through to
the existing 404 path (preserving the discovery-gate contract).

Tests flipped green:
  - prs_fetch_unknown_path_serves_empty_repo_with_grasp_06
  - prs_push_other_refs_rejected_with_grasp_06       (ERR pkt-line)
  - standard_push_does_not_mirror_to_prs_with_grasp_06

Tests still green (no regression):
  - prs_namespace_404_when_grasp06_not_advertised_no_grasp_06
  - nip11_advertises_grasp_06_when_enabled
  - pr_event_rejected_when_clone_tag_does_not_name_prs_endpoint_with_grasp_06

Tests still red (deferred):
  - prs_push_refs_nostr_event_id_accepted_with_grasp_06
  - pr_event_accepted_when_clone_tag_names_prs_endpoint_with_grasp_06
  - prs_push_mirrors_to_announced_repo_with_grasp_06

`tempfile` is promoted from a dev-dependency to a runtime dependency
because the empty-repo synthesis path needs it. No new config options,
so the four-place config-sync contract is unaffected.
2026-05-15 15:48:34 +00:00

135 lines
4.4 KiB
Rust

//! URL parsing for the GRASP-06 `/prs/` endpoint.
//!
//! Per the spec, the path layout is:
//!
//! ```text
//! /prs/<npub>/<identifier>.git/<subpath>
//! ```
//!
//! The submitter is always supplied as a bech32 `npub1...` — hex pubkeys
//! are rejected per the spec. The identifier is percent-decoded so URLs
//! like `/prs/<npub>/my%20repo.git/info/refs` resolve to the same
//! identifier as the standard endpoint stores on disk.
use nostr_sdk::prelude::*;
use crate::git::percent_decode;
use crate::grasp06::paths::PRS_URL_PREFIX;
/// A parsed `/prs/<npub>/<id>.git/<subpath>` URL.
#[derive(Debug, Clone)]
pub struct PrsUrl {
/// The submitter's public key, decoded from the `npub1...` URL segment.
pub submitter: PublicKey,
/// The percent-decoded NIP-34 `d` identifier (no `.git` suffix).
pub identifier: String,
/// Everything after `.git/` in the URL path. May be empty.
pub subpath: String,
}
/// Parse a `/prs/<npub>/<identifier>.git/<subpath>` URL.
///
/// Returns `None` if the path is not in the `/prs/` URL space, if the
/// `<npub>` segment is not a valid bech32 npub, or if the second segment
/// does not end in `.git`. Identifiers are percent-decoded.
///
/// This intentionally rejects hex pubkeys: GRASP-06 specifies the URL
/// uses an `npub`, and accepting hex would silently change the on-disk
/// path layout assumed by [`crate::grasp06::paths::prs_repo_path`].
pub fn parse_prs_url(path: &str) -> Option<PrsUrl> {
let path = path.strip_prefix('/').unwrap_or(path);
// Require the literal `prs/` prefix.
let rest = path.strip_prefix(PRS_URL_PREFIX)?;
let rest = rest.strip_prefix('/')?;
// Split off `<npub>/<id>.git/<subpath>` (subpath may be empty).
let mut parts = rest.splitn(3, '/');
let npub_segment = parts.next()?;
let repo_segment = parts.next()?;
let subpath = parts.next().unwrap_or("").to_string();
if npub_segment.is_empty() || repo_segment.is_empty() {
return None;
}
// Spec is npub-only: reject hex (and anything else that isn't a
// valid bech32 npub).
if !npub_segment.starts_with("npub1") {
return None;
}
let submitter = PublicKey::from_bech32(npub_segment).ok()?;
// `<identifier>.git` (URL-encoded). Decode then strip the suffix.
let decoded = percent_decode(repo_segment);
let identifier = decoded.strip_suffix(".git")?.to_string();
if identifier.is_empty() {
return None;
}
Some(PrsUrl {
submitter,
identifier,
subpath,
})
}
#[cfg(test)]
mod tests {
use super::*;
/// A valid bech32 npub for use in tests (generated once, deterministic).
fn sample_npub() -> String {
let keys = Keys::generate();
keys.public_key().to_bech32().unwrap()
}
#[test]
fn parses_info_refs() {
let npub = sample_npub();
let path = format!("/prs/{}/my-repo.git/info/refs", npub);
let parsed = parse_prs_url(&path).expect("should parse");
assert_eq!(parsed.identifier, "my-repo");
assert_eq!(parsed.subpath, "info/refs");
assert_eq!(parsed.submitter.to_bech32().unwrap(), npub);
}
#[test]
fn parses_with_empty_subpath() {
// Tolerate trailing `.git` with no subpath (e.g. `/prs/<npub>/<id>.git/`).
let npub = sample_npub();
let path = format!("/prs/{}/my-repo.git/", npub);
let parsed = parse_prs_url(&path).expect("should parse");
assert_eq!(parsed.subpath, "");
}
#[test]
fn requires_trailing_dot_git() {
let npub = sample_npub();
assert!(parse_prs_url(&format!("/prs/{}/my-repo/info/refs", npub)).is_none());
}
#[test]
fn rejects_hex_pubkey() {
// 64-hex is a valid pubkey form but the spec wants bech32.
let hex = "0".repeat(64);
assert!(parse_prs_url(&format!("/prs/{}/my-repo.git/info/refs", hex)).is_none());
}
#[test]
fn rejects_paths_outside_prs() {
let npub = sample_npub();
assert!(parse_prs_url(&format!("/{}/my-repo.git/info/refs", npub)).is_none());
assert!(parse_prs_url("/").is_none());
assert!(parse_prs_url("/prs/").is_none());
}
#[test]
fn percent_decodes_identifier() {
let npub = sample_npub();
let path = format!("/prs/{}/my%20repo.git/info/refs", npub);
let parsed = parse_prs_url(&path).expect("should parse");
assert_eq!(parsed.identifier, "my repo");
}
}