-
Notifications
You must be signed in to change notification settings - Fork 22k
Provide a more useful definition of json_escape #6094
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
Conversation
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.
+1 |
3 similar comments
+1 |
+1 |
+1 |
So, what's the state with this? The current |
The current state is 'nobody from core has reviewed it.' |
This needs a rebase. |
@jfirebaugh are you able to rebase this so that it can be reviewed? Looks like a useful change 👍 |
+1 |
+1, I've often worried about |
-1 the widely used |
@lidaobing The already-merged #3578 removed the |
@gabetax got it, thanks |
+1, ran into this issue, and it would be great for Rails to have a way of handling this. |
+1 |
I'm not sure if this is the right place but I thought I should point out that the rubydocs don't reflect this change instead of the new regex JSON_ESCAPE_REGEXP = /[\u2028\u2029&><]/u Similarly it's still instead of the new JSON_ESCAPE = { '&' => '\u0026', '>' => '\u003e', '<' => '\u003c', "\u2028" => '\u2028', "\u2029" => '\u2029' } http://rubydoc.info/docs/rails/ERB/Util#json_escape-class_method |
This is only fixed on master (and 4.1.beta1), you can see it on On Wed, Feb 5, 2014 at 11:56 AM, MCodyB notifications@github.com wrote:
|
The existing definition removes double quote characters, and hence returns invalid JSON, making it unsuitable for the most common use case: bootstrapping JSON within a
<script>
element.I am unaware of any use cases satisfied by the current behavior, which was previously discussed on lighthouse without coming to a satisfactory resolution. The original commit at 0ff7a2d does not indicate 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
.Since Rails does not make it easy to correctly escape bootstrapped JSON, incorrect and insecure methods are widespread and incorrectly recommended: 1, 2, 3. This change, together with community education, would alleviate the situation.
It's worth discussion whether
json_escape
should always return HTML-safe strings, such that it can be used without explicitly callinghtml_safe
:Also related is the discussion of the other
j
helper at #3578.