Prevent infinite loops

This commit is contained in:
Fabien Potencier
2024-07-27 17:13:00 +02:00
parent fa1a217f1a
commit 481e7f0d81
2 changed files with 19 additions and 0 deletions
+4
View File
@@ -105,6 +105,10 @@ final class LoopContext
public function __invoke($iterator): \Generator
{
if ($this->depth > 50) {
throw new \RuntimeException('Nesting level too deep.');
}
yield from ($this->recurseFunc)(new LoopIterator($iterator), $this->parent, $this->blocks, $this->recurseFunc, $this->depth + 1);
}
@@ -0,0 +1,15 @@
--TEST--
"for" tags prevents infinite recursions
--TEMPLATE--
{% for item in sitemap %}
{{ loop(sitemap) }}
{% endfor %}
--DATA--
return [
'sitemap' => [
[ 'title' => 'foo' ],
[ 'title' => 'bar' ],
],
]
--EXCEPTION--
Twig\Error\RuntimeError: An exception has been thrown during the rendering of a template ("Nesting level too deep.") in "index.twig" at line 3.