Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* Add a way to mark a `Twig\Markup` instance as safe for some escaping strategies only
* Make `Environment::getExtension()` return the runtime of an extension class registered with `AttributeExtension`
* Make `Environment::getRuntime()` fall back to the registered extension of the same class
* Speed up calling `map`, `filter`, `sort`, and the other callables that need to know whether the sandbox is enabled

# 3.30.0 (2026-09-25)

Expand Down
8 changes: 7 additions & 1 deletion src/Node/Expression/CallExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Twig\Compiler;
use Twig\Error\SyntaxError;
use Twig\Extension\ExtensionInterface;
use Twig\Extension\SandboxExtension;
use Twig\Node\Node;
use Twig\TwigCallableInterface;
use Twig\TwigFilter;
Expand Down Expand Up @@ -104,7 +105,12 @@ protected function compileArguments(Compiler $compiler, $isArray = false): void
if (!$first) {
$compiler->raw(', ');
}
$compiler->raw('$this->env->hasExtension(\Twig\Extension\SandboxExtension::class) && $this->env->getExtension(\Twig\Extension\SandboxExtension::class)->getChecker()->isSandboxed($this->source)');
// the registered extensions are part of the cache key of the compiled template
if ($compiler->getEnvironment()->hasExtension(SandboxExtension::class)) {
$compiler->raw('$this->env->getExtension(\Twig\Extension\SandboxExtension::class)->getChecker()->isSandboxed($this->source)');
} else {
$compiler->raw('false');
}
$first = false;
}

Expand Down
14 changes: 11 additions & 3 deletions tests/Node/Expression/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
use Twig\Environment;
use Twig\Error\SyntaxError;
use Twig\Extension\AbstractExtension;
use Twig\Extension\SandboxExtension;
use Twig\Loader\ArrayLoader;
use Twig\Node\EmptyNode;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\FilterExpression;
use Twig\Node\Nodes;
use Twig\Sandbox\SecurityPolicy;
use Twig\Test\NodeTestCase;
use Twig\TwigFilter;

Expand Down Expand Up @@ -87,13 +89,19 @@ public static function provideTests(): iterable
$node = self::createFilter($environment, new ConstantExpression('foo', 1), 'anonymous');
$tests[] = [$node, '$this->env->getFilter(\'anonymous\')->getCallable()("foo")'];

// needs sandbox
// needs sandbox, without the sandbox extension registered
$node = self::createFilter($environment, $string, 'bar_sandbox');
$tests[] = [$node, 'Twig\Tests\Node\Expression\twig_tests_filter_sandbox($this->env->hasExtension(\Twig\Extension\SandboxExtension::class) && $this->env->getExtension(\Twig\Extension\SandboxExtension::class)->getChecker()->isSandboxed($this->source), "abc")', $environment];
$tests[] = [$node, 'Twig\Tests\Node\Expression\twig_tests_filter_sandbox(false, "abc")', $environment];

// needs sandbox, with the sandbox extension registered
$sandboxedEnvironment = self::createEnvironment();
$sandboxedEnvironment->addExtension(new SandboxExtension(new SecurityPolicy()));
$node = self::createFilter($sandboxedEnvironment, $string, 'bar_sandbox');
$tests[] = [$node, 'Twig\Tests\Node\Expression\twig_tests_filter_sandbox($this->env->getExtension(\Twig\Extension\SandboxExtension::class)->getChecker()->isSandboxed($this->source), "abc")', $sandboxedEnvironment];

// needs charset, environment, context, and sandbox
$node = self::createFilter($environment, $string, 'bar_all');
$tests[] = [$node, 'Twig\Tests\Node\Expression\twig_tests_filter_all($this->env->getCharset(), $this->env, $context, $this->env->hasExtension(\Twig\Extension\SandboxExtension::class) && $this->env->getExtension(\Twig\Extension\SandboxExtension::class)->getChecker()->isSandboxed($this->source), "abc")', $environment];
$tests[] = [$node, 'Twig\Tests\Node\Expression\twig_tests_filter_all($this->env->getCharset(), $this->env, $context, false, "abc")', $environment];

// needs environment
$node = self::createFilter($environment, $string, 'bar');
Expand Down
Loading