Closed (won't fix)
Project:
Drupal core
Version:
9.3.x-dev
Component:
token system
Priority:
Major
Category:
Task
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
1 Oct 2015 at 19:01 UTC
Updated:
16 Nov 2021 at 20:19 UTC
Jump to comment: Most recent
Comments
Comment #2
catchOuch, nicely caught.
Comment #3
effulgentsia commentedI was curious if there were any real use cases of Token::generate() being called by anything other than hook_tokens() implementations themselves, and here's a couple that I found:
Not sure if we want the docs here expanded to explain such use cases and/or expanded to also mention that hook_tokens() implementations call generate() for their nested tokens.
Comment #4
alexpottLet's get test coverage for this.
Comment #5
dawehnerInteresting, well I always thought Token::generate() was just public because its also used in some hook_tokens() implementations.
Comment #6
catchIf hook_tokens() call it, wouldn't that result in double escaping if we move the sanitization to Token::generate() - Html::escape() twice.
Comment #7
dawehnerYeah I think so.
Comment #8
effulgentsia commentedNo, because of the SafeString::create() in:
Comment #10
dawehnerAh perfect, well, then we could easily move it to
Token::generate(), sorry for the noise.Comment #19
adamps commentedIt's not always the case that a token will be used to form markup. For example, they could be used for email headers, or to make a URL (see RRSSB module). Or the value might be some intermediate stage that after further calculations would later be converted to markup. In #2580723: Fix token system confusion, with new function Token::replacePlain() we helped clarify these cases by adding new methods
replacePlain()andreplaceMarkup().Therefore if we consider that
generate()is part of the public interface, then I propose that it should return the token values as-is. There is no ambiguity, because the markup once implementMarkupInterface. There is no security risk, because Drupal has mechanisms to auto-escape a plain text value (e.g. in a twig template).This fix as it stands would be a BC-break. If any code is calling
generate()then using the result in a plain-text context, then HTML escaping could corrupt the value. For example if a module added a handy token [user:mail_display], which returns something likeAdam<adam@example.com>then it certainly doesn't want to be escaped.I propose this issue could be marked as "Closed (works as designed)"
Comment #20
darvanenI tend to agree. I'd like a third opinion though.
Comment #21
larowlanI'll ping @effulgentsia and ask if he agrees to close this in favour of #2580723: Fix token system confusion, with new function Token::replacePlain()
Comment #22
andypostTests should cover translatability of markup, ++ to new methods and deprecate old ones for D11
In D10 under PHP 8.0 it could use https://wiki.php.net/rfc/stringable which has polyfill in Synfony
Comment #23
mxh commentedThis is exactly how I'm using the
Token::generate()method in Mustache Templates. Moving the sanitization part toToken::generate()would get me into trouble, possibly making the original core service unusable and I'd need to either replace that service by my own or decorate it (once more). Thus +1 for #19..ECA heavily relies on the Token service as well, there it's being used for many different purposes, including for variable assignments. Thus changing the behavior of
::generate()might likely also make the service harder to use in that regard.Comment #24
mxh commentedComment #25
darvanen@larowlan I'm guessing there was no response from @effulgentsia?
I propose we close this ticket based on the recent feedback with the caveat that they can reopen it if there's a need.
Comment #26
larowlanHe's been busy with CKEditor 5, I'll follow up
Comment #27
effulgentsia commentedRegardless of the original intent of this issue, I agree that at this point, we should not break BC of Token::generate(), so closing this.
I haven't yet reviewed #2580723: Fix token system confusion, with new function Token::replacePlain() (and no need to delay that on me if others are happy with it) to know if it addresses the underlying problems that prompted this issue, but even if it doesn't, it doesn't have to be that issue's responsibility to do that.