Skip to content

Commit 0babe1c

Browse files
committed
replace "dot" with "set not containing whitespace"
Fixed issue in lexer in the same category as that of 🎫`366` where the regexp used to match an end tag didn't correctly organize for matching characters surrounded by whitespace, leading to high memory / interpreter hang if a closing tag incorrectly had a large amount of unterminated space in it. Credit to Sebastian Chnelik for locating the issue. As Mako templates inherently render and directly invoke arbitrary Python code from the template source, it is **never** appropriate to create templates that contain untrusted input. Fixes: #367 Change-Id: I2f3a8665e92c1b6efcf36b1dba6e58fe0975b7da
1 parent c2f392e commit 0babe1c

4 files changed

Lines changed: 27 additions & 3 deletions

File tree

doc/build/changelog.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ Changelog
2222
correctly interpret quoted sections individually. While this parsing issue
2323
still produced the same expected tag structure later on, the mis-handling
2424
of quoted sections was also subject to a regexp crash if a tag had a large
25-
number of quotes within its quoted sections.
25+
number of quotes within its quoted sections. Credit to Sebastian
26+
Chnelik for locating the issue.
27+
28+
As Mako templates inherently render and directly invoke arbitrary Python
29+
code from the template source, it is **never** appropriate to create
30+
templates that contain untrusted input.
2631

2732
.. changelog::
2833
:version: 1.2.1

doc/build/unreleased/367.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.. change::
2+
:tags: bug, lexer
3+
:tickets: 367
4+
5+
Fixed issue in lexer in the same category as that of :ticket:`366` where
6+
the regexp used to match an end tag didn't correctly organize for matching
7+
characters surrounded by whitespace, leading to high memory / interpreter
8+
hang if a closing tag incorrectly had a large amount of unterminated space
9+
in it. Credit to Sebastian Chnelik for locating the issue.
10+
11+
As Mako templates inherently render and directly invoke arbitrary Python
12+
code from the template source, it is **never** appropriate to create
13+
templates that contain untrusted input.

mako/lexer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ def match_tag_start(self):
322322
return True
323323

324324
def match_tag_end(self):
325-
match = self.match(r"\</%[\t ]*(.+?)[\t ]*>")
325+
match = self.match(r"\</%[\t ]*([^\t ]+?)[\t ]*>")
326326
if match:
327327
if not len(self.tag):
328328
raise exceptions.SyntaxException(

test/test_lexer.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,13 @@ def test_noexpr_allowed(self):
148148
"""
149149
assert_raises(exceptions.CompileException, Lexer(template).parse)
150150

151-
def test_tag_many_quotes(self):
151+
def test_closing_tag_many_spaces(self):
152+
"""test #367"""
153+
template = '<%def name="foo()"> this is a def. </%' + " " * 10000
154+
assert_raises(exceptions.SyntaxException, Lexer(template).parse)
155+
156+
def test_opening_tag_many_quotes(self):
157+
"""test #366"""
152158
template = "<%0" + '"' * 3000
153159
assert_raises(exceptions.SyntaxException, Lexer(template).parse)
154160

0 commit comments

Comments
 (0)