From 231dbb2e27c941b4b41f0ac652002613daacf625 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Thu, 19 Sep 2024 10:11:56 +0200 Subject: [PATCH] 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] = + &["||", "&&", "==", "!=", ">=", "<=", "<<", ">>", "..", "..="];