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.

merge(rc4-w1): Stream D — stability / resource limits (blockers 11, 12, 13, 14)

xerj-orgcac434302812
diff --cc engine/crates/xerj-api/src/es_compat.rs
index 8bf20553a,9e4e21235..7254f6185
--- a/engine/crates/xerj-api/src/es_compat.rs
+++ b/engine/crates/xerj-api/src/es_compat.rs
@@@ -3678,46 -3688,39 +3688,78 @@@ fn default_size() -> usize
10
}
--/// Build a `SearchRequest` from the ES body, forwarding all relevant options.
+/// Walk a request JSON subtree for inline Painless script sources — values
+/// under a `script` key or any `*_script` key (e.g. scripted_metric's
+/// `map_script`) — and validate each against the engine's script resource
+/// limits. Returns the first limit-violation message, if any.
+///
+/// Only genuine limit violations (oversized source, or nesting depth beyond
+/// the parser's cap) are reported. Ordinary scripts — including ones outside
+/// our Painless subset — return `None` so they keep degrading gracefully at
+/// score time rather than turning into spurious 400s (which would break
+/// otherwise-passing requests and the ES-compat gate). This is the request-time
+/// guard that converts an adversarial deeply-nested `script_score` from a
+/// process-aborting stack overflow into a clean 400.
+fn find_script_limit_violation(v: &Value) -> Option<String> {
+ match v {
+ Value::Object(map) => {
+ for (k, val) in map {
+ if k == "script" || k.ends_with("_script") {
+ let src = match val {
+ Value::String(s) => Some(s.as_str()),
+ Value::Object(o) => o.get("source").and_then(Value::as_str),
+ _ => None,
+ };
+ if let Some(src) = src {
+ if let Err(e) = xerj_engine::painless::check_script_limits(src) {
+ return Some(e);
+ }
+ }
+ }
+ if let Some(hit) = find_script_limit_violation(val) {
+ return Some(hit);
+ }
+ }
+ None
+ }
+ Value::Array(arr) => arr.iter().find_map(find_script_limit_violation),
+ _ => None,
+ }
+}
+
+ /// Parse an ES time-units string to milliseconds: `"500ms"`, `"30s"`,
+ /// `"2m"`, `"1h"`, `"1d"`, `"5micros"`, `"100nanos"`, bare number =
+ /// millis. Sub-millisecond values round up to 1 ms so `timeout=100nanos`
+ /// still arms a (minimal) deadline instead of none at all.
+ fn parse_time_to_millis(s: &str) -> Option<u64> {
+ let s = s.trim();
+ if s.is_empty() {
+ return None;
+ }
+ if let Some(n) = s.strip_suffix("nanos") {
+ let v: u64 = n.parse().ok()?;
+ return Some((v / 1_000_000).max(1));
+ }
+ if let Some(n) = s.strip_suffix("micros") {
+ let v: u64 = n.parse().ok()?;
+ return Some((v / 1_000).max(1));
+ }
+ if let Some(n) = s.strip_suffix("ms") {
+ let v: u64 = n.parse().ok()?;
+ return Some(v.max(1));
+ }
+ let (digits, mul_ms) = match s.as_bytes().last()? {
+ b's' => (&s[..s.len() - 1], 1_000u64),
+ b'm' => (&s[..s.len() - 1], 60_000),
+ b'h' => (&s[..s.len() - 1], 3_600_000),
+ b'd' => (&s[..s.len() - 1], 86_400_000),
+ _ => (s, 1), // bare number → milliseconds (ES convention)
+ };
+ let n: u64 = digits.parse().ok()?;
+ Some(n.saturating_mul(mul_ms).max(1))
+ }
+
++/// Build a `SearchRequest` from the ES body, forwarding all relevant options.
fn build_search_request(
body: &EsSearchBody,
aggs_value: Option<Value>,