diff --git a/meilisearch-http/src/data/mod.rs b/meilisearch-http/src/data/mod.rs index ed5ce4952..fee29561a 100644 --- a/meilisearch-http/src/data/mod.rs +++ b/meilisearch-http/src/data/mod.rs @@ -8,6 +8,7 @@ use std::ops::Deref; use std::sync::Arc; use sha2::Digest; +use anyhow::bail; use crate::index_controller::{IndexController, LocalIndexController, IndexMetadata, Settings, IndexSettings}; use crate::option::Opt; @@ -126,6 +127,9 @@ impl Data { } pub fn create_index(&self, name: impl AsRef, primary_key: Option>) -> anyhow::Result { + if !is_index_uid_valid(name.as_ref()) { + bail!("invalid index uid: {:?}", name.as_ref()) + } let settings = IndexSettings { name: Some(name.as_ref().to_string()), primary_key: primary_key.map(|s| s.as_ref().to_string()), @@ -145,3 +149,8 @@ impl Data { &self.api_keys } } + +fn is_index_uid_valid(uid: &str) -> bool { + uid.chars().all(|x| x.is_ascii_alphanumeric() || x == '-' || x == '_') +} + diff --git a/meilisearch-http/src/data/updates.rs b/meilisearch-http/src/data/updates.rs index fbb9be801..a784dce99 100644 --- a/meilisearch-http/src/data/updates.rs +++ b/meilisearch-http/src/data/updates.rs @@ -1,13 +1,13 @@ use std::ops::Deref; +use anyhow::bail; use async_compression::tokio_02::write::GzipEncoder; use futures_util::stream::StreamExt; use milli::update::{IndexDocumentsMethod, UpdateFormat}; use tokio::io::AsyncWriteExt; -use crate::index_controller::UpdateStatus; -use crate::index_controller::{IndexController, Settings, IndexSettings, IndexMetadata}; -use super::Data; +use super::{Data, is_index_uid_valid}; +use crate::index_controller::{UpdateStatus, IndexController, Settings, IndexSettings, IndexMetadata}; impl Data { pub async fn add_documents( @@ -22,6 +22,10 @@ impl Data { B: Deref, E: std::error::Error + Send + Sync + 'static, { + if !is_index_uid_valid(index.as_ref()) { + bail!("invalid index uid: {:?}", index.as_ref()) + } + let file = tokio::task::spawn_blocking(tempfile::tempfile).await?; let file = tokio::fs::File::from_std(file?); let mut encoder = GzipEncoder::new(file); @@ -57,6 +61,9 @@ impl Data { index: impl AsRef + Send + Sync + 'static, settings: Settings ) -> anyhow::Result { + if !is_index_uid_valid(index.as_ref()) { + bail!("invalid index uid: {:?}", index.as_ref()) + } let index_controller = self.index_controller.clone(); let update = tokio::task::spawn_blocking(move || index_controller.update_settings(index, settings)).await??; Ok(update.into())