From 5774107a2d8f5522d9f6bf612fa1baafa57ac7ed Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Thu, 27 Feb 2020 10:22:55 +0530 Subject: [PATCH] FIX: downloaded image URLs incorrectly replaced in post raw. (#9014) Previously, while replacing the downloaded image URL `http://wiki.mozilla.org/images/2/2e/Longcat1.png` similar non-image URL `http://wiki.mozilla.org/images/2` was replaced wrongly. --- app/jobs/regular/pull_hotlinked_images.rb | 2 +- spec/jobs/pull_hotlinked_images_spec.rb | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/jobs/regular/pull_hotlinked_images.rb b/app/jobs/regular/pull_hotlinked_images.rb index 252c3790523..25703659460 100644 --- a/app/jobs/regular/pull_hotlinked_images.rb +++ b/app/jobs/regular/pull_hotlinked_images.rb @@ -95,7 +95,7 @@ module Jobs escaped_src = Regexp.escape(original_src) replace_raw = ->(match, match_src, replacement, _index) { - if src.include?(match_src) + if remove_scheme(src) == remove_scheme(match_src) replacement = if replacement.include?(InlineUploads::PLACEHOLDER) diff --git a/spec/jobs/pull_hotlinked_images_spec.rb b/spec/jobs/pull_hotlinked_images_spec.rb index 40406744509..6e3a21ca9a7 100644 --- a/spec/jobs/pull_hotlinked_images_spec.rb +++ b/spec/jobs/pull_hotlinked_images_spec.rb @@ -83,6 +83,17 @@ describe Jobs::PullHotlinkedImages do RAW end + it 'replaces correct image URL' do + url = image_url.sub("/2e/Longcat1.png", '') + post = Fabricate(:post, raw: "[Images](#{url})\n![](#{image_url})") + + expect do + Jobs::PullHotlinkedImages.new.execute(post_id: post.id) + end.to change { Upload.count }.by(1) + + expect(post.reload.raw).to eq("[Images](#{url})\n![](#{Upload.last.short_url})") + end + it 'replaces images without protocol' do url = image_url.sub(/^https?\:/, '') post = Fabricate(:post, raw: "test")