diff --git a/core/lib/Drupal/Core/Datetime/DateFormatter.php b/core/lib/Drupal/Core/Datetime/DateFormatter.php index db8e6bf..e6e47b2 100644 --- a/core/lib/Drupal/Core/Datetime/DateFormatter.php +++ b/core/lib/Drupal/Core/Datetime/DateFormatter.php @@ -7,7 +7,6 @@ namespace Drupal\Core\Datetime; -use Drupal\Component\Utility\Xss; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Entity\EntityManagerInterface; @@ -130,8 +129,9 @@ public function __construct(EntityManagerInterface $entity_manager, LanguageMana * (optional) Language code to translate to. NULL (default) means to use * the user interface language for the page. * - * @return \Drupal\Core\Datetime\DateString - * A translated date string in the requested format. + * @return string + * A translated date string in the requested format. All HTML will be + * stripped from the output. */ public function format($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) { if (!isset($timezone)) { diff --git a/core/lib/Drupal/Core/Datetime/DateString.php b/core/lib/Drupal/Core/Datetime/DateString.php deleted file mode 100644 index cf21c73..0000000 --- a/core/lib/Drupal/Core/Datetime/DateString.php +++ /dev/null @@ -1,37 +0,0 @@ -string); - } - -} diff --git a/core/lib/Drupal/Core/Datetime/DrupalDateTime.php b/core/lib/Drupal/Core/Datetime/DrupalDateTime.php index 68f46da..f69afc9 100644 --- a/core/lib/Drupal/Core/Datetime/DrupalDateTime.php +++ b/core/lib/Drupal/Core/Datetime/DrupalDateTime.php @@ -89,8 +89,9 @@ protected function prepareTimezone($timezone) { * - langcode: (optional) String two letter language code used to control * the result of the format() method. Defaults to NULL. * - * @return \Drupal\Core\Datetime\DateString - * A date string in the requested format. + * @return string + * The formatted value of the date. All HTML will be stripped from the + * output. */ public function format($format, $settings = array()) { $langcode = !empty($settings['langcode']) ? $settings['langcode'] : $this->langcode; @@ -134,7 +135,7 @@ public function format($format, $settings = array()) { catch (\Exception $e) { $this->errors[] = $e->getMessage(); } - return DateString::create($value); + return strip_tags($value); } } diff --git a/core/lib/Drupal/Core/Template/Attribute.php b/core/lib/Drupal/Core/Template/Attribute.php index 76d7c6a..cc0d591 100644 --- a/core/lib/Drupal/Core/Template/Attribute.php +++ b/core/lib/Drupal/Core/Template/Attribute.php @@ -109,8 +109,7 @@ protected function createAttributeValue($name, $value) { elseif (is_bool($value)) { $value = new AttributeBoolean($name, $value); } - // As a development aid, we allow the value to be a safe string object. - elseif (!is_object($value) || $value instanceof SafeStringInterface) { + elseif (!is_object($value)) { $value = new AttributeString($name, $value); } return $value; diff --git a/core/modules/system/src/Tests/Common/FormatDateTest.php b/core/modules/system/src/Tests/Common/FormatDateTest.php index 0ece458..efd2bf1 100644 --- a/core/modules/system/src/Tests/Common/FormatDateTest.php +++ b/core/modules/system/src/Tests/Common/FormatDateTest.php @@ -117,5 +117,10 @@ function testFormatDate() { $this->assertIdentical(format_date($timestamp, 'html_week'), '2007-W12', 'Test html_week date format.'); $this->assertIdentical(format_date($timestamp, 'html_month'), '2007-03', 'Test html_month date format.'); $this->assertIdentical(format_date($timestamp, 'html_year'), '2007', 'Test html_year date format.'); + + // Test HTML tag stripping. + $formatter = \Drupal::service('date.formatter'); + $this->assertIdentical($formatter->format($timestamp, 'custom', '\<\s\c\r\i\p\t\>\a\l\e\r\t\(\'Y\'\)\;\<\/\s\c\r\i\p\t\>'), "alert('2007');", 'Script tags are removed from dates.'); + $this->assertIdentical($formatter->format($timestamp, 'custom', '\<\e\m\>Y\<\/\e\m\>'), '2007', 'Em tags are removed from dates.'); } } diff --git a/core/modules/system/src/Tests/System/DateTimeTest.php b/core/modules/system/src/Tests/System/DateTimeTest.php index 517ac13..4cc2359 100644 --- a/core/modules/system/src/Tests/System/DateTimeTest.php +++ b/core/modules/system/src/Tests/System/DateTimeTest.php @@ -147,21 +147,8 @@ function testDateFormatConfiguration() { $this->assertUrl(\Drupal::url('entity.date_format.collection', [], ['absolute' => TRUE]), [], 'Correct page redirection.'); $this->assertText(t('Custom date format added.'), 'Date format added confirmation message appears.'); $this->assertText($name, 'Custom date format appears in the date format list.'); - $this->assertRaw('& ' . date("Y") . ''); + $this->assertRaw('& ' . date("Y")); + $this->assertNoRaw('' . date("Y") . ''); } - /** - * Test that date formats are sanitized. - */ - function testDateFormatXSS() { - $date_format = entity_create('date_format', array( - 'id' => 'xss_short', - 'label' => 'XSS format', - 'pattern' => '\<\s\c\r\i\p\t\>\a\l\e\r\t\(\'\X\S\S\'\)\;\<\/\s\c\r\i\p\t\>', - )); - $date_format->save(); - - $this->drupalGet('admin/config/regional/date-time'); - $this->assertNoRaw("", 'The date format was properly sanitized'); - } }