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(api): don't send x-elastic-product to detected OpenSearch callers

Vincenzo Lombardo774b7352b8a1
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rs
index 4aad9f178..2d0a2120e 100644
--- a/engine/crates/xerj-api/src/es_compat.rs
+++ b/engine/crates/xerj-api/src/es_compat.rs
@@ -84,19 +84,8 @@ const FALLBACK_OPENSEARCH_VERSION: &str = "2.11.0";
/// `EsVersion::default()`.
fn resolve_compat_version(state: &AppState, headers: &axum::http::HeaderMap) -> EsVersion {
let mut version = EsVersion::default();
-
let cfg = &state.config.compat;
- let user_agent = headers
- .get(axum::http::header::USER_AGENT)
- .and_then(|v| v.to_str().ok());
-
- let is_opensearch = if !cfg.distribution.is_empty() {
- cfg.distribution == "opensearch"
- } else {
- user_agent
- .map(|ua| ua.to_ascii_lowercase().contains("opensearch"))
- .unwrap_or(false)
- };
+ let is_opensearch = is_opensearch_caller(state, headers);
if is_opensearch {
version.distribution = Some("opensearch".to_string());
@@ -114,6 +103,33 @@ fn resolve_compat_version(state: &AppState, headers: &axum::http::HeaderMap) ->
version
}
+/// Is THIS caller (or is xerj forced to treat every caller as) an
+/// OpenSearch client? Shared by `resolve_compat_version` (the `version`/
+/// `distribution` block) and `router::es_headers_middleware` (the
+/// `x-elastic-product` response header — a real OpenSearch server never
+/// sends this at all, so an OpenSearch-detected caller shouldn't receive
+/// an Elastic-specific product-identity marker, on general correctness
+/// grounds — this alone was NOT sufficient to get a real OpenSearch
+/// Dashboards container past its `.kibana` saved-objects-migration 404,
+/// see PR description for what was actually confirmed).
+///
+/// Same two-tier priority as `resolve_compat_version`: an explicit
+/// `--compat-distribution` override always wins; otherwise a
+/// case-insensitive "contains opensearch" match on `User-Agent` (verified
+/// empirically against real `opensearch-py`/`opensearch-js` — including
+/// OpenSearch Dashboards' own internal client).
+pub(crate) fn is_opensearch_caller(state: &AppState, headers: &axum::http::HeaderMap) -> bool {
+ let cfg = &state.config.compat;
+ if !cfg.distribution.is_empty() {
+ return cfg.distribution == "opensearch";
+ }
+ headers
+ .get(axum::http::header::USER_AGENT)
+ .and_then(|v| v.to_str().ok())
+ .map(|ua| ua.to_ascii_lowercase().contains("opensearch"))
+ .unwrap_or(false)
+}
+
// ─────────────────────────────────────────────────────────────────────────────
// GET /_cluster/health
// ─────────────────────────────────────────────────────────────────────────────
diff --git a/engine/crates/xerj-api/src/router.rs b/engine/crates/xerj-api/src/router.rs
index 2a50cdfb6..f2fa9848f 100644
--- a/engine/crates/xerj-api/src/router.rs
+++ b/engine/crates/xerj-api/src/router.rs
@@ -9,7 +9,7 @@
//! to both.
use axum::{
- extract::{DefaultBodyLimit, Request},
+ extract::{DefaultBodyLimit, Request, State},
http::{HeaderValue, Method},
middleware::{self, Next},
response::Response,
@@ -753,8 +753,11 @@ pub fn build_es_compat_router(state: AppState) -> Router {
// Shared state
.with_state(state.clone())
// Middleware stack (applied outermost-last)
- .layer(middleware::from_fn_with_state(state, auth_middleware))
- .layer(middleware::from_fn(es_headers_middleware))
+ .layer(middleware::from_fn_with_state(
+ state.clone(),
+ auth_middleware,
+ ))
+ .layer(middleware::from_fn_with_state(state, es_headers_middleware))
.layer(middleware::from_fn(request_id_middleware))
.layer(trace_layer(access_log))
.layer(cors)
@@ -772,18 +775,34 @@ pub fn build_es_compat_router(state: AppState) -> Router {
/// Middleware that sets ES-compatible product headers on every response.
///
/// Kibana and other Elastic clients verify the presence of `X-Elastic-Product`
-/// before trusting the response. A `Warning` header is included for
-/// compatibility with tools that check for deprecation notices.
-async fn es_headers_middleware(req: Request, next: Next) -> Response {
+/// before trusting the response. A real OpenSearch server never sends this
+/// header at all, so a caller detected (or forced, see
+/// [`es_compat::is_opensearch_caller`]) as OpenSearch must NOT receive it —
+/// some OpenSearch-ecosystem clients treat an unexpected product header as
+/// just another response header, but there's no reason to send an
+/// Elastic-specific identity marker to a client we've already decided isn't
+/// Elastic. A `Warning` header is included for Elastic callers only, for the
+/// same reason.
+async fn es_headers_middleware(
+ State(state): State<AppState>,
+ req: Request,
+ next: Next,
+) -> Response {
+ let is_opensearch = es_compat::is_opensearch_caller(&state, req.headers());
let mut resp = next.run(req).await;
let headers = resp.headers_mut();
- headers.insert(
- "x-elastic-product",
- HeaderValue::from_static("Elasticsearch"),
- );
- // RFC 7234 warning header — signals no specific deprecation for now.
- if let Ok(v) = HeaderValue::from_str("299 Elasticsearch-8.13.0 \"\"") {
- headers.insert("warning", v);
+ if is_opensearch {
+ headers.remove("x-elastic-product");
+ headers.remove("warning");
+ } else {
+ headers.insert(
+ "x-elastic-product",
+ HeaderValue::from_static("Elasticsearch"),
+ );
+ // RFC 7234 warning header — signals no specific deprecation for now.
+ if let Ok(v) = HeaderValue::from_str("299 Elasticsearch-8.13.0 \"\"") {
+ headers.insert("warning", v);
+ }
}
resp
}