Uh oh!
There was an error while loading. Please reload this page.
Escape / and > also so apps cannot be broken by browsers trying to 'correct' the HTML - #22
Escape / and > also so apps cannot be broken by browsers trying to 'correct' the HTML#22JackMc wants to merge 1 commit into
Conversation
ljharb
commented
Oct 1, 2019
Can you elaborate more on how this doesn't work as-is? |
JackMc
commented
Oct 1, 2019
Oh whoops, I didn't know I was opening this on the main repo. :) Sorry for the small description. In Firefox, any of the props containing |
JackMc
commented
Oct 1, 2019
Additionally, for the failing test, it seems to also fail on master so not sure how to fix it. |
clayton-shopify
commented
Nov 8, 2019
Since it's JSON that's being generated, I think it would make more sense to use JSON escaping rather than HTML escaping. Rails' We could use that approach here like so: diff --git a/lib/hypernova/blank_renderer.rb b/lib/hypernova/blank_renderer.rb
index 59ae67d..00bd3c7 100644
--- a/lib/hypernova/blank_renderer.rb+++ b/lib/hypernova/blank_renderer.rb@@ -16,12 +16,20 @@ class Hypernova::BlankRenderer
attr_reader :job
+ ESCAPED_CHARS = {+ "\u2028" => '\u2028',+ "\u2029" => '\u2029',+ ">" => '\u003e',+ "<" => '\u003c',+ "&" => '\u0026',+ }+
def data
job[:data]
end
def encode
- JSON.generate(data).gsub(/&/, '&').gsub(/>/, '>')+ JSON.generate(data).gsub(/[\u2028\u2029><&]/u, ESCAPED_CHARS)
end
def key
diff --git a/spec/blank_renderer_spec.rb b/spec/blank_renderer_spec.rb
index c5b6255..9cfe9c9 100644
--- a/spec/blank_renderer_spec.rb+++ b/spec/blank_renderer_spec.rb@@ -32,15 +32,15 @@ describe Hypernova::BlankRenderer do
it "encodes data correctly" do
str = described_class.new({
data: {
- foo: '</script>',+ foo: "\u2028\u2029</script>",
bar: '>',
baz: '&',
}
}).send(:encode)
- expect(str).to match(/<\/script>/)- expect(str).to match(/&gt;/)- expect(str).to match(/&amp;/)+ expect(str).to match(/\\u2028\\u2029\\u003c\/script\\u003e/)+ expect(str).to match(/\\u0026gt;/)+ expect(str).to match(/\\u0026amp;/)
end
end |
richter-alex
commented
Feb 26, 2020
Hey @ljharb or @goatslacker - would either of you have any bandwidth available to offer thoughts on the above proposal? Also, running tests locally seems to be passing now, so maybe we can restart the build. |
No description provided.