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

Rename escape_slash in script_safe and also escape E+2028 and E+2029 #525

Merged
merged 1 commit into from Dec 1, 2023

Conversation

casperisfine
Copy link

Fix: #215
Fix:#214

It is rather common to directly interpolate JSON string inside <script> tags in HTML as to provide configuration or parameters to a script.

However this may lead to XSS vulnerabilities, to prevent that 3 characters need to be escaped:

  • / (forward slash)
  • U+2028 (LINE SEPARATOR)
  • U+2029 (PARAGRAPH SEPARATOR)

The forward slash need to be escaped to prevent closing the script tag early, and the other two are valid JSON but invalid Javascript and can be used to break JS parsing.

Given that the intent of escaping forward slash is the same than escaping U+2028 and U+2029, I chose to rename and repurpose the existing escape_slash option.

cc @hsbt @nurse

This could be used to very significantly speedup ActiveSupport::JSON (cc @jhawthorn)

@hsbt
Copy link
Collaborator

hsbt commented Apr 14, 2023

@casperisfine
Copy link
Author

@hsbt done. Also sorry for including this change, I'm not sure what was going on but I couldn't get JRuby to work with that json-java.gemspec.

But I can revert that part, no problem.

@casperisfine
Copy link
Author

Some note here.

As implemented in this PR, this option make the json safe to include in <script> tags, but not elsewhere in HTML.

Some JSON libraries do offer an "xss_safe" mode, that also escape < as \u003e. It may make sense to implement it like this here, as to make the option more generally safe.

It is rather common to directly interpolate JSON string inside
<script> tags in HTML as to provide configuration or parameters to a
script.

However this may lead to XSS vulnerabilities, to prevent that 3
characters need to be escaped:

  - `/` (forward slash)
  - `U+2028` (LINE SEPARATOR)
  - `U+2029` (PARAGRAPH SEPARATOR)

The forward slash need to be escaped to prevent closing the script
tag early, and the other two are valid JSON but invalid Javascript
and can be used to break JS parsing.

Given that the intent of escaping forward slash is the same than escaping
U+2028 and U+2029, I chos to rename and repurpose the existing `escape_slash`
option.
@casperisfine
Copy link
Author

Ok, I reverted the Gemfile and gemspec changes because it was failing on CI. I don't know what's wrong with JRuby on my machine :/

@hsbt hsbt merged commit c065433 into flori:master Dec 1, 2023
65 checks passed
matzbot pushed a commit to ruby/ruby that referenced this pull request Dec 1, 2023
  > flori/json#525
  > Rename escape_slash in script_safe and also escape E+2028 and E+2029

  Co-authored-by: Jean Boussier <jean.boussier@gmail.com>

  > flori/json#454
  > Remove unnecessary initialization of create_id in JSON.parse()

  Co-authored-by: Watson <watson1978@gmail.com>
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

3 participants