Skip to content

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

Allow custom link schemes

Oliver Güntherb7476c693b4d
diff --git a/app/controllers/admin/settings/general_settings_controller.rb b/app/controllers/admin/settings/general_settings_controller.rb
index 2b2f14b20b6..711c03bcf93 100644
--- a/app/controllers/admin/settings/general_settings_controller.rb
+++ b/app/controllers/admin/settings/general_settings_controller.rb
@@ -30,6 +30,18 @@ module Admin::Settings
class GeneralSettingsController < ::Admin::SettingsController
menu_item :settings_general
+ def settings_params
+ super.tap do |settings|
+ settings["allowed_link_protocols"] = settings["allowed_link_protocols"].split(/\r?\n/)
+ end
+ end
+
+ def extra_permitted_filters
+ # attachment_whitelist is normally permitted as an array parameter.
+ # Explicitly permit it as a string here.
+ [:allowed_link_protocols]
+ end
+
def show
super
@guessed_host = request.host_with_port.dup
diff --git a/app/models/setting/allowed_link_protocols.rb b/app/models/setting/allowed_link_protocols.rb
new file mode 100644
index 00000000000..a47c4201b4b
--- /dev/null
+++ b/app/models/setting/allowed_link_protocols.rb
@@ -0,0 +1,39 @@
+# 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.
+#++
+
+class Setting
+ module AllowedLinkProtocols
+ ALWAYS_ALLOWED = %w[http https mailto].freeze
+
+ def self.all
+ (Setting.allowed_link_protocols + ALWAYS_ALLOWED).compact
+ end
+ end
+end
diff --git a/app/views/admin/settings/general_settings/show.html.erb b/app/views/admin/settings/general_settings/show.html.erb
index 1abccb9facc..241424d98a6 100644
--- a/app/views/admin/settings/general_settings/show.html.erb
+++ b/app/views/admin/settings/general_settings/show.html.erb
@@ -60,6 +60,12 @@ See COPYRIGHT and LICENSE files for more details.
</span>
</div>
<div class="form--field"><%= setting_check_box :cache_formatted_text %></div>
+ <div class="form--field">
+ <%= setting_text_area :allowed_link_protocols, rows: 5, container_class: "-wide" %>
+ <div class="form--field-instructions">
+ <%= t(:setting_allowed_link_protocols_text_html) %>
+ </div>
+ </div>
<div class="form--field"><%= setting_check_box :feeds_enabled, size: 6 %></div>
<div class="form--field"><%= setting_text_field :feeds_limit, size: 6, container_class: "-xslim" %></div>
<div class="form--field"><%= setting_text_field :work_packages_projects_export_limit, size: 6, container_class: "-xslim" %></div>
diff --git a/config/constants/settings/definition.rb b/config/constants/settings/definition.rb
index 3078b2d7452..87b9cda28be 100644
--- a/config/constants/settings/definition.rb
+++ b/config/constants/settings/definition.rb
@@ -44,6 +44,11 @@ module Settings
format: :string,
default: nil
},
+ allowed_link_protocols: {
+ format: :array,
+ description: "Allowed protocols for links in the WYSIWYG editor and formatted texts",
+ default: []
+ },
apiv3_cors_enabled: {
description: "Enable CORS headers for APIv3 server responses",
default: false
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 8afc1681081..6825887195f 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -3694,6 +3694,11 @@ en:
search_input_placeholder: "Search ..."
+ setting_allowed_link_protocols: "Allowed link protocols"
+ setting_allowed_link_protocols_text_html: >
+ Allow these protocols to be rendered as links in work package descriptions, long text fields and comments. For example, <code>tel</code> or <code>element</code>
+ <br/>
+ http(s) and mailto are always allowed.
setting_after_first_login_redirect_url: "First login redirect"
setting_after_first_login_redirect_url_text_html: >
Set a path to redirect users after their first login. If empty, redirects to the home page for the onboarding tour.
diff --git a/frontend/src/app/core/config/configuration.service.ts b/frontend/src/app/core/config/configuration.service.ts
index ff612a2de78..70c89131431 100644
--- a/frontend/src/app/core/config/configuration.service.ts
+++ b/frontend/src/app/core/config/configuration.service.ts
@@ -87,6 +87,10 @@ export class ConfigurationService {
return this.systemPreference('perPageOptions');
}
+ public get allowedLinkProtocols():string[]|null {
+ return this.systemPreference('allowedLinkProtocols') || null;
+ }
+
public dateFormatPresent():boolean {
return !!this.systemPreference('dateFormat');
}
diff --git a/frontend/src/app/features/hal/resources/configuration-resource.ts b/frontend/src/app/features/hal/resources/configuration-resource.ts
index 5ef3ca6364b..39b8547c926 100644
--- a/frontend/src/app/features/hal/resources/configuration-resource.ts
+++ b/frontend/src/app/features/hal/resources/configuration-resource.ts
@@ -29,5 +29,6 @@
import { HalResource } from 'core-app/features/hal/resources/hal-resource';
export class ConfigurationResource extends HalResource {
- public perPageOptions:Array<number>;
+ public perPageOptions:number[];
+ public allowedLinkProtocols?:string[];
}
diff --git a/frontend/src/app/shared/components/editor/components/ckeditor/ckeditor-setup.service.ts b/frontend/src/app/shared/components/editor/components/ckeditor/ckeditor-setup.service.ts
index 70ff6befc70..349b6ac0db4 100644
--- a/frontend/src/app/shared/components/editor/components/ckeditor/ckeditor-setup.service.ts
+++ b/frontend/src/app/shared/components/editor/components/ckeditor/ckeditor-setup.service.ts
@@ -6,6 +6,7 @@ import {
ICKEditorWatchdog,
} from 'core-app/shared/components/editor/components/ckeditor/ckeditor.types';
import { Constructor } from '@angular/cdk/schematics';
+import { ConfigurationService } from 'core-app/core/config/configuration.service';
export type ICKEditorType = 'full'|'constrained';
export type ICKEditorMacroType = 'none'|'resource'|'full'|boolean|string[];
@@ -26,7 +27,10 @@ export class CKEditorSetupService {
/** Prefetch ckeditor when browser is idle */
private prefetch:Promise<unknown>;
- constructor(private PathHelper:PathHelperService) {
+ constructor(
+ readonly PathHelper:PathHelperService,
+ readonly configurationService:ConfigurationService,
+ ) {
}
public initialize() {
@@ -59,11 +63,33 @@ export class CKEditorSetupService {
const toolbarWrapper = wrapper.querySelector('.document-editor__toolbar') as HTMLElement;
const contentWrapper = wrapper.querySelector('.document-editor__editable') as HTMLElement;
+ const config = this.createConfig(context, initialData);
+
+ return this
+ .createWatchdog(editorClass, contentWrapper, config)
+ .then((watchdog:ICKEditorWatchdog) => {
+ const { editor } = watchdog;
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
+ toolbarWrapper.appendChild(editor.ui.view.toolbar.element);
+
+ // Allow custom events on wrapper to set/get data for debugging
+ jQuery(wrapper)
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-return
+ .on('op:ckeditor:autosave', () => editor.config.get('autosave').save(editor))
+ .on('op:ckeditor:setData', (_, data:string) => editor.setData(data))
+ .on('op:ckeditor:clear', () => editor.setData(' '))
+ .on('op:ckeditor:getData', (_, cb:(data:string) => void) => cb(editor.getData({ trim: false })));
+
+ return watchdog;
+ });
+ }
+
+ private createConfig(context:ICKEditorContext, initialData:string|null) {
const uiLocale = this.loadedLocale;
const contentLanguage = context.options && context.options.rtl ? 'ar' : 'en';
const config = {
- openProject: this.createConfig(context),
+ openProject: this.createContext(context),
removePlugins: context.removePlugins,
initialData,
ui: {
@@ -75,24 +101,15 @@ export class CKEditorSetupService {
ui: uiLocale,
content: contentLanguage,
},
+ link: {},
};
- return this
- .createWatchdog(editorClass, contentWrapper, config)
- .then((watchdog:ICKEditorWatchdog) => {
- const { editor } = watchdog;
- toolbarWrapper.appendChild(editor.ui.view.toolbar.element);
-
- // Allow custom events on wrapper to set/get data for debugging
- jQuery(wrapper)
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-return
- .on('op:ckeditor:autosave', () => editor.config.get('autosave').save(editor))
- .on('op:ckeditor:setData', (_, data:string) => editor.setData(data))
- .on('op:ckeditor:clear', () => editor.setData(' '))
- .on('op:ckeditor:getData', (_, cb:(data:string) => void) => cb(editor.getData({ trim: false })));
+ const allowedLinkProtocols = this.configurationService.allowedLinkProtocols;
+ if (allowedLinkProtocols) {
+ config.link = { allowedLinkProtocols };
+ }
- return watchdog;
- });
+ return config;
}
/**
@@ -143,7 +160,7 @@ export class CKEditorSetupService {
}
}
- private createConfig(context:ICKEditorContext):unknown {
+ private createContext(context:ICKEditorContext):unknown {
if (context.macros === 'none') {
context.macros = false;
} else if (context.macros === 'resource') {
diff --git a/lib/api/v3/configuration/configuration_representer.rb b/lib/api/v3/configuration/configuration_representer.rb
index e629d5d0d53..ee412fd47c9 100644
--- a/lib/api/v3/configuration/configuration_representer.rb
+++ b/lib/api/v3/configuration/configuration_representer.rb
@@ -98,6 +98,9 @@ module API
.map { |flag| flag.camelize(:lower) }
}
+ property :allowed_link_protocols,
+ getter: ->(*) { Setting::AllowedLinkProtocols.all }
+
property :user_preferences,
embedded: true,
exec_context: :decorator,
diff --git a/lib/open_project/text_formatting/filters/sanitization_filter.rb b/lib/open_project/text_formatting/filters/sanitization_filter.rb
index a6f7b817dd9..26408b49a5e 100644
--- a/lib/open_project/text_formatting/filters/sanitization_filter.rb
+++ b/lib/open_project/text_formatting/filters/sanitization_filter.rb
@@ -62,7 +62,12 @@ module OpenProject::TextFormatting
transformers: base[:transformers] + transformers,
# Allow relaxed CSS styles for the given attributes
- css: ::Sanitize::Config::RELAXED[:css]
+ css: ::Sanitize::Config::RELAXED[:css],
+
+ # Allow our protocols, and relative links always
+ protocols: {
+ "a" => { "href" => Setting::AllowedLinkProtocols.all + %i[relative] }
+ }
)
end
diff --git a/spec/features/admin/allowed_link_protocols_spec.rb b/spec/features/admin/allowed_link_protocols_spec.rb
new file mode 100644
index 00000000000..4dda8ebd881
--- /dev/null
+++ b/spec/features/admin/allowed_link_protocols_spec.rb
@@ -0,0 +1,61 @@
+# 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.
+#++
+
+require "spec_helper"
+
+RSpec.describe "Allowed link protocols", :js do
+ let(:admin) { create(:admin) }
+ let(:always_allowed_protocols) { Setting::AllowedLinkProtocols::ALWAYS_ALLOWED }
+
+ before do
+ login_as(admin)
+ visit admin_settings_general_path
+ end
+
+ it "allows updating the allowed link protocols" do
+ always_allowed_protocols.each do |protocol|
+ expect(Setting.allowed_link_protocols).not_to include(protocol)
+ expect(Setting::AllowedLinkProtocols.all).to include(protocol)
+ end
+
+ scroll_to_element find_by_id("settings_allowed_link_protocols")
+
+ custom_protocols = %w[ftp sftp data]
+ find_by_id("settings_allowed_link_protocols").set(custom_protocols.join("\n"))
+
+ click_on "Save"
+ expect(page).to have_text I18n.t(:notice_successful_update)
+
+ RequestStore.clear!
+ expect(Setting.allowed_link_protocols).to match_array(custom_protocols)
+ expect(Setting::AllowedLinkProtocols.all).to include(*custom_protocols)
+ expect(Setting::AllowedLinkProtocols.all).to include(*always_allowed_protocols)
+ end
+end
diff --git a/spec/features/wysiwyg/linking_spec.rb b/spec/features/wysiwyg/linking_spec.rb
index 38dcd4424c3..10456c7bf27 100644
--- a/spec/features/wysiwyg/linking_spec.rb
+++ b/spec/features/wysiwyg/linking_spec.rb
@@ -29,8 +29,8 @@
require "spec_helper"
RSpec.describe "Wysiwyg linking", :js do
- let(:user) { create(:admin) }
- let(:project) { create(:project, enabled_module_names: %w[wiki work_package_tracking]) }
+ shared_let(:user) { create(:admin) }
+ shared_let(:project) { create(:project, enabled_module_names: %w[wiki work_package_tracking]) }
let(:editor) { Components::WysiwygEditor.new }
before do
@@ -57,7 +57,41 @@ RSpec.describe "Wysiwyg linking", :js do
"[http://example.org/link with spaces](http://example.org/link%20with%20spaces)"
)
- expect(page).to have_css('a[href="http://example.org/link%20with%20spaces"]')
+ expect(page).to have_link(href: "http://example.org/link%20with%20spaces")
+ end
+ end
+
+ describe "linking in a wiki page with different protocols",
+ with_settings: { allowed_link_protocols: %w[data] } do
+ let(:wiki_page) { create(:wiki_page, wiki: project.wiki) }
+ let(:always_allowed_protocols) { Setting::AllowedLinkProtocols::ALWAYS_ALLOWED }
+
+ before do
+ visit edit_project_wiki_path(project, :wiki)
+ end
+
+ it "allows linking with the always allowed protocols" do
+ editor.insert_link "https://example.org/path"
+ click_on "Save"
+ expect_flash(message: "Successful update.")
+ expect(page).to have_link(href: "https://example.org/path")
+ end
+
+ it "allows linking the custom protocol" do
+ editor.insert_link "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ=="
+ click_on "Save"
+ expect_flash(message: "Successful update.")
+ expect(page).to have_link(href: "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==")
+ end
+
+ it "prevents linking to a non-allowed custom protocol" do
+ editor.insert_link "ftp://example.org/path"
+ click_on "Save"
+ expect_flash(message: "Successful update.")
+ expect(page).to have_no_link(href: "ftp://example.org/path")
+
+ link = page.find("a", text: "ftp://example.org/path")
+ expect(link["href"]).to be_nil
end
end
end
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.rb
new file mode 100644
index 00000000000..7d6dc06b06a
--- /dev/null
+++ b/spec/lib/open_project/text_formatting/allowed_link_protocols_spec.rb
@@ -0,0 +1,154 @@
+# 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.
+#++
+
+require "spec_helper"
+require_relative "markdown/expected_markdown"
+
+RSpec.describe OpenProject::TextFormatting, "OpenProject allowed link protocols" do # rubocop:disable RSpec/SpecFilePathFormat
+ include_context "expected markdown modules"
+
+ context "with default link protocols" do
+ it_behaves_like "format_text produces" do
+ let(:raw) do
+ <<~RAW
+ A link to [OpenProject](https://www.openproject.org)
+ RAW
+ end
+
+ let(:expected) do
+ <<~EXPECTED
+ <p class="op-uc-p">A link to <a href="https://www.openproject.org" rel="noopener noreferrer" target="_top" class="op-uc-link">OpenProject</a></p>
+ EXPECTED
+ end
+ end
+
+ it_behaves_like "format_text produces" do
+ let(:raw) do
+ <<~RAW
+ Contact us via [email](mailto:[email protected])
+ RAW
+ end
+
+ let(:expected) do
+ <<~EXPECTED
+ <p class="op-uc-p">Contact us via <a href="mailto:[email protected]" rel="noopener noreferrer" target="_top" class="op-uc-link">email</a></p>
+ EXPECTED
+ end
+ end
+
+ it_behaves_like "format_text produces" do
+ let(:raw) do
+ <<~RAW
+ A link with [custom protocol](ftp://example.org)
+ RAW
+ end
+
+ let(:expected) do
+ <<~EXPECTED
+ <p class="op-uc-p">A link with <a rel="noopener noreferrer" target="_top" class="op-uc-link">custom protocol</a></p>
+ EXPECTED
+ end
+ end
+ end
+
+ context "with custom allowed protocols", with_settings: { allowed_link_protocols: %w[ftp sftp] } do
+ it_behaves_like "format_text produces" do
+ let(:raw) do
+ <<~RAW
+ A link to [OpenProject](https://www.openproject.org)
+ RAW
+ end
+
+ let(:expected) do
+ <<~EXPECTED
+ <p class="op-uc-p">A link to <a href="https://www.openproject.org" rel="noopener noreferrer" target="_top" class="op-uc-link">OpenProject</a></p>
+ EXPECTED
+ end
+ end
+
+ it_behaves_like "format_text produces" do
+ let(:raw) do
+ <<~RAW
+ A link with [FTP protocol](ftp://example.org)
+ RAW
+ end
+
+ let(:expected) do
+ <<~EXPECTED
+ <p class="op-uc-p">A link with <a href="ftp://example.org" rel="noopener noreferrer" target="_top" class="op-uc-link">FTP protocol</a></p>
+ EXPECTED
+ end
+ end
+
+ it_behaves_like "format_text produces" do
+ let(:raw) do
+ <<~RAW
+ A link with [SFTP protocol](sftp://example.org)
+ RAW
+ end
+
+ let(:expected) do
+ <<~EXPECTED
+ <p class="op-uc-p">A link with <a href="sftp://example.org" rel="noopener noreferrer" target="_top" class="op-uc-link">SFTP protocol</a></p>
+ EXPECTED
+ end
+ end
+
+ it_behaves_like "format_text produces" do
+ let(:raw) do
+ <<~RAW
+ A link with [data protocol](data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==)
+ RAW
+ end
+
+ let(:expected) do
+ <<~EXPECTED
+ <p class="op-uc-p">A link with <a rel="noopener noreferrer" target="_top" class="op-uc-link">data protocol</a></p>
+ EXPECTED
+ end
+ end
+ end
+
+ context "with data protocol allowed", with_settings: { allowed_link_protocols: %w[data] } do
+ it_behaves_like "format_text produces" do
+ let(:raw) do
+ <<~RAW
+ A link with [data protocol](data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==)
+ RAW
+ end
+
+ let(:expected) do
+ <<~EXPECTED
+ <p class="op-uc-p">A link with <a href="data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==" rel="noopener noreferrer" target="_top" class="op-uc-link">data protocol</a></p>
+ EXPECTED
+ end
+ end
+ end
+end