From 831ceea92429708c2621e31d076d57a13a712b41 Mon Sep 17 00:00:00 2001 From: Felipe Artur Date: Mon, 5 Aug 2019 17:52:38 -0300 Subject: [PATCH] Prevent rewritting plain links as embedded Prevents rewritting plain image/video links as embedded when moving issues. --- changelogs/unreleased/issue_58494.yml | 5 +++++ lib/gitlab/gfm/uploads_rewriter.rb | 14 +++++++++++++- spec/lib/gitlab/gfm/uploads_rewriter_spec.rb | 11 +++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 changelogs/unreleased/issue_58494.yml diff --git a/changelogs/unreleased/issue_58494.yml b/changelogs/unreleased/issue_58494.yml new file mode 100644 index 00000000000..c74768fc020 --- /dev/null +++ b/changelogs/unreleased/issue_58494.yml @@ -0,0 +1,5 @@ +--- +title: Prevent turning plain links into embedded when moving issues +merge_request: 31489 +author: +type: fixed diff --git a/lib/gitlab/gfm/uploads_rewriter.rb b/lib/gitlab/gfm/uploads_rewriter.rb index 2d1c9ac40ae..6b52d6e88e5 100644 --- a/lib/gitlab/gfm/uploads_rewriter.rb +++ b/lib/gitlab/gfm/uploads_rewriter.rb @@ -27,7 +27,15 @@ module Gitlab klass = target_parent.is_a?(Namespace) ? NamespaceFileUploader : FileUploader moved = klass.copy_to(file, target_parent) - moved.markdown_link + + moved_markdown = moved.markdown_link + + # Prevents rewrite of plain links as embedded + if was_embedded?(markdown) + moved_markdown + else + moved_markdown.sub(/\A!/, "") + end end end @@ -43,6 +51,10 @@ module Gitlab referenced_files.compact.select(&:exists?) end + def was_embedded?(markdown) + markdown.starts_with?("!") + end + private def find_file(project, secret, file) diff --git a/spec/lib/gitlab/gfm/uploads_rewriter_spec.rb b/spec/lib/gitlab/gfm/uploads_rewriter_spec.rb index ef52a25f47e..d24f5c45107 100644 --- a/spec/lib/gitlab/gfm/uploads_rewriter_spec.rb +++ b/spec/lib/gitlab/gfm/uploads_rewriter_spec.rb @@ -55,6 +55,17 @@ describe Gitlab::Gfm::UploadsRewriter do end end + it 'does not rewrite plain links as embedded' do + embedded_link = image_uploader.markdown_link + plain_image_link = embedded_link.sub(/\A!/, "") + text = "#{plain_image_link} and #{embedded_link}" + + moved_text = described_class.new(text, old_project, user).rewrite(new_project) + + expect(moved_text.scan(/!\[.*?\]/).count).to eq(1) + expect(moved_text.scan(/\A\[.*?\]/).count).to eq(1) + end + context "file are stored locally" do include_examples "files are accessible" end