OWASP/owasp-java-encoder

Implement Encode.forJSON()

OpenGG opened this issue · 6 comments

The XSS prevention cheat sheet mentioned the anti-pattern, placing json inside js block.

<script>
  var initData = <%= data.to_json %>; // Do NOT do this without encoding the data with one of the techniques listed below.
</script>

This has been a common practice, and is an important part of redux's server rendering. But owasp-java-encoder doesn't has the right encoding function to deal with it.

Is it consider doable and necessary, to implement Encode.forJSON()?

From what I read on the cheat sheet, converting < to \u003c would be good enough.

@jmanico

  • Encoding everything in the unicode format \u0000 inflates the output, and may not be the default setting for most JSON encoders.
  • An extra HTML-encode-js-decode operation bloats the problem and make it more complicated. Consider the situation that multiple JSON being embed into one HTML.
  • var initData = JSON.parse('<%= Encoder.forJavaScript(data.to_json) %>'); does work, but not every browser supports JSON.parse(), hence server-side encoding introduces client-side dependency.

For now I can go with JSON.parse('<%= Encoder.forJavaScript(data.to_json) %>'), but I really hope for a more intuitive Encode.forJSON().

As a common practice, embedding JSON in HTML is popular. Implementing such encoding function can help the community moving forward.

@jmanico

Again, we can’t guarantee an encoding JSON format that will work in all situations. It depends where you embed JSON.

Make sense, Consider the following two cases, one encoding function Encode.forJSON() won't work well in both of them.

<a onclick="javascript:x=${Encode.forJSON(data)}">

<script>
x = ${Encode.forJSON(data)}
</script>

This could be the limitation of encoder libraries like owasp-java-encoder. Without further information of the context, an encoding library can't properly escape complex data type like JSON.

I will look into contextual automatic escaping templates, and see if they can do better. Meanwhile, please mark this issue as "wont fix".

I agree that automatic encoding templates are the best path forward. Thanks for jumping in here with this thread.