Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(databases): bound SQL expression stack depth (#7617)
Wolf Mermelsteind85873a8d8a1
diff --git a/crates/database_sql/AGENTS.md b/crates/database_sql/AGENTS.mdindex bfcb4d21fa..ae75bd4d75 100644--- a/crates/database_sql/AGENTS.md+++ b/crates/database_sql/AGENTS.md@@ -51,6 +51,9 @@ uses the wasm build directly).## Gotchas+- Reject more than 64 parenthesis levels before parsing SQL, and keep pushed+ AND/OR/IN filters balanced so long statements serialize and drop safely.+- Keep the crate free of native-only dependencies (no `tokio`, no `sqlx`) —it must compile to `wasm32`; native-only tooling goes behind the `cli` feature.- The grammar is the contract: a statement this crate rejects must never reachdiff --git a/crates/database_sql/src/engine/test.rs b/crates/database_sql/src/engine/test.rsindex ad29d83a10..36bebee867 100644--- a/crates/database_sql/src/engine/test.rs+++ b/crates/database_sql/src/engine/test.rs@@ -2,6 +2,7 @@ use models_databases::position::Position;use models_databases::{DatabaseId, OptionId, RowId};use std::collections::HashMap;+mod limits;mod projection;mod transcripts;diff --git a/crates/database_sql/src/engine/test/limits.rs b/crates/database_sql/src/engine/test/limits.rsnew file mode 100644index 0000000000..ce7f7140f5--- /dev/null+++ b/crates/database_sql/src/engine/test/limits.rs@@ -0,0 +1,50 @@+use super::*;++fn finish(sql: &str) {+ assert!(sql.len() < models_databases::MAX_STATEMENT_LENGTH);+ let (mut engine, step) = Engine::start(&catalog(), sql).expect("a supported long query");+ serde_json::to_string(&step).expect("the request crosses the wire");+ let Step::Fetch(request) = step else {+ panic!("expected a row fetch");+ };+ let result = engine+ .feed_page(+ request.id,+ Page {+ rows: vec![],+ next: None,+ },+ )+ .expect("the query finishes");+ assert!(matches!(result, Step::Done(_)));+}++#[test]+fn long_flat_and_conditions_complete() {+ for condition in ["amount = 1", "stage = 'Won'"] {+ finish(&format!(+ "SELECT * FROM crm.deals WHERE {}",+ vec![condition; 10_000].join(" AND ")+ ));+ }+}++#[test]+fn long_flat_or_conditions_complete() {+ for condition in ["amount = 1", "stage = 'Won'"] {+ finish(&format!(+ "SELECT * FROM crm.deals WHERE {}",+ vec![condition; 10_000].join(" OR ")+ ));+ }+}++#[test]+fn long_in_lists_complete() {+ for (column, value) in [("amount", "1"), ("stage", "'Won'")] {+ finish(&format!(+ "SELECT * FROM crm.deals WHERE {column} IN ({})",+ vec![value; 10_000].join(",")+ ));+ }+}diff --git a/crates/database_sql/src/parse/parser.rs b/crates/database_sql/src/parse/parser.rsindex ed62a35fda..285f36c589 100644--- a/crates/database_sql/src/parse/parser.rs+++ b/crates/database_sql/src/parse/parser.rs@@ -48,11 +48,37 @@ impl nom::error::ParseError<Tokens<'_>> for ParseError {/// Parse one statement from its tokens; `end` is the length of the source.pub fn statement(tokens: &[Token], end: usize) -> Result<Statement, ParseError> {+ check_nesting(tokens)?;statement_rule(Tokens { tokens, end }).finish().map(|(_, statement)| statement)}+/// Bound condition recursion before entering nom, including unclosed conditions.+/// Quoted parentheses are part of a string or identifier token and do not count.+fn check_nesting(tokens: &[Token]) -> Result<(), ParseError> {+ const MAX_NESTING_DEPTH: usize = 64;+ let mut depth: usize = 0;+ for token in tokens {+ match token.kind {+ TokenKind::LeftParen => {+ depth += 1;+ if depth > MAX_NESTING_DEPTH {+ return Err(ParseError {+ span: token.span.clone(),+ message: format!(+ "SQL nesting exceeds {MAX_NESTING_DEPTH} levels; simplify the condition"+ ),+ });+ }+ }+ TokenKind::RightParen => depth = depth.saturating_sub(1),+ _ => {}+ }+ }+ Ok(())+}+fn statement_rule(input: Tokens<'_>) -> ParseResult<'_, Statement> {terminated(expecting(diff --git a/crates/database_sql/src/parse/test.rs b/crates/database_sql/src/parse/test.rsindex 691a2e415e..5558b6b1fb 100644--- a/crates/database_sql/src/parse/test.rs+++ b/crates/database_sql/src/parse/test.rs@@ -1,3 +1,5 @@+mod nesting;+use super::*;use models_databases::{ColumnKind as OpColumnKind, EntityKind};diff --git a/crates/database_sql/src/parse/test/nesting.rs b/crates/database_sql/src/parse/test/nesting.rsnew file mode 100644index 0000000000..4d1e9e8ea8--- /dev/null+++ b/crates/database_sql/src/parse/test/nesting.rs@@ -0,0 +1,64 @@+use crate::parse::parse;++#[test]+fn excessive_condition_nesting_returns_an_error() {+ let depth = 2_048;+ let sql = format!(+ "SELECT * FROM deals WHERE {}amount = 1{}",+ "(".repeat(depth),+ ")".repeat(depth)+ );+ let error = parse(&sql).expect_err("excessive nesting must be refused before parsing");+ assert!(error.message.contains("nesting"), "{error}");+}++#[test]+fn condition_nesting_limit_reports_the_first_excessive_parenthesis() {+ for prefix in [+ "SELECT * FROM deals WHERE ",+ "UPDATE deals SET amount = 2 WHERE ",+ "DELETE FROM deals WHERE ",+ ] {+ for closing in [")".repeat(65), String::new()] {+ let sql = format!("{prefix}{}amount = 1{closing}", "(".repeat(65));+ let error = parse(&sql).expect_err("the 65th nested condition is refused");+ assert_eq!(error.span, prefix.len() + 64..prefix.len() + 65);+ assert_eq!(+ error.message,+ "SQL nesting exceeds 64 levels; simplify the condition"+ );+ }+ }+}++#[test]+fn supported_nesting_and_quoted_parentheses_still_parse() {+ let nested = format!(+ "SELECT * FROM deals WHERE {}amount = 1{}",+ "(".repeat(64),+ ")".repeat(64)+ );+ assert!(parse(&nested).is_ok());+ let quoted = format!(+ "SELECT \"{}\" FROM deals WHERE name = '{}'",+ "(".repeat(2_048),+ ")".repeat(2_048)+ );+ assert!(parse("ed).is_ok());+ let flat = format!(+ "SELECT * FROM deals WHERE {}",+ vec!["(amount = 1)"; 2_048].join(" OR ")+ );+ assert!(parse(&flat).is_ok());+}++#[test]+fn repeated_not_keywords_are_refused_without_recursion() {+ for prefix in ["", "amount "] {+ let sql = format!(+ "SELECT * FROM deals WHERE {prefix}{}amount = 1",+ "NOT ".repeat(10_000)+ );+ assert!(parse(&sql).is_err());+ }+}diff --git a/crates/database_sql/src/split/pushdown.rs b/crates/database_sql/src/split/pushdown.rsindex 709ae57286..36037d70c7 100644--- a/crates/database_sql/src/split/pushdown.rs+++ b/crates/database_sql/src/split/pushdown.rs@@ -42,12 +42,9 @@ pub fn divide(pushable(filter, bindings, relations).filter(|(relation, _)| !left_joined.contains(relation))};- let mut pushed: Vec<Option<Expr<PropertiesLiteral>>> = vec![None; relations.len()];+ let mut pushed: Vec<Vec<Expr<PropertiesLiteral>>> = vec![Vec::new(); relations.len()];let mut push_into = |relation: usize, expr: Expr<PropertiesLiteral>| {- pushed[relation] = Some(match pushed[relation].take() {- Some(existing) => Expr::and(existing, expr),- None => expr,- });+ pushed[relation].push(expr);};let residual = match filter {Filter::And(parts) => {@@ -72,7 +69,13 @@ pub fn divide(None => Some(other),},};- (pushed, residual)+ (+ pushed+ .into_iter()+ .map(|parts| balanced(parts, Expr::and))+ .collect(),+ residual,+ )}/// The relation a whole filter tests and its `propf` form, or `None` if it@@ -112,33 +115,56 @@ fn push(filter: &Filter, bindings: &[Binding]) -> Option<Expr<PropertiesLiteral>column,values,negated: false,- } => values- .iter()- .map(|value| literal(*column, value, bindings))- .collect::<Option<Vec<_>>>()?- .into_iter()- .reduce(Expr::or),+ } => balanced(+ values+ .iter()+ .map(|value| literal(*column, value, bindings))+ .collect::<Option<Vec<_>>>()?,+ Expr::or,+ ),Filter::Has {column,value,negated: false,} => literal(*column, value, bindings),- Filter::And(parts) => parts- .iter()- .map(|part| push(part, bindings))- .collect::<Option<Vec<_>>>()?- .into_iter()- .reduce(Expr::and),- Filter::Or(parts) => parts- .iter()- .map(|part| push(part, bindings))- .collect::<Option<Vec<_>>>()?- .into_iter()- .reduce(Expr::or),+ Filter::And(parts) => balanced(+ parts+ .iter()+ .map(|part| push(part, bindings))+ .collect::<Option<Vec<_>>>()?,+ Expr::and,+ ),+ Filter::Or(parts) => balanced(+ parts+ .iter()+ .map(|part| push(part, bindings))+ .collect::<Option<Vec<_>>>()?,+ Expr::or,+ ),_ => None,}}+/// Pair adjacent terms in rounds so long associative filters have logarithmic+/// depth when cloned, serialized, or dropped.+fn balanced<T>(+ mut parts: Vec<Expr<T>>,+ combine: fn(Expr<T>, Expr<T>) -> Expr<T>,+) -> Option<Expr<T>> {+ while parts.len() > 1 {+ let mut terms = parts.into_iter();+ parts = std::iter::from_fn(|| {+ let left = terms.next()?;+ Some(match terms.next() {+ Some(right) => combine(left, right),+ None => left,+ })+ })+ .collect();+ }+ parts.pop()+}+/// A match on one option or one entity reference of the property behind a/// key.fn literal(diff --git a/docs/agent-guide/databases.md b/docs/agent-guide/databases.mdindex 11cb9a090a..dbe1b282e0 100644--- a/docs/agent-guide/databases.md+++ b/docs/agent-guide/databases.md@@ -702,6 +702,9 @@ instead of the engine's message. For development, start the web app withback the SQL toggle and editor, **View SQL**, the raw error under **Technicaldetails**, and the server's summary on query tool rows.+SQL conditions may nest parentheses up to 64 levels. Deeper input returns a+parse error asking the assistant to simplify the condition.+## Sharing and files**Share** opens Macro's standard sharing dialog. The owner can share with people