mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-30 03:57:21 +00:00
Merge branch '2.x' into 3.x
* 2.x: Update format_time.rst Fix "include(template_from_string())" Fix PHPUnit config
This commit is contained in:
@@ -394,7 +394,7 @@
|
||||
|
||||
# 1.44.1 (2020-XX-XX)
|
||||
|
||||
* n/a
|
||||
* Fix "include(template_from_string())"
|
||||
|
||||
# 1.44.0 (2020-10-21)
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
===============
|
||||
|
||||
The ``format_time`` filter formats a time. It behaves in the exact same way as
|
||||
the ``format_datetime`` filter, but without the date.
|
||||
the :doc:`format_datetime<format_datetime>` filter, but without the date.
|
||||
|
||||
.. note::
|
||||
|
||||
|
||||
@@ -12,9 +12,4 @@
|
||||
<listeners>
|
||||
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
|
||||
</listeners>
|
||||
<coverage>
|
||||
<include>
|
||||
<directory suffix=".php">./src/</directory>
|
||||
</include>
|
||||
</coverage>
|
||||
</phpunit>
|
||||
|
||||
@@ -1243,11 +1243,13 @@ function twig_include(Environment $env, $context, $template, $variables = [], $w
|
||||
if (!$alreadySandboxed = $sandbox->isSandboxed()) {
|
||||
$sandbox->enableSandbox();
|
||||
}
|
||||
}
|
||||
|
||||
// if a Template instance is passed, it might have been instantiated outside of a sandbox, check security
|
||||
if ($template instanceof TemplateWrapper || $template instanceof Template) {
|
||||
$template->unwrap()->checkSecurity();
|
||||
foreach ((\is_array($template) ? $template : [$template]) as $name) {
|
||||
// if a Template instance is passed, it might have been instantiated outside of a sandbox, check security
|
||||
if ($name instanceof TemplateWrapper || $name instanceof Template) {
|
||||
$name->unwrap()->checkSecurity();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
@@ -51,7 +51,8 @@ class SandboxTest extends TestCase
|
||||
'1_layout' => '{% block content %}{% endblock %}',
|
||||
'1_child' => "{% extends \"1_layout\" %}\n{% block content %}\n{{ \"a\"|json_encode }}\n{% endblock %}",
|
||||
'1_include' => '{{ include("1_basic1", sandboxed=true) }}',
|
||||
'1_basic2_include_template_from_string' => '{{ include(template_from_string("{{ name|upper }}"), sandboxed=true) }}',
|
||||
'1_basic2_include_template_from_string_sandboxed' => '{{ include(template_from_string("{{ name|upper }}"), sandboxed=true) }}',
|
||||
'1_basic2_include_template_from_string' => '{{ include(template_from_string("{{ name|upper }}")) }}',
|
||||
'1_range_operator' => '{{ (1..2)[0] }}',
|
||||
'1_syntax_error_wrapper' => '{% sandbox %}{% include "1_syntax_error" %}{% endsandbox %}',
|
||||
'1_syntax_error' => '{% syntax error }}',
|
||||
@@ -98,10 +99,43 @@ class SandboxTest extends TestCase
|
||||
}
|
||||
}
|
||||
|
||||
public function testSandboxUnallowedFilterWithIncludeTemplateFromString()
|
||||
public function testSandboxGloballyFalseUnallowedFilterWithIncludeTemplateFromStringSandboxed()
|
||||
{
|
||||
$twig = $this->getEnvironment(false, [], self::$templates);
|
||||
$twig->addExtension(new StringLoaderExtension());
|
||||
try {
|
||||
$twig->load('1_basic2_include_template_from_string_sandboxed')->render(self::$params);
|
||||
$this->fail('Sandbox throws a SecurityError exception if an unallowed filter is called');
|
||||
} catch (SecurityError $e) {
|
||||
$this->assertInstanceOf('\Twig\Sandbox\SecurityNotAllowedFilterError', $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedFilterError');
|
||||
$this->assertEquals('upper', $e->getFilterName(), 'Exception should be raised on the "upper" filter');
|
||||
}
|
||||
}
|
||||
|
||||
public function testSandboxGloballyTrueUnallowedFilterWithIncludeTemplateFromStringSandboxed()
|
||||
{
|
||||
$twig = $this->getEnvironment(true, [], self::$templates, [], [], [], [], ['include', 'template_from_string']);
|
||||
$twig->addExtension(new StringLoaderExtension());
|
||||
try {
|
||||
$twig->load('1_basic2_include_template_from_string_sandboxed')->render(self::$params);
|
||||
$this->fail('Sandbox throws a SecurityError exception if an unallowed filter is called');
|
||||
} catch (SecurityError $e) {
|
||||
$this->assertInstanceOf('\Twig\Sandbox\SecurityNotAllowedFilterError', $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedFilterError');
|
||||
$this->assertEquals('upper', $e->getFilterName(), 'Exception should be raised on the "upper" filter');
|
||||
}
|
||||
}
|
||||
|
||||
public function testSandboxGloballyFalseUnallowedFilterWithIncludeTemplateFromStringNotSandboxed()
|
||||
{
|
||||
$twig = $this->getEnvironment(false, [], self::$templates);
|
||||
$twig->addExtension(new StringLoaderExtension());
|
||||
$this->assertSame('FABIEN', $twig->load('1_basic2_include_template_from_string')->render(self::$params));
|
||||
}
|
||||
|
||||
public function testSandboxGloballyTrueUnallowedFilterWithIncludeTemplateFromStringNotSandboxed()
|
||||
{
|
||||
$twig = $this->getEnvironment(true, [], self::$templates, [], [], [], [], ['include', 'template_from_string']);
|
||||
$twig->addExtension(new StringLoaderExtension());
|
||||
try {
|
||||
$twig->load('1_basic2_include_template_from_string')->render(self::$params);
|
||||
$this->fail('Sandbox throws a SecurityError exception if an unallowed filter is called');
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Twig.
|
||||
*
|
||||
* (c) Fabien Potencier
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Twig\Tests\Extension;
|
||||
|
||||
use Twig\Environment;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Twig\Extension\StringLoaderExtension;
|
||||
|
||||
class StringLoaderExtensionTest extends TestCase
|
||||
{
|
||||
public function testIncludeWithTemplateStringAndNoSandbox()
|
||||
{
|
||||
$twig = new Environment($this->createMock('\Twig\Loader\LoaderInterface'));
|
||||
$twig->addExtension(new StringLoaderExtension());
|
||||
$this->assertSame('something', twig_include($twig, [], twig_template_from_string($twig, "something")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user