A platform for community discussion. Free, open, simple.
FIX: Don't escape HTML entities twice in oneboxes (#37141)
Loïc Guitautcaaa8f9c9e0d
diff --git a/lib/onebox/normalizer.rb b/lib/onebox/normalizer.rbindex 2a5e3c2e401..2f374039865 100644--- a/lib/onebox/normalizer.rb+++ b/lib/onebox/normalizer.rb@@ -11,6 +11,8 @@ module Oneboxsanitize_value(value, *args)end+ private+def method_missing(attr, *args, &block)value = get(attr, *args)@@ -26,7 +28,17 @@ module Oneboxendend- private+ def respond_to_missing?(attr, include_private = false)+ data.key?(attr) || super+ end++ def sanitize_value(value, length = nil, sanitize = true)+ value = html_entities.decode(value)+ value = html_entities.decode(Sanitize.fragment(value)) if sanitize+ value.strip!+ value = Onebox::Helpers.truncate(value, length) if length+ value+ enddef integer_suffixes%w[width height]@@ -39,13 +51,5 @@ module Oneboxdef html_entities@html_entities ||= HTMLEntities.newend-- def sanitize_value(value, length = nil, sanitize = true)- value = html_entities.decode(value)- value = Sanitize.fragment(value) if sanitize- value.strip!- value = Onebox::Helpers.truncate(value, length) if length- value- endendenddiff --git a/spec/lib/onebox/open_graph_spec.rb b/spec/lib/onebox/open_graph_spec.rbindex 71de9468e8f..7b915828a27 100644--- a/spec/lib/onebox/open_graph_spec.rb+++ b/spec/lib/onebox/open_graph_spec.rb@@ -3,26 +3,29 @@require "onebox/open_graph"RSpec.describe Onebox::OpenGraph do- it "excludes html tags in title" do- doc =+ describe "Normalization" do+ subject(:graph) { described_class.new(doc) }++ let(:doc) doNokogiri.HTML(- '<html><title>Did’ you <b>miss me</b>? - Album on Imgur</title><meta name="og:description" content="Post with 7 votes and 151 views. Shared by vinothkannans. Did you <b>miss me</b>?" /><meta property="og:image" content="https://i.imgur.com/j1CNCZY.gif?noredirect" /></html>',+ '<html><title>Did’ you <b>miss me</b>? - Album on Imgur</title><meta name="og:description" content="Post with 7 votes and 151 views. Shared by vinothkannans. Did you <b>miss me</b>?" /><meta property="og:image" content="http://test.com/test\'ing.mp3" /><meta name="og:author" content="Batman & Robin" /></html>',)- og = described_class.new(doc)- expect(og.title).to eq("Did’ you miss me? - Album on Imgur")- expect(og.description).to eq(- "Post with 7 votes and 151 views. Shared by vinothkannans. Did you miss me?",- )- expect(og.image).to eq("https://i.imgur.com/j1CNCZY.gif?noredirect")- end+ end- it "correctly normalizes the url properties" do- doc =- Nokogiri.HTML(- "<html><meta property=\"og:image\" content=\"http://test.com/test'ing.mp3\" /></html>",+ it "excludes html tags" do+ expect(graph).to have_attributes(+ title: "Did’ you miss me? - Album on Imgur",+ description: "Post with 7 votes and 151 views. Shared by vinothkannans. Did you miss me?",)- og = described_class.new(doc)- expect(og.image).to eq("http://test.com/test'ing.mp3")+ end++ it "correctly normalizes the url properties" do+ expect(graph.image).to eq("http://test.com/test'ing.mp3")+ end++ it "normalizes ampersands properly" do+ expect(graph.author).to eq("Batman & Robin")+ endenddescribe "Collections" do