mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-16 14:33:14 +00:00
initial import
This commit is contained in:
20
lib/classes/Twig/Sandbox/SecurityError.php
Executable file
20
lib/classes/Twig/Sandbox/SecurityError.php
Executable file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Twig.
|
||||
*
|
||||
* (c) 2009 Fabien Potencier
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Exception thrown when a security error occurs at runtime.
|
||||
*
|
||||
* @package twig
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*/
|
||||
class Twig_Sandbox_SecurityError extends Twig_Error
|
||||
{
|
||||
}
|
99
lib/classes/Twig/Sandbox/SecurityPolicy.php
Executable file
99
lib/classes/Twig/Sandbox/SecurityPolicy.php
Executable file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Twig.
|
||||
*
|
||||
* (c) 2009 Fabien Potencier
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Represents a security policy which need to be enforced when sandbox mode is enabled.
|
||||
*
|
||||
* @package twig
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*/
|
||||
class Twig_Sandbox_SecurityPolicy implements Twig_Sandbox_SecurityPolicyInterface
|
||||
{
|
||||
protected $allowedTags;
|
||||
protected $allowedFilters;
|
||||
protected $allowedMethods;
|
||||
protected $allowedProperties;
|
||||
|
||||
public function __construct(array $allowedTags = array(), array $allowedFilters = array(), array $allowedMethods = array(), array $allowedProperties = array())
|
||||
{
|
||||
$this->allowedTags = $allowedTags;
|
||||
$this->allowedFilters = $allowedFilters;
|
||||
$this->allowedMethods = $allowedMethods;
|
||||
$this->allowedProperties = $allowedProperties;
|
||||
}
|
||||
|
||||
public function setAllowedTags(array $tags)
|
||||
{
|
||||
$this->allowedTags = $tags;
|
||||
}
|
||||
|
||||
public function setAllowedFilters(array $filters)
|
||||
{
|
||||
$this->allowedFilters = $filters;
|
||||
}
|
||||
|
||||
public function setAllowedMethods(array $methods)
|
||||
{
|
||||
$this->allowedMethods = $methods;
|
||||
}
|
||||
|
||||
public function setAllowedProperties(array $properties)
|
||||
{
|
||||
$this->allowedProperties = $properties;
|
||||
}
|
||||
|
||||
public function checkSecurity($tags, $filters)
|
||||
{
|
||||
foreach ($tags as $tag) {
|
||||
if (!in_array($tag, $this->allowedTags)) {
|
||||
throw new Twig_Sandbox_SecurityError(sprintf('Tag "%s" is not allowed.', $tag));
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($filters as $filter) {
|
||||
if (!in_array($filter, $this->allowedFilters)) {
|
||||
throw new Twig_Sandbox_SecurityError(sprintf('Filter "%s" is not allowed.', $filter));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function checkMethodAllowed($obj, $method)
|
||||
{
|
||||
$allowed = false;
|
||||
foreach ($this->allowedMethods as $class => $methods) {
|
||||
if ($obj instanceof $class) {
|
||||
$allowed = in_array($method, is_array($methods) ? $methods : array($methods));
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$allowed) {
|
||||
throw new Twig_Sandbox_SecurityError(sprintf('Calling "%s" method on a "%s" object is not allowed.', $method, get_class($obj)));
|
||||
}
|
||||
}
|
||||
|
||||
public function checkPropertyAllowed($obj, $property)
|
||||
{
|
||||
$allowed = false;
|
||||
foreach ($this->allowedProperties as $class => $properties) {
|
||||
if ($obj instanceof $class) {
|
||||
$allowed = in_array($property, is_array($properties) ? $properties : array($properties));
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$allowed) {
|
||||
throw new Twig_Sandbox_SecurityError(sprintf('Calling "%s" property on a "%s" object is not allowed.', $property, get_class($obj)));
|
||||
}
|
||||
}
|
||||
}
|
25
lib/classes/Twig/Sandbox/SecurityPolicyInterface.php
Executable file
25
lib/classes/Twig/Sandbox/SecurityPolicyInterface.php
Executable file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Twig.
|
||||
*
|
||||
* (c) 2009 Fabien Potencier
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Interfaces that all security policy classes must implements.
|
||||
*
|
||||
* @package twig
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*/
|
||||
interface Twig_Sandbox_SecurityPolicyInterface
|
||||
{
|
||||
public function checkSecurity($tags, $filters);
|
||||
|
||||
public function checkMethodAllowed($obj, $method);
|
||||
|
||||
public function checkPropertyAllowed($obj, $method);
|
||||
}
|
Reference in New Issue
Block a user