Skip to content
Closed
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
fix: address review feedback on View-URL PR (#370)
- Extract fetch_node_web_url() helper for testability; add 6 unit tests
  covering: web_url present, trailing-slash trim, absent, empty string,
  whitespace-only, and server error
- Reject empty/whitespace-only GITLAWB_WEB_URL at boot in Config::validate()
- Trim whitespace from web_url in the CLI helper (not just trailing slashes)
- Document GITLAWB_WEB_URL in .env.example alongside GITLAWB_PUBLIC_URL
- Mirror uses shared fetch_node_web_url() helper
  • Loading branch information
Gravirei committed Aug 25, 2026
commit 195a0179b8da8b561dfe7c5475746704717f5bce
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ GITLAWB_KEY=/data/keys/identity.pem
# Publicly reachable URL of this node (used in peer announcements)
GITLAWB_PUBLIC_URL=https://your-node.example.com

# Base URL for the web view of repos on this node (used by `gl` to print a
# working View: link after repo creation). Omit for nodes with no web front-end.
# Distinct from GITLAWB_PUBLIC_URL: that is API reachability for peers;
# this is browser reachability for humans.
# GITLAWB_WEB_URL=https://gitlawb.com

# ── Server ────────────────────────────────────────────────────────────────
GITLAWB_HOST=0.0.0.0
GITLAWB_PORT=7545
Expand Down
36 changes: 36 additions & 0 deletions crates/gitlawb-node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,16 @@ impl Config {
floor
));
}
// GITLAWB_WEB_URL is advertised in GET / so the CLI can print a working
// View: link. Clap maps "" to Some("") which would produce a broken URL;
// reject early instead of serving a malformed link.
if let Some(url) = &self.web_url {
if url.trim().is_empty() {
return Err("GITLAWB_WEB_URL must not be empty or whitespace-only — \
set it to a full URL like https://gitlawb.com or leave it unset."
.into());
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Ok(())
}
}
Expand Down Expand Up @@ -979,4 +989,30 @@ mod tests {
"db_max_connections at the floor (pushes + headroom) must validate"
);
}

