From 8144daa17b36cdaa3d3dc2ef4624f0661ea8fdfe Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Wed, 18 Nov 2015 12:06:42 -0800 Subject: [PATCH] Disallow garbage after #end blocks --- Cheetah/legacy_parser.py | 4 +++- tests/Parser_test.py | 18 ++++++++++++++++++ tests/SyntaxAndOutput_test.py | 6 +++--- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Cheetah/legacy_parser.py b/Cheetah/legacy_parser.py index 6eef9b49..65240f49 100644 --- a/Cheetah/legacy_parser.py +++ b/Cheetah/legacy_parser.py @@ -877,9 +877,11 @@ def eatEndDirective(self): break if not directiveName: raise ParseError(self, msg='Invalid end directive') + self.advance(len(directiveName)) endOfFirstLinePos = self.findEOL() - self.getExpression() # eat in any extra comment-like crap + if self.getExpression().strip(): + raise ParseError(self, 'Invalid garbage after #end directive') self._eatRestOfDirectiveTag(isLineClearToStartToken, endOfFirstLinePos) assert directiveName in CLOSABLE_DIRECTIVES self.popFromOpenDirectivesStack(directiveName) diff --git a/tests/Parser_test.py b/tests/Parser_test.py index c19f9dfa..0af79f76 100644 --- a/tests/Parser_test.py +++ b/tests/Parser_test.py @@ -579,3 +579,21 @@ def test_errors_on_blinged_kwarg(): compile_to_class( '$foo($bar=$baz)' ) + + +def test_errors_garbage_after_end_directive(): + with assert_raises_exactly( + ParseError, + '\n\n' + 'Invalid garbage after #end directive\n' + 'Line 2, column 15\n\n' + 'Line|Cheetah Code\n' + '----|-------------------------------------------------------------\n' + '1 |#block foo\n' + '2 |#end block foo\n' + ' ^\n' + ): + compile_to_class( + '#block foo\n' + '#end block foo\n' + ) diff --git a/tests/SyntaxAndOutput_test.py b/tests/SyntaxAndOutput_test.py index 15d17039..edc42b1a 100644 --- a/tests/SyntaxAndOutput_test.py +++ b/tests/SyntaxAndOutput_test.py @@ -970,10 +970,10 @@ def test5(self): outer #block innerNest inner -#end block innerNest -#end block outerNest +#end block +#end block --- -#end block testBlock +#end block """, "this is a test block\nouter\ninner\n---\n")