diff --git a/docs/src/js/interfaces/IndexOptions.md b/docs/src/js/interfaces/IndexOptions.md index e0990fc8d..f864c5135 100644 --- a/docs/src/js/interfaces/IndexOptions.md +++ b/docs/src/js/interfaces/IndexOptions.md @@ -34,7 +34,14 @@ optional name: string; Optional custom name for the index. +Custom names must contain 1–128 ASCII letters, digits, dashes, underscores, +or periods, excluding `.` and `..`. + If not provided, a default name will be generated based on the column name. +Existing and generated names remain addressable through index operations, +except that remote tables reject empty names, `.` and `..` to prevent URL +path normalization. Existing `.` or `..` indices on managed tables require +server-side cleanup, or a native connection with direct dataset access. *** diff --git a/nodejs/lancedb/indices.ts b/nodejs/lancedb/indices.ts index 53cbf8a4d..b7d9d2437 100644 --- a/nodejs/lancedb/indices.ts +++ b/nodejs/lancedb/indices.ts @@ -949,7 +949,14 @@ export interface IndexOptions { /** * Optional custom name for the index. * + * Custom names must contain 1–128 ASCII letters, digits, dashes, underscores, + * or periods, excluding `.` and `..`. + * * If not provided, a default name will be generated based on the column name. + * Existing and generated names remain addressable through index operations, + * except that remote tables reject empty names, `.` and `..` to prevent URL + * path normalization. Existing `.` or `..` indices on managed tables require + * server-side cleanup, or a native connection with direct dataset access. */ name?: string; diff --git a/python/python/lancedb/table.py b/python/python/lancedb/table.py index 3161eda30..904f82936 100644 --- a/python/python/lancedb/table.py +++ b/python/python/lancedb/table.py @@ -1917,7 +1917,9 @@ class Table(ABC): LanceDB Enterprise only: how long to wait for the index build to finish. If not set, the call returns once the build is scheduled. name : str, optional - Custom name for the index. + Custom name for the index: 1–128 ASCII letters, digits, dashes, + underscores, or periods, excluding ``.`` and ``..``. If omitted, + a default name is generated from the column name. train : bool, default True Whether to train the index with existing data. @@ -2024,6 +2026,11 @@ class Table(ABC): Use [list_indices][lancedb.table.Table.list_indices] to find the names of the indices. + + Remote tables reject empty names, ``.`` and ``..`` to prevent URL path + normalization. Existing indices named ``.`` or ``..`` require server-side + cleanup on managed tables, or a native connection with direct access to + the dataset. Other existing and generated names remain addressable. """ raise NotImplementedError("drop_index is not supported for this table type") @@ -2076,6 +2083,8 @@ class Table(ABC): The timeout to wait if indexing is asynchronous. name: str, optional The name of the index. If not provided, a default name will be generated. + Custom names must contain 1–128 ASCII letters, digits, dashes, + underscores, or periods, excluding ``.`` and ``..``. Examples -------- @@ -2234,6 +2243,8 @@ class Table(ABC): The timeout to wait if indexing is asynchronous. name: str, optional The name of the index. If not provided, a default name will be generated. + Custom names must contain 1–128 ASCII letters, digits, dashes, + underscores, or periods, excluding ``.`` and ``..``. Notes ----- @@ -4396,7 +4407,9 @@ class LanceTable(Table): wait_timeout : timedelta, optional Timeout to wait for async indexing to complete. name : str, optional - Custom name for the index. + Custom name for the index: 1–128 ASCII letters, digits, dashes, + underscores, or periods, excluding ``.`` and ``..``. If omitted, + a default name is generated from the column name. train : bool, default True Whether to train the index with existing data. @@ -6832,6 +6845,8 @@ class AsyncTable: If not set, the call returns once the build is scheduled. name: str, optional The name of the index. If not provided, a default name will be generated. + Custom names must contain 1–128 ASCII letters, digits, dashes, + underscores, or periods, excluding ``.`` and ``..``. train: bool, default True Whether to train the index with existing data. Vector indices always train with existing data. @@ -6965,6 +6980,11 @@ class AsyncTable: Use [list_indices][lancedb.table.AsyncTable.list_indices] to find the names of the indices. + + Remote tables reject empty names, ``.`` and ``..`` to prevent URL path + normalization. Existing indices named ``.`` or ``..`` require server-side + cleanup on managed tables, or a native connection with direct access to + the dataset. Other existing and generated names remain addressable. """ await self._inner.drop_index(name) diff --git a/rust/lancedb/src/remote/table.rs b/rust/lancedb/src/remote/table.rs index ae09fb7f9..50c1c7e26 100644 --- a/rust/lancedb/src/remote/table.rs +++ b/rust/lancedb/src/remote/table.rs @@ -83,6 +83,7 @@ use lance_core::datatypes::Schema as LanceSchema; use lance_datafusion::exec::{OneShotExec, execute_plan}; use reqwest::{RequestBuilder, Response}; use serde::{Deserialize, Serialize}; +use std::borrow::Cow; use std::collections::{HashMap, HashSet}; use std::io::Cursor; use std::pin::Pin; @@ -97,6 +98,18 @@ const MIN_READ_VERSION_HEADER: HeaderName = HeaderName::from_static("x-lancedb-m const VERSION_HEADER: HeaderName = HeaderName::from_static("x-lancedb-version"); const QUERY_SNAPSHOT_HEADER: &str = "x-lancedb-query-snapshot"; +/// Encode an existing or server-generated name without applying custom creation rules. +fn encode_index_name(name: &str) -> Result> { + // Percent-encoding preserves dot segments, which URL parsing would normalize + // into another route. All other name characters are safe after encoding. + if matches!(name, "" | "." | "..") { + return Err(Error::InvalidInput { + message: format!("Illegal index name '{name}' in remote route."), + }); + } + Ok(urlencoding::encode(name)) +} + fn valid_query_snapshot_identity(value: &str) -> bool { value.strip_prefix("v1:").is_some_and(|digest| { digest.len() == 64 @@ -2421,11 +2434,8 @@ impl RemoteTable { index_name: &str, read_snapshot: ReadSnapshot, ) -> Result> { - // Percent-encoding alone leaves dot segments intact, and URL parsing - // would normalize them into a different route. This also guards the - // stats requests made while listing indices on legacy servers. - validate_index_name(index_name)?; - let encoded_name = urlencoding::encode(index_name); + // This also guards the stats requests made while listing indices on legacy servers. + let encoded_name = encode_index_name(index_name)?; let mut body = serde_json::json!({ "version": read_snapshot.version }); self.apply_branch_body(&mut body); let request = self @@ -4519,8 +4529,7 @@ impl BaseTable for RemoteTable { } async fn drop_index(&self, index_name: &str) -> Result<()> { - validate_index_name(index_name)?; - let encoded_name = urlencoding::encode(index_name); + let encoded_name = encode_index_name(index_name)?; let request = self.apply_branch_query(self.client.post(&format!( "/v1/table/{}/index/{encoded_name}/drop/", self.identifier @@ -4536,8 +4545,7 @@ impl BaseTable for RemoteTable { } async fn prewarm_index(&self, index_name: &str) -> Result<()> { - validate_index_name(index_name)?; - let encoded_name = urlencoding::encode(index_name); + let encoded_name = encode_index_name(index_name)?; let request = self.client.post(&format!( "/v1/table/{}/index/{encoded_name}/prewarm/", self.identifier @@ -12997,7 +13005,92 @@ mod tests { } #[tokio::test] - async fn test_invalid_index_names_never_send_requests() { + async fn test_generated_index_name_remains_addressable() { + let conn = crate::connect("memory://").execute().await.unwrap(); + let batch = record_batch!(("café", Int32, [1])).unwrap(); + let schema = batch.schema(); + let native = conn + .create_table("my_table", batch) + .execute() + .await + .unwrap(); + native + .create_index(&["café"], Index::Auto) + .execute() + .await + .unwrap(); + let generated_name = native.list_indices().await.unwrap()[0].name.clone(); + assert_eq!(generated_name, "café_idx"); + + for enriched in [true, false] { + let schema = schema.clone(); + let table = Table::new_with_handler("my_table", move |request| { + let body = match request.url().path() { + "/v1/table/my_table/describe/" => describe_response(&schema), + "/v1/table/my_table/create_index/" => { + let body: serde_json::Value = + serde_json::from_slice(request.body().unwrap().as_bytes().unwrap()) + .unwrap(); + assert!(body.get("name").is_none()); + "{}".to_string() + } + "/v1/table/my_table/index/list/" => { + let mut entry = json!({"index_name": "café_idx", "columns": ["café"]}); + if enriched { + entry["index_type"] = json!("BTREE"); + } + json!({"indexes": [entry]}).to_string() + } + "/v1/table/my_table/index/caf%C3%A9_idx/stats/" => json!({ + "num_indexed_rows": 1, + "num_unindexed_rows": 0, + "index_type": "BTREE" + }) + .to_string(), + "/v1/table/my_table/index/caf%C3%A9_idx/drop/" + | "/v1/table/my_table/index/caf%C3%A9_idx/prewarm/" => "{}".to_string(), + path => panic!("Unexpected request: {path}"), + }; + http::Response::builder().status(200).body(body).unwrap() + }); + table + .create_index(&["café"], Index::Auto) + .execute() + .await + .unwrap(); + assert_eq!(table.list_indices().await.unwrap()[0].name, generated_name); + assert!(table.index_stats(&generated_name).await.unwrap().is_some()); + table.prewarm_index(&generated_name).await.unwrap(); + table + .wait_for_index(&[&generated_name], Duration::from_secs(1)) + .await + .unwrap(); + table.drop_index(&generated_name).await.unwrap(); + } + } + + #[tokio::test] + async fn test_unsafe_index_paths_never_send_requests() { + let table = Table::new_with_handler("my_table", |request| -> http::Response { + panic!("Unsafe index name sent a request to {}", request.url()); + }); + + for name in ["..", ".", ""] { + for result in [ + table.drop_index(name).await, + table.prewarm_index(name).await, + table.index_stats(name).await.map(|_| ()), + ] { + assert!( + matches!(result, Err(Error::InvalidInput { .. })), + "Expected invalid input for {name:?}, got {result:?}" + ); + } + } + } + + #[tokio::test] + async fn test_invalid_custom_index_names_never_send_requests() { let table = Table::new_with_handler("my_table", |request| -> http::Response { panic!("Invalid index name sent a request to {}", request.url()); }); @@ -13010,11 +13103,9 @@ mod tests { "my\\index", "%2e%2e", "my/index?a#b c", + "café_idx", ] { for result in [ - table.drop_index(name).await, - table.prewarm_index(name).await, - table.index_stats(name).await.map(|_| ()), table .create_index(&["a"], Index::BTree(Default::default())) .name(name.to_string()) @@ -13066,37 +13157,41 @@ mod tests { } } + #[rstest] + #[case("my_index-1..v2", "my_index-1..v2")] + #[case("café_idx", "caf%C3%A9_idx")] + #[case("my/index", "my%2Findex")] + #[case("my\\index", "my%5Cindex")] + #[case("%2e%2e", "%252e%252e")] + #[case("my/index?a#b c", "my%2Findex%3Fa%23b%20c")] #[tokio::test] - async fn test_per_index_paths_preserve_valid_names() { - const NAME: &str = "my_index-1..v2"; - const PREFIX: &str = "/v1/table/my_table/index/my_index-1..v2"; - - let table = Table::new_with_handler("my_table", |request| { - assert_eq!(request.url().path(), format!("{PREFIX}/stats/")); - let body = serde_json::json!({ - "num_indexed_rows": 1, - "num_unindexed_rows": 0, - "index_type": "IVF_PQ", - "distance_type": "l2" - }); + async fn test_per_index_paths_preserve_existing_names( + #[case] name: &str, + #[case] encoded_name: &str, + ) { + let prefix = format!("/v1/table/my_table/index/{encoded_name}"); + let table = Table::new_with_handler("my_table", move |request| { + let path = request.url().path(); + let suffix = path + .strip_prefix(&prefix) + .unwrap_or_else(|| panic!("Unexpected index route: {path}")); + let body = match suffix { + "/stats/" => json!({ + "num_indexed_rows": 1, + "num_unindexed_rows": 0, + "index_type": "BTREE" + }), + "/drop/" | "/prewarm/" => json!({}), + _ => panic!("Unexpected index route: {path}"), + }; http::Response::builder() .status(200) - .body(serde_json::to_string(&body).unwrap()) + .body(body.to_string()) .unwrap() }); - assert!(table.index_stats(NAME).await.unwrap().is_some()); - - let table = Table::new_with_handler("my_table", |request| { - assert_eq!(request.url().path(), format!("{PREFIX}/drop/")); - http::Response::builder().status(200).body("{}").unwrap() - }); - table.drop_index(NAME).await.unwrap(); - - let table = Table::new_with_handler("my_table", |request| { - assert_eq!(request.url().path(), format!("{PREFIX}/prewarm/")); - http::Response::builder().status(200).body("{}").unwrap() - }); - table.prewarm_index(NAME).await.unwrap(); + assert!(table.index_stats(name).await.unwrap().is_some()); + table.prewarm_index(name).await.unwrap(); + table.drop_index(name).await.unwrap(); } #[tokio::test] diff --git a/rust/lancedb/src/table.rs b/rust/lancedb/src/table.rs index 8a254efaf..9bbef2469 100644 --- a/rust/lancedb/src/table.rs +++ b/rust/lancedb/src/table.rs @@ -2915,7 +2915,7 @@ impl Table { /// Get statistics about an index. /// Returns None if the index does not exist. - /// Remote tables require a name valid for [`IndexBuilder::name`]. + /// Remote tables reject empty names, `.` and `..` to prevent URL normalization. pub async fn index_stats( &self, index_name: impl AsRef, @@ -2925,10 +2925,12 @@ impl Table { /// Drop an index from the table. /// - /// Remote tables reject names that are not valid for [`IndexBuilder::name`]. - /// Legacy indices with such names can still be dropped through a native + /// Remote tables reject empty names, `.` and `..` to prevent URL normalization. + /// Legacy indices named `.` or `..` can still be dropped through a native /// connection with direct access to the dataset. On managed remote tables, - /// these indices require server-side cleanup. + /// these indices require server-side cleanup. Other existing and generated + /// names remain addressable, even when they do not meet the custom-name + /// rules in [`IndexBuilder::name`]. /// /// This does not delete the index from disk, it just removes it from the table. /// To delete the index, run [`Self::optimize()`] after dropping the index. @@ -2952,7 +2954,7 @@ impl Table { /// indices will silently ignore the request. /// /// Use [`Self::list_indices()`] to find the names of the indices. - /// Remote tables require a name valid for [`IndexBuilder::name`]. + /// Remote tables reject empty names, `.` and `..` to prevent URL normalization. pub async fn prewarm_index(&self, name: &str) -> Result<()> { self.inner.prewarm_index(name).await } diff --git a/rust/lancedb/src/utils/mod.rs b/rust/lancedb/src/utils/mod.rs index eae5e5dbf..6832ebf2b 100644 --- a/rust/lancedb/src/utils/mod.rs +++ b/rust/lancedb/src/utils/mod.rs @@ -139,7 +139,7 @@ pub fn validate_table_name(name: &str) -> Result<()> { Ok(()) } -/// Validate a custom index name before creation or use as a remote path segment. +/// Validate a custom index name before creation. pub(crate) fn validate_index_name(name: &str) -> Result<()> { validate_view_or_namespace_or_secret(name, "index name", MAX_INDEX_NAME_CHARS) }