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

Rebase #6094 "Provide a more useful definition of json_escape" and update docs. #11526

Closed
wants to merge 2 commits into from

Conversation

Empact
Copy link
Contributor

@Empact Empact commented Jul 21, 2013

See #6094 for discussion.

jfirebaugh and others added 2 commits July 20, 2013 18:21
The previous definition removed double quote characters,
and hence returned invalid JSON, making it unsuitable
for the most common use case: bootstrapping JSON in
a <script> element.

The original definition was at 0ff7a2d,
without indication that the double quote behavior was intentional.
It seems likely that it was simply an oversight after
copy and pasting the definition of html_escape.

Furthermore, since json_escape does not return a HTML-safe
string if not passed one, it is unnecessary for it to escape
characters other than the slash.
@sorentwo
Copy link
Contributor

👍 Thanks for rebasing this!

@jcaudle
Copy link

jcaudle commented Jul 31, 2013

👍 on the rebase. @steveklabnik is this something that could work?

@steveklabnik
Copy link
Member

I like to get a 👍 from @NZKoz before merging anything even vaguely security related, and I think this counts.

@NZKoz
Copy link
Member

NZKoz commented Jul 31, 2013

👎 we should do this differently.

You're changing the escaping methods here, and changing the tests, and changing the intention of what this method is used for. At the very least this would not be suitable for backporting to the 4.0 series and would have to live in master until 4.1 ships after a long painful beta series :)

The whole rationale for and method of using json_escape is kind of broken. When you're escaping values you need to work at the components, not the document as a whole. So with JSON this means escaping has to happen to the contents of the strings in JSON, not the document as a whole. It's the same reason you need to escape every fragment of html you generate and components of an SQL query, you can't just do:

  response.body = h(response.body)
  sql = connection.escape_some_how(entire_sql)

The reason it strips your " chars is that it can't possibly tell the difference between a " in user provided data and one in the json that you want to keep. Clearly the helper isn't fit for purpose, and can't be made so.

So going back to what people want to do, ideally it's this:

<script>
  var someThing = <%= @some_thing.to_json %>;
</script>

Which they can do if they remember to set escape_html_entities_in_json which is weirdly implemented and poorly documented. However they'll still have to call raw or html_safe to have it output correctly. There's really no reason for turning this option off, the strings are still perfectly valid JSON, it's just encoded differently.

So if we're going to go to the trouble of changing all this escaping, and we should, I'd suggest:

  1. always escape the html entities in json, remove that option
  2. make the encoded json be html_safe as we know the values are safe for inclusion in the body of the document at that point.

The only issue that would raise from my POV would be someone doing something crazy like:

<div id='something' json-in-attribute-for-some-reason="<%= @something.to_json %>">

I'm not sure if there'd be an attack vector there, it'd be worth investigating? Would that solve the issues people asked about in #6094 ?

@Empact
Copy link
Contributor Author

Empact commented Aug 12, 2013

@NZKoz, @steveklabnik for the record I'm 😐 about this PR. Just want to get the the PRs moving. I'll leave it to @jfirebaugh and #6094 to defend.

@Empact
Copy link
Contributor Author

Empact commented Sep 4, 2013

Closing due to my own indifference to pursuing this.

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

Successfully merging this pull request may close these issues.

None yet

6 participants