You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Restricted mode: Avoid variable expressions returning strings in processors for event handlers (th:on*) #705
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:
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
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!
Reacted by Nikolas Schmidt-Voigt and Clint Checketts
Activity
Fixes #705 - Restricted mode: Avoid variable expressions returning st…
danielfernandez commentedon Jul 21, 2018
Already in
3.0.10-SNAPSHOTAdded tests for thymeleaf/thymeleaf#705
Added tests for thymeleaf/thymeleaf#705
Added tests for thymeleaf/thymeleaf#705
Added tests for thymeleaf/thymeleaf#705
Added tests for thymeleaf/thymeleaf#705
Added tests for thymeleaf/thymeleaf#705
arkaev commentedon Oct 18, 2018
Is th:on-* deprecated now? I don't understand how to use them now
T3rm1 commentedon Oct 25, 2018
It still works if you put a link expression around it. I doubt this is intended though.
<div th:onclick="@{doSomething('${mydata}');}">philBrown commentedon Nov 23, 2018
This seems an extreme change for a patch release
madness-inc commentedon Jan 28, 2019
Agree with @philBrown, in fact this breaks backwards-compatibility.
Needed to adjust all our templates.
BELAGGOUN commentedon Jun 11, 2019
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
Update template for changes in Thymeleaf 3
MrStanaland commentedon Jul 23, 2019
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!
Fixes thymeleaf#705 - Restricted mode: Avoid variable expressions ret…