diff -u b/core/lib/Drupal/Component/Utility/SafeMarkup.php b/core/lib/Drupal/Component/Utility/SafeMarkup.php
--- b/core/lib/Drupal/Component/Utility/SafeMarkup.php
+++ b/core/lib/Drupal/Component/Utility/SafeMarkup.php
@@ -207,13 +207,7 @@
* @see \Drupal\Component\Utility\SafeMarkup::isSafe()
*/
public static function xssFilter($string, $html_tags = NULL) {
- if (is_null($html_tags)) {
- $string = Xss::filter($string);
- }
- else {
- $string = Xss::filter($string, $html_tags);
- }
- return static::set($string);
+ return static::set(Xss::filter($string, $html_tags));
}
/**
@@ -224,7 +218,14 @@
* can cause an XSS attack. The string provided will always be escaped
* regardless of whether the string is already marked as safe. It also will
* be truncated so that the text equivalent of it (ie the HTML tags
- * removed) is shorter than $length
+ * removed) is shorter than $length. For example:
+ * @code
+ * Xss::truncate('foobar', 5)
+ * @endcode
+ * will return
+ * @code
+ * fooba
+ * @endcode
* @param int $max_length
* An upper limit on the returned string length, including trailing ellipsis
* if $add_ellipsis is TRUE.
@@ -264,13 +265,7 @@
* @see \Drupal\Component\Utility\SafeMarkup::isSafe()
*/
public static function xssTruncate($string, $length, $wordsafe = FALSE, $add_ellipsis = FALSE, $min_wordsafe_length = 1, $html_tags = NULL) {
- if (isset($html_tags)) {
- $string = Xss::truncate($string, $length, $wordsafe, $add_ellipsis, $min_wordsafe_length, $html_tags);
- }
- else {
- $string = Xss::truncate($string, $length, $wordsafe, $add_ellipsis, $min_wordsafe_length);
- }
- return static::set($string);
+ return static::set(Xss::truncate($string, $length, $wordsafe, $add_ellipsis, $min_wordsafe_length, $html_tags));
}
/**
diff -u b/core/lib/Drupal/Component/Utility/Xss.php b/core/lib/Drupal/Component/Utility/Xss.php
--- b/core/lib/Drupal/Component/Utility/Xss.php
+++ b/core/lib/Drupal/Component/Utility/Xss.php
@@ -34,6 +34,15 @@
*/
protected static $adminTags = array('a', 'abbr', 'acronym', 'address', 'article', 'aside', 'b', 'bdi', 'bdo', 'big', 'blockquote', 'br', 'caption', 'cite', 'code', 'col', 'colgroup', 'command', 'dd', 'del', 'details', 'dfn', 'div', 'dl', 'dt', 'em', 'figcaption', 'figure', 'footer', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'hgroup', 'hr', 'i', 'img', 'ins', 'kbd', 'li', 'mark', 'menu', 'meter', 'nav', 'ol', 'output', 'p', 'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'section', 'small', 'span', 'strong', 'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'tfoot', 'th', 'thead', 'time', 'tr', 'tt', 'u', 'ul', 'var', 'wbr');
+ /**
+ * The default list of html tags allowed by filter() and truncate().
+ *
+ * @var array
+ *
+ * @see \Drupal\Component\Utility\Xss::filterAdmin()
+ */
+ protected static $defaultFilterTags = array('a', 'em', 'strong', 'cite', 'blockquote', 'code', 'ul', 'ol', 'li', 'dl', 'dt', 'dd');
+
/**
* Filters HTML to prevent cross-site-scripting (XSS) vulnerabilities.
*
@@ -69,12 +78,15 @@
*
* @ingroup sanitization
*/
- public static function filter($string, $html_tags = array('a', 'em', 'strong', 'cite', 'blockquote', 'code', 'ul', 'ol', 'li', 'dl', 'dt', 'dd')) {
+ public static function filter($string, $html_tags = NULL) {
// Only operate on valid UTF-8 strings. This is necessary to prevent cross
// site scripting issues on Internet Explorer 6.
if (!Unicode::validateUtf8($string)) {
return '';
}
+ if (!isset($html_tags)) {
+ $html_tags = static::$defaultFilterTags;
+ }
$string = static::prepareFilter($string);
$html_tags = array_flip($html_tags);
@@ -135,7 +147,14 @@
* can cause an XSS attack. The string provided will always be escaped
* regardless of whether the string is already marked as safe. It also will
* be truncated so that the text equivalent of it (ie the HTML tags
- * removed) is shorter than $length
+ * removed) is shorter than $length. For example:
+ * @code
+ * Xss::truncate('foobar', 5)
+ * @endcode
+ * will return
+ * @code
+ * fooba
+ * @endcode
* @param int $max_length
* An upper limit on the returned string length, including trailing ellipsis
* if $add_ellipsis is TRUE.
@@ -168,10 +187,13 @@
* An XSS-safe and truncated version of $string, or an empty string if
* $string is not valid UTF-8.
*/
- public static function truncate($string, $length, $wordsafe = FALSE, $add_ellipsis = FALSE, $min_wordsafe_length = 1, $html_tags = array('a', 'em', 'strong', 'cite', 'blockquote', 'code', 'ul', 'ol', 'li', 'dl', 'dt', 'dd')) {
+ public static function truncate($string, $length, $wordsafe = FALSE, $add_ellipsis = FALSE, $min_wordsafe_length = 1, $html_tags = NULL) {
if (!Unicode::validateUtf8($string)) {
return '';
}
+ if (!isset($html_tags)) {
+ $html_tags = static::$defaultFilterTags;
+ }
$string = static::prepareFilter($string);
$html_tags = array_flip($html_tags);
$class = get_called_class();
only in patch2:
unchanged:
--- a/core/tests/Drupal/Tests/Component/Utility/XssTest.php
+++ b/core/tests/Drupal/Tests/Component/Utility/XssTest.php
@@ -8,6 +8,7 @@
namespace Drupal\Tests\Component\Utility;
use Drupal\Component\Utility\Html;
+use Drupal\Component\Utility\Unicode;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Component\Utility\Xss;
use Drupal\Tests\UnitTestCase;
@@ -505,14 +506,14 @@ public function testAttribute($value, $expected, $message, $allowed_tags = NULL)
public function providerTestAttributes() {
return array(
array(
- '
',
- '
',
+ '
',
+ '
',
'Image tag with alt and title attribute',
array('img')
),
array(
- '
',
- '
',
+ '
',
+ '
',
'Image tag with data attribute',
array('img')
),
@@ -606,4 +607,66 @@ protected function assertNotNormalized($haystack, $needle, $message = '', $group
$this->assertTrue(strpos(strtolower(Html::decodeEntities($haystack)), $needle) === FALSE, $message, $group);
}
+
+ /**
+ * @param $value
+ * @param array $allowed_tags
+ * @return string
+ */
+ protected function convertFilterToTruncate($value, &$expected, $allowed_tags = NULL) {
+ if (!isset($allowed_tags)) {
+ $r = (new \ReflectionClass('\Drupal\Component\Utility\Xss'))->getProperty('defaultFilterTags');
+ $r->setAccessible(TRUE);
+ $allowed_tags = $r->getValue();
+ }
+ $filtered_length = Unicode::strlen(strip_tags(Xss::filter($value, $allowed_tags)));
+ if ($filtered_length) {
+ $allowed_tags[] = 'test';
+ $value .= 'fluffybunnies';
+ $expected .= 'fluffybunn';
+ $filtered_length += 10;
+ }
+ return Xss::truncate($value, $filtered_length, FALSE, FALSE, 1, $allowed_tags);
+ }
+
+ /**
+ * @covers ::truncate
+ *
+ * @dataProvider providerTestFilterXssNormalized
+ */
+ public function testTruncateXssNormalized($value, $expected, $message, array $allowed_tags = NULL) {
+ $value = $this->convertFilterToTruncate($value, $expected, $allowed_tags);
+ $this->assertNormalized($value, $expected, $message);
+ }
+
+ /**
+ * @covers ::truncate
+ *
+ * @dataProvider providerTestFilterXssNotNormalized
+ */
+ public function testTruncateXssNotNormalized($value, $expected, $message, array $allowed_tags = NULL) {
+ $value = $this->convertFilterToTruncate($value, $expected, $allowed_tags);
+ $this->assertNotNormalized($value, $expected, $message);
+ }
+
+ /**
+ * @covers ::truncate
+ *
+ * @dataProvider providerTestInvalidMultiByte
+ */
+ public function testTruncateInvalidMultiByte($value, $expected, $message) {
+ $value = $this->convertFilterToTruncate($value, $expected);
+ $this->assertEquals($value, $expected, $message);
+ }
+
+ /**
+ * @covers ::truncate
+ *
+ * @dataProvider providerTestAttributes
+ */
+ public function testTruncateAttribute($value, $expected, $message, $allowed_tags = NULL) {
+ $value = $this->convertFilterToTruncate($value, $expected, $allowed_tags);
+ $this->assertEquals($expected, $value, $message);
+ }
+
}