From 8fc7420f83da1ac2e8892930a17cbd2a04ecf94f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Mon, 30 Jan 2017 18:06:48 +0100 Subject: [PATCH] FIX: prevent huge custom emojis in emails --- lib/email/styles.rb | 25 ++++++++++--------------- spec/components/email/styles_spec.rb | 8 +++++++- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/lib/email/styles.rb b/lib/email/styles.rb index eaa69c81456..bcdd792adfb 100644 --- a/lib/email/styles.rb +++ b/lib/email/styles.rb @@ -37,25 +37,20 @@ module Email @fragment.css('img').each do |img| next if img['class'] == 'site-logo' - if img['class'] == "emoji" || img['src'] =~ /(plugins|images)\/emoji/ - img['width'] = 20 - img['height'] = 20 + if (img['class'] && img['class']['emoji']) || (img['src'] && img['src'][/\/_?emoji\//]) + img['width'] = img['height'] = 20 else # use dimensions of original iPhone screen for 'too big, let device rescale' if img['width'].to_i > 320 or img['height'].to_i > 480 - img['width'] = 'auto' - img['height'] = 'auto' + img['width'] = img['height'] = 'auto' end end - # ensure all urls are absolute - if img['src'] =~ /^\/[^\/]/ - img['src'] = "#{Discourse.base_url}#{img['src']}" - end - - # ensure no schemaless urls - if img['src'] && img['src'].starts_with?("//") - img['src'] = "#{uri.scheme}:#{img['src']}" + if img['src'] + # ensure all urls are absolute + img['src'] = "#{Discourse.base_url}#{img['src']}" if img['src'][/^\/[^\/]/] + # ensure no schemaless urls + img['src'] = "#{uri.scheme}:#{img['src']}" if img['src'][/^\/\//] end end @@ -199,12 +194,12 @@ module Email def strip_avatars_and_emojis @fragment.search('img').each do |img| - if img['src'] =~ /_avatar/ + if img['src'][/_avatar/] img.parent['style'] = "vertical-align: top;" if img.parent.name == 'td' img.remove end - if img['title'] && (img['src'] =~ /images\/emoji/ || img['src'] =~ /uploads\/default\/_emoji/) + if img['title'] && img['src'][/\/_?emoji\//] img.add_previous_sibling(img['title'] || "emoji") img.remove end diff --git a/spec/components/email/styles_spec.rb b/spec/components/email/styles_spec.rb index 07061dbc3ae..38ccdde9629 100644 --- a/spec/components/email/styles_spec.rb +++ b/spec/components/email/styles_spec.rb @@ -30,12 +30,18 @@ describe Email::Styles do expect(frag.at("img")["style"]).to match("max-width") end - it "adds a width and height to images with an emoji path" do + it "adds a width and height to emojis" do frag = basic_fragment("") expect(frag.at("img")["width"]).to eq("20") expect(frag.at("img")["height"]).to eq("20") end + it "adds a width and height to custom emojis" do + frag = basic_fragment("") + expect(frag.at("img")["width"]).to eq("20") + expect(frag.at("img")["height"]).to eq("20") + end + it "converts relative paths to absolute paths" do frag = basic_fragment("") expect(frag.at("img")["src"]).to eq("#{Discourse.base_url}/some-image.png")