Start working
Modified some paths to be more generic and not require rewrite. Some instances of phase names have been changed, others are still in process.
This commit is contained in:
19
Twig/Sandbox/SecurityError.php
Normal file
19
Twig/Sandbox/SecurityError.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?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.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class Twig_Sandbox_SecurityError extends Twig_Error
|
||||
{
|
||||
}
|
31
Twig/Sandbox/SecurityNotAllowedFilterError.php
Normal file
31
Twig/Sandbox/SecurityNotAllowedFilterError.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?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 not allowed filter is used in a template.
|
||||
*
|
||||
* @author Martin Hasoň <martin.hason@gmail.com>
|
||||
*/
|
||||
class Twig_Sandbox_SecurityNotAllowedFilterError extends Twig_Sandbox_SecurityError
|
||||
{
|
||||
private $filterName;
|
||||
|
||||
public function __construct($message, $functionName, $lineno = -1, $filename = null, Exception $previous = null)
|
||||
{
|
||||
parent::__construct($message, $lineno, $filename, $previous);
|
||||
$this->filterName = $functionName;
|
||||
}
|
||||
|
||||
public function getFilterName()
|
||||
{
|
||||
return $this->filterName;
|
||||
}
|
||||
}
|
31
Twig/Sandbox/SecurityNotAllowedFunctionError.php
Normal file
31
Twig/Sandbox/SecurityNotAllowedFunctionError.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?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 not allowed function is used in a template.
|
||||
*
|
||||
* @author Martin Hasoň <martin.hason@gmail.com>
|
||||
*/
|
||||
class Twig_Sandbox_SecurityNotAllowedFunctionError extends Twig_Sandbox_SecurityError
|
||||
{
|
||||
private $functionName;
|
||||
|
||||
public function __construct($message, $functionName, $lineno = -1, $filename = null, Exception $previous = null)
|
||||
{
|
||||
parent::__construct($message, $lineno, $filename, $previous);
|
||||
$this->functionName = $functionName;
|
||||
}
|
||||
|
||||
public function getFunctionName()
|
||||
{
|
||||
return $this->functionName;
|
||||
}
|
||||
}
|
31
Twig/Sandbox/SecurityNotAllowedTagError.php
Normal file
31
Twig/Sandbox/SecurityNotAllowedTagError.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?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 not allowed tag is used in a template.
|
||||
*
|
||||
* @author Martin Hasoň <martin.hason@gmail.com>
|
||||
*/
|
||||
class Twig_Sandbox_SecurityNotAllowedTagError extends Twig_Sandbox_SecurityError
|
||||
{
|
||||
private $tagName;
|
||||
|
||||
public function __construct($message, $tagName, $lineno = -1, $filename = null, Exception $previous = null)
|
||||
{
|
||||
parent::__construct($message, $lineno, $filename, $previous);
|
||||
$this->tagName = $tagName;
|
||||
}
|
||||
|
||||
public function getTagName()
|
||||
{
|
||||
return $this->tagName;
|
||||
}
|
||||
}
|
119
Twig/Sandbox/SecurityPolicy.php
Normal file
119
Twig/Sandbox/SecurityPolicy.php
Normal file
@ -0,0 +1,119 @@
|
||||
<?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.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class Twig_Sandbox_SecurityPolicy implements Twig_Sandbox_SecurityPolicyInterface
|
||||
{
|
||||
protected $allowedTags;
|
||||
protected $allowedFilters;
|
||||
protected $allowedMethods;
|
||||
protected $allowedProperties;
|
||||
protected $allowedFunctions;
|
||||
|
||||
public function __construct(array $allowedTags = array(), array $allowedFilters = array(), array $allowedMethods = array(), array $allowedProperties = array(), array $allowedFunctions = array())
|
||||
{
|
||||
$this->allowedTags = $allowedTags;
|
||||
$this->allowedFilters = $allowedFilters;
|
||||
$this->setAllowedMethods($allowedMethods);
|
||||
$this->allowedProperties = $allowedProperties;
|
||||
$this->allowedFunctions = $allowedFunctions;
|
||||
}
|
||||
|
||||
public function setAllowedTags(array $tags)
|
||||
{
|
||||
$this->allowedTags = $tags;
|
||||
}
|
||||
|
||||
public function setAllowedFilters(array $filters)
|
||||
{
|
||||
$this->allowedFilters = $filters;
|
||||
}
|
||||
|
||||
public function setAllowedMethods(array $methods)
|
||||
{
|
||||
$this->allowedMethods = array();
|
||||
foreach ($methods as $class => $m) {
|
||||
$this->allowedMethods[$class] = array_map('strtolower', is_array($m) ? $m : array($m));
|
||||
}
|
||||
}
|
||||
|
||||
public function setAllowedProperties(array $properties)
|
||||
{
|
||||
$this->allowedProperties = $properties;
|
||||
}
|
||||
|
||||
public function setAllowedFunctions(array $functions)
|
||||
{
|
||||
$this->allowedFunctions = $functions;
|
||||
}
|
||||
|
||||
public function checkSecurity($tags, $filters, $functions)
|
||||
{
|
||||
foreach ($tags as $tag) {
|
||||
if (!in_array($tag, $this->allowedTags)) {
|
||||
throw new Twig_Sandbox_SecurityNotAllowedTagError(sprintf('Tag "%s" is not allowed.', $tag), $tag);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($filters as $filter) {
|
||||
if (!in_array($filter, $this->allowedFilters)) {
|
||||
throw new Twig_Sandbox_SecurityNotAllowedFilterError(sprintf('Filter "%s" is not allowed.', $filter), $filter);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($functions as $function) {
|
||||
if (!in_array($function, $this->allowedFunctions)) {
|
||||
throw new Twig_Sandbox_SecurityNotAllowedFunctionError(sprintf('Function "%s" is not allowed.', $function), $function);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function checkMethodAllowed($obj, $method)
|
||||
{
|
||||
if ($obj instanceof Twig_TemplateInterface || $obj instanceof Twig_Markup) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$allowed = false;
|
||||
$method = strtolower($method);
|
||||
foreach ($this->allowedMethods as $class => $methods) {
|
||||
if ($obj instanceof $class) {
|
||||
$allowed = in_array($method, $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)));
|
||||
}
|
||||
}
|
||||
}
|
24
Twig/Sandbox/SecurityPolicyInterface.php
Normal file
24
Twig/Sandbox/SecurityPolicyInterface.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?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.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
interface Twig_Sandbox_SecurityPolicyInterface
|
||||
{
|
||||
public function checkSecurity($tags, $filters, $functions);
|
||||
|
||||
public function checkMethodAllowed($obj, $method);
|
||||
|
||||
public function checkPropertyAllowed($obj, $method);
|
||||
}
|
Reference in New Issue
Block a user