This repository has been archived by the owner on Jan 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'security/escaper-usage'
Fixes a number of components that were not using Zend\Escaper to escape HTML, HTML attributes, and/or URLs.
- Loading branch information
81 parents
e7c04f9
+
7015d6e
+
67d62a9
+
9acf647
+
33f48c3
+
db34ff9
+
c4d0f17
+
f6b222a
+
fb9a1b3
+
9b34c27
+
457dc51
+
bd1a8cd
+
21c6816
+
e63ad13
+
ebb65de
+
feaeb97
+
6d76c21
+
8080e99
+
3d8fc08
+
376eb68
+
846ff4b
+
a0b34c2
+
40bdf5a
+
e01831f
+
f22306a
+
8248197
+
740d2ca
+
3e4973e
+
1f71371
+
f945a68
+
6737231
+
7554e22
+
f43a14f
+
5680e05
+
8c71fbe
+
7640752
+
17fca5b
+
3e4bdb7
+
a1ec0e4
+
73e93c4
+
954b983
+
5a4d447
+
08d604f
+
a198091
+
3b2d396
+
ed13b76
+
8c1c902
+
66db3c0
+
88e5c45
+
4766aaf
+
af7efc8
+
2c6ae34
+
ea85a56
+
d1750df
+
665f6e2
+
3e28dac
+
edefcee
+
fed5933
+
55c0906
+
067d44c
+
694c574
+
1ea7f1a
+
e705000
+
5b6a369
+
ce9b366
+
61eaaee
+
1c21891
+
1302107
+
b74ab9c
+
031d20b
+
d5b278f
+
9115e94
+
65eecd8
+
3beca1c
+
8d86ba5
+
71f73d2
+
f41cf4b
+
34b4e0f
+
5441ea0
+
c3fe325
+
e1cc162
commit 2817c6f
Showing
10 changed files
with
371 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
* @package Zend_Tag | ||
*/ | ||
|
||
namespace Zend\Tag\Cloud\Decorator; | ||
|
||
use Traversable; | ||
use Zend\Escaper\Escaper; | ||
use Zend\Stdlib\ArrayUtils; | ||
use Zend\Tag\Cloud\Decorator\DecoratorInterface as Decorator; | ||
use Zend\Tag\Exception; | ||
|
||
/** | ||
* Abstract class for decorators | ||
* | ||
* @category Zend | ||
* @package Zend_Tag | ||
*/ | ||
abstract class AbstractDecorator implements Decorator | ||
{ | ||
/** | ||
* @var string Encoding to use | ||
*/ | ||
protected $encoding = 'UTF-8'; | ||
|
||
/** | ||
* @var Escaper | ||
*/ | ||
protected $escaper; | ||
|
||
/** | ||
* Option keys to skip when calling setOptions() | ||
* | ||
* @var array | ||
*/ | ||
protected $skipOptions = array( | ||
'options', | ||
'config', | ||
); | ||
|
||
/** | ||
* Create a new decorator with options | ||
* | ||
* @param array|Traversable $options | ||
*/ | ||
public function __construct($options = null) | ||
{ | ||
if ($options instanceof Traversable) { | ||
$options = ArrayUtils::iteratorToArray($options); | ||
} | ||
if (is_array($options)) { | ||
$this->setOptions($options); | ||
} | ||
} | ||
|
||
/** | ||
* Set options from array | ||
* | ||
* @param array $options Configuration for the decorator | ||
* @return AbstractTag | ||
*/ | ||
public function setOptions(array $options) | ||
{ | ||
foreach ($options as $key => $value) { | ||
if (in_array(strtolower($key), $this->skipOptions)) { | ||
continue; | ||
} | ||
|
||
$method = 'set' . $key; | ||
if (method_exists($this, $method)) { | ||
$this->$method($value); | ||
} | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get encoding | ||
* | ||
* @return string | ||
*/ | ||
public function getEncoding() | ||
{ | ||
return $this->encoding; | ||
} | ||
|
||
/** | ||
* Set encoding | ||
* | ||
* @param string | ||
* @return HTMLCloud | ||
*/ | ||
public function setEncoding($value) | ||
{ | ||
$this->encoding = (string) $value; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Set Escaper instance | ||
* | ||
* @param Escaper $escaper | ||
* @return HtmlCloud | ||
*/ | ||
public function setEscaper($escaper) | ||
{ | ||
$this->escaper = $escaper; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Retrieve Escaper instance | ||
* | ||
* If none registered, instantiates and registers one using current encoding. | ||
* | ||
* @return Escaper | ||
*/ | ||
public function getEscaper() | ||
{ | ||
if (null === $this->escaper) { | ||
$this->setEscaper(new Escaper($this->getEncoding())); | ||
} | ||
return $this->escaper; | ||
} | ||
|
||
/** | ||
* Validate an HTML element name | ||
* | ||
* @param string $name | ||
* @throws Exception\InvalidElementNameException | ||
*/ | ||
protected function validateElementName($name) | ||
{ | ||
if (!preg_match('/^[a-z0-9]+$/i', $name)) { | ||
throw new Exception\InvalidElementNameException(sprintf( | ||
'%s: Invalid element name "%s" provided; please provide valid HTML element names', | ||
__METHOD__, | ||
$this->getEscaper()->escapeHtml($name) | ||
)); | ||
} | ||
} | ||
|
||
/** | ||
* Validate an HTML attribute name | ||
* | ||
* @param string $name | ||
* @throws Exception\InvalidAttributeNameException | ||
*/ | ||
protected function validateAttributeName($name) | ||
{ | ||
if (!preg_match('/^[a-z_:][-a-z0-9_:.]*$/i', $name)) { | ||
throw new Exception\InvalidAttributeNameException(sprintf( | ||
'%s: Invalid HTML attribute name "%s" provided; please provide valid HTML attribute names', | ||
__METHOD__, | ||
$this->getEscaper()->escapeHtml($name) | ||
)); | ||
} | ||
} | ||
|
||
protected function wrapTag($html) | ||
{ | ||
$escaper = $this->getEscaper(); | ||
foreach ($this->getHTMLTags() as $key => $data) { | ||
if (is_array($data)) { | ||
$attributes = ''; | ||
$htmlTag = $key; | ||
$this->validateElementName($htmlTag); | ||
|
||
foreach ($data as $param => $value) { | ||
$this->validateAttributeName($param); | ||
$attributes .= ' ' . $param . '="' . $escaper->escapeHtmlAttr($value) . '"'; | ||
} | ||
} else { | ||
$attributes = ''; | ||
$htmlTag = $data; | ||
$this->validateElementName($htmlTag); | ||
} | ||
|
||
$html = sprintf('<%1$s%3$s>%2$s</%1$s>', $htmlTag, $html, $attributes); | ||
} | ||
return $html; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.