Proposed commit message
Issue #2469563 by marthinal, pjonckiere, b0unty, laurii, dawehner, Fabianx: Double-escape on Views filters
Problem/Motivation
When Views filter list has groups, some of the links on the filters list gets double-escaped.
See #2297711: Fix HTML escaping due to Twig autoescape and https://www.drupal.org/node/2311123 for backstory.

Proposed resolution
- Use SafeMarkup::format() instead of concatenating directly.
-
Remaining tasks
-Create a patch fixing the bug
User interface changes
-
API changes
-
| Comment | File | Size | Author |
|---|---|---|---|
| #33 | filters-2469563-33.patch | 4.27 KB | marthinal |
| #33 | interdiff-2469563-28-33.txt | 1.12 KB | marthinal |
| #28 | interdiff-2469563-27-28.txt | 1020 bytes | marthinal |
| #28 | filters-2469563-28.patch | 4.26 KB | marthinal |
| #27 | interdiff-2469563-22-27.txt | 2.25 KB | marthinal |
Comments
Comment #1
marthinal commentedProbably Major.
We need to mark as secure and probably when creating the link.
Comment #2
lauriiiThe change looks good to me. All the text that is being marked as safe in the patch can be considered as safe. We could still add a test coverage for this.
Comment #3
marthinal commentedComment #4
marthinal commentedComment #5
marthinal commentedUsing inline_template to avoid SafeMarkup::set().
Comment #6
lauriiiGood work, I like the usage of inline template here if it solves the problem!
Is this change intentional? I don't think this fits into the scope of this issue.
Comment #7
marthinal commentedOops, Sorry I forgot to comment that @dawhener told me to comment this change with FabianX.
Comment #8
dawehnerLet me explain, where this is coming from.
So the settings links are part of core/modules/views_ui/templates/views-ui-display-tab-setting.html.twig, which has the following twig code:
{{ settings_links|safe_join('<span class="label"> | </span>') }}The links which are part of this issue, are put into that safe_join, and are now, after this change, a render array, so we need be able to render both a string and a render array in there as well.
Comment #9
lauriiiWe need to document that change then.
Comment #10
dawehnerThe inline template itself, looks fine, so all we need is a test
Comment #11
lauriiiNeeds work for tests
Comment #12
lauriiiComment #13
marthinal commentedworking on the test.
Comment #14
Anonymous (not verified) commentedThis seems like a double from #2432759: views filter formatting in or group.
This issue probably has a better fix, but the other one already has some test coverage.
Comment #15
marthinal commented@pjonckiere thanks!
I think we need to create a FilterUITest and move the filter handler UI test there. So my test is different... also using test_filter_groups with this test.
The patch verifies if we have an array or not before escape.
Comment #18
fabianx commentedOne way that would work without inline templates is:
Because link should be safe to output and operator is safe to output (as it came via $this->t), the whole thing should work.
That's not the right fix.
And I don't think we want to support that for drupal_join.
This should be calling twig_drupal_escape_filter (which was recently moved to the twig extension).
If we wanted to do that at all ...
Comment #19
marthinal commented@Fabianx thanks!
So let's try it then.
Comment #21
Anonymous (not verified) commentedAn interdiff would have come in handy to see how you addressed the feedback from @Fabianx in #18 :)
Anyway, looks good to me. Some nitpicking though:
I think this needs @inheritdoc.
This wrapping should be 80 chars.
I think this could be one line.
Comment #22
marthinal commented@pjonckiere sure!
Please add author attribution to @pjonckiere and @b0unty because they were working on the same bug but from a different issue issue. Detected in #14. :)
Comment #24
fabianx commentedAdded proposed commit message.
Comment #25
fabianx commentedRTBC, looks great now.
Comment #26
xjmThat's a pretty ugly bug -- good find. Thanks for the test-only patch as well. I just have a couple questions about the patch:
Minor: This should be only a single line of fewer than 80 characters, so we can take a few words out or move details to a separate paragraph.
Why not just two separate full
t()? I think the word order here could be affected by the language, which means we should give translators control over it, right? Rather than translating "AND" and "OR" separately with no context.Also, per #22, I added credit for reviewers, but @b0unty has not commented on the issue so will need to be added to the commit message manually as per the summary.
Comment #27
marthinal commented@xjm thanks for reviewing it :)
Well, maybe this is the way to give translators control over it...
Comment #28
marthinal commentedShort array syntax.
Comment #29
dawehnerIt is odd that you can mark pretty much everything es safe by using t() in connection with
SafeMarkup::format(). Honestly, I don't see the advantage over the previous solution ofusing an inline template, in case there is one obvious one, please let's update the documentation .. Its not that we call that kind of code extremely often, it a rare operation on some admin UI.
Comment #31
marthinal commented@dawehner The problem with the inline_template is that, in this case, we cannot render correctly.
at core/themes/engines/twig/twig.engine
$output .= $separator . SafeMarkup::escape($item);We need render() instead SafeMarkup::escape() to render correctly.
To be honest I'm not sure why in other places we can use the inline_template and not here... I need to investigate about it...
Maybe @FabianX knows more about it...
Comment #32
xjmThis is still translating the "or" and "and" twice and then formatting them again after that. We should just have one
t()and noSafeMarkup::format()call. Like:No need to nest the calls; this makes it harder for translators.
Comment #33
marthinal commented@xjm Yes, makes no sense at all...
SafeMarkup::format
Comment #34
dawehnerI still think that this is an abuse of t() as string concatenation but well.
Comment #35
alexpottWhy can't we do the following...
I think this is better because
'!link OR'is more likely to result in errors than translating'OR'Should be... Contains \Drupal\views\Tests\FilterUITest
Comment #36
xjm@alexpott, did you read #26 and #32?
Comment #37
alexpott@xjm pointed out that
'!link OR'and'!link AND' at least gives some context.Comment #38
alexpottThis issue is a normal bug fix, and doesn't include any disruptive changes, so it is allowed per https://www.drupal.org/core/beta-changes. Committed 6b8cc66 and pushed to 8.0.x. Thanks!
Fixed on commit.
Comment #40
xjmFor the record, @dawehner and I discussed the use of the
t()in IRC. The correct thing to do would be to make the whole thing a template, but that's out of scope for fixing this bug.My perspective is isn't an "abuse" of
t(); it's exactly whatt()is for -- to translate a word in context. In this case the context is short, but it is still a relevant. Languages do not translate word-for-word, and the little bit of context will help.IMO the actual "abuse" is using
for formatting in the first place, but having it inside at()is better than not usingt()with context for the link placeholder. (Edited for nonsense typos and clarity.)Comment #41
xjm