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(es-compat): apply the alias filter on the _eql fan-out (#451, filtered-alias leak)

xerj-org51ad31853c5a
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rs
index 339fd08a6..6125bbd68 100644
--- a/engine/crates/xerj-api/src/es_compat.rs
+++ b/engine/crates/xerj-api/src/es_compat.rs
@@ -33189,23 +33189,32 @@ pub async fn eql_search(
// Translate EQL -> xerj DSL, then run it through the normal search path.
let inner_query = eql_to_query(&eql);
let size = body.get("size").and_then(Value::as_u64).unwrap_or(10);
- let search_body = json!({ "query": inner_query, "size": size, "from": 0 });
-
- let req = match xerj_query::parse_request(&search_body)
- .map_err(|e| xerj_common::XerjError::invalid_query(e.to_string()))
- {
- Ok(r) => r,
- Err(e) => return ApiError::new(e).into_response(),
- };
// Fan out to every member of a multi-index alias (#451 `_eql` arm):
// eql_search used Engine::get_index -> aliased.first(), silently returning
// only the first member's events. Search each member and merge the event
- // lists, reporting the concrete member in `_index`.
- let (members, _filters) = match resolve_selector_with_filters(&state, &index).await {
+ // lists, reporting the concrete member in `_index`. Resolve BEFORE building
+ // the request so any alias `filter` is ANDed into the query: a *filtered*
+ // alias is a document boundary (#451 class C, commit 170f41f2 closed this
+ // exact leak for _search/_msearch), so EQL must honour it too — fanning out
+ // unfiltered would leak the hidden members' rows.
+ let (members, filters) = match resolve_selector_with_filters(&state, &index).await {
Ok(t) => t,
Err(e) => return ApiError::new(e).into_response(),
};
+ let effective_query = if filters.is_empty() {
+ inner_query
+ } else {
+ json!({ "bool": { "must": [inner_query], "filter": filters } })
+ };
+ let search_body = json!({ "query": effective_query, "size": size, "from": 0 });
+
+ let req = match xerj_query::parse_request(&search_body)
+ .map_err(|e| xerj_common::XerjError::invalid_query(e.to_string()))
+ {
+ Ok(r) => r,
+ Err(e) => return ApiError::new(e).into_response(),
+ };
let mut events: Vec<Value> = Vec::new();
let mut total: u64 = 0;
for (mname, midx) in &members {
diff --git a/engine/crates/xerj-api/tests/eql_through_alias_sees_every_member.rs b/engine/crates/xerj-api/tests/eql_through_alias_sees_every_member.rs
index 43bed7f37..0eaa705e0 100644
--- a/engine/crates/xerj-api/tests/eql_through_alias_sees_every_member.rs
+++ b/engine/crates/xerj-api/tests/eql_through_alias_sees_every_member.rs
@@ -104,3 +104,80 @@ async fn eql_search_through_a_multi_index_alias_sees_every_member() {
(#451 _eql arm): {body}"
);
}
+
+/// An EQL search through a *filtered* alias must honour the filter on EVERY
+/// member — a filtered alias is a document boundary (#451 class C, the same
+/// leak #559 closed for `_msearch`). The fan-out must not return the members'
+/// out-of-slice rows. Regression guard: fanning out unfiltered leaked them.
+#[tokio::test]
+async fn eql_search_through_a_filtered_alias_honours_the_filter_on_every_member() {
+ let (app, _dir) = app().await;
+
+ // idx-a holds an in-slice doc, idx-b holds an out-of-slice doc; both share
+ // `host` so the EQL condition matches both and ONLY the alias filter can
+ // exclude idx-b's row.
+ for (member, status) in [("idx-a", "active"), ("idx-b", "deleted")] {
+ let (st, _) = call(
+ &app,
+ "PUT",
+ &format!("/{member}"),
+ json!({"mappings": {"properties": {
+ "status": {"type": "keyword"}, "host": {"type": "keyword"}
+ }}}),
+ )
+ .await;
+ assert_eq!(st, StatusCode::OK, "create {member}");
+ let (st, _) = call(
+ &app,
+ "POST",
+ &format!("/{member}/_doc/{member}"),
+ json!({ "status": status, "host": "h1" }),
+ )
+ .await;
+ assert_eq!(st, StatusCode::CREATED, "index into {member}");
+ let (st, _) = call(&app, "POST", &format!("/{member}/_refresh"), Value::Null).await;
+ assert_eq!(st, StatusCode::OK, "refresh {member}");
+ }
+
+ // Filtered alias `hot` (status==active) over BOTH members.
+ for member in ["idx-a", "idx-b"] {
+ let (st, body) = call(
+ &app,
+ "PUT",
+ &format!("/{member}/_alias/hot"),
+ json!({ "filter": { "term": { "status": "active" } } }),
+ )
+ .await;
+ assert_eq!(st, StatusCode::OK, "filtered alias on {member}: {body}");
+ }
+
+ let (status, body) = call(
+ &app,
+ "POST",
+ "/hot/_eql/search",
+ json!({ "query": "any where host == \"h1\"" }),
+ )
+ .await;
+ assert_eq!(status, StatusCode::OK, "_eql/search status: {body}");
+
+ let events = body
+ .pointer("/hits/events")
+ .and_then(Value::as_array)
+ .cloned()
+ .unwrap_or_default();
+ assert_eq!(
+ events.len(),
+ 1,
+ "_eql/search through a filtered alias leaked an out-of-slice document: the EQL \
+ condition matched both members' docs, but the alias filter (status==active) must \
+ hide idx-b's status=deleted row. Fanning out unfiltered returns it (#451 _eql arm \
+ must AND the alias filter in, like _search/_msearch do): {body}"
+ );
+ // The one surviving event is the in-slice member's, not the leaked one.
+ assert_eq!(
+ events[0].get("_index").and_then(Value::as_str),
+ Some("idx-a"),
+ "the surviving event must be idx-a's in-slice doc, reported under its concrete \
+ member name: {body}"
+ );
+}