Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Encode.forJSON() #16

Closed
OpenGG opened this issue May 9, 2018 · 6 comments
Closed

Implement Encode.forJSON() #16

OpenGG opened this issue May 9, 2018 · 6 comments

Comments

@OpenGG
Copy link

OpenGG commented May 9, 2018

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
Copy link
Member

jmanico commented May 9, 2018 via email

@jmanico
Copy link
Member

jmanico commented May 9, 2018 via email

@OpenGG
Copy link
Author

OpenGG commented May 10, 2018

@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
Copy link
Member

jmanico commented May 10, 2018 via email

@OpenGG
Copy link
Author

OpenGG commented May 15, 2018

@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".

@jmanico
Copy link
Member

jmanico commented Aug 15, 2018

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

@jmanico jmanico closed this as completed Aug 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants