OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Fix auto-linking custom protocols
Oliver Günther936a5edb23db
diff --git a/lib/open_project/text_formatting/filters/autolink_custom_protocols_filter.rb b/lib/open_project/text_formatting/filters/autolink_custom_protocols_filter.rbnew file mode 100644index 00000000000..1c817834e24--- /dev/null+++ b/lib/open_project/text_formatting/filters/autolink_custom_protocols_filter.rb@@ -0,0 +1,94 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++module OpenProject::TextFormatting+ module Filters+ # HTML Filter for auto_linking custom protocols in HTML.+ #+ # Context options:+ #+ # autolink:+ # classes: (string) Classes to add to auto linked urls+ # enabled: (boolean)+ #+ # This filter does not write additional information to the context.+ class AutolinkCustomProtocolsFilter < HTML::Pipeline::Filter+ include ActionView::Helpers::TagHelper++ # Skip text nodes that are within preformatted or linked blocks+ SKIPPED_BLOCKS = %w(pre code kbd a).to_set++ # Match custom schemes and ignore trailing punctuation+ def self.regexp+ %r{((?:#{Setting.allowed_link_protocols.join('|')}):/?/?[^\s<\u00A0"]*[^\s<\u00A0",;\.])}i+ end++ def call+ return doc if Setting.allowed_link_protocols.empty?++ autolink_context = default_autolink_options.merge context.fetch(:autolink, {})+ return doc if autolink_context[:enabled] == false++ doc.search(".//text()").each do |node|+ next if has_ancestor?(node, SKIPPED_BLOCKS)++ auto_link_custom_protocols(node, autolink_context)+ end++ doc+ end++ def auto_link_custom_protocols(node, autolink_context)+ content = node.to_html++ matched = false+ content.gsub!(self.class.regexp) do |href|+ matched = true+ content_tag(:a,+ href,+ href:,+ class: autolink_context[:classes],+ rel: "noopener noreferrer")+ end++ node.replace(content) if matched+ end++ def default_autolink_options+ {+ enabled: true,+ # Having to specify the link class again here is unfortunate. But as rinku seems to run latest,+ # it cannot receive the link class like all the rest of the links.+ classes: "op-uc-link"+ }+ end+ end+ end+enddiff --git a/lib/open_project/text_formatting/formats/markdown/formatter.rb b/lib/open_project/text_formatting/formats/markdown/formatter.rbindex 0070a1e4989..c15434ee52d 100644--- a/lib/open_project/text_formatting/formats/markdown/formatter.rb+++ b/lib/open_project/text_formatting/formats/markdown/formatter.rb@@ -53,6 +53,7 @@ module OpenProject::TextFormatting::Formats::MarkdownOpenProject::TextFormatting::Filters::SyntaxHighlightFilter,OpenProject::TextFormatting::Filters::AttachmentFilter,OpenProject::TextFormatting::Filters::AutolinkFilter,+ OpenProject::TextFormatting::Filters::AutolinkCustomProtocolsFilter,OpenProject::TextFormatting::Filters::RelativeLinkFilter,OpenProject::TextFormatting::Filters::LinkAttributeFilter,OpenProject::TextFormatting::Filters::FigureWrappedFilter,diff --git a/spec/lib/open_project/text_formatting/allowed_link_protocols_spec.rb b/spec/lib/open_project/text_formatting/allowed_link_protocols_spec.rbindex 7d6dc06b06a..07fcedc7a12 100644--- a/spec/lib/open_project/text_formatting/allowed_link_protocols_spec.rb+++ b/spec/lib/open_project/text_formatting/allowed_link_protocols_spec.rb@@ -63,6 +63,20 @@ RSpec.describe OpenProject::TextFormatting, "OpenProject allowed link protocols"endend+ it_behaves_like "format_text produces" do+ let(:raw) do+ <<~RAW+ An autolinked data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==.+ RAW+ end++ let(:expected) do+ <<~EXPECTED+ <p class="op-uc-p">An autolinked data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==.</p>+ EXPECTED+ end+ end+it_behaves_like "format_text produces" dolet(:raw) do<<~RAW@@ -107,6 +121,34 @@ RSpec.describe OpenProject::TextFormatting, "OpenProject allowed link protocols"endend+ it_behaves_like "format_text produces" do+ let(:raw) do+ <<~RAW+ An autolinked ftp://example.org+ RAW+ end++ let(:expected) do+ <<~EXPECTED+ <p class="op-uc-p">An autolinked <a href="ftp://example.org" rel="noopener noreferrer" target="_top" class="op-uc-link">ftp://example.org</a></p>+ EXPECTED+ end+ end++ it_behaves_like "format_text produces" do+ let(:raw) do+ <<~RAW+ An autolinked ftp://example.org.+ RAW+ end++ let(:expected) do+ <<~EXPECTED+ <p class="op-uc-p">An autolinked <a href="ftp://example.org" rel="noopener noreferrer" target="_top" class="op-uc-link">ftp://example.org</a>.</p>+ EXPECTED+ end+ end+it_behaves_like "format_text produces" dolet(:raw) do<<~RAW@@ -150,5 +192,19 @@ RSpec.describe OpenProject::TextFormatting, "OpenProject allowed link protocols"EXPECTEDendend++ it_behaves_like "format_text produces" do+ let(:raw) do+ <<~RAW+ An autolinked data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==.+ RAW+ end++ let(:expected) do+ <<~EXPECTED+ <p class="op-uc-p">An autolinked <a href="data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==" rel="noopener noreferrer" target="_top" class="op-uc-link">data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==</a>.</p>+ EXPECTED+ end+ endendend