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(hub): a catalogue URL that serves HTML reads as "catalogue unavailable" (#1587)

xerj-org629e2b3e47d2
diff --git a/engine/crates/xerj-hub/src/catalogue.rs b/engine/crates/xerj-hub/src/catalogue.rs
index 1bc8b0873..d23e42672 100644
--- a/engine/crates/xerj-hub/src/catalogue.rs
+++ b/engine/crates/xerj-hub/src/catalogue.rs
@@ -481,10 +481,21 @@ fn fetch_and_store(hub_url: &str, cache_file: &Path, etag_file: &Path) -> Result
.get(reqwest::header::ETAG)
.and_then(|v| v.to_str().ok())
.map(str::to_string);
+ let cat_type = content_type(&resp);
let body = resp
.bytes()
.with_context(|| format!("read body of {cat_url}"))?
.to_vec();
+ // A static host answers an unknown path with its HTML page and a 200, so a
+ // success status alone does not mean a catalogue is published. Say so plainly
+ // instead of failing later on a signature that is really an HTML page.
+ if !starts_like_json_object(&body) {
+ bail!(
+ "the hub catalogue is unavailable: {cat_url} returned {} ({cat_type}), not a signed \
+ catalogue; no catalogue is served at this address",
+ preview(&body)
+ );
+ }
let sig_resp = client
.get(&sig_url)
@@ -524,6 +535,33 @@ fn fetch_and_store(hub_url: &str, cache_file: &Path, etag_file: &Path) -> Result
Ok(cat)
}
+/// The response's `Content-Type`, or `no content type`.
+fn content_type(resp: &reqwest::blocking::Response) -> String {
+ resp.headers()
+ .get(reqwest::header::CONTENT_TYPE)
+ .and_then(|v| v.to_str().ok())
+ .unwrap_or("no content type")
+ .to_string()
+}
+
+/// A catalogue is a JSON object: its first non-whitespace byte is `{`.
+fn starts_like_json_object(body: &[u8]) -> bool {
+ body.iter().find(|b| !b.is_ascii_whitespace()) == Some(&b'{')
+}
+
+/// A short, printable description of a body for an error message.
+fn preview(body: &[u8]) -> String {
+ if body.is_empty() {
+ return "an empty body".to_string();
+ }
+ let text = String::from_utf8_lossy(&body[..body.len().min(40)]);
+ let flat: String = text
+ .chars()
+ .map(|c| if c.is_control() { ' ' } else { c })
+ .collect();
+ format!("{} bytes starting {:?}", body.len(), flat.trim())
+}
+
/// `<cache_file>.sig` (e.g. `catalogue.json.sig`).
fn sig_sibling(cache_file: &Path) -> PathBuf {
let mut name = cache_file.file_name().unwrap_or_default().to_os_string();
@@ -961,6 +999,54 @@ mod tests {
assert_eq!(on_disk, good_bytes, "the cache is untouched");
}
+ /// A localhost "static site" that answers every GET with the same HTML page
+ /// and a 200, for `gets` requests.
+ fn spawn_html_site(gets: usize) -> (String, std::thread::JoinHandle<()>) {
+ let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
+ let url = format!("http://{}", listener.local_addr().unwrap());
+ let handle = std::thread::spawn(move || {
+ for _ in 0..gets {
+ let Ok((mut stream, _)) = listener.accept() else {
+ break;
+ };
+ let mut buf = [0u8; 2048];
+ let _ = stream.read(&mut buf);
+ let body = "<!doctype html><html><body>hub</body></html>";
+ let header = format!(
+ "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\
+ Content-Length: {}\r\nConnection: close\r\n\r\n",
+ body.len()
+ );
+ let _ = stream.write_all(header.as_bytes());
+ let _ = stream.write_all(body.as_bytes());
+ }
+ });
+ (url, handle)
+ }
+
+ #[test]
+ fn an_html_page_served_as_the_catalogue_reads_as_unavailable() {
+ let _guard = ENV_LOCK.lock().unwrap();
+ let fx = tempfile::tempdir().unwrap();
+ let (_, _, public) = fixture_bytes(fx.path());
+ let cache = tempfile::tempdir().unwrap();
+ // One GET: the catalogue is refused before the signature is even asked for.
+ let (url, handle) = spawn_html_site(1);
+
+ std::env::set_var("XERJ_HUB_PUBKEY", &public);
+ let err = load_catalogue(&url, cache.path(), false).expect_err("HTML is not a catalogue");
+ std::env::remove_var("XERJ_HUB_PUBKEY");
+ handle.join().unwrap();
+
+ let msg = format!("{err:#}");
+ assert!(msg.contains("the hub catalogue is unavailable"), "{msg}");
+ assert!(
+ msg.contains("text/html"),
+ "the message names what came back: {msg}"
+ );
+ assert!(!cache.path().join(CACHE_FILE).exists(), "nothing is cached");
+ }
+
#[test]
fn load_with_no_key_fetches_nothing_and_caches_nothing() {
let _guard = ENV_LOCK.lock().unwrap();