diff --git a/lib/email/styles.rb b/lib/email/styles.rb index 4e99c6f63fb..b27324a8cf3 100644 --- a/lib/email/styles.rb +++ b/lib/email/styles.rb @@ -27,6 +27,7 @@ module Email def format_basic uri = URI(Discourse.base_url) + # images @fragment.css('img').each do |img| next if img['class'] == 'site-logo' @@ -51,6 +52,20 @@ module Email img['src'] = "#{uri.scheme}:#{img['src']}" end end + + # attachments + @fragment.css('a.attachment').each do |a| + + # ensure all urls are absolute + if a['href'] =~ /^\/[^\/]/ + a['href'] = "#{Discourse.base_url}#{a['href']}" + end + + # ensure no schemaless urls + if a['href'] && a['href'].starts_with?("//") + a['href'] = "#{uri.scheme}:#{a['href']}" + end + end end def format_notification diff --git a/spec/components/email/styles_spec.rb b/spec/components/email/styles_spec.rb index 10b8b9c29f5..91b3cdbe5b4 100644 --- a/spec/components/email/styles_spec.rb +++ b/spec/components/email/styles_spec.rb @@ -113,6 +113,11 @@ describe Email::Styles do frag.at('a')['href'].should == "http://test.localhost/discourse" end + it "rewrites the href for attachment files to have http" do + frag = html_fragment('attachment_file.txt') + frag.at('a')['href'].should == "http://try-discourse.global.ssl.fastly.net/uploads/default/368/40b610b0aa90cfcf.txt" + end + it "rewrites the src to have http" do frag = html_fragment('') frag.at('img')['src'].should == "http://test.localhost/blah.jpg" @@ -124,11 +129,16 @@ describe Email::Styles do SiteSetting.stubs(:use_https).returns(true) end - it "rewrites the forum URL to have http" do + it "rewrites the forum URL to have https" do frag = html_fragment('hello') frag.at('a')['href'].should == "https://test.localhost/discourse" end + it "rewrites the href for attachment files to have https" do + frag = html_fragment('attachment_file.txt') + frag.at('a')['href'].should == "https://try-discourse.global.ssl.fastly.net/uploads/default/368/40b610b0aa90cfcf.txt" + end + it "rewrites the src to have https" do frag = html_fragment('') frag.at('img')['src'].should == "https://test.localhost/blah.jpg"