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.
Rust agent-memoryai-agentsbm25claude-codeclicode-searchcursordatabaseelasticsearchfull-text-searchhybrid-searchjsonllocal-firstmcpmcp-servermodel-context-protocolragrustsearch-enginevector-search
fix(api): report a bad index name as invalid_index_name_exception (#1666)
xerj-orgb20cf45f0797
diff --git a/engine/crates/xerj-api/src/error.rs b/engine/crates/xerj-api/src/error.rsindex e6f72735f..a0d95c28b 100644--- a/engine/crates/xerj-api/src/error.rs+++ b/engine/crates/xerj-api/src/error.rs@@ -219,23 +219,27 @@ impl ApiError {// index name — several other 404s reuse this variant to carry a// sentence (e.g. "index template [x] missing"), which must keep its// existing shape. (RC4 Wave-3 item 4f.)- let (res_type, res_id, res_uuid, top_index) =- if matches!(self.inner, XerjError::IndexNotFound { .. }) {- match index_name.as_deref() {- Some(n) if is_plain_index_name(n) => {- reason = format!("no such index [{n}]");- (- Some("index_or_alias".to_string()),- Some(n.to_string()),- Some("_na_".to_string()),- Some(n.to_string()),- )- }- _ => (None, None, None, None),+ let (res_type, res_id, res_uuid, top_index) = match &self.inner {+ XerjError::IndexNotFound { .. } => match index_name.as_deref() {+ Some(n) if is_plain_index_name(n) => {+ reason = format!("no such index [{n}]");+ (+ Some("index_or_alias".to_string()),+ Some(n.to_string()),+ Some("_na_".to_string()),+ Some(n.to_string()),+ )}- } else {- (None, None, None, None)- };+ _ => (None, None, None, None),+ },+ // A rejected name is not a mapping problem. ES puts the attempted+ // name and `index_uuid: "_na_"` on `invalid_index_name_exception`+ // and leaves the reason as the name rule that failed.+ XerjError::InvalidIndexName { index, .. } => {+ (None, None, Some("_na_".to_string()), Some(index.clone()))+ }+ _ => (None, None, None, None),+ };let http_status =StatusCode::from_u16(status_code).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);@@ -290,6 +294,7 @@ impl IntoResponse for ApiError {/// |---|---|---|/// | `IndexNotFound` | `index_not_found_exception` | 404 |/// | `IndexAlreadyExists` | `resource_already_exists_exception` | 409 |+/// | `InvalidIndexName` | `invalid_index_name_exception` | 400 |/// | `IndexUnavailable` | `no_shard_available_action_exception` | 503 |/// | `DocumentNotFound` | `document_missing_exception` | 404 |/// | `InvalidMapping` | `mapper_parsing_exception` | 400 |@@ -311,6 +316,7 @@ fn xerj_error_type(e: &XerjError) -> String {// ── Index lifecycle ───────────────────────────────────────────────XerjError::IndexNotFound { .. } => "index_not_found_exception",XerjError::IndexAlreadyExists { .. } => "resource_already_exists_exception",+ XerjError::InvalidIndexName { .. } => "invalid_index_name_exception",// The index exists; its only copy cannot be opened. ES reports an// existing-but-unallocated index this way, so dashboards that already// understand a red shard understand this too.@@ -359,6 +365,7 @@ fn extract_index_name(e: &XerjError) -> Option<String> {XerjError::IndexNotFound { name } | XerjError::IndexAlreadyExists { name } => {Some(name.clone())}+ XerjError::InvalidIndexName { index, .. } => Some(index.clone()),XerjError::DocumentNotFound { index, .. } => Some(index.clone()),XerjError::IndexBlocked { index, .. } | XerjError::IndexUnavailable { index, .. } => {Some(index.clone())diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rsindex b59a3bc35..629ad6125 100644--- a/engine/crates/xerj-api/src/es_compat.rs+++ b/engine/crates/xerj-api/src/es_compat.rs@@ -1059,9 +1059,10 @@ pub async fn create_index(// Leading dots are allowed for system indices like .kibana, .security-*.if index.starts_with('_') || index.starts_with('-') || index.starts_with('+') {- let e = xerj_common::XerjError::invalid_mapping(format!(- "Invalid index name [{index}]: must not start with '_', '-', or '+'"- ));+ let e = xerj_common::XerjError::invalid_index_name(+ index.clone(),+ format!("Invalid index name [{index}]: must not start with '_', '-', or '+'"),+ );return ApiError::new(e).into_response();}@@ -1944,6 +1945,64 @@ mod runtime_field_type_tests {"lookup is a legal ES runtime field type; got {status} {body}");}++ async fn put_index(path: &str) -> (StatusCode, Value) {+ let response = crate::router::build_es_compat_router(test_state())+ .oneshot(+ Request::put(path)+ .header("content-type", "application/json")+ .body(Body::from("{}"))+ .unwrap(),+ )+ .await+ .unwrap();+ let status = response.status();+ let bytes = to_bytes(response.into_body(), usize::MAX).await.unwrap();+ (+ status,+ serde_json::from_slice(&bytes).unwrap_or(Value::Null),+ )+ }++ /// `PUT /BadName` used to come back as `mapper_parsing_exception` because+ /// every name rule was an `InvalidMapping`. ES answers a bad name with+ /// `invalid_index_name_exception` before it looks at the mapping. A real+ /// mapping error stays `mapper_parsing_exception` (the runtime-field test+ /// above).+ #[tokio::test]+ async fn create_index_rejects_a_bad_name_as_invalid_index_name_exception() {+ for (path, needle) in [+ (+ "/Bad",+ "index name must start with a lowercase letter or '.', got 'B'",+ ),+ (+ "/_secret",+ "Invalid index name [_secret]: must not start with '_', '-', or '+'",+ ),+ ("/bad!name", "index name contains invalid character '!'"),+ ] {+ let (status, body) = put_index(path).await;+ assert_eq!(status, StatusCode::BAD_REQUEST, "{path}: {body}");+ assert_eq!(+ body["error"]["type"], "invalid_index_name_exception",+ "{path}: {body}"+ );+ assert_eq!(body["status"], 400, "{path}");+ let reason = body["error"]["reason"].as_str().unwrap_or("");+ assert!(reason.contains(needle), "{path}: {reason}");+ assert_eq!(body["error"]["index_uuid"], "_na_", "{path}: {body}");+ assert_eq!(+ body["error"]["index"],+ path.trim_start_matches('/'),+ "{path}: {body}"+ );+ assert_eq!(+ body["error"]["root_cause"][0]["type"], "invalid_index_name_exception",+ "{path}"+ );+ }+ }}// ─────────────────────────────────────────────────────────────────────────────diff --git a/engine/crates/xerj-common/src/error.rs b/engine/crates/xerj-common/src/error.rsindex 3e028e2f4..6f6931653 100644--- a/engine/crates/xerj-common/src/error.rs+++ b/engine/crates/xerj-common/src/error.rs@@ -22,6 +22,14 @@ pub enum XerjError {#[error("index already exists: {name}")]IndexAlreadyExists { name: String },+ /// The caller asked to create or address an index whose name ES would+ /// reject before any mapping is parsed. Display is the bare reason:+ /// the HTTP mapper emits `invalid_index_name_exception` and must not+ /// prefix it the way `InvalidMapping` used to (`invalid mapping:`),+ /// which is what made `PUT /BadName` look like a mapping error.+ #[error("{reason}")]+ InvalidIndexName { index: String, reason: String },+/// The index directory exists but could not be opened, so the index/// cannot serve. Deliberately distinct from [`XerjError::IndexNotFound`]:/// the name is real, the data is still on disk, and the operator's move is@@ -159,6 +167,13 @@ impl XerjError {Self::IndexAlreadyExists { name: name.into() }}+ pub fn invalid_index_name(index: impl Into<String>, reason: impl Into<String>) -> Self {+ Self::InvalidIndexName {+ index: index.into(),+ reason: reason.into(),+ }+ }+pub fn index_unavailable(index: impl Into<String>, reason: impl Into<String>) -> Self {Self::IndexUnavailable {index: index.into(),@@ -293,7 +308,8 @@ impl XerjError {match self {Self::IndexNotFound { .. } | Self::DocumentNotFound { .. } => 404,Self::IndexAlreadyExists { .. } | Self::VersionConflict { .. } => 409,- Self::InvalidMapping { .. }+ Self::InvalidIndexName { .. }+ | Self::InvalidMapping { .. }| Self::InvalidQuery { .. }| Self::InvalidDocumentJson { .. }| Self::ConfigError { .. }diff --git a/engine/crates/xerj-common/src/types.rs b/engine/crates/xerj-common/src/types.rsindex 41a210155..3e6f57d79 100644--- a/engine/crates/xerj-common/src/types.rs+++ b/engine/crates/xerj-common/src/types.rs@@ -98,8 +98,9 @@ impl IndexName {/// es_compat `create_index` surface, #80) can reject `..`/separators/NUL/// locally instead of relying solely on the deeper `IndexName::new` guard.pub fn validate(name: &str) -> Result<(), XerjError> {+ let reject = |reason: String| XerjError::invalid_index_name(name, reason);if name.len() > 255 {- return Err(XerjError::invalid_mapping(format!(+ return Err(reject(format!("index name too long: {} chars (max 255)",name.len())));@@ -108,7 +109,7 @@ impl IndexName {// empty-name check into the extraction so the two facts can't// drift apart in a future refactor.let Some(first) = name.chars().next() else {- return Err(XerjError::invalid_mapping("index name cannot be empty"));+ return Err(reject("index name cannot be empty".to_string()));};// Reject the literal names "." and ".." and any name containing a// path separator. Previously a leading '.' was carved out for system@@ -116,18 +117,18 @@ impl IndexName {// which let `..` through — `IndexName::new("..")` succeeded and// `data_dir.join("..")` traversed out of the data directory.if name == "." || name == ".." {- return Err(XerjError::invalid_mapping(format!(+ return Err(reject(format!("index name must not be '.' or '..' (got '{name}')")));}if name.contains('/') || name.contains('\\') || name.contains('\0') {- return Err(XerjError::invalid_mapping(format!(+ return Err(reject(format!("index name must not contain a path separator (got '{name}')")));}// Allow leading '.' for system indices like .kibana, .security-* etc.if !first.is_ascii_lowercase() && first != '.' {- return Err(XerjError::invalid_mapping(format!(+ return Err(reject(format!("index name must start with a lowercase letter or '.', got '{first}'")));}@@ -135,7 +136,7 @@ impl IndexName {let rest = if first == '.' { &name[1..] } else { name };for ch in rest.chars() {if !matches!(ch, 'a'..='z' | '0'..='9' | '-' | '_' | '.') {- return Err(XerjError::invalid_mapping(format!(+ return Err(reject(format!("index name contains invalid character '{ch}' \(only lowercase letters, digits, '-', '_', '.' allowed)")));@@ -145,7 +146,7 @@ impl IndexName {// anywhere in the name is a path-traversal vector — reject it the// same way the /_memory/{ns} validator does.if name.contains("..") {- return Err(XerjError::invalid_mapping(format!(+ return Err(reject(format!("index name must not contain '..' (got '{name}')")));}