Problem/Motivation

See #2566503: [meta] Replace remaining !placeholder for Non-URL HTML outputs only

Date stuff.

Look out for format_date() and 'date.formatter' => format() and maybe variables assigned with the result of those two values.
Note: Currently the result of that is xss filtered, so actually not marked as safe, mhhh.

Proposed resolution

HTML support in date formats in questionable. Firstly there is the fact that these values are using in attributes. Secondly there is the possibility of adding HTML where it really is not expected.

The one remaining question is do translators have a need to add HTML to a date format string?

Remaining tasks

Agree that removing HTML support makes sense.

User interface changes

None

API changes

Date format strings no longer support adding HTML using the \ escape character.

Data model changes

None

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Bug because at the moment date formats support HTML but it is escaped
Issue priority Major because part of the critical to remove !placeholder
Disruption Disruptive for existing sites that are adding HTML to date formats. If HTML is required in a formatted date then the site should implement a custom field formatter to do this.

Comments

dawehner created an issue. See original summary.

alexpott’s picture

Title: Replace remaining !placeholder for Non-URL HTML outputs only in search_excerpt() » Replace remaining !placeholder for Non-URL HTML outputs only where format_date() and 'date.formatter' => format() are used
Parent issue: #2568611: Replace remaining !placeholder for Non-URL HTML outputs only in search_excerpt() » #2566503: [meta] Replace remaining !placeholder for Non-URL HTML outputs only
dawehner’s picture

Assigned: Unassigned » dawehner

Ups.

alexpott’s picture

Status: Active » Needs review
StatusFileSize
new8.21 KB

Here's a first cut at finding them...

alexpott’s picture

StatusFileSize
new3.28 KB
new4.67 KB

Whoops

dawehner’s picture

I searched using

ag "date" | grep "format" | grep "!" | grep -v "js" | grep -v "css"

(yes that is horrible)

+++ b/core/modules/tracker/tracker.pages.inc
@@ -106,8 +106,8 @@ function tracker_page($account = NULL) {
       );
diff --git a/core/modules/views_ui/src/ViewEditForm.php b/core/modules/views_ui/src/ViewEditForm.php

diff --git a/core/modules/views_ui/src/ViewEditForm.php b/core/modules/views_ui/src/ViewEditForm.php
index 1b335de..cad9be5 100644

index 1b335de..cad9be5 100644
--- a/core/modules/views_ui/src/ViewEditForm.php

--- a/core/modules/views_ui/src/ViewEditForm.php
+++ b/core/modules/views_ui/src/ViewEditForm.php

