mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-05 15:07:11 +00:00
Fix a security issue when an included sandboxed template has been loaded before without the sandbox context
This commit is contained in:
@@ -1400,13 +1400,6 @@ final class CoreExtension extends AbstractExtension
|
||||
if (!$alreadySandboxed = $sandbox->isSandboxed()) {
|
||||
$sandbox->enableSandbox();
|
||||
}
|
||||
|
||||
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 {
|
||||
@@ -1419,6 +1412,10 @@ final class CoreExtension extends AbstractExtension
|
||||
}
|
||||
}
|
||||
|
||||
if ($isSandboxed && $loaded) {
|
||||
$loaded->unwrap()->checkSecurity();
|
||||
}
|
||||
|
||||
return $loaded ? $loaded->render($variables) : '';
|
||||
} finally {
|
||||
if ($isSandboxed && !$alreadySandboxed) {
|
||||
|
||||
@@ -12,8 +12,13 @@ namespace Twig\Tests\Extension;
|
||||
*/
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Twig\Environment;
|
||||
use Twig\Error\RuntimeError;
|
||||
use Twig\Extension\CoreExtension;
|
||||
use Twig\Extension\SandboxExtension;
|
||||
use Twig\Loader\ArrayLoader;
|
||||
use Twig\Sandbox\SecurityError;
|
||||
use Twig\Sandbox\SecurityPolicy;
|
||||
|
||||
class CoreTest extends TestCase
|
||||
{
|
||||
@@ -313,6 +318,40 @@ class CoreTest extends TestCase
|
||||
[1, 42, "\x00\x34\x32"],
|
||||
];
|
||||
}
|
||||
|
||||
public function testSandboxedInclude()
|
||||
{
|
||||
$twig = new Environment(new ArrayLoader([
|
||||
'index' => '{{ include("included", sandboxed=true) }}',
|
||||
'included' => '{{ "included"|e }}',
|
||||
]));
|
||||
$policy = new SecurityPolicy(allowedFunctions: ['include']);
|
||||
$sandbox = new SandboxExtension($policy, false);
|
||||
$twig->addExtension($sandbox);
|
||||
|
||||
// We expect a compile error
|
||||
$this->expectException(SecurityError::class);
|
||||
$twig->render('index');
|
||||
}
|
||||
|
||||
public function testSandboxedIncludeWithPreloadedTemplate()
|
||||
{
|
||||
$twig = new Environment(new ArrayLoader([
|
||||
'index' => '{{ include("included", sandboxed=true) }}',
|
||||
'included' => '{{ "included"|e }}',
|
||||
]));
|
||||
$policy = new SecurityPolicy(allowedFunctions: ['include']);
|
||||
$sandbox = new SandboxExtension($policy, false);
|
||||
$twig->addExtension($sandbox);
|
||||
|
||||
// The template is loaded without the sandbox enabled
|
||||
// so, no compile error
|
||||
$twig->load('included');
|
||||
|
||||
// We expect a runtime error
|
||||
$this->expectException(SecurityError::class);
|
||||
$twig->render('index');
|
||||
}
|
||||
}
|
||||
|
||||
final class CoreTestIteratorAggregate implements \IteratorAggregate
|
||||
|
||||
Reference in New Issue
Block a user