I'm trying to map out how the Play framework supports escaping.
This is a nice page spelling out the needed functionality: https://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet
So I'm trying to relate that to Play template features and fully understand what Play does and doesn't do.
${}
or the escape()
functionescapeJavaScript()
http://www.playframework.org/documentation/1.2/javaextensionsAnother point of confusion is the support for index.json
(i.e. using templates to build JSON instead of HTML). Does ${}
magically switch to JavaScript escaping in a JSON document, or does it still escape HTML, so everything in a JSON template has to have an explicit escapeJavaScript()
?
There's also an addSlashes() on http://www.playframework.org/documentation/1.2/javaextensions , but it doesn't seem quite right for any of the situations I can think of. (?)
It would be great to have a thorough guide on how to do all the flavors of escaping in Play. It looks to me like the answer is "roll your own" in several cases but maybe I'm missing what's included.
I've been looking into this so decided to write up my own answer based on what you already had, this OWASP cheat sheet and some experimentation of my own
HTML escaping:
Attribute escaping: (common attributes)
<a id=${data.value} href="...">...</a>
<a id='${data.value}' href="...">...</a>
% href=javascript:alert('XSS')
%' href=javascript:alert(window.location)
JavaScript escaping: (and complex attributes)
<a onmouseover="x='${data.value}'; ..." href="...">...</a>
'; javascript:alert(window.location);//
CSS escaping:
URL escaping: