I have a simple Java object that I'm using Jackson to serialize to JSON, and then I'm dropping that into a <script>
tag in my JSP page as part of initializing a JavaScript object. e.g.
<script>SomeLib.load(${someObject});</script>
This works great unless one of the fields of someObject is a String that contains "</script>"
, because of this issue. That is, if the output looks like this:
<script>SomeLib.load({"someValue":"hacked!</script>"});</script>
then the browser (tested in both Chrome and FF so far) believes the </script>
tag after hacked!
is closing the script tag. Which breaks the JavaScript and leaves "});</script>
visible to the user.
Is there a way to get Jackson to escape that value in some fashion that will fix this problem?