Part-DB.Part-DB-server/src/Twig/Sandbox/InheritanceSecurityPolicy.php

150 lines
4.8 KiB
PHP
Raw Normal View History

<?php
2020-05-10 21:39:31 +02:00
declare(strict_types=1);
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Twig\Sandbox;
use Twig\Markup;
use Twig\Sandbox\SecurityNotAllowedFilterError;
use Twig\Sandbox\SecurityNotAllowedFunctionError;
use Twig\Sandbox\SecurityNotAllowedMethodError;
use Twig\Sandbox\SecurityNotAllowedPropertyError;
use Twig\Sandbox\SecurityNotAllowedTagError;
use Twig\Sandbox\SecurityPolicyInterface;
use Twig\Template;
2022-08-14 19:32:53 +02:00
use function get_class;
use function in_array;
use function is_array;
/**
* Represents a security policy which need to be enforced when sandbox mode is enabled.
*
* Modified by Jan Böhmer, to allow inheritance of methods and properties via class hierachy.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
final class InheritanceSecurityPolicy implements SecurityPolicyInterface
{
2022-09-18 22:59:31 +02:00
private array $allowedTags;
private array $allowedFilters;
private array $allowedMethods;
private array $allowedProperties;
private array $allowedFunctions;
public function __construct(array $allowedTags = [], array $allowedFilters = [], array $allowedMethods = [], array $allowedProperties = [], array $allowedFunctions = [])
{
$this->allowedTags = $allowedTags;
$this->allowedFilters = $allowedFilters;
$this->setAllowedMethods($allowedMethods);
$this->allowedProperties = $allowedProperties;
$this->allowedFunctions = $allowedFunctions;
}
public function setAllowedTags(array $tags): void
{
$this->allowedTags = $tags;
}
public function setAllowedFilters(array $filters): void
{
$this->allowedFilters = $filters;
}
public function setAllowedMethods(array $methods): void
{
$this->allowedMethods = [];
foreach ($methods as $class => $m) {
2020-08-21 22:43:37 +02:00
$this->allowedMethods[$class] = array_map(
2022-08-14 19:32:53 +02:00
static function ($value) { return strtr($value, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); }, is_array($m) ? $m : [$m]);
}
}
public function setAllowedProperties(array $properties): void
{
$this->allowedProperties = $properties;
}
public function setAllowedFunctions(array $functions): void
{
$this->allowedFunctions = $functions;
}
public function checkSecurity($tags, $filters, $functions): void
{
foreach ($tags as $tag) {
2022-08-14 19:32:53 +02:00
if (!in_array($tag, $this->allowedTags, true)) {
throw new SecurityNotAllowedTagError(sprintf('Tag "%s" is not allowed.', $tag), $tag);
}
}
foreach ($filters as $filter) {
2022-08-14 19:32:53 +02:00
if (!in_array($filter, $this->allowedFilters, true)) {
throw new SecurityNotAllowedFilterError(sprintf('Filter "%s" is not allowed.', $filter), $filter);
}
}
foreach ($functions as $function) {
2022-08-14 19:32:53 +02:00
if (!in_array($function, $this->allowedFunctions, true)) {
throw new SecurityNotAllowedFunctionError(sprintf('Function "%s" is not allowed.', $function), $function);
}
}
}
public function checkMethodAllowed($obj, $method): void
{
if ($obj instanceof Template || $obj instanceof Markup) {
return;
}
$allowed = false;
$method = strtr($method, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
foreach ($this->allowedMethods as $class => $methods) {
if ($obj instanceof $class) {
2022-08-14 19:32:53 +02:00
$allowed = in_array($method, $methods, true);
2023-04-15 23:14:53 +02:00
//CHANGED: Only break if the method is allowed, otherwise try it on the other methods
if ($allowed) {
break;
}
}
}
2020-08-21 21:36:22 +02:00
if (!$allowed) {
2022-08-14 19:32:53 +02:00
$class = get_class($obj);
2020-05-10 21:39:31 +02:00
throw new SecurityNotAllowedMethodError(sprintf('Calling "%s" method on a "%s" object is not allowed.', $method, $class), $class, $method);
}
}
public function checkPropertyAllowed($obj, $property): void
{
$allowed = false;
foreach ($this->allowedProperties as $class => $properties) {
if ($obj instanceof $class) {
2022-08-14 19:32:53 +02:00
$allowed = in_array($property, is_array($properties) ? $properties : [$properties], true);
2023-04-15 23:14:53 +02:00
//CHANGED: Only break if the method is allowed, otherwise try it on the other methods
if ($allowed) {
break;
}
}
}
2020-08-21 21:36:22 +02:00
if (!$allowed) {
2022-08-14 19:32:53 +02:00
$class = get_class($obj);
2020-05-10 21:39:31 +02:00
throw new SecurityNotAllowedPropertyError(sprintf('Calling "%s" property on a "%s" object is not allowed.', $property, $class), $class, $property);
}
}
}