Skip to content

Commit

Permalink
Delegate checking for null Source to SandboxExtension instead of Sour…
Browse files Browse the repository at this point in the history
…cePolicy
  • Loading branch information
YSaxon committed Oct 27, 2023
1 parent 6783091 commit cbbf38b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/Extension/SandboxExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ public function isSandboxedGlobally()

private function isSourceSandboxed(?Source $source) : bool
{
return $this->sourcePolicy && $this->sourcePolicy->enableSandbox($source);
if ($source === null || $this->sourcePolicy === null) {
return false;
}
return $this->sourcePolicy->enableSandbox($source);
}

public function setSecurityPolicy(SecurityPolicyInterface $policy)
Expand Down
2 changes: 1 addition & 1 deletion src/Sandbox/SourcePolicyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
*/
interface SourcePolicyInterface
{
public function enableSandbox(?Source $source): bool;
public function enableSandbox(Source $source): bool;
}
10 changes: 5 additions & 5 deletions tests/Extension/SandboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,9 @@ protected function getEnvironment($sandboxed, $options, $templates, $tags = [],
public function testSandboxSourcePolicyEnableReturningFalse()
{
$twig = $this->getEnvironment(false, [], self::$templates, [], [], [], [], [], new class() implements \Twig\Sandbox\SourcePolicyInterface {
public function enableSandbox(Source $source = null): bool
public function enableSandbox(Source $source): bool
{
return $source && '1_basic' != $source->getName();
return '1_basic' != $source->getName();
}
});
$this->assertEquals('FOO', $twig->load('1_basic')->render(self::$params));
Expand All @@ -474,9 +474,9 @@ public function enableSandbox(Source $source = null): bool
public function testSandboxSourcePolicyEnableReturningTrue()
{
$twig = $this->getEnvironment(false, [], self::$templates, [], [], [], [], [], new class() implements \Twig\Sandbox\SourcePolicyInterface {
public function enableSandbox(Source $source = null): bool
public function enableSandbox(Source $source): bool
{
return $source && '1_basic' === $source->getName();
return '1_basic' === $source->getName();
}
});
$this->expectException(SecurityError::class);
Expand All @@ -486,7 +486,7 @@ public function enableSandbox(Source $source = null): bool
public function testSandboxSourcePolicyFalseDoesntOverrideOtherEnables()
{
$twig = $this->getEnvironment(true, [], self::$templates, [], [], [], [], [], new class() implements \Twig\Sandbox\SourcePolicyInterface {
public function enableSandbox(Source $source = null): bool
public function enableSandbox(Source $source): bool
{
return false;
}
Expand Down

0 comments on commit cbbf38b

Please sign in to comment.