Skip to content

Commit

Permalink
Latte: try to fix parsing of malformed macros [nette#711]
Browse files Browse the repository at this point in the history
  • Loading branch information
Majkl578 committed Jul 26, 2012
1 parent bff18d2 commit c3b5735
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
19 changes: 16 additions & 3 deletions Nette/Latte/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ public function parse($input)
if (!$matches) { // EOF
break;

} else if (!empty($matches['malformed'])) {
file_put_contents('/tmp/lattetest', $matches[0]);
throw new CompileException("Malformed macro declaration {{$matches['macro']}}.");

} elseif (!empty($matches['comment'])) { // {* *}
$this->addToken(Token::COMMENT, $matches[0]);

Expand Down Expand Up @@ -320,9 +324,18 @@ public function setDelimiters($left, $right)
$this->macroRe = '
(?P<comment>' . $left . '\\*.*?\\*' . $right . '\n{0,2})|
' . $left . '
(?P<macro>(?:' . self::RE_STRING . '|\{
(?P<inner>' . self::RE_STRING . '|\{(?P>inner)\}|[^\'"{}])*+
\}|[^\'"{}])+?)
(?P<macro>(?:' . self::RE_STRING . '|
\{(?P<inner>' . self::RE_STRING . '|
\{(?P>inner)\}|
[^\'"{}])*+\}|
[^\'"{}]|
(?P<malformed>
\'(?:\\\\.|[^\'\\\\])*|
(?:\\\\.|[^\'\\\\])*\'|
"(?:\\\\.|[^"\\\\])*|
(?:\\\\.|[^"\\\\])*"
)
)+?)
' . $right . '
(?P<rmargin>[ \t]*(?=\n))?
';
Expand Down
34 changes: 34 additions & 0 deletions tests/Nette/Latte/Parser.invalid.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* Test: Nette\Latte\Parser::parse() - invalid macro.
*
* @author Michael Moravec
* @package Nette\Latte
* @subpackage UnitTests
*/

use Nette\Latte;



require __DIR__ . '/../bootstrap.php';

require __DIR__ . '/Template.inc';



$parser = new Latte\Parser();

Assert::throws(function () use ($parser) {
$parser->parse('{var foo = \'nette}');
}, 'Nette\Latte\CompileException', 'Malformed macro declaration {%a%}.');
Assert::throws(function () use ($parser) {
$parser->parse('{var foo = nette\'}');
}, 'Nette\Latte\CompileException', 'Malformed macro declaration {%a%}.');
Assert::throws(function () use ($parser) {
$parser->parse('{var foo = "nette}');
}, 'Nette\Latte\CompileException', 'Malformed macro declaration {%a%}.');
Assert::throws(function () use ($parser) {
$parser->parse('{var foo = nette"}');
}, 'Nette\Latte\CompileException', 'Malformed macro declaration {%a%}.');

0 comments on commit c3b5735

Please sign in to comment.