Skip to content

Commit

Permalink
url decode the search index pattern (#4292)
Browse files Browse the repository at this point in the history
* url decode the search index pattern

* use percent_decode crate

* tiny fix

* refine error message

* remove duplicated decode

* code format

* Remove unwrap in extract_index_id_patterns

---------

Co-authored-by: fmassot <[email protected]>
  • Loading branch information
tuziben and fmassot authored Dec 20, 2023
1 parent 9bcbf39 commit fbe8dab
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
1 change: 1 addition & 0 deletions quickwit/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions quickwit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ openssl-probe = "0.1.5"
opentelemetry = { version = "0.19", features = ["rt-tokio"] }
opentelemetry-otlp = "0.12.0"
ouroboros = "0.18.0"
percent-encoding = "2.3.1"
pin-project = "1.1.0"
pnet = { version = "0.33.0", features = ["std"] }
postcard = { version = "1.0.4", features = [
Expand Down
1 change: 1 addition & 0 deletions quickwit/quickwit-serve/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ itertools = { workspace = true }
mime_guess = { workspace = true }
num_cpus = { workspace = true }
once_cell = { workspace = true }
percent-encoding = { workspace = true }
regex = { workspace = true }
rust-embed = { workspace = true }
serde = { workspace = true }
Expand Down
30 changes: 14 additions & 16 deletions quickwit/quickwit-serve/src/search_api/rest_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
use std::convert::TryFrom;
use std::sync::Arc;

use anyhow::Context;
use futures::stream::StreamExt;
use hyper::header::HeaderValue;
use hyper::HeaderMap;
use once_cell::sync::Lazy;
use percent_encoding::percent_decode_str;
use quickwit_config::validate_index_id_pattern;
use quickwit_proto::search::{CountHits, OutputFormat, SortField, SortOrder};
use quickwit_proto::ServiceError;
use quickwit_query::query_ast::query_ast_from_user_text;
use quickwit_search::{SearchError, SearchResponseRest, SearchService};
use regex::Regex;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use serde_json::Value as JsonValue;
use tracing::info;
Expand All @@ -56,20 +56,18 @@ use crate::{with_arg, BodyFormat};
)]
pub struct SearchApi;

// Matches index patterns separated by commas or its URL encoded version '%2C'.
static COMMA_SEPARATED_INDEX_PATTERNS_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r",|%2C").expect("the regular expression should compile"));

pub(crate) async fn extract_index_id_patterns(
comma_separated_index_patterns: String,
) -> Result<Vec<String>, Rejection> {
let index_pattern = percent_decode_str(&comma_separated_index_patterns)
.decode_utf8()
.context("index pattern does not contain valid utf8 characters")
.map_err(|error| crate::rest::InvalidArgument(error.to_string()))?;

let mut index_ids_patterns = Vec::new();
for index_id_pattern in
COMMA_SEPARATED_INDEX_PATTERNS_REGEX.split(&comma_separated_index_patterns)
{
validate_index_id_pattern(index_id_pattern).map_err(|error| {
warp::reject::custom(crate::rest::InvalidArgument(error.to_string()))
})?;
for index_id_pattern in index_pattern.split(',').collect::<Vec<_>>() {
validate_index_id_pattern(index_id_pattern)
.map_err(|error| crate::rest::InvalidArgument(error.to_string()))?;
index_ids_patterns.push(index_id_pattern.to_string());
}
assert!(!index_ids_patterns.is_empty());
Expand Down Expand Up @@ -538,16 +536,16 @@ mod tests {
.await
.unwrap();
assert_eq!(
extract_index_id_patterns("my-index-1,my-index-2".to_string())
extract_index_id_patterns("my-index-1,my-index-2%2A".to_string())
.await
.unwrap(),
vec!["my-index-1".to_string(), "my-index-2".to_string()]
vec!["my-index-1".to_string(), "my-index-2*".to_string()]
);
assert_eq!(
extract_index_id_patterns("my-index-1%2Cmy-index-2".to_string())
extract_index_id_patterns("my-index-1%2Cmy-index-%2A".to_string())
.await
.unwrap(),
vec!["my-index-1".to_string(), "my-index-2".to_string()]
vec!["my-index-1".to_string(), "my-index-*".to_string()]
);
extract_index_id_patterns("".to_string()).await.unwrap_err();
extract_index_id_patterns(" ".to_string())
Expand Down

0 comments on commit fbe8dab

Please sign in to comment.