Skip to content

Restricted mode: Avoid variable expressions returning strings in processors for event handlers (th:on*) #705

Closed

Description

@danielfernandez

When executing HTML template code such as:

<div th:onclick="|doSomething('${mydata}');|">

The resulting attribute value for onclick will be correctly HTML-escaped, but the result of ${mydata} inside that expression will not be escaped as a JavaScript literal because Thymeleaf does not know about the specifics of the JavaScript code written inside this attribute, or whether it is JavaScript code at all.

In some cases, this might lead to confusion from developers who might think their use of ${mydata} in that code is safe, while from the standpoint of JavaScript code the value of that variable will be output unescaped (only HTML-escaping will be performed, but this is because of it being an HTML attribute). This could lead to unintended code injection scenarios.

In order to help devs avoiding this scenarios, Thymeleaf should extend its restricted expression evaluation mode for all th:on*attribute processors –for JavaScript event handlers– in order to forbid output of any variable expression that results in anything but a Number or a Boolean. This will in effect forbid variable expressions returning String or any other object that can be rendered as a text literal in a way that wouldn't be necessarily trustable.

With this, a better way to write the code above would be to use (adequately HTML-escaped) data- attributes for text literal variables, like:

<div th:data-thing="${mydata}" onclick="doSomething(this.getAttribute('data-thing'));">

Activity

  1. added this to the Thymeleaf 3.0 milestone on Jul 21, 2018
  2. added a commit that references this issue on Jul 21, 2018
    b242fd3
  3. danielfernandez commented on Jul 21, 2018

    @danielfernandez
    MemberAuthor

    Already in 3.0.10-SNAPSHOT

  4. arkaev commented on Oct 18, 2018

    @arkaev

    Is th:on-* deprecated now? I don't understand how to use them now

  5. T3rm1 commented on Oct 25, 2018

    @T3rm1

    It still works if you put a link expression around it. I doubt this is intended though.
    <div th:onclick="@{doSomething('${mydata}');}">

  6. philBrown commented on Nov 23, 2018

    @philBrown

    This seems an extreme change for a patch release

  7. madness-inc commented on Jan 28, 2019

    @madness-inc

    Agree with @philBrown, in fact this breaks backwards-compatibility.
    Needed to adjust all our templates.

  8. BELAGGOUN commented on Jun 11, 2019

    @BELAGGOUN

    In my project, I want to perform a searching method invoked by the keypress event. (in an Input)
    can I have an example showing how to do that or explaining how to call a controller method from a JS function?
    thanks

  9. MrStanaland commented on Jul 23, 2019

    @MrStanaland

    Aside from branching Thymeleaf, does anyone have any suggestions for disabling this "feature" entirely?

    Or, at least how to pass onclick's as fragment parameters? For example, if we want to pass the entire onclick to the following fragment:

    widget.html:
    <th:block th:fragment="widget(id, name, text, icon, onclick, color)">
    //Complex widget created using the parameters.
    </th:block>

    This is a large project, hence we have frameworks built around Thymeleaf, including a custom dialect. Dear maintainers, we are putting our trust in you....please avoid crippling changes in a point release!

  10. added a commit that references this issue on Jul 18, 2023
    3d538c6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions