question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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'));">

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
philBrowncommented, Nov 23, 2018

This seems an extreme change for a patch release

4reactions
T3rm1commented, 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}');}">

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting and tips — Numba 0.50.1 documentation
A common reason for Numba failing to compile (especially in nopython mode) is a type inference failure, essentially Numba cannot work out what...
Read more >
Release Notes - Numba documentation - Read the Docs
Will Numba work for my code? What is nopython mode? How to measure the performance of Numba? How fast is it? How does...
Read more >
learning-zone/python-basics: Python Basics ( v3.x ) - GitHub
The language comes with a large standard library that covers areas such as string processing like regular expressions, Unicode, calculating differences ...
Read more >
Python 2.7 Quick Reference
Python (from 2001), Text processing in Python (from 2003) ... If no scriptFile or command, Py thon enters interactiv e mode.
Read more >
Nim Manual
Since constant expressions can be used as an input to semantic analysis (such as for defining array bounds), this flexibility requires the compiler...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found