From 2d5d15b4bb6d8dffa029026e3257ccdeb044890e Mon Sep 17 00:00:00 2001 From: David Taylor Date: Wed, 6 Jul 2022 11:46:33 +0100 Subject: [PATCH] FIX: Ensure pull-hotlinked can rewrite lone oneboxes (#17354) Mutating the `raw` variable like this would cause issues upstream, meaning that the modification is not persisted. Instead, we should allocate a new string like the other replacement methods. --- app/services/inline_uploads.rb | 2 +- spec/jobs/pull_hotlinked_images_spec.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/services/inline_uploads.rb b/app/services/inline_uploads.rb index 3cc45a6b5a9..62d081caa32 100644 --- a/app/services/inline_uploads.rb +++ b/app/services/inline_uploads.rb @@ -245,7 +245,7 @@ class InlineUploads # Markdown inline - ![alt](http://... "image title") InlineUploads.match_md_inline_img(raw, external_src: true, &replace) - raw.gsub!(/^(https?:\/\/\S+)(\s?)$/) do |match| + raw = raw.gsub(/^(https?:\/\/\S+)(\s?)$/) do |match| if upload = blk.call(match) "![](#{upload.short_url})" else diff --git a/spec/jobs/pull_hotlinked_images_spec.rb b/spec/jobs/pull_hotlinked_images_spec.rb index 3de3afc1c83..187e93e6881 100644 --- a/spec/jobs/pull_hotlinked_images_spec.rb +++ b/spec/jobs/pull_hotlinked_images_spec.rb @@ -423,6 +423,25 @@ describe Jobs::PullHotlinkedImages do expect(post.cooked).to match(//) end + + it 'rewrites a lone onebox' do + post = Fabricate(:post, raw: <<~MD) + Onebox here: + #{image_url} + MD + stub_image_size + + post.rebake! + + post.reload + + expect(post.raw).to eq(<<~MD.chomp) + Onebox here: + ![](upload://z2QSs1KJWoj51uYhDjb6ifCzxH6.gif) + MD + + expect(post.cooked).to match(/