Closed (fixed)
Project:
Drupal core
Version:
8.4.x-dev
Component:
datetime.module
Priority:
Major
Category:
Task
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
29 Jul 2016 at 19:34 UTC
Updated:
3 Apr 2017 at 08:04 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
mpdonadioWill be done as part of #2161337: Add a Date Range field type with support for end date.
Comment #3
jhedstromThis is applicable to existing formatters, even before #2161337: Add a Date Range field type with support for end date is taken into account.
Comment #5
jhedstromThis, in theory anyway, should not require changes to the tests, so I'm going to get started (if changes to tests are needed, it should wait on #2786583: Create a base class for DateTimeFieldTest and DateRangeFieldTest and move common code into it).
Comment #6
mpdonadioAgree that there should be no test changes. We also need to look at the #293 patch and see what refactoring needs to get back in, so we can use that in the DareRange followups. Both should be able to be handled in this issue.
Comment #7
jhedstromOne thing I just noticed is that because this logic is so duplicated, the fix for #2139551: Support RDFa output in date formatter only was applied to one of the 3 formatters, so this might qualify as a bug.
Comment #8
jhedstromThis is a first-pass. It simply pushes the
viewElements()method that was duplicated into the base formatter.Still need to look at what this means for the daterange formatters.
Comment #9
mpdonadioRe #7, that would almost make Default, Plain, and Custom identical...
Comment #10
jhedstromIndeed--I realized that, and am now trying to determine the original intent of the Plain formatter. The docblocks are less than clear:
If the intent of Plain is to not have the ISO metadata, then that one should retain its own
viewElements()method.The fact that the tests pass if this is the intent, means we need additional test coverage.
In the meantime, here's the patch with daterange's
viewElementsfactored into the trait.Comment #12
jhedstromAh, so we do have coverage for the date ranges, it's just missing from the datetime formatter.
Comment #13
jhedstromSo the 'plain' formatter having different markup makes sense, but for the 'custom' one, is there a reason not to support RDFa as the default one does?
Comment #14
mpdonadioI could see custom having the time element + RDFa. The default pulls from the date formats you have defined in config, and then custom lets you specify your own format. They are two different use cases.
Comment #15
mpdonadioTook a quick look look at this. Do we want to start from the DateTime formatter changes from https://www.drupal.org/files/issues/2161337-293.patch and see what can be improved there? I'm wondering if moving logic to the buildDate() function (which can be reused by the range formatters) outweighs trying to consolidate viewElements().
Comment #16
jhedstromI was focused on the
viewElementsmethod since it had the heaviest duplication, and the formatting itself is (in theory) handled in a separate method. For the custom formatter in the datetime field type, it honestly looked like an oversight from the RDFa patch that it had been left out since prior to the patch, the 2viewElementsmethods appeared identical. I'll try to round up on the changes from #2161337: Add a Date Range field type with support for end date shortly.Comment #17
jhedstromI opened #2793143: The datetime and daterange formatters (except plain) should use the HTML time element for the custom formatter issue.
This patch moves the
viewElementsmethod back to custom and plain, and simplifies the duplicated code back into the build methods.Comment #18
jhedstromComment #19
jhedstromComment #21
mpdonadioThis needs a reroll b/c #2811725: Error when render Datetime Range field: Error: Unsupported operand types. I was hoping some other patches would land before addressing this, but we should get this in as a step towards other work that needs to be done. Assuming a good reroll, and passing tests, I am OK with this.
Comment #22
denutkarsh commented@mpdonadio Here is the patch that i rerolled.
Comment #23
denutkarsh commentedComment #25
denutkarsh commentedOk, so in this issue
#2811725: Error when render Datetime Range field: Error: Unsupported operand typesthe way viewElements() work was also changed. Lets try the current patchComment #27
jhedstromThanks @denutkarsh! I think the rebase needed to actually update the moved method's logic in the trait, rather than re-introduce the method (which this issue is trying to consolidate those).
Comment #28
cilefen commentedComment #29
mpdonadioReroll looks good; the merge conflict looks like it is taken care of properly, plus we have test coverage of that portion.
I think this is good to go.
Comment #30
bkosborneThis issue is blocking #2632040: [PP-1] Add ability to select a timezone for datetime field. I reviewed it as well and agree it looks good.
Comment #31
mpdonadioComment #33
bkosborneLooks like it passed to me?
Comment #35
mpdonadiohttps://www.drupal.org/pift-ci-job/581855 seems to be a known random, #2829040-31: [meta] Known intermittent, random, and environment-specific test failures
Comment #37
mpdonadioBack b/c a CI error, https://www.drupal.org/pift-ci-job/582322
Comment #39
xjmThis @todo looks incorrect. Remove the method? At most it should be deprecated, or I guess the implementation removed and... I dunno.
Does this mean something like "Remove the foobar strategy in https://www.drupal.org/node/2793143"? Because #2793143: The datetime and daterange formatters (except plain) should use the HTML time element looks like it changes the code but does not remove the implementation of the method.
So if this implementation is removed, is it falling back to a base implementation?...
...Ah yes, this base implementation that is being added. I get it now. That's kind of the point. :)
NW for the first point though. Also queuing updated test runs against the correct environment.
Comment #40
xjmBumping to major since it is marked as blocking #2632040: [PP-1] Add ability to select a timezone for datetime field which is major.
Comment #41
mpdonadioFor those following along, the DrEditor context isn't correct in the paste in #39. We are discussing the comment on DateTimeCustomFormatter::viewElements and DateRangeCustomFormatter::viewElements():
I disagree with that comment being wrong, but we can remove it if desired.
This issue is about cleaning up some redundant methods in the formatters, and moving code onto the base class (share all the code!).
DateTimeCustomFormatter::viewElements and DateRangeCustomFormatter::viewElements cannot really go away now and inherit the base methods because the field markup is different. #2793143: The datetime and daterange formatters (except plain) should use the HTML time element will result in all of the datetime/daterange fields using the same markup, except for the plain formatters: plain fields are um, plain, and the others get wrapped in a
<time>element. So, in the final version of that, the plain formatters will have their own viewElements, and the other formatters will use the one from the base class, which is what that comment is talking about.So, #2793143: The datetime and daterange formatters (except plain) should use the HTML time element is really soft blocked on this issue, which is why that issue wasn't postponed at the time (we could still make progress on it), and more important work was being done on other issues. I just postponed it.
Comment #42
xjmDoesn't really need RM review specifically.
I don't really follow #41. Can we just improve the comment to say what the intended followup is, rather than "Remove this method" which is ambiguous and seems to contradict the proposed patch on the issue referenced in the @todo?
Comment #43
mpdonadioGood idea. My suggestion is
Or something along those lines that reads better. Updated the other issue to better reflect the scope and solve.
Comment #44
jhedstromGood call on rewording that
@todoI had to figure out what the comment was talking about even though I added it :)Comment #45
mpdonadioCan we change this is ->getTimestamp() to match #2851902: Replace $date->format('U') with $date->getTimestamp()?
Otherwise RTBC.
Comment #46
denutkarsh commented@mpdonadio Here is the new patch.
Comment #47
mpdonadioLooks good to me. Thanks.
Comment #48
xjmSo I am reviewing this patch carefully doing word diffs on the refactored lines. I've looked at all but the date range trait so far. One question has come up:
So this appears to be changed from
#markupto#plain_text. That's probably a good security hardening, but could there be any formatting regressions or double-escaping depending on the format? What is the expected return format offormatDate()? Do we have test coverage for date formats that include restricted characters, and how is it sanitized in HEAD?Comment #49
xjmOkay and my only other question is,
DateTimeRangeTrait::viewElements()seems to be added in this patch, but not used anywhere that I can tell?Comment #50
mpdonadio#49, crap er I mean nice catch; we had a bad reroll somewhere and removed hunks got back in somewhere between #27 and #46.
#48, that is likely b/c the daterange formatters use '#plain_text', so this was done for consistency. The comment on DateFormatterInterface::format() is
And see also \Drupal\KernelTests\Core\Datetime\FormatDateTest(). We don't explicitly test this in the field tests, but I just manually tested this and didn't get XSS in 8.4.x HEAD (even when I hardcode the string in the formatter itself) with the datetime custom formatter and get double escaping with the daterange custom formatter. Crap.
I'm punting this directly to jhedstrom for a look, since the majority of the work on this was his.
Comment #51
jhedstromWow, great catch! That would have been too bad to get the cleanup task in with half the cleanup not there :)
Comment #52
jhedstromStill need to address #48.
Comment #53
jhedstrom@mpdonadio
is the double-escaping just an issue with this patch, or it happens with or without?
Comment #54
mpdonadioI agree with @xjm that this is going to be a regression. I think we should
- Change this to '#markup' to preserve how the datetime formatters work
- Possible CR to announce that daterange custom will now be '#markup'
- Add assertions to datetime and daterange to custom formatters to make sure normal tags (eg, bold) get preserved
- Add assertions to datetime and daterange to make sure an XSS gets stripped
This will partially be undone by #2793143: The datetime and daterange formatters (except plain) should use the HTML time element, but at least the test coverage will be there to make sure we don't break anything.
Comment #55
jhedstromHere's the fixes/assertions for #54. It also fixes the failures from #51 which were due to that patch removing the daterange plain formatter's
viewElements.Regarding a CR, this isn't really a change to custom formatters since they already use
#markup:Comment #56
jhedstromAh, now I see--this actually fixes a bug for the daterange formatter where it displays the double-escaped html (and script tags). Do we typically do CRs for bug fixes? I'd prefer not to split that up into a separate issue, so can write a CR along the lines of bringing the daterange formatter into step with the normal date formatter...
Comment #57
mpdonadioYeah, it changes how the daterange custom formatter works. We don't need a CR for the bugfix, but we would need one to announce that a render array is changing, in case anyone is monkeying with it directly.
Need to look at this closer tomorrow, but
I think we want this to have the equivalent of
<script>alert("XSS")</script>instead.Comment #58
jhedstromI've added a CR here: https://www.drupal.org/node/2854661
This changes the XSS to the suggestion in #57.
Comment #59
mpdonadioLooks good. Here are those two lines w/o the crazy escaping (did search/replace)
I think this is good to go. I would much prefer #1918994: Improve Datetime and Daterange Widget accessibility land before, this, though in case we get into reroll-purgatory. That patch is much more important than this one right now.
Comment #60
xjmThanks for adding tests for XSS sanitization!
Can we use a quoteless XSS exploit like in the first comment of #2506021: When testing for XSS (cross-site scripting) with assembled HTML, use a test exploit that does not contain quotes in the tests? Given the string manipulation here it seems like that would be a better choice.
I definitely agree #1918994: Improve Datetime and Daterange Widget accessibility is more important, but this patch is easier to commit so long as they don't break each other. I haven't had an opportunity to do a full review of the other.
Comment #61
mpdonadioNo fuzz / merge warnings. DateTimeFieldTest, DateRangeFieldTest, DateTimeItemTest, and DateRangeItemTest all pass locally.
Comment #62
jhedstromThis changes the XSS test to be quoteless.
Comment #63
xjmIs that valid JS?
Comment #64
jhedstromLol, probably not. I'll rework :)
Comment #65
mpdonadio<script>alert(String.fromCharCode(88,83,83))</script>with all of the whacky encoding will probably work.Comment #66
jhedstromHere's the updated XSS suggested above.
Comment #67
mpdonadioInterdiff looks good ; took the whacky XSS strings and searched/replaced to make sure they are valid JS when decoded.
Comment #68
xjmChecking the full interdiff since the last time I reviewed...
Comment #69
xjmSome unused use. I can fix these on commit later when my airplane is not landing, or someone else can clean them up in the interim. Leaving RTBC.
Comment #71
xjmCommitted to 8.4.x with the unused uses fixed. Thanks!
Comment #73
denutkarsh commented