Skip to content

Commit fb3f27c

Browse files
committed
Fix sandbox for methods
1 parent 15aa49e commit fb3f27c

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/Template.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Twig\Error\Error;
1616
use Twig\Error\LoaderError;
1717
use Twig\Error\RuntimeError;
18+
use Twig\Extension\SandboxExtension;
19+
use Twig\Sandbox\SecurityError;
1820

1921
/**
2022
* Default base class for compiled templates.
@@ -526,6 +528,7 @@ final protected function getContext($context, $item, $ignoreStrictCheck = false)
526528
* @return mixed The attribute value, or a Boolean when $isDefinedTest is true, or null when the attribute is not set and $ignoreStrictCheck is true
527529
*
528530
* @throws RuntimeError if the attribute does not exist and Twig is running in strict mode and $isDefinedTest is false
531+
* @throws SecurityError if the attribute is not allowed
529532
*
530533
* @internal
531534
*/
@@ -601,17 +604,23 @@ protected function getAttribute($object, $item, array $arguments = [], $type = s
601604
}
602605

603606
// object property
607+
$propertySandboxException = null;
604608
if (self::METHOD_CALL !== $type && !$object instanceof self) { // \Twig\Template does not have public properties, and we don't want to allow access to internal ones
605609
if (isset($object->$item) || \array_key_exists((string) $item, (array) $object)) {
606610
if ($isDefinedTest) {
607611
return true;
608612
}
609613

610614
if ($this->env->hasExtension('\Twig\Extension\SandboxExtension')) {
611-
$this->env->getExtension('\Twig\Extension\SandboxExtension')->checkPropertyAllowed($object, $item);
615+
try {
616+
$this->env->getExtension('\Twig\Extension\SandboxExtension')->checkPropertyAllowed($object, $item);
617+
} catch (SecurityError $propertySandboxException) {
618+
}
612619
}
613620

614-
return $object->$item;
621+
if (null === $propertySandboxException) {
622+
return $object->$item;
623+
}
615624
}
616625
}
617626

@@ -678,6 +687,10 @@ protected function getAttribute($object, $item, array $arguments = [], $type = s
678687
return false;
679688
}
680689

690+
if (null !== $propertySandboxException) {
691+
throw $propertySandboxException;
692+
}
693+
681694
if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
682695
return;
683696
}
@@ -690,7 +703,15 @@ protected function getAttribute($object, $item, array $arguments = [], $type = s
690703
}
691704

692705
if ($this->env->hasExtension('\Twig\Extension\SandboxExtension')) {
693-
$this->env->getExtension('\Twig\Extension\SandboxExtension')->checkMethodAllowed($object, $method);
706+
try {
707+
$this->env->getExtension(SandboxExtension::class)->checkMethodAllowed($object, $call ? '__call' : $method);
708+
} catch (SecurityError $e) {
709+
if ($call && null !== $propertySandboxException) {
710+
throw $propertySandboxException;
711+
}
712+
713+
throw $e;
714+
}
694715
}
695716

696717
// Some objects throw exceptions when they have __call, and the method we try
@@ -702,6 +723,10 @@ protected function getAttribute($object, $item, array $arguments = [], $type = s
702723
$ret = \call_user_func_array([$object, $method], $arguments);
703724
}
704725
} catch (\BadMethodCallException $e) {
726+
if ($call && null !== $propertySandboxException) {
727+
throw $propertySandboxException;
728+
}
729+
705730
if ($call && ($ignoreStrictCheck || !$this->env->isStrictVariables())) {
706731
return;
707732
}

0 commit comments

Comments
 (0)