Skip to content

Commit

Permalink
checks for valid macro/filter/function/block names
Browse files Browse the repository at this point in the history
- deprecates macros names ~#%^& (BC break)
- block name cannot be number (because there are type problems when used as key)
  • Loading branch information
dg committed Nov 12, 2020
1 parent bb74672 commit 9200047
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/Latte/Compiler/Compiler.php
Expand Up @@ -105,8 +105,12 @@ class Compiler
*/
public function addMacro(string $name, Macro $macro, int $flags = null)
{
if (!isset($this->flags[$name])) {
if (!preg_match('#^[a-z_=][\w-]*$#iD', $name)) {
throw new \LogicException("Invalid tag name '$name'.");

} elseif (!isset($this->flags[$name])) {
$this->flags[$name] = $flags ?: Macro::DEFAULT_FLAGS;

} elseif ($flags && $this->flags[$name] !== $flags) {
throw new \LogicException("Incompatible flags for tag '$name'.");
}
Expand Down
2 changes: 1 addition & 1 deletion src/Latte/Compiler/Parser.php
Expand Up @@ -393,7 +393,7 @@ public function parseMacroTag(string $tag): ?array
(?P<closing>/?)
(
(?P<name>\?|[a-z]\w*+(?:[.:]\w+)*+(?!::|\(|\\\\))| ## ?, name, /name, but not function( or class:: or namespace\
(?P<shortname>[=\~#%^&_]?) ## expression, =expression, ...
(?P<shortname>[=_]?) ## expression, =expression, ...
)(?P<args>(?:' . self::RE_STRING . '|[^\'"])*?)
(?P<modifiers>(?<!\|)\|[a-z](?P<modArgs>(?:' . self::RE_STRING . '|(?:\((?P>modArgs)\))|[^\'"/()]|/(?=.))*+))?
(?P<empty>/?$)
Expand Down
9 changes: 9 additions & 0 deletions src/Latte/Engine.php
Expand Up @@ -267,6 +267,9 @@ public function getTemplateClass(string $name): string
*/
public function addFilter(?string $name, callable $callback)
{
if ($name !== null && !preg_match('#^[a-z]\w*$#iD', $name)) {
throw new \LogicException("Invalid filter name '$name'.");
}
$this->filters->add($name, $callback);
return $this;
}
Expand Down Expand Up @@ -309,6 +312,9 @@ public function addMacro(string $name, Macro $macro)
*/
public function addFunction(string $name, callable $callback)
{
if (!preg_match('#^[a-z]\w*$#iD', $name)) {
throw new \LogicException("Invalid function name '$name'.");
}
$this->functions->$name = $callback;
return $this;
}
Expand Down Expand Up @@ -336,6 +342,9 @@ public function invokeFunction(string $name, array $args)
*/
public function addProvider(string $name, $value)
{
if (!preg_match('#^[a-z]\w*$#iD', $name)) {
throw new \LogicException("Invalid provider name '$name'.");
}
$this->providers[$name] = $value;
return $this;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Latte/Macros/BlockMacros.php
Expand Up @@ -268,8 +268,8 @@ public function macroBlock(MacroNode $node, PhpWriter $writer)
. "\$this->blockQueue[$fname][] = [\$this, '{$node->data->func}'];";
}

} elseif ($name[0] === '_') {
throw new CompileException("Block name '$name' must not start with an underscore.");
} elseif (!preg_match('#^[a-z]#iD', $name)) {
throw new CompileException("Block name must start with letter a-z, '$name' given.");
}

// static snippet/snippetArea
Expand Down
6 changes: 5 additions & 1 deletion tests/Latte/BlockMacros.block5.phpt
Expand Up @@ -29,4 +29,8 @@ Assert::match(

Assert::exception(function () use ($latte) {
$latte->renderToString('{define _foobar}Hello{/define}');
}, Latte\CompileException::class, "Block name '_foobar' must not start with an underscore.");
}, Latte\CompileException::class, "Block name must start with letter a-z, '_foobar' given.");

Assert::exception(function () use ($latte) {
$latte->renderToString('{define 123}Hello{/define}');
}, Latte\CompileException::class, "Block name must start with letter a-z, '123' given.");

0 comments on commit 9200047

Please sign in to comment.