Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
Check the filter max depth limit and reject the invalid ones
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerollmops committed Dec 7, 2021
1 parent 49c2db9 commit 90f49ea
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions milli/src/search/facet/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ use crate::heed_codec::facet::{
};
use crate::{distance_between_two_points, CboRoaringBitmapCodec, FieldId, Index, Result};

/// The maximum number of filters the filter AST can process.
const MAX_FILTER_DEPTH: usize = 1000;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Filter<'a> {
condition: FilterCondition<'a>,
Expand All @@ -27,6 +30,7 @@ enum FilterError<'a> {
BadGeoLat(f64),
BadGeoLng(f64),
Reserved(&'a str),
TooDeep,
InternalError,
}
impl<'a> std::error::Error for FilterError<'a> {}
Expand All @@ -40,6 +44,10 @@ impl<'a> Display for FilterError<'a> {
attribute,
filterable,
),
Self::TooDeep => write!(f,
"Too many filter conditions, can't process more than {} filters.",
MAX_FILTER_DEPTH
),
Self::Reserved(keyword) => write!(
f,
"`{}` is a reserved keyword and thus can't be used as a filter expression.",
Expand Down Expand Up @@ -108,6 +116,10 @@ impl<'a> Filter<'a> {
}
}

if let Some(token) = ands.as_ref().and_then(|fc| fc.token_at_depth(MAX_FILTER_DEPTH)) {
return Err(token.as_external_error(FilterError::TooDeep).into());
}

Ok(ands.map(|ands| Self { condition: ands }))
}

Expand Down

0 comments on commit 90f49ea

Please sign in to comment.