diff --git a/core/lib/Drupal/Component/Datetime/DateTimePlus.php b/core/lib/Drupal/Component/Datetime/DateTimePlus.php index 44c0e65..114d68c 100644 --- a/core/lib/Drupal/Component/Datetime/DateTimePlus.php +++ b/core/lib/Drupal/Component/Datetime/DateTimePlus.php @@ -278,7 +278,7 @@ public function __construct($time = 'now', $timezone = NULL, $settings = array() * * @return string */ - protected function render() { + public function render() { return $this->format(static::FORMAT) . ' ' . $this->getTimeZone()->getName(); } diff --git a/core/lib/Drupal/Core/Datetime/DateFormatter.php b/core/lib/Drupal/Core/Datetime/DateFormatter.php index e6e47b2..a1cd4a0 100644 --- a/core/lib/Drupal/Core/Datetime/DateFormatter.php +++ b/core/lib/Drupal/Core/Datetime/DateFormatter.php @@ -130,8 +130,8 @@ public function __construct(EntityManagerInterface $entity_manager, LanguageMana * the user interface language for the page. * * @return string - * A translated date string in the requested format. All HTML will be - * stripped from the output. + * A translated date string in the requested format. Since the format may + * contain user input, this value should be escaped when output. */ public function format($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) { if (!isset($timezone)) { diff --git a/core/lib/Drupal/Core/Datetime/DrupalDateTime.php b/core/lib/Drupal/Core/Datetime/DrupalDateTime.php index f69afc9..7c63621 100644 --- a/core/lib/Drupal/Core/Datetime/DrupalDateTime.php +++ b/core/lib/Drupal/Core/Datetime/DrupalDateTime.php @@ -90,8 +90,8 @@ protected function prepareTimezone($timezone) { * the result of the format() method. Defaults to NULL. * * @return string - * The formatted value of the date. All HTML will be stripped from the - * output. + * The formatted value of the date. Since the format may contain user input, + * this value should be escaped when output. */ public function format($format, $settings = array()) { $langcode = !empty($settings['langcode']) ? $settings['langcode'] : $this->langcode; @@ -135,7 +135,7 @@ public function format($format, $settings = array()) { catch (\Exception $e) { $this->errors[] = $e->getMessage(); } - return strip_tags($value); + return $value; } } diff --git a/core/modules/system/src/Tests/Common/FormatDateTest.php b/core/modules/system/src/Tests/Common/FormatDateTest.php index efd2bf1..6455018 100644 --- a/core/modules/system/src/Tests/Common/FormatDateTest.php +++ b/core/modules/system/src/Tests/Common/FormatDateTest.php @@ -118,9 +118,9 @@ function testFormatDate() { $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. + // HTML is not escaped by the date formatter, it must be escaped later. $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.'); + $this->assertIdentical($formatter->format($timestamp, 'custom', '\<\s\c\r\i\p\t\>\a\l\e\r\t\(\'Y\'\)\;\<\/\s\c\r\i\p\t\>'), "", 'Script tags not removed from dates.'); + $this->assertIdentical($formatter->format($timestamp, 'custom', '\<\e\m\>Y\<\/\e\m\>'), '2007', 'Em tags are not removed from dates.'); } } diff --git a/core/modules/system/src/Tests/System/DateTimeTest.php b/core/modules/system/src/Tests/System/DateTimeTest.php index 4cc2359..ab37bae 100644 --- a/core/modules/system/src/Tests/System/DateTimeTest.php +++ b/core/modules/system/src/Tests/System/DateTimeTest.php @@ -7,6 +7,7 @@ namespace Drupal\system\Tests\System; +use Drupal\Core\Url; use Drupal\simpletest\WebTestBase; /** @@ -134,6 +135,17 @@ function testDateFormatConfiguration() { $this->assertText($name, 'Custom date format appears in the date format list.'); $this->assertText(t('Delete'), 'Delete link for custom date format appears.'); + // + $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(Url::fromRoute('entity.date_format.collection')); + $this->assertEscaped("", 'The date format was properly escaped'); + // Add a new date format with HTML in it. $date_format_id = strtolower($this->randomMachineName(8)); $name = ucwords($date_format_id); @@ -147,8 +159,7 @@ 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->assertNoRaw('' . date("Y") . ''); + $this->assertEscaped('' . date("Y") . ''); } }