+++ b/core/modules/views_ui/src/ViewEditForm.php
+++ b/core/modules/views_ui/src/ViewEditForm.php
@@ -140,13 +140,13 @@ public function form(array $form, FormStateInterface $form_state) {

@@ -140,13 +140,13 @@ public function form(array $form, FormStateInterface $form_state) {
       );
       $lock_message_substitutions = array(
         '!user' => drupal_render($username),
-        '!age' => $this->dateFormatter->formatTimeDiffSince($view->lock->updated),
+        '@age' => $this->dateFormatter->formatTimeDiffSince($view->lock->updated),
         '@url' => $view->url('break-lock-form'),
       );
       $form['locked'] = array(
         '#type' => 'container',
         '#attributes' => array('class' => array('view-locked', 'messages', 'messages--warning')),
-        '#children' => $this->t('This view is being edited by user !user, and is therefore locked from editing by others. This lock is !age old. Click here to <a href="@url">break this lock</a>.', $lock_message_substitutions),

Didn't we excluded views?

dawehner’s picture

Assigned: dawehner » Unassigned
Status: Needs review » Reviewed & tested by the community

Fair point, this is about date formatting, so let's deal with that.

xjm’s picture

Status: Reviewed & tested by the community » Needs review

I thought these were the results of a format_plural(), but the summary indicates otherwise. Can we get some manual testing that the escaping is correct when a date format includes special characters, screenshot, documentation of test coverage?

sutharsan’s picture

Issue summary: View changes
Status: Needs review » Needs work
StatusFileSize
new44.66 KB
new87.26 KB

The date format is double escaped in the title tag. See screenshots. Though it is not likely to occur. To reproduce:

  1. Add a date or date + time field to a content type.
  2. Export the HTML Date format (admin/config/development/configuration/single/export)
  3. Change the pattern from 'pattern: 'Y-m-d' to 'pattern: 'Y-m-&>'
  4. Re-import the HTML date format (admin/config/development/configuration/single/import)
  5. Go to the date field, refresh and hover over the field.
  6. Observe the content of tooltip.

alexpott’s picture

So I thought I broke this when we removed the SafeMarkup XSS stuff... however this line:

return Xss::filter($date->format($format, $settings)); 

Is from #2003934: Convert format_date to a service and predates safe markup. Ah but I did break it. I broke it when we stopped Xss::filter() from marking things as safe. If we want to be able to put markup in date formats then we need to make this return an object that implements SafeStringInterface. So now the question has to be is there any case for markup. I guess the presence of Xss::filter() suggests there is. However its not there in D7 ... so what issue introduced it... #1892640: XSS in date formats admin page . Hmm the comment about being clear about whether something has HTML or not is quite pertinent.

alexpott’s picture

Status: Needs work » Needs review
StatusFileSize
new3.98 KB
new9.11 KB

Here's an implementation the pushes the XSS security to the correct level and adds a test.

Status: Needs review » Needs work

The last submitted patch, 11: 2568613-11.patch, failed testing.

The last submitted patch, 11: 2568613-11.patch, failed testing.

alexpott’s picture

StatusFileSize
new669 bytes
new9.77 KB

Fixing the tests... looks like Attribute filtering could be important.

alexpott’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 14: 2568613-14.patch, failed testing.

The last submitted patch, 14: 2568613-14.patch, failed testing.

alexpott’s picture

Hmmm... I'm really not sure that we should be allowing markup here.

mpdonadio’s picture

I can see the use case of allowing HTML in custom format strings, eg to allow date and time to be styled separately, but I see this as an edge case and it can also be solved with a custom formatter (which aren't that hard to make in Drupal 8). Site builders may be at a small disadvantage, but I still see this as an edge case.

So, I am totally on board with not allowing HTML here.

larowlan’s picture

agree, we should filter out markup characters and recommend formatters now that they're a single file plugin (in D7 the effort wasn't worth it)

alexpott’s picture

Status: Needs work » Needs review
StatusFileSize
new6.19 KB
new9.75 KB
   * @param string $format
   *   (optional) If $type is 'custom', a PHP date format string suitable for
   *   input to date(). Use a backslash to escape ordinary text, so it does not
   *   get interpreted as date format characters.

Yes ordinary text but, no, not HTML :)

alexpott’s picture

Title: Replace remaining !placeholder for Non-URL HTML outputs only where format_date() and 'date.formatter' => format() are used » Remove HTML support from date formats and replace remaining !placeholder where format_date() and 'date.formatter' => format() are used
Category: Task » Bug report
Issue summary: View changes
alexpott’s picture

Issue summary: View changes
alexpott’s picture

Discussed with @GaborHojtsy in IRC

I never put markup in date formats, don’t think people have a <sup> for time or anything
sounds a bit crazy :D

And this is coming from a Hungarian which does not have the simplest date format rules...

The year is always written with Arabic numerals. The number of the month is usually written with Arabic numerals but it also can be written with Roman numerals, or the month's full name can be written out, the first letter not being capitalized. The day is also written with Arabic numerals.

See https://en.wikipedia.org/wiki/Date_format_by_country

Scanning https://en.wikipedia.org/wiki/Date_format_by_country I can not see any date format there that requires HTML to be achieved on the browser... perhaps Norway's fractional date...

d.m.y; the fraction form d/m-y is common, but incorrect

wizonesolutions’s picture

I wouldn't say Norway is a consideration for this. As Wikipedia says, the fractional date format is incorrect. On top of that, I can't recall any instances of having seen it in print. I see it handwritten occasionally, probably for brevity, but I don't think any Norwegian developers would be blocked by not being able to do this in core.

eiriksm’s picture

Second the comment about the fractional version in Norway. I would say it is exclusively a handwriting thing in Norway, and I have never seen it used on a screen like that. Although d/m would be a common use, which would still work nicely.

alexpott’s picture

Priority: Major » Critical

This is part of the critical meta.

dawehner’s picture

As long we can make things swappable so in case someone really wants to, they could return a SafeStringInterface, I think its fair to remove the support for it in core.

joelpittet’s picture

Status: Needs review » Needs work
+++ b/core/lib/Drupal/Core/Datetime/DrupalDateTime.php
@@ -134,7 +135,7 @@ public function format($format, $settings = array()) {
-    return $value;
+    return strip_tags($value);

What tags are we expecting that this needs to be stripped? Why not let them escape to leave this alone?

@dawehner re #28 That sounds like a nice idea. Can we currently swap this out now or do we need to convert DrupalDateTime to a service to make it swappable? In that case we'd need a CR, I'd expect?

@lauriii was mentioning to me that it's a value object and shouldn't be a service so I don't know how we would make this swappable? Any suggestions, maybe a hook in format?

dawehner’s picture

@joelpittet
Well, DrupalDateTime is both a value object and it contains the logic to actually render the thing. You know, this is why I was pushing against putting logic into the TranslationWrapper for example. The value object (whether we use it in the strict sense or not) is there to provide a schematic helpful piece of information.

PS: DateTimePlus has a protected render() method, that conflicts with the method in the ToStringTrait

pwolanin’s picture

Assigned: Unassigned » pwolanin

Let me do some work on this.

pwolanin’s picture

Assigned: pwolanin » Unassigned
Status: Needs work » Needs review
StatusFileSize
new10.5 KB
new5.41 KB

I agree with @joelpittet that we should not stip tags, but rather let them be escaped. Discussed with @alexpott too.

I don't think we need to make the patch more complex, but I did also fix the bug dawehner mentioned where we were showing a PHP error by trying to override a public method to protected.

I don't think adding more swappability is in scope for this issue. Sounds like 8.1.

xano’s picture

Status: Needs review » Needs work
  1. +++ b/core/lib/Drupal/Core/Datetime/DateFormatter.php
    @@ -130,8 +130,8 @@ public function __construct(EntityManagerInterface $entity_manager, LanguageMana
        * @return string
    

    If we want to allow (and not implement) returning SafeStringInterface any time during the D8 lifecycle, we must document it before 8.0.0 comes out, or it will be a BC break.

  2. +++ b/core/lib/Drupal/Core/Datetime/DrupalDateTime.php
    @@ -90,8 +90,8 @@ protected function prepareTimezone($timezone) {
        * @return string
    

    If we want to allow (and not implement) returning SafeStringInterface any time during the D8 lifecycle, we must document it before 8.0.0 comes out, or it will be a BC break.

  3. +++ b/core/modules/system/src/Tests/System/DateTimeTest.php
    @@ -134,6 +135,17 @@ function testDateFormatConfiguration() {
    +    //
    

    Empty comment.

lauriii’s picture

Status: Needs work » Needs review
StatusFileSize
new10.78 KB
new1.74 KB
cpj’s picture

Assigned: Unassigned » cpj
cpj’s picture

Assigned: cpj » Unassigned
Status: Needs review » Reviewed & tested by the community

I've reviewed the changes and it looks fine to me. So I'm going to mark it RTBC

dawehner’s picture

+++ b/core/lib/Drupal/Core/Datetime/DateFormatter.php
@@ -130,8 +129,9 @@ public function __construct(EntityManagerInterface $entity_manager, LanguageMana
-   *   A translated date string in the requested format.
+   * @return string|\Drupal\Component\Utility\SafeStringInterface
+   *   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) {

@@ -168,7 +168,7 @@ public function format($timestamp, $type = 'medium', $format = '', $timezone = N
     $settings = array(
       'langcode' => $langcode,
     );
-    return Xss::filter($date->format($format, $settings));
+    return $date->format($format, $settings);

IMHO this is still kinda a lie, isn't it? DateTimePlus always returns a string due to the usage of $value = preg_replace_callback('/\xEF([AaeDlMTF]?)(.*?)\xFF/', $translation_callback, $format);

Does someone mind giving some extra lights for me why this happens?

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

@dawehner it was because what @Xano wrote in #33. Personally I think those changes should be rolled back. The returns should not specify something they do not do.

alexpott’s picture

Can someone create a followup to implement a DateFormatterInterface so that the DateFormatter service is at least swappable.

mpdonadio’s picture

Created #2571647: Create DateFormatterInterface. I'll have a patch ready tonight or tomorrow, and will handle any re-rolls of it b/c doc changes here.

lauriii’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new10.49 KB
new1.2 KB
dawehner’s picture

Thank you @lauriii

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 42: remove_html_support-2568613-42.patch, failed testing.

lauriii’s picture

Status: Needs work » Needs review
StatusFileSize
new9.21 KB

Rerolled the patch

stefan.r’s picture

Status: Needs review » Reviewed & tested by the community

checked the interdiff and reroll looks good

  • effulgentsia committed 3a0b017 on 8.0.x
    Issue #2568613 by alexpott, lauriii, pwolanin: Remove HTML support from...
effulgentsia’s picture

Title: Remove HTML support from date formats and replace remaining !placeholder where format_date() and 'date.formatter' => format() are used » [maybe needs change record] Remove HTML support from date formats and replace remaining !placeholder where format_date() and 'date.formatter' => format() are used
Status: Reviewed & tested by the community » Needs review
Issue tags: +Needs change record

Pushed to 8.0.x, but I'm not sure if this needs a change record. I think this restores 8.0.x to the same behavior with respect to date formats that 7.x has. And I don't know when the last time HTML in date formats actually worked in D8 HEAD, but it might be worth searching through existing change records to see if HTML-in-date-formats was ever mentioned, and at least correcting that. And maybe a new CR explicitly mentioning the change here makes sense too. Setting to "needs review" rather than "needs work", since I don't know if any changes or a new CR is needed.

alexpott’s picture

Title: [maybe needs change record] Remove HTML support from date formats and replace remaining !placeholder where format_date() and 'date.formatter' => format() are used » Remove HTML support from date formats and replace remaining !placeholder where format_date() and 'date.formatter' => format() are used
Status: Needs review » Fixed

There is no harm in having a CR to say this is no longer supported - created https://www.drupal.org/node/2572299

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.