Merge pull request #2922 from techAPJ/patch-2

FIX: attachment links in mail lacks protocol
This commit is contained in:
Robin Ward 2014-10-27 15:49:23 -04:00
commit 0965baad72
2 changed files with 26 additions and 1 deletions

View file

@ -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

View file

@ -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('<a class="attachment" href="//try-discourse.global.ssl.fastly.net/uploads/default/368/40b610b0aa90cfcf.txt">attachment_file.txt</a>')
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('<img src="//test.localhost/blah.jpg">')
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('<a href="//test.localhost/discourse">hello</a>')
frag.at('a')['href'].should == "https://test.localhost/discourse"
end
it "rewrites the href for attachment files to have https" do
frag = html_fragment('<a class="attachment" href="//try-discourse.global.ssl.fastly.net/uploads/default/368/40b610b0aa90cfcf.txt">attachment_file.txt</a>')
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('<img src="//test.localhost/blah.jpg">')
frag.at('img')['src'].should == "https://test.localhost/blah.jpg"