diff --git a/app/jobs/regular/pull_hotlinked_images.rb b/app/jobs/regular/pull_hotlinked_images.rb index 919eae5b9d5..ed158bef130 100644 --- a/app/jobs/regular/pull_hotlinked_images.rb +++ b/app/jobs/regular/pull_hotlinked_images.rb @@ -28,7 +28,7 @@ module Jobs extract_images_from(post.cooked).each do |image| src = original_src = image['src'] - src = "http:" + src if src.start_with?("//") + src = "http:#{src}" if src.start_with?("//") if is_valid_image_url(src) hotlinked = nil @@ -113,17 +113,22 @@ module Jobs return false if Discourse.store.has_been_uploaded?(src) # we don't want to pull relative images return false if src =~ /\A\/[^\/]/i + # parse the src begin uri = URI.parse(src) rescue URI::InvalidURIError return false end + + hostname = uri.hostname + return false unless hostname + # we don't want to pull images hosted on the CDN (if we use one) - return false if Discourse.asset_host.present? && URI.parse(Discourse.asset_host).hostname == uri.hostname - return false if SiteSetting.s3_cdn_url.present? && URI.parse(SiteSetting.s3_cdn_url).hostname == uri.hostname + return false if Discourse.asset_host.present? && URI.parse(Discourse.asset_host).hostname == hostname + return false if SiteSetting.s3_cdn_url.present? && URI.parse(SiteSetting.s3_cdn_url).hostname == hostname # we don't want to pull images hosted on the main domain - return false if URI.parse(Discourse.base_url_no_prefix).hostname == uri.hostname + return false if URI.parse(Discourse.base_url_no_prefix).hostname == hostname # check the domains blacklist SiteSetting.should_download_images?(src) end diff --git a/spec/jobs/pull_hotlinked_images_spec.rb b/spec/jobs/pull_hotlinked_images_spec.rb index e1f5cb16d3e..19e0049d059 100644 --- a/spec/jobs/pull_hotlinked_images_spec.rb +++ b/spec/jobs/pull_hotlinked_images_spec.rb @@ -3,84 +3,102 @@ require 'jobs/regular/pull_hotlinked_images' describe Jobs::PullHotlinkedImages do - let(:image_url) { "http://wiki.mozilla.org/images/2/2e/Longcat1.png" } - let(:png) { Base64.decode64("R0lGODlhAQABALMAAAAAAIAAAACAAICAAAAAgIAAgACAgMDAwICAgP8AAAD/AP//AAAA//8A/wD//wBiZCH5BAEAAA8ALAAAAAABAAEAAAQC8EUAOw==") } - - before do - stub_request(:get, image_url).to_return(body: png, headers: { "Content-Type" => "image/png" }) - stub_request(:head, image_url) - SiteSetting.download_remote_images_to_local = true - FastImage.expects(:size).returns([100, 100]).at_least_once - end - - it 'replaces images' do - post = Fabricate(:post, raw: "") - - Jobs::PullHotlinkedImages.new.execute(post_id: post.id) - post.reload - - expect(post.raw).to match(/^") - - Jobs::PullHotlinkedImages.new.execute(post_id: post.id) - post.reload - - expect(post.raw).to match(/^ "image/png" }) - stub_request(:head, extensionless_url) - post = Fabricate(:post, raw: "") - - Jobs::PullHotlinkedImages.new.execute(post_id: post.id) - post.reload - - expect(post.raw).to match(/^ "image/png" }) + stub_request(:head, image_url) + SiteSetting.download_remote_images_to_local = true + FastImage.expects(:size).returns([100, 100]).at_least_once end - it 'replaces image src' do - post = Fabricate(:post, raw: "#{url}") + it 'replaces images' do + post = Fabricate(:post, raw: "") - Jobs::ProcessPost.new.execute(post_id: post.id) Jobs::PullHotlinkedImages.new.execute(post_id: post.id) - Jobs::ProcessPost.new.execute(post_id: post.id) post.reload - expect(post.cooked).to match(/") + + Jobs::PullHotlinkedImages.new.execute(post_id: post.id) + post.reload + + expect(post.raw).to match(/^ "image/png" }) + stub_request(:head, extensionless_url) + post = Fabricate(:post, raw: "") + + Jobs::PullHotlinkedImages.new.execute(post_id: post.id) + post.reload + + expect(post.raw).to match(/^