-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add strict_properties, array_methods options #3913
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1469,7 +1469,7 @@ function twig_array_batch($items, $size, $fill = null, $preserveKeys = true) | |
function twig_get_attribute(Environment $env, Source $source, $object, $item, array $arguments = [], $type = /* Template::ANY_CALL */ 'any', $isDefinedTest = false, $ignoreStrictCheck = false, $sandboxed = false, int $lineno = -1) | ||
{ | ||
// array | ||
if (/* Template::METHOD_CALL */ 'method' !== $type) { | ||
if (/* Template::METHOD_CALL */ 'method' !== $type || $env->isArrayMethods()) { | ||
$arrayItem = \is_bool($item) || \is_float($item) ? (int) $item : $item; | ||
|
||
if (((\is_array($object) || $object instanceof \ArrayObject) && (isset($object[$arrayItem]) || \array_key_exists($arrayItem, (array) $object))) | ||
|
@@ -1479,7 +1479,38 @@ function twig_get_attribute(Environment $env, Source $source, $object, $item, ar | |
return true; | ||
} | ||
|
||
return $object[$arrayItem]; | ||
if ($type === 'method') { | ||
if (is_callable($object[$arrayItem])) { | ||
if ($sandboxed) { | ||
if (is_array($object[$arrayItem])) { | ||
$env->getExtension(SandboxExtension::class)->checkMethodAllowed( | ||
$object[$arrayItem][0], | ||
$object[$arrayItem][0], | ||
$lineno, | ||
$source | ||
); | ||
} elseif (is_string($object[$arrayItem])) { | ||
$env->getExtension(SandboxExtension::class)->checkSecurity( | ||
[], | ||
[], | ||
[$object[$arrayItem]], | ||
); | ||
} elseif ($object[$arrayItem] instanceof Closure) { | ||
$env->getExtension(SandboxExtension::class)->checkMethodAllowed( | ||
$object[$arrayItem], | ||
'call', | ||
$lineno, | ||
$source | ||
); | ||
} else { | ||
throw new AssertionError("Unreachable"); | ||
} | ||
} | ||
return $object[$arrayItem](...$arguments); | ||
} | ||
} else { | ||
return $object[$arrayItem]; | ||
} | ||
} | ||
|
||
if (/* Template::ARRAY_CALL */ 'array' === $type || !\is_object($object)) { | ||
|
@@ -1554,6 +1585,18 @@ function twig_get_attribute(Environment $env, Source $source, $object, $item, ar | |
|
||
return $object->$item; | ||
} | ||
|
||
if ($env->isStrictProperties()) { | ||
if ($isDefinedTest) { | ||
return false; | ||
} | ||
|
||
if ($ignoreStrictCheck || !$env->isStrictVariables()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why a check on strict variables here ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This avoids falling through to the method call logic below, without throwing an exception (for consistency with the chosen There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic aims precisely at maintaining this behavior, regardless of the value of strict_properties. |
||
return; | ||
} | ||
|
||
throw new RuntimeError(sprintf('The property "%1$s" does not exist in class "%2$s".', $item, get_class($object)), $lineno, $source); | ||
} | ||
} | ||
|
||
static $cache = []; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not properly integrated with the sandbox system.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!