From 231dbb2e27c941b4b41f0ac652002613daacf625 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Thu, 19 Sep 2024 10:11:56 +0200 Subject: [PATCH 1/2] Error in case an operator is used as a delimiter --- askama_derive/Cargo.toml | 2 +- askama_derive/src/config.rs | 3 +++ askama_parser/Cargo.toml | 2 +- askama_parser/src/expr.rs | 4 ++++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/askama_derive/Cargo.toml b/askama_derive/Cargo.toml index 4a676443..b1c3981f 100644 --- a/askama_derive/Cargo.toml +++ b/askama_derive/Cargo.toml @@ -25,7 +25,7 @@ with-rocket = [] with-warp = [] [dependencies] -parser = { package = "askama_parser", version = "0.3", path = "../askama_parser" } +parser = { package = "askama_parser", version = "0.3.1", path = "../askama_parser" } mime = "0.3" mime_guess = "2" proc-macro2 = "1" diff --git a/askama_derive/src/config.rs b/askama_derive/src/config.rs index 599d5a24..285ddd87 100644 --- a/askama_derive/src/config.rs +++ b/askama_derive/src/config.rs @@ -7,6 +7,7 @@ use std::{env, fs}; use serde::Deserialize; use crate::{CompileError, CRATE}; +use parser::expr::TWO_PLUS_CHAR_OPS; use parser::node::Whitespace; use parser::Syntax; @@ -161,6 +162,8 @@ impl<'a> TryInto> for RawSyntax<'a> { ); } else if s.chars().any(|c| c.is_whitespace()) { return Err(format!("delimiters may not contain white spaces: {s:?}").into()); + } else if TWO_PLUS_CHAR_OPS.contains(&s) { + return Err(format!("delimiters may not contain operators: {s:?}").into()); } } diff --git a/askama_parser/Cargo.toml b/askama_parser/Cargo.toml index 789a41fc..92727a91 100644 --- a/askama_parser/Cargo.toml +++ b/askama_parser/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "askama_parser" -version = "0.3.0" +version = "0.3.1" description = "Parser for Askama templates" documentation = "https://docs.rs/askama" keywords = ["markup", "template", "jinja2", "html"] diff --git a/askama_parser/src/expr.rs b/askama_parser/src/expr.rs index 16069b2d..4ea4fe60 100644 --- a/askama_parser/src/expr.rs +++ b/askama_parser/src/expr.rs @@ -172,6 +172,7 @@ impl<'a> Expr<'a> { ))(i) } + // Keep in sync with `TWO_PLUS_CHAR_OPS`, below expr_prec_layer!(or, and, "||"); expr_prec_layer!(and, compare, "&&"); expr_prec_layer!(compare, bor, "==", "!=", ">=", ">", "<=", "<"); @@ -430,3 +431,6 @@ impl<'a> Suffix<'a> { map(preceded(take_till(not_ws), char('?')), |_| Self::Try)(i) } } + +pub const TWO_PLUS_CHAR_OPS: &[&str] = + &["||", "&&", "==", "!=", ">=", "<=", "<<", ">>", "..", "..="]; From 0c9351c0a05cbb99dcd14887286498f3cae594dc Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Thu, 19 Sep 2024 10:18:16 +0200 Subject: [PATCH 2/2] derive: make syntax element errors more specific --- askama_derive/src/config.rs | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/askama_derive/src/config.rs b/askama_derive/src/config.rs index 285ddd87..19358371 100644 --- a/askama_derive/src/config.rs +++ b/askama_derive/src/config.rs @@ -148,22 +148,23 @@ impl<'a> TryInto> for RawSyntax<'a> { comment_end: self.comment_end.unwrap_or(default.comment_end), }; - for s in [ - syntax.block_start, - syntax.block_end, - syntax.expr_start, - syntax.expr_end, - syntax.comment_start, - syntax.comment_end, + for (s, kind) in [ + (syntax.block_start, "block_start"), + (syntax.block_end, "block_end"), + (syntax.expr_start, "expr_start"), + (syntax.expr_end, "expr_end"), + (syntax.comment_start, "comment_start"), + (syntax.comment_end, "comment_end"), ] { if s.len() < 2 { - return Err( - format!("delimiters must be at least two characters long: {s:?}").into(), - ); + return Err(format!( + "{kind} delimiter must be at least two characters long: {s:?}" + ) + .into()); } else if s.chars().any(|c| c.is_whitespace()) { - return Err(format!("delimiters may not contain white spaces: {s:?}").into()); + return Err(format!("{kind} delimiter may not contain whitespace: {s:?}").into()); } else if TWO_PLUS_CHAR_OPS.contains(&s) { - return Err(format!("delimiters may not contain operators: {s:?}").into()); + return Err(format!("{kind} delimiter may not contain operators: {s:?}").into()); } } @@ -489,7 +490,7 @@ mod tests { let config = Config::new(raw_config, None); assert_eq!( config.unwrap_err().msg, - r#"delimiters must be at least two characters long: "<""#, + r#"block_start delimiter must be at least two characters long: "<""#, ); let raw_config = r#" @@ -500,7 +501,7 @@ mod tests { let config = Config::new(raw_config, None); assert_eq!( config.unwrap_err().msg, - r#"delimiters may not contain white spaces: " {{ ""#, + r#"block_start delimiter may not contain whitespace: " {{ ""#, ); let raw_config = r#"