Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit nested filters #20

Merged
merged 3 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 9 additions & 19 deletions rinja_parser/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,27 +199,17 @@ impl<'a> Expr<'a> {
expr_prec_layer!(addsub, muldivmod, "+", "-");
expr_prec_layer!(muldivmod, filtered, "*", "/", "%");

fn filtered(i: &'a str, level: Level) -> ParseResult<'a, WithSpan<'a, Self>> {
let (_, level) = level.nest(i)?;
fn filtered(i: &'a str, mut level: Level) -> ParseResult<'a, WithSpan<'a, Self>> {
let start = i;
let (i, (obj, filters)) =
tuple((|i| Self::prefix(i, level), many0(|i| filter(i, level))))(i)?;

let mut res = obj;
for (fname, args) in filters {
res = WithSpan::new(
Self::Filter(Filter {
name: fname,
arguments: {
let mut args = args.unwrap_or_default();
args.insert(0, res);
args
},
}),
start,
);
}
let (mut i, mut res) = Self::prefix(i, level)?;
while let (j, Some((name, args))) = opt(|i| filter(i, &mut level))(i)? {
i = j;

let mut arguments = args.unwrap_or_else(|| Vec::with_capacity(1));
arguments.insert(0, res);

res = WithSpan::new(Self::Filter(Filter { name, arguments }), start);
}
Ok((i, res))
}

Expand Down
14 changes: 7 additions & 7 deletions rinja_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,13 +728,13 @@ impl Level {
}

#[allow(clippy::type_complexity)]
fn filter(i: &str, level: Level) -> ParseResult<'_, (&str, Option<Vec<WithSpan<'_, Expr<'_>>>>)> {
let (i, (_, fname, args)) = tuple((
char('|'),
ws(identifier),
opt(|i| Expr::arguments(i, level, false)),
))(i)?;
Ok((i, (fname, args)))
fn filter<'a>(
i: &'a str,
level: &mut Level,
) -> ParseResult<'a, (&'a str, Option<Vec<WithSpan<'a, Expr<'a>>>>)> {
let (i, _) = char('|')(i)?;
*level = level.nest(i)?.1;
pair(ws(identifier), opt(|i| Expr::arguments(i, *level, false)))(i)
}

/// Returns the common parts of two paths.
Expand Down
18 changes: 11 additions & 7 deletions rinja_parser/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ impl<'a> Node<'a> {
result.map(|(i, n)| (i, func(n)))
}

let start = i;
let (j, tag) = preceded(
|i| s.tag_block_start(i),
peek(preceded(
Expand Down Expand Up @@ -100,7 +101,7 @@ impl<'a> Node<'a> {
)))(i)?;
match closed {
true => Ok((i, node)),
false => Err(ErrorContext::unclosed("block", s.syntax.block_end, i).into()),
false => Err(ErrorContext::unclosed("block", s.syntax.block_end, start).into()),
}
}

Expand Down Expand Up @@ -137,6 +138,7 @@ impl<'a> Node<'a> {
}

