0

I have to secure a java web application against XSS attacks. There are parts of the code that look like this:

<script>
  jsvariable = ${jspvariable}
  use(jsvariable)
<script>

My first tough was to do something like this:

<script>
  jsvariable = ${fn:escapeXml(jspvariable)}
  use(jsvariable)
<script>

But escapeXml doesn't avoid XSS inside a script tag. What's the correct way to send a value from jsp to javascript.

jspurim
  • 925
  • 8
  • 25

1 Answers1

3

You could write your jsp data in a text/json block which will be ignored by the browser:

<script id="demo" type="text/json">
  ${jspvariable}
</script>

You can then parse the values in your javascript file on dom ready:

console.log(JSON.parse(document.getElementById('demo').innerHTML));

A XSS injection could lead only to a JSON parse error

jantimon
  • 36,840
  • 23
  • 122
  • 185
  • Actually what about this case: `jspvariable = {} – jspurim Aug 18 '14 at 01:56
  • 2
    You should combine it with CSP and disallow any inline javascript: http://www.html5rocks.com/en/tutorials/security/content-security-policy/?redirect_from_locale=de – jantimon Aug 18 '14 at 06:29