From 93fd7757dc4900da697b9d5e884e1a343e2facda Mon Sep 17 00:00:00 2001 From: Onur Gündüz Date: Tue, 29 Sep 2026 13:54:48 +0200 Subject: Bound filter parsing and reject excessive expression depth --- src/ast.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src/ast.rs') diff --git a/src/ast.rs b/src/ast.rs index 54128d8..7612c35 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -104,3 +104,33 @@ pub enum Expr { args: Vec, }, } + +impl Expr { + pub(crate) fn check_limits(&self) -> Result<(), String> { + let mut pending = vec![(self, 1)]; + let mut count = 0; + while let Some((expr, depth)) = pending.pop() { + count += 1; + if depth > 128 || count > 512 { + return Err("filter exceeds expression limit (128 levels, 512 nodes)".into()); + } + match expr { + Expr::Binary { left, right, .. } | Expr::Comparison { left, right, .. } => { + pending.push((left, depth + 1)); + pending.push((right, depth + 1)); + } + Expr::Unary { expr, .. } => pending.push((expr, depth + 1)), + Expr::Member { holder, .. } => pending.push((holder, depth + 1)), + Expr::Function { func, args } => { + if args.len() > 512 { + return Err("filter exceeds expression limit (512 nodes)".into()); + } + pending.push((func, depth + 1)); + pending.extend(args.iter().map(|arg| (arg, depth + 1))); + } + Expr::Name { .. } => {} + } + } + Ok(()) + } +} -- cgit v1.3.1