Skip to content
Merged
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: reject query/fragment web_urls and warn on node info errors (#370)
Both web_url validators now reject URLs carrying a query string or
fragment: the CLI builds View links by string-appending /{owner}/{repo},
so "?a=1" would swallow the repo path into the query. The CLI also no
longer swallows transport-level failures on GET / — a refused or failed
connection warns on stderr like the other degraded-link paths.
  • Loading branch information
Gravirei committed Aug 25, 2026
commit 57276ca23fd5a57b5ada219f4b7e39769b14baa7
34 changes: 31 additions & 3 deletions crates/gitlawb-node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,9 +612,10 @@ impl Config {

/// Validate a `web_url` value for use as a browser-reachable base URL.
/// Blank values are rejected (clap maps `--web-url ""` to `Some("")`), and
/// non-blank values must parse as absolute `http`/`https` URLs — the CLI
/// treats this as a string prefix to append paths to, so anything else
/// produces links no browser can follow.
/// non-blank values must parse as absolute `http`/`https` URLs with no query
/// or fragment — the CLI treats this as a string prefix to append paths to,
/// so anything else produces links no browser can follow (`?a=1/owner/repo`
/// puts the repo path inside the query string).
pub(crate) fn validate_web_url(raw: &str) -> Result<(), String> {
if raw.trim().is_empty() {
return Err("must not be empty or whitespace-only".into());
Expand All @@ -629,6 +630,16 @@ pub(crate) fn validate_web_url(raw: &str) -> Result<(), String> {
}
// url::Url cannot represent a URL without a host for http/https schemes, so
// reaching here guarantees an absolute browser-usable base.
if parsed.query().is_some() {
return Err(
"must not contain a query string — it is used as a path prefix for View links".into(),
);
}
if parsed.fragment().is_some() {
return Err(
"must not contain a fragment — it is used as a path prefix for View links".into(),
);
}
Ok(())
}

Expand Down Expand Up @@ -1060,6 +1071,23 @@ mod tests {
assert!(cfg.validate().is_err(), "web_url {bad:?} must be rejected");
}

// Query strings and fragments corrupt the appended repo path, since
// web_url is used as a raw string prefix (`?a=1` would swallow
// `/{owner}/{repo}` into the query).
for bad in [
"https://gitlawb.com?a=1",
"https://gitlawb.com/?utm_source=docs",
"https://gitlawb.com#section",
"https://gitlawb.com/#faq",
] {
let mut cfg = Config::parse_from(["gitlawb-node"]);
cfg.web_url = Some(bad.into());
assert!(
cfg.validate().is_err(),
"web_url {bad:?} (query or fragment) must be rejected"
);
}

// Surrounding whitespace around a valid URL is tolerated.
let mut cfg = Config::parse_from(["gitlawb-node"]);
cfg.web_url = Some(" https://gitlawb.com ".into());
Expand Down
59 changes: 43 additions & 16 deletions crates/gl/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,18 +225,21 @@ async fn resolve_owner_did(_node: &str, dir: Option<&std::path::Path>) -> Result
/// Fetch `GET /` from the node and return the `web_url` if the node advertises one.
///
/// The View: link is a nice-to-have, so every failure mode degrades to `None`
/// rather than failing the enclosing command — but failures are not all silent:
/// a non-success HTTP status is surfaced as a stderr warning (with the status)
/// since that means the node itself is misbehaving or unreachable at the app
/// level, which the user would otherwise never learn. A successful response
/// that lacks a usable `web_url` (field missing, blank, malformed, or not an
/// absolute http(s) URL) omits the link silently: self-hosted nodes without a
/// web front-end are expected to not advertise one (#370).
/// rather than failing the enclosing command — but failures are surfaced as
/// stderr warnings (request error, non-success HTTP status with the status,
/// malformed advertised value), since they all mean the node itself is
/// misbehaving or misconfigured, which the user would otherwise never learn.
/// A successful response that lacks a usable `web_url` (field missing or
/// blank) omits the link silently: self-hosted nodes without a web front-end
/// are expected to not advertise one (#370).
pub(crate) async fn fetch_node_web_url(node: &str) -> Option<String> {
let info_client = NodeClient::new(node, None);
let info_resp = match info_client.get("/").await {
Ok(resp) => resp,
Err(_) => return None,
Err(err) => {
eprintln!("warning: node info request failed ({err}); skipping View link");
return None;
}
};
let status = info_resp.status();
if !status.is_success() {
Expand All @@ -248,21 +251,28 @@ pub(crate) async fn fetch_node_web_url(node: &str) -> Option<String> {
Err(_) => return None,
};
// Missing field / non-string degrade to None without warning — same contract
// as before; only the HTTP-status path warns there.
// as before; only transport- and advertisement-level failures warn there.
let raw = info["web_url"].as_str()?;
let trimmed = raw.trim().trim_end_matches('/');
if trimmed.is_empty() {
return None;
}
// A present-but-malformed value means the node is misconfigured; say so
// instead of quietly dropping the link. Mirrors the node-side boot
// validation: must parse as an absolute http(s) URL.
let malformed = match trimmed.parse::<url::Url>() {
Ok(parsed) => !matches!(parsed.scheme(), "http" | "https"),
Err(_) => true,
// validation: must be an absolute http(s) URL with no query or fragment —
// the link is built by string-appending `/{owner}/{repo}` to it.
let malformed_reason: Option<&str> = match trimmed.parse::<url::Url>() {
Err(_) => Some("not an absolute URL"),
Ok(parsed) if !matches!(parsed.scheme(), "http" | "https") => Some("not http(s)"),
Ok(parsed) if parsed.query().is_some() || parsed.fragment().is_some() => {
Some("contains a query or fragment")
}
Ok(_) => None,
};
if malformed {
eprintln!("warning: node advertised a malformed web_url ({trimmed:?}); skipping View link");
if let Some(reason) = malformed_reason {
eprintln!(
"warning: node advertised a malformed web_url ({trimmed:?}, {reason}); skipping View link"
);
return None;
}
Some(trimmed.to_string())
Expand Down Expand Up @@ -989,7 +999,14 @@ mod tests {
/// malformed advertisement means the node is misconfigured (#370).
#[tokio::test]
async fn test_fetch_node_web_url_malformed_value_is_rejected() {
for bad in ["gitlawb.com", "not a url", "ftp://files.example.com"] {
for bad in [
"gitlawb.com",
"not a url",
"ftp://files.example.com",
// Query/fragment corrupt the appended /{owner}/{repo} path.
"https://example.com?a=1",
"https://example.com/#faq",
] {
let mut server = mockito::Server::new_async().await;
let _m = server
.mock("GET", "/")
Expand All @@ -1007,6 +1024,16 @@ mod tests {
}
}

/// A transport-level failure (connection refused) degrades to None but is
/// no longer silent: the request error is surfaced on stderr so an
/// unreachable node isn't indistinguishable from one without a web_url.
#[tokio::test]
async fn test_fetch_node_web_url_connection_refused_warns() {
// Port 1 on localhost is reserved (tcpmux) and refuses connections.
let web_url = fetch_node_web_url("http://127.0.0.1:1").await;
assert_eq!(web_url, None);
}

#[tokio::test]
async fn test_fetch_node_web_url_validates_after_trimming() {
// Trailing slash is trimmed before validation; result stays usable.
Expand Down
Loading