#[test]
fn web_url_rejects_empty_and_whitespace() {
// Unset is fine.
Config::parse_from(["gitlawb-node"])
.validate()
.expect("no web_url must validate");

// A real URL validates.
let mut cfg = Config::parse_from(["gitlawb-node"]);
cfg.web_url = Some("https://gitlawb.com".into());
assert!(cfg.validate().is_ok());

// Empty string is rejected.
let mut cfg = Config::parse_from(["gitlawb-node"]);
cfg.web_url = Some("".into());
assert!(cfg.validate().is_err(), "empty web_url must be rejected");

// Whitespace-only is rejected.
let mut cfg = Config::parse_from(["gitlawb-node"]);
cfg.web_url = Some(" ".into());
assert!(
cfg.validate().is_err(),
"whitespace-only web_url must be rejected"
);
}
}
14 changes: 2 additions & 12 deletions crates/gl/src/mirror.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,8 @@ pub async fn run(args: MirrorArgs) -> Result<()> {
println!(" Clone: git clone {gitlawb_url}");
// Only print View: when the node advertises a web_url — self-hosted nodes
// without a web front-end would otherwise produce a 404 link (#370).
let info_client = NodeClient::new(&args.node, None);
if let Ok(info_resp) = info_client.get("/").await {
if info_resp.status().is_success() {
if let Ok(info) = info_resp.json::<Value>().await {
if let Some(web_url) = info["web_url"].as_str() {
let web_url = web_url.trim_end_matches('/');
if !web_url.is_empty() {
println!(" View: {web_url}/{owner_short}/{name}");
}
}
}
}
if let Some(web_url) = crate::repo::fetch_node_web_url(&args.node).await {
println!(" View: {web_url}/{owner_short}/{name}");
}

Ok(())
Expand Down
120 changes: 108 additions & 12 deletions crates/gl/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,24 @@ async fn resolve_owner_did(_node: &str, dir: Option<&std::path::Path>) -> Result
Ok(did.split(':').next_back().unwrap_or(&did).to_string())
}

/// Fetch `GET /` from the node and return the `web_url` if the node advertises one.
/// Returns `None` on any failure (network error, non-success status, missing field).
pub(crate) async fn fetch_node_web_url(node: &str) -> Option<String> {
let info_client = NodeClient::new(node, None);
let info_resp = info_client.get("/").await.ok()?;
if !info_resp.status().is_success() {
return None;
}
let info: Value = info_resp.json().await.ok()?;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
let raw = info["web_url"].as_str()?;
let trimmed = raw.trim().trim_end_matches('/');
if trimmed.is_empty() {
None
} else {
Some(trimmed.to_string())
}
}

async fn cmd_create(
name: String,
description: Option<String>,
Expand Down Expand Up @@ -267,18 +285,8 @@ async fn cmd_create(
println!(" HTTP: {clone_url}");
// Only print View: when the node advertises a web_url — self-hosted nodes
// without a web front-end would otherwise produce a 404 link (#370).
let info_client = NodeClient::new(&node, None);
if let Ok(info_resp) = info_client.get("/").await {
if info_resp.status().is_success() {
if let Ok(info) = info_resp.json::<Value>().await {
if let Some(web_url) = info["web_url"].as_str() {
let web_url = web_url.trim_end_matches('/');
if !web_url.is_empty() {
println!(" View: {web_url}/{owner_short}/{name}");
}
}
}
}
if let Some(web_url) = fetch_node_web_url(&node).await {
println!(" View: {web_url}/{owner_short}/{name}");
}
if let Some(desc) = payload["description"].as_str().filter(|s| !s.is_empty()) {
println!(" Desc: {desc}");
Expand Down Expand Up @@ -860,6 +868,94 @@ mod tests {
.unwrap();
}

#[tokio::test]
async fn test_fetch_node_web_url_with_web_url() {
let mut server = mockito::Server::new_async().await;
let _m = server
.mock("GET", "/")
.with_status(200)
.with_header("content-type", "application/json")
.with_body(r#"{"web_url":"https://example.com","did":"did:key:z6Mk"}"#)
.create_async()
.await;

let web_url = fetch_node_web_url(&server.url()).await;
assert_eq!(web_url.as_deref(), Some("https://example.com"));
}

#[tokio::test]
async fn test_fetch_node_web_url_trims_trailing_slash() {
let mut server = mockito::Server::new_async().await;
let _m = server
.mock("GET", "/")
.with_status(200)
.with_header("content-type", "application/json")
.with_body(r#"{"web_url":"https://example.com/"}"#)
.create_async()
.await;

let web_url = fetch_node_web_url(&server.url()).await;
assert_eq!(web_url.as_deref(), Some("https://example.com"));
}

#[tokio::test]
async fn test_fetch_node_web_url_without_web_url() {
let mut server = mockito::Server::new_async().await;
let _m = server
.mock("GET", "/")
.with_status(200)
.with_header("content-type", "application/json")
.with_body(r#"{"did":"did:key:z6Mk"}"#)
.create_async()
.await;

let web_url = fetch_node_web_url(&server.url()).await;
assert_eq!(web_url, None);
}

#[tokio::test]
async fn test_fetch_node_web_url_empty_string() {
let mut server = mockito::Server::new_async().await;
let _m = server
.mock("GET", "/")
.with_status(200)
.with_header("content-type", "application/json")
.with_body(r#"{"web_url":""}"#)
.create_async()
.await;

let web_url = fetch_node_web_url(&server.url()).await;
assert_eq!(web_url, None);
}

#[tokio::test]
async fn test_fetch_node_web_url_whitespace_only() {
let mut server = mockito::Server::new_async().await;
let _m = server
.mock("GET", "/")
.with_status(200)
.with_header("content-type", "application/json")
.with_body(r#"{"web_url":" "}"#)
.create_async()
.await;

let web_url = fetch_node_web_url(&server.url()).await;
assert_eq!(web_url, None);
}

#[tokio::test]
async fn test_fetch_node_web_url_server_error() {
let mut server = mockito::Server::new_async().await;
let _m = server
.mock("GET", "/")
.with_status(500)
.create_async()
.await;

let web_url = fetch_node_web_url(&server.url()).await;
assert_eq!(web_url, None);
}

#[tokio::test]
async fn test_cmd_create_server_error() {
let dir = TempDir::new().unwrap();
Expand Down
Loading