Twig icon indicating copy to clipboard operation
Twig copied to clipboard

Exception Only "include" tags are allowed within a "sandbox" section when using the include function

Open ericmorand opened this issue 6 years ago • 5 comments

When trying to render the following template, we get an exception:

{% sandbox %}{{ include('foo.twig') }}{% endsandbox %}

Twig\Error\SyntaxError: Only "include" tags are allowed within a "sandbox" section in "index" at line 1

Now, it would make sense if the include function was not recommended over the include tag:

As of Twig 1.12, it is recommended to use the include function instead as it provides the same features with a bit more flexibility:

https://twig.symfony.com/doc/2.x/tags/include.html

In the current state, it's not possible to follow the official recommendation and use the sandbox tag of TwigPHP.

ericmorand avatar Oct 08 '19 08:10 ericmorand

Does it also not work if you whitelist the include() function in the SecurityPolicy?

xabbuh avatar Oct 14 '19 09:10 xabbuh

It doesn't work either when whitelisting include function.

It is expected since the error is thrown by the parser.

ericmorand avatar Oct 14 '19 19:10 ericmorand

I can confirm. I have same error: Only "include" tags are allowed within a "sandbox" section.

This is the code:

{% sandbox %}
   {{ include(template_from_string(email_template.template)) }}
{% endsandbox %}

Whitelisting include in functions does not help.

Refactoring code to:

{% include template_from_string(email_template.template) %} fixes the error.

Warxcell avatar Jun 09 '20 07:06 Warxcell

Actually the correct way for this is {{ include('template.html.twig', sandboxed = true) }} I think it can be closed.

Warxcell avatar Jun 09 '20 21:06 Warxcell

@Warxcell well, considering that the usage of the include tag is not recommended, where does it put the sandbox tag? If the include tag is not recommended, then the sandbox tag is not recommended either since it only applies to the include tag?

https://twig.symfony.com/doc/3.x/tags/include.html

ericmorand avatar Nov 23 '23 22:11 ericmorand