use crate::error::{Failure, Result};
use sqlparser::ast::Statement;
use sqlparser::dialect::MySqlDialect;
use sqlparser::parser::Parser;
pub const DEFAULT_MAX_STATEMENT_BYTES: usize = 1 << 20;
pub const DEFAULT_PARSER_STACK_BYTES: usize = 64 << 20;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ParseLimits {
max_statement_bytes: usize,
parser_stack_bytes: usize,
}
impl Default for ParseLimits {
fn default() -> Self {
Self {
max_statement_bytes: DEFAULT_MAX_STATEMENT_BYTES,
parser_stack_bytes: DEFAULT_PARSER_STACK_BYTES,
}
}
}
impl ParseLimits {
#[must_use]
pub const fn new(max_statement_bytes: usize, parser_stack_bytes: usize) -> Self {
Self {
max_statement_bytes: if max_statement_bytes == 0 {
DEFAULT_MAX_STATEMENT_BYTES
} else {
max_statement_bytes
},
parser_stack_bytes: if parser_stack_bytes == 0 {
DEFAULT_PARSER_STACK_BYTES
} else {
parser_stack_bytes
},
}
}
#[must_use]
pub const fn max_statement_bytes(self) -> usize {
self.max_statement_bytes
}
#[must_use]
pub const fn parser_stack_bytes(self) -> usize {
self.parser_stack_bytes
}
}
pub fn parse(sql: &str, limits: ParseLimits) -> Result<Vec<Statement>> {
let ceiling = limits.max_statement_bytes();
if sql.len() > ceiling {
return Err(Failure::validation(
"sql.too_large",
format!(
"the statement has {} bytes and the ceiling is {ceiling}",
sql.len()
),
)
.with_detail("detail", format!("{} > {ceiling}", sql.len()))
.with_hint("split the statement or raise the statement size ceiling"));
}
parse_on_dedicated_stack(crate::sql::strip::expand_conditional_comments(sql), limits)
}
fn parse_on_dedicated_stack(sql: String, limits: ParseLimits) -> Result<Vec<Statement>> {
let worker = std::thread::Builder::new()
.name("sql-parse".to_owned())
.stack_size(limits.parser_stack_bytes())
.spawn(move || Parser::parse_sql(&MySqlDialect {}, &sql))
.map_err(|cause| {
Failure::validation("sql.parser_unavailable", "the parser could not be started")
.with_cause(cause.to_string())
})?;
let parsed = worker.join().map_err(|_| {
Failure::validation(
"sql.too_deep",
"the statement is nested too deeply to be read",
)
.with_hint("flatten the statement or raise the parser stack ceiling")
})?;
parsed.map_err(|cause| {
Failure::validation("sql.unparsable", "the statement could not be read")
.with_hint("check the statement against the server manual")
.with_cause(cause.to_string())
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_readable_statement_produces_one_tree() {
let parsed = parse("SELECT 1", ParseLimits::default()).expect("readable");
assert_eq!(parsed.len(), 1);
}
#[test]
fn every_statement_in_the_text_reaches_the_caller() {
let parsed = parse("SELECT 1; SELECT 2", ParseLimits::default()).expect("readable");
assert_eq!(
parsed.len(),
2,
"counting them is what lets the guard refuse them"
);
}
#[test]
fn an_unreadable_statement_is_refused() {
let failure =
parse("SELECT FROM WHERE", ParseLimits::default()).expect_err("unreadable text");
assert_eq!(failure.code(), "sql.unparsable");
assert!(!failure.causes().is_empty(), "the parser reason is kept");
}
#[test]
fn a_statement_over_the_size_ceiling_is_refused_before_the_parser_runs() {
let limits = ParseLimits::new(16, DEFAULT_PARSER_STACK_BYTES);
let failure = parse("SELECT 1 FROM a_very_long_table_name", limits)
.expect_err("the ceiling is enforced");
assert_eq!(failure.code(), "sql.too_large");
}
#[test]
fn a_statement_within_the_size_ceiling_is_accepted() {
let limits = ParseLimits::new(64, DEFAULT_PARSER_STACK_BYTES);
assert_eq!(
parse("SELECT 1", limits).expect("within the ceiling").len(),
1
);
}
#[test]
fn the_size_ceiling_counts_bytes_and_not_characters() {
let sql = "SELECT 'ááááááááá'";
assert!(sql.chars().count() < sql.len(), "the text is multi-byte");
let limits = ParseLimits::new(sql.chars().count(), DEFAULT_PARSER_STACK_BYTES);
assert_eq!(
parse(sql, limits).expect_err("measured in bytes").code(),
"sql.too_large"
);
}
#[test]
fn deep_nesting_ends_in_a_refusal_and_not_in_a_dead_process() {
let depth = 2_000;
let sql = format!("SELECT {}1{}", "(".repeat(depth), ")".repeat(depth));
match parse(&sql, ParseLimits::default()) {
Ok(parsed) => assert_eq!(parsed.len(), 1),
Err(failure) => assert!(
matches!(
failure.code(),
"sql.too_deep" | "sql.unparsable" | "sql.too_large"
),
"unexpected code {}",
failure.code()
),
}
}
#[test]
fn a_zero_bound_falls_back_to_the_default_instead_of_refusing_everything() {
let limits = ParseLimits::new(0, 0);
assert_eq!(limits.max_statement_bytes(), DEFAULT_MAX_STATEMENT_BYTES);
assert_eq!(limits.parser_stack_bytes(), DEFAULT_PARSER_STACK_BYTES);
assert!(parse("SELECT 1", limits).is_ok());
}
#[test]
fn the_default_bounds_match_the_published_constants() {
let limits = ParseLimits::default();
assert_eq!(limits.max_statement_bytes(), DEFAULT_MAX_STATEMENT_BYTES);
assert_eq!(limits.parser_stack_bytes(), DEFAULT_PARSER_STACK_BYTES);
}
}