Skip to content

armgit / xerj

Public Imported from GitHub
0

AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.

fix(console): derive the WebAuthn RP from the bound address — passkey enrolment works on any port (#935)

xerj-orgccbc1ec57be8
diff --git a/engine/crates/xerj-console-api/src/auth/webauthn_setup.rs b/engine/crates/xerj-console-api/src/auth/webauthn_setup.rs
index 6ccfa2b38..97f289a3e 100644
--- a/engine/crates/xerj-console-api/src/auth/webauthn_setup.rs
+++ b/engine/crates/xerj-console-api/src/auth/webauthn_setup.rs
@@ -15,7 +15,73 @@ pub fn build(state: &ConsoleState) -> ConsoleResult<Webauthn> {
let builder = WebauthnBuilder::new(&state.rp.rp_id, &origin)
.map_err(|e| ConsoleApiError::Internal(format!("webauthn build: {e}")))?
.rp_name(&state.rp.rp_name);
+ // A loopback deployment may be reached on a port other than the one the
+ // origin was derived from (a container port map, or a passkey enrolled
+ // when the node listened elsewhere). `webauthn-rs` still requires the
+ // scheme and host to match — only the port check is relaxed, and only
+ // for `localhost` (#935). Named-host deployments keep strict matching.
+ let builder = if state.rp.rp_id == "localhost" {
+ builder.allow_any_port(true)
+ } else {
+ builder
+ };
builder
.build()
.map_err(|e| ConsoleApiError::Internal(format!("webauthn finalise: {e}")))
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::state::{ClusterMode, RpConfig};
+ use tempfile::TempDir;
+ use xerj_common::config::Config;
+ use xerj_engine::Engine;
+
+ fn state_with_rp(rp: RpConfig) -> (ConsoleState, TempDir) {
+ let dir = TempDir::new().unwrap();
+ let mut cfg = Config::default();
+ cfg.server.data_dir = dir.path().to_str().unwrap().to_string();
+ let engine = Engine::new(cfg).expect("engine");
+ let state = ConsoleState::new_with_rp(
+ engine,
+ "local".into(),
+ [0u8; 32],
+ ClusterMode::Standalone,
+ rp,
+ );
+ (state, dir)
+ }
+
+ #[tokio::test]
+ async fn a_derived_loopback_rp_builds_on_any_port() {
+ // #935: the RP used to be hard-coded to http://localhost:9200, so a
+ // node on any other port had no constructible relying party for the
+ // origin its browser actually used.
+ let rp = RpConfig::from_bind_url("http://127.0.0.1:9550").unwrap();
+ assert_eq!(rp.rp_origin, "http://localhost:9550");
+ let (state, _dir) = state_with_rp(rp);
+ assert!(build(&state).is_ok());
+ }
+
+ #[tokio::test]
+ async fn an_ip_literal_origin_cannot_build_an_rp() {
+ // Why `from_bind_url` rewrites the host instead of using the bind URL
+ // verbatim: `WebauthnBuilder` requires the rp_id to be an effective
+ // domain of the origin, and an IP-literal origin has no domain.
+ let rp = RpConfig {
+ rp_id: "localhost".into(),
+ rp_origin: "http://127.0.0.1:9550".into(),
+ rp_name: "Xerj Console".into(),
+ };
+ let (state, _dir) = state_with_rp(rp);
+ assert!(build(&state).is_err());
+ }
+
+ #[tokio::test]
+ async fn a_named_host_rp_builds() {
+ let rp = RpConfig::from_bind_url("http://console.example.com:9200").unwrap();
+ let (state, _dir) = state_with_rp(rp);
+ assert!(build(&state).is_ok());
+ }
+}
diff --git a/engine/crates/xerj-console-api/src/lib.rs b/engine/crates/xerj-console-api/src/lib.rs
index aeb0cc09d..440d2a2e6 100644
--- a/engine/crates/xerj-console-api/src/lib.rs
+++ b/engine/crates/xerj-console-api/src/lib.rs
@@ -62,4 +62,4 @@ pub mod views;
pub use client_ip::ClientIp;
pub use error::{ConsoleApiError, ConsoleResult};
pub use router::xerj_console_router;
-pub use state::ConsoleState;
+pub use state::{public_bind_host, ConsoleState, RpConfig};
diff --git a/engine/crates/xerj-console-api/src/state.rs b/engine/crates/xerj-console-api/src/state.rs
index 3d58cd645..c124e43aa 100644
--- a/engine/crates/xerj-console-api/src/state.rs
+++ b/engine/crates/xerj-console-api/src/state.rs
@@ -12,6 +12,7 @@ use xerj_common::net::TrustedProxies;
use xerj_engine::Engine;
use crate::client_ip::TrustedProxySource;
+use crate::error::{ConsoleApiError, ConsoleResult};
use crate::time::now_epoch_ms;
/// Server start time, in epoch ms. Used by `/cluster/info` so the SPA can
@@ -90,11 +91,100 @@ impl Default for RpConfig {
Self {
rp_id: "localhost".to_string(),
rp_origin: "http://localhost:9200".to_string(),
- rp_name: "Xerj Console".to_string(),
+ rp_name: default_rp_name(),
}
}
}
+fn default_rp_name() -> String {
+ "Xerj Console".to_string()
+}
+
+impl RpConfig {
+ /// Derive the relying party from the URL the console is actually
+ /// served on (in `xerj-server` that is the bound address plus the
+ /// *bound* ES-compat port, so ephemeral ports are already resolved —
+ /// issue #469).
+ ///
+ /// * Loopback and unspecified hosts (`localhost`, `127.0.0.1`, `::1`,
+ /// `0.0.0.0`, `::`) all map to `rp_id = "localhost"` with the origin
+ /// rewritten to `<scheme>://localhost:<port>`. Browsers refuse an
+ /// IP-literal WebAuthn origin outright, so keeping the bind address
+ /// in the origin makes enrolment impossible on any port; and keeping
+ /// `rp_id` on `localhost` preserves credentials enrolled against the
+ /// historical hard-coded default (#935).
+ /// * A named host is used verbatim: `rp_id` = host, origin = the URL.
+ /// * A non-loopback IP literal is refused: a WebAuthn RP id must be a
+ /// registrable domain, so there is no honest configuration for
+ /// `http://192.168.0.5:9200`. Serve the console behind a named host
+ /// (reverse proxy) or bind loopback to use passkeys.
+ pub fn from_bind_url(bind_url: &str) -> ConsoleResult<Self> {
+ let url = webauthn_rs::prelude::Url::parse(bind_url).map_err(|e| {
+ ConsoleApiError::Internal(format!("console bind url `{bind_url}`: {e}"))
+ })?;
+ let Some(host) = url.host_str().map(|h| h.to_string()) else {
+ return Err(ConsoleApiError::Internal(format!(
+ "console bind url `{bind_url}` has no host"
+ )));
+ };
+ // `Url::host_str` keeps the brackets on an IPv6 literal
+ // (`http://[::1]:9550` → `"[::1]"`); a domain name can never contain
+ // one, so trimming them restores the parseable spelling.
+ let host = host
+ .trim_start_matches('[')
+ .trim_end_matches(']')
+ .to_string();
+ let port = url.port().map(|p| format!(":{p}")).unwrap_or_default();
+ if bind_host_is_local(&host) {
+ Ok(Self {
+ rp_id: "localhost".to_string(),
+ rp_origin: format!("{}://localhost{port}", url.scheme()),
+ rp_name: default_rp_name(),
+ })
+ } else if host.parse::<std::net::IpAddr>().is_ok() {
+ Err(ConsoleApiError::Internal(format!(
+ "cannot derive a WebAuthn relying party from `{bind_url}`: a relying-party id \
+ must be a domain, not the IP literal `{host}` — serve the console behind a \
+ named host or bind loopback to use passkeys"
+ )))
+ } else {
+ Ok(Self {
+ rp_id: host.clone(),
+ rp_origin: format!("{}://{host}{port}", url.scheme()),
+ rp_name: default_rp_name(),
+ })
+ }
+ }
+}
+
+/// Is this URL host the machine itself, in any of its spellings?
+fn bind_host_is_local(host: &str) -> bool {
+ match host.parse::<std::net::IpAddr>() {
+ Ok(ip) => ip.is_loopback() || ip.is_unspecified(),
+ Err(_) => host.eq_ignore_ascii_case("localhost"),
+ }
+}
+
+/// The host a human should be sent to for the console, given the address
+/// the node bound (`server.bind_address`).
+///
+/// Every loopback or unspecified bind becomes `localhost`: browsers refuse
+/// an IP-literal WebAuthn origin, so a first-launch link printed as
+/// `http://127.0.0.1:<port>/…` can never complete passkey enrolment even
+/// when the relying party is derived correctly (#935). Any other address
+/// keeps its spelling, IPv6 bracketed for a URL (`http://[fd00::1]:…`).
+pub fn public_bind_host(bind_address: &str) -> String {
+ let raw = bind_address.trim();
+ match raw.parse::<std::net::IpAddr>() {
+ Ok(ip) if ip.is_loopback() || ip.is_unspecified() => "localhost".to_string(),
+ Ok(std::net::IpAddr::V6(v6)) => format!("[{v6}]"),
+ Ok(_) => raw.to_string(),
+ // `localhost` itself, and any name that slips past the server's
+ // IP-literal bind guard, keeps its spelling.
+ Err(_) => raw.to_string(),
+ }
+}
+
/// Top-level state object shared into every Xerj Console handler.
#[derive(Clone)]
pub struct ConsoleState {
@@ -208,3 +298,89 @@ impl std::fmt::Debug for ConsoleState {
.finish_non_exhaustive()
}
}
+
+#[cfg(test)]
+mod rp_config_tests {
+ use super::*;
+
+ /// The default bind is `127.0.0.1` — before #935 this was exactly the
+ /// node that could not enrol on any port but 9200.
+ #[test]
+ fn loopback_ipv4_bind_maps_to_localhost_with_the_bound_port() {
+ let rp = RpConfig::from_bind_url("http://127.0.0.1:9550").unwrap();
+ assert_eq!(rp.rp_id, "localhost");
+ assert_eq!(rp.rp_origin, "http://localhost:9550");
+ }
+
+ #[test]
+ fn loopback_ipv6_bind_maps_to_localhost() {
+ let rp = RpConfig::from_bind_url("http://[::1]:9550").unwrap();
+ assert_eq!(rp.rp_id, "localhost");
+ assert_eq!(rp.rp_origin, "http://localhost:9550");
+ }
+
+ #[test]
+ fn localhost_name_and_unspecified_binds_map_to_localhost() {
+ let a = RpConfig::from_bind_url("http://localhost:9550").unwrap();
+ let b = RpConfig::from_bind_url("http://0.0.0.0:9200").unwrap();
+ let c = RpConfig::from_bind_url("http://[::]:9200").unwrap();
+ assert_eq!(
+ (a.rp_id.as_str(), a.rp_origin.as_str()),
+ ("localhost", "http://localhost:9550")
+ );
+ assert_eq!(
+ (b.rp_id.as_str(), b.rp_origin.as_str()),
+ ("localhost", "http://localhost:9200")
+ );
+ assert_eq!(
+ (c.rp_id.as_str(), c.rp_origin.as_str()),
+ ("localhost", "http://localhost:9200")
+ );
+ }
+
+ #[test]
+ fn non_loopback_ip_bind_is_refused_with_the_address_named() {
+ let err = RpConfig::from_bind_url("http://192.168.1.5:9200").unwrap_err();
+ let msg = err.to_string();
+ assert!(
+ msg.contains("192.168.1.5"),
+ "the refusal must name the address so the operator can act: {msg}"
+ );
+ assert!(RpConfig::from_bind_url("http://[fd00::1]:9200").is_err());
+ }
+
+ #[test]
+ fn named_host_is_used_verbatim() {
+ let rp = RpConfig::from_bind_url("http://console.example.com:9200").unwrap();
+ assert_eq!(rp.rp_id, "console.example.com");
+ assert_eq!(rp.rp_origin, "http://console.example.com:9200");
+ }
+
+ #[test]
+ fn portless_url_keeps_a_portless_origin() {
+ let rp = RpConfig::from_bind_url("http://localhost").unwrap();
+ assert_eq!(rp.rp_id, "localhost");
+ assert_eq!(rp.rp_origin, "http://localhost");
+ }
+
+ #[test]
+ fn unparseable_url_is_refused() {
+ assert!(RpConfig::from_bind_url("not a url").is_err());
+ assert!(RpConfig::from_bind_url("http://").is_err());
+ }
+
+ #[test]
+ fn public_bind_host_normalises_every_loopback_spelling() {
+ assert_eq!(public_bind_host("127.0.0.1"), "localhost");
+ assert_eq!(public_bind_host("::1"), "localhost");
+ assert_eq!(public_bind_host("0.0.0.0"), "localhost");
+ assert_eq!(public_bind_host("::"), "localhost");
+ assert_eq!(public_bind_host("localhost"), "localhost");
+ }
+
+ #[test]
+ fn public_bind_host_keeps_non_loopback_addresses_bracketing_ipv6() {
+ assert_eq!(public_bind_host("192.168.1.5"), "192.168.1.5");
+ assert_eq!(public_bind_host("fd00::1"), "[fd00::1]");
+ }
+}
diff --git a/engine/crates/xerj-console-api/tests/phase2_auth_flow.rs b/engine/crates/xerj-console-api/tests/phase2_auth_flow.rs
index e4ea57b62..59055030b 100644
--- a/engine/crates/xerj-console-api/tests/phase2_auth_flow.rs
+++ b/engine/crates/xerj-console-api/tests/phase2_auth_flow.rs
@@ -27,7 +27,7 @@ use xerj_common::config::Config;
use xerj_console_api::{
auth::{sessions, store},
state::ClusterMode,
- xerj_console_router, ConsoleState,
+ xerj_console_router, ConsoleState, RpConfig,
};
use xerj_engine::Engine;
@@ -56,6 +56,36 @@ async fn boot() -> (Engine, Router, TempDir, String) {
(engine, xerj_console_router(state), dir, token)
}
+/// Boot the console the way `xerj-server` does after #935: derive the
+/// WebAuthn relying party from the console bind URL instead of the
+/// hard-coded `http://localhost:9200`.
+async fn boot_on_bind_url(bind_url: &str) -> (Router, TempDir, String) {
+ let dir = TempDir::new().unwrap();
+ let mut cfg = Config::default();
+ cfg.server.data_dir = dir.path().to_str().unwrap().to_string();
+ let engine = Engine::new(cfg).expect("engine");
+ let outcome = xerj_console_api::bootstrap::run(&engine, dir.path(), bind_url)
+ .await
+ .expect("bootstrap");
+ let token = outcome
+ .magic_link
+ .clone()
+ .unwrap()
+ .rsplit_once("token=")
+ .unwrap()
+ .1
+ .to_string();
+ let rp = RpConfig::from_bind_url(bind_url).expect("derive rp from bind url");
+ let state = ConsoleState::new_with_rp(
+ engine,
+ "local".into(),
+ outcome.master_key,
+ ClusterMode::Standalone,
+ rp,
+ );
+ (xerj_console_router(state), dir, token)
+}
+
async fn body_json(resp: axum::response::Response) -> (axum::http::StatusCode, Value) {
let status = resp.status();
let bytes = resp.into_body().collect().await.unwrap().to_bytes();
@@ -230,6 +260,55 @@ async fn passkey_begin_returns_creation_options_for_valid_enrollment() {
assert!(opts["user"].is_object());
}
+/// Issue #935: a node bound to a loopback IP on a port other than 9200 used
+/// to run its WebAuthn relying party as the hard-coded
+/// `http://localhost:9200`, so the first-launch enrolment was refused with
+/// "the clients relying party origin does not match our servers information"
+/// and nobody could sign in to the console. Deriving the RP from the bind
+/// URL must name `localhost` on the *bound* port, whatever the port is.
+#[tokio::test]
+async fn passkey_begin_on_a_loopback_non_9200_port_names_localhost_as_rp() {
+ let (router, _dir, token) = boot_on_bind_url("http://127.0.0.1:9550").await;
+
+ let r = router
+ .clone()
+ .oneshot(post_json(
+ "/_xerj-console/api/v1/auth/magic/redeem",
+ json!({ "token": token }),
+ ))
+ .await
+ .unwrap();
+ let (_, redeem_body) = body_json(r).await;
+ let enroll_id = redeem_body["data"]["enrollment_session_id"]
+ .as_str()
+ .unwrap()
+ .to_string();
+
+ let r = router
+ .oneshot(post_json(
+ "/_xerj-console/api/v1/auth/passkey/begin",
+ json!({
+ "enrollment_session_id": enroll_id,
+ "email": "[email protected]",
+ "display_name": "Owner"
+ }),
+ ))
+ .await
+ .unwrap();
+ let (status, body) = body_json(r).await;
+ assert_eq!(status, 200, "body: {body}");
+ let opts = &body["data"]["creation_options"]["publicKey"];
+ assert_eq!(
+ opts["rp"]["id"].as_str(),
+ Some("localhost"),
+ "the relying party must be localhost regardless of the bound IP/port: {body}"
+ );
+ assert!(
+ opts["challenge"].is_string(),
+ "creation_options missing challenge: {body}"
+ );
+}
+
#[tokio::test]
async fn passkey_begin_rejects_unknown_enrollment_session() {
let (_engine, router, _dir, _token) = boot().await;
diff --git a/engine/crates/xerj-server/src/main.rs b/engine/crates/xerj-server/src/main.rs
index cd7812c42..887a7c2b6 100644
--- a/engine/crates/xerj-server/src/main.rs
+++ b/engine/crates/xerj-server/src/main.rs
@@ -89,7 +89,7 @@ use tracing_subscriber::EnvFilter;
use xerj_api::{build_es_compat_router, build_native_router, AppState};
use xerj_cluster::{transport::TcpTransport, ClusterNode, ClusterRunner};
use xerj_common::{config::Config, metrics::Metrics};
-use xerj_console_api::{state::ClusterMode, ConsoleState};
+use xerj_console_api::{state::ClusterMode, ConsoleState, RpConfig};
use xerj_engine::Engine;
mod brain;
@@ -2327,14 +2327,13 @@ async fn async_main() -> Result<()> {
// key or partially-initialized stores.
let xerj_console_bind_url = format!(
"http://{}:{}",
- if cfg.server.bind_address == "0.0.0.0" || cfg.server.bind_address == "::" {
- "localhost".to_string()
- } else {
- // Bracketed for IPv6 — an unbracketed `http://::1:9200/…` is not a
- // URL any browser will open, and this string is printed in the
- // first-launch console link.
- url_host(&cfg.server.bind_address)
- },
+ // Every loopback or unspecified bind is *addressed* as `localhost`
+ // (issue #935): browsers refuse an IP-literal WebAuthn origin
+ // outright, so a first-launch link printed with `127.0.0.1` or
+ // `[::1]` could never complete passkey enrolment even once the
+ // relying party is derived from this same URL. Non-loopback
+ // addresses keep their spelling, IPv6 bracketed.
+ xerj_console_api::public_bind_host(&cfg.server.bind_address),
// The bound ES-compat port (8c), not `cfg.server.es_compat_port`: with
// an ephemeral port (`= 0`) the config value is never the port the
// node ends up listening on (issue #469).
@@ -2379,15 +2378,31 @@ async fn async_main() -> Result<()> {
)
.await
.context("xerj-console bootstrap")?;
- Some(
- ConsoleState::new(
+ Some({
+ // #935: the WebAuthn relying party follows the address and port
+ // the console is actually served on — `ConsoleState::new`'s
+ // hard-coded `http://localhost:9200` origin meant every node on
+ // another port refused the very first passkey enrolment with
+ // "origin does not match". A non-loopback IP bind has no honest
+ // RP (WebAuthn ids must be domains), so the console's passkey
+ // sign-in is unavailable there rather than the node refusing to
+ // serve its data-plane API over a console-only limitation.
+ let console_rp = match RpConfig::from_bind_url(&xerj_console_bind_url) {
+ Ok(rp) => rp,
+ Err(why) => {
+ warn!("Xerj Console passkey sign-in is unavailable: {why}");
+ RpConfig::default()
+ }
+ };
+ ConsoleState::new_with_rp(
engine.clone(),
xerj_console_node_id,
outcome.master_key,
xerj_console_cluster_mode,
+ console_rp,
)
- .with_trusted_proxies(trusted_proxies),
- )
+ .with_trusted_proxies(trusted_proxies)
+ })
} else {
warn!(
"Xerj Console bootstrap and routes are disabled while cluster-state storage is unavailable"
diff --git a/engine/crates/xerj-server/tests/ephemeral_port_console_link.rs b/engine/crates/xerj-server/tests/ephemeral_port_console_link.rs
index 1c8cf5898..c73260c41 100644
--- a/engine/crates/xerj-server/tests/ephemeral_port_console_link.rs
+++ b/engine/crates/xerj-server/tests/ephemeral_port_console_link.rs
@@ -145,6 +145,23 @@ fn port_from_setup_link(stderr: &str) -> Option<u16> {
port_str.parse().ok()
}
+/// Pull the host out of the same `…http://HOST:port/_xerj-console/setup#token=…`.
+fn host_from_setup_link(stderr: &str) -> Option<String> {
+ let marker = "/_xerj-console/setup#token=";
+ let end = stderr.find(marker)?;
+ let authority_end = &stderr[..end];
+ let start = authority_end.rfind("http://")?;
+ let authority = &authority_end[start + "http://".len()..end];
+ let (host, port_str) = authority.rsplit_once(':').unwrap_or((authority, ""));
+ // Guard against a future authority shape confusing the split: whatever
+ // follows the colon must be the port for the host to mean anything.
+ if port_str.is_empty() || port_str.chars().all(|c| c.is_ascii_digit()) {
+ Some(host.to_string())
+ } else {
+ None
+ }
+}
+
/// Pull the port out of the banner's `ES-compat 127.0.0.1:PORT [...]` line.
fn port_from_banner(stdout: &str) -> Option<u16> {
let line = stdout
@@ -206,3 +223,40 @@ fn ephemeral_es_port_console_link_names_the_bound_port() {
out.stdout, out.stderr
);
}
+
+/// Issue #935: the first-launch link must be *usable*. A browser treats
+/// `http://127.0.0.1:<port>` as an IP-literal origin, and WebAuthn refuses
+/// IP-literal origins outright — so a setup link printed with the bind
+/// address could never complete passkey enrolment, whatever the relying
+/// party is configured as. Every loopback bind is therefore printed (and
+/// its relying party derived) as `localhost`.
+#[test]
+fn loopback_bind_prints_the_setup_link_as_localhost() {
+ let dir = tempfile::tempdir().unwrap();
+ let (rest, grpc) = two_free_ports();
+
+ let out = boot_until(
+ &config_with_ephemeral_es_port(dir.path(), rest, grpc),
+ dir.path(),
+ Duration::from_secs(60),
+ |stdout, stderr| {
+ stdout.contains("ES-compat") && stderr.contains("/_xerj-console/setup#token=")
+ },
+ );
+
+ let host = host_from_setup_link(&out.stderr).unwrap_or_else(|| {
+ panic!(
+ "no parsable first-launch setup link found on stderr within the boot window\n\
+ --- stdout ---\n{}\n--- stderr ---\n{}",
+ out.stdout, out.stderr
+ )
+ });
+
+ assert_eq!(
+ host, "localhost",
+ "a loopback bind (127.0.0.1) must be printed as localhost in the \
+ setup link: an IP-literal origin cannot complete WebAuthn enrolment \
+ (#935)\n--- stderr ---\n{}",
+ out.stderr
+ );
+}