fn expr(i: &'a str, s: &State<'_>) -> ParseResult<'a, Self> {
let start = i;
let (i, (pws, expr)) = preceded(
|i| s.tag_expr_start(i),
cut(pair(
Expand All @@ -151,7 +153,7 @@ impl<'a> Node<'a> {
))(i)?;
match closed {
true => Ok((i, Self::Expr(Ws(pws, nws), expr))),
false => Err(ErrorContext::unclosed("expression", s.syntax.expr_end, i).into()),
false => Err(ErrorContext::unclosed("expression", s.syntax.expr_end, start).into()),
}
}

Expand Down Expand Up @@ -640,16 +642,15 @@ pub struct FilterBlock<'a> {

impl<'a> FilterBlock<'a> {
fn parse(i: &'a str, s: &State<'_>) -> ParseResult<'a, WithSpan<'a, Self>> {
let mut level = s.level.get();
let start_s = i;
let mut start = tuple((
opt(Whitespace::parse),
ws(keyword("filter")),
cut(tuple((
ws(identifier),
opt(|i| Expr::arguments(i, s.level.get(), false)),
many0(|i| {
filter(i, s.level.get()).map(|(j, (name, params))| (j, (name, params, i)))
}),
many0(|i| filter(i, &mut level).map(|(j, (name, params))| (j, (name, params, i)))),
ws(|i| Ok((i, ()))),
opt(Whitespace::parse),
|i| s.tag_block_end(i),
Expand Down Expand Up @@ -1141,17 +1142,20 @@ impl<'a> Comment<'a> {
fn content<'a>(mut i: &'a str, s: &State<'_>) -> ParseResult<'a, ()> {
let mut depth = 0usize;
loop {
let start = i;
let (_, tag) = opt(skip_till(|i| tag(i, s)))(i)?;
let Some((j, tag)) = tag else {
return Err(ErrorContext::unclosed("comment", s.syntax.comment_end, i).into());
return Err(
ErrorContext::unclosed("comment", s.syntax.comment_end, start).into(),
);
};
match tag {
Tag::Open => match depth.checked_add(1) {
Some(new_depth) => depth = new_depth,
None => {
return Err(nom::Err::Failure(ErrorContext::new(
"too deeply nested comments",
i,
start,
)));
}
},
Expand Down
6 changes: 6 additions & 0 deletions rinja_parser/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,3 +1113,9 @@ fn let_set() {
.nodes(),
);
}

#[test]
fn fuzzed_filter_recursion() {
const TEMPLATE: &str = include_str!("../tests/filter-recursion.txt");
assert!(Ast::from_str(TEMPLATE, None, &Syntax::default()).is_err());
}
1 change: 1 addition & 0 deletions rinja_parser/tests/filter-recursion.txt

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion testing/tests/ui/excessive_nesting.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: failed to parse template source at row 14, column 34 near:
error: failed to parse template source at row 14, column 42 near:
"%}{%if 1%}{%if 1%}{%if 1%}{%if 1%}{%if 1"...
--> tests/ui/excessive_nesting.rs:3:10
|
Expand Down
9 changes: 9 additions & 0 deletions testing/tests/ui/filter-recursion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use rinja::Template;

#[derive(Template)]
#[template(source = "{{ s|a|a|a|a|a|a|a|A|a|a|a|a|a|a|a|a|a|a|a|a|a", ext = "txt")]
struct Filtered {
s: &'static str,
}

fn main() {}
9 changes: 9 additions & 0 deletions testing/tests/ui/filter-recursion.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error: unclosed expression, missing "}}"
failed to parse template source at row 1, column 0 near:
"{{ s|a|a|a|a|a|a|a|A|a|a|a|a|a|a|a|a|a|a"...
--> tests/ui/filter-recursion.rs:3:10
|
3 | #[derive(Template)]
| ^^^^^^^^
|
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)
24 changes: 12 additions & 12 deletions testing/tests/ui/unclosed-nodes.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
error: unclosed expression, missing "}}"
failed to parse template source at row 1, column 7 near:
""
failed to parse template source at row 1, column 0 near:
"{{ expr"
--> tests/ui/unclosed-nodes.rs:3:10
|
3 | #[derive(Template)]
Expand All @@ -9,8 +9,8 @@ error: unclosed expression, missing "}}"
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unclosed expression, missing "}}"
failed to parse template source at row 1, column 8 near:
""
failed to parse template source at row 1, column 0 near:
"{{ expr "
--> tests/ui/unclosed-nodes.rs:7:10
|
7 | #[derive(Template)]
Expand All @@ -19,8 +19,8 @@ error: unclosed expression, missing "}}"
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unclosed expression, missing "}}"
failed to parse template source at row 1, column 9 near:
""
failed to parse template source at row 1, column 0 near:
"{{ expr -"
--> tests/ui/unclosed-nodes.rs:11:10
|
11 | #[derive(Template)]
Expand All @@ -38,8 +38,8 @@ error: failed to parse template source at row 1, column 9 near:
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unclosed block, missing "%}"
failed to parse template source at row 1, column 8 near:
""
failed to parse template source at row 1, column 0 near:
"{% let x"
--> tests/ui/unclosed-nodes.rs:19:10
|
19 | #[derive(Template)]
Expand All @@ -48,8 +48,8 @@ error: unclosed block, missing "%}"
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unclosed block, missing "%}"
failed to parse template source at row 1, column 9 near:
""
failed to parse template source at row 1, column 0 near:
"{% let x "
--> tests/ui/unclosed-nodes.rs:23:10
|
23 | #[derive(Template)]
Expand All @@ -58,8 +58,8 @@ error: unclosed block, missing "%}"
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unclosed block, missing "%}"
failed to parse template source at row 1, column 10 near:
""
failed to parse template source at row 1, column 0 near:
"{% let x -"
--> tests/ui/unclosed-nodes.rs:27:10
|
27 | #[derive(Template)]
Expand Down