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

Open external links with window.open using link hook (#12874)

Oliver Güntherd8b4b830d479
diff --git a/frontend/src/app/core/setup/globals/global-listeners.ts b/frontend/src/app/core/setup/globals/global-listeners.ts
index f2acbf8f325..dbdd790e4f4 100644
--- a/frontend/src/app/core/setup/globals/global-listeners.ts
+++ b/frontend/src/app/core/setup/globals/global-listeners.ts
@@ -36,22 +36,49 @@ import { dangerZoneValidation } from 'core-app/core/setup/globals/global-listene
import { setupServerResponse } from 'core-app/core/setup/globals/global-listeners/setup-server-response';
import { listenToSettingChanges } from 'core-app/core/setup/globals/global-listeners/settings';
import { detectOnboardingTour } from 'core-app/core/setup/globals/onboarding/onboarding_tour_trigger';
-import { performAnchorHijacking } from './global-listeners/link-hijacking';
+import {
+ openExternalLinksInNewTab,
+ performAnchorHijacking,
+} from './global-listeners/link-hijacking';
import { fixFragmentAnchors } from 'core-app/core/setup/globals/global-listeners/fix-fragment-anchors';
/**
* A set of listeners that are relevant on every page to set sensible defaults
*/
export function initializeGlobalListeners():void {
- jQuery(document.documentElement)
- .on('click', (evt:any) => {
- const target = jQuery(evt.target) as JQuery;
+ document
+ .documentElement
+ .addEventListener('click', (evt:MouseEvent) => {
+ const target = evt.target as HTMLElement;
+
+ // Avoid defaulting clicks on elements already removed from DOM
+ if (!document.contains(target)) {
+ evt.preventDefault();
+ return;
+ }
+
+ // Avoid handling clicks on anything other than a
+ const linkElement = target.closest<HTMLAnchorElement>('a');
+ if (!linkElement) {
+ return;
+ }
+
+ const callbacks = [
+ openExternalLinksInNewTab,
+ performAnchorHijacking,
+ ];
+
+ // eslint-disable-next-line no-restricted-syntax
+ for (const fn of callbacks) {
+ if (fn.call(linkElement, evt, linkElement)) {
+ evt.preventDefault();
+ break;
+ }
+ }
// Prevent angular handling clicks on href="#..." links from other libraries
// (especially jquery-ui and its datepicker) from routing to <base url>/#
- performAnchorHijacking(evt, target);
-
- return true;
+ performAnchorHijacking(evt, linkElement);
});
// Jump to the element given by location.hash, if present
diff --git a/frontend/src/app/core/setup/globals/global-listeners/link-hijacking.ts b/frontend/src/app/core/setup/globals/global-listeners/link-hijacking.ts
index 305a204d738..7c65bb7f369 100644
--- a/frontend/src/app/core/setup/globals/global-listeners/link-hijacking.ts
+++ b/frontend/src/app/core/setup/globals/global-listeners/link-hijacking.ts
@@ -1,28 +1,19 @@
+import { isClickedWithModifier } from 'core-app/shared/helpers/link-handling/link-handling';
+
/**
* Our application is still a hybrid one, meaning most routes are still
* handled by Rails. As such, we disable the default link-hijacking that
* Angular's HTML5-mode with <base href="/"> results in
* @param evt
- * @param target
+ * @param linkElement
*/
-export function performAnchorHijacking(evt:JQuery.TriggeredEvent, target:JQuery):void {
- // Avoid defaulting clicks on elements already removed from DOM
- if (!document.contains(evt.target as Element)) {
- evt.preventDefault();
- }
-
- // Avoid handling clicks on anything other than a
- const linkElement = target.closest('a');
- if (linkElement.length === 0) {
- return;
- }
-
- const link = linkElement.attr('href') || '';
+export function performAnchorHijacking(evt:MouseEvent, linkElement:HTMLAnchorElement):boolean {
+ const link = linkElement.getAttribute('href') || '';
const hashPos = link.indexOf('#');
// If link is neither empty nor starts with hash, ignore it
if (link !== '' && hashPos !== 0) {
- return;
+ return false;
}
// Set the location to the hash if there is any
@@ -31,5 +22,37 @@ export function performAnchorHijacking(evt:JQuery.TriggeredEvent, target:JQuery)
window.location.hash = link;
}
- evt.preventDefault();
+ return true;
+}
+
+/**
+ * Detect the origin of a clicked link
+ * @param evt
+ * @param linkElement
+ */
+export function openExternalLinksInNewTab(evt:MouseEvent, linkElement:HTMLAnchorElement):boolean {
+ if (isClickedWithModifier(evt)) {
+ return false;
+ }
+
+ const link = linkElement.href || '';
+
+ if (link === '') {
+ return false;
+ }
+
+ const origin = window.location.origin;
+
+ try {
+ const url = new URL(link, window.location.origin);
+ if (origin !== url.origin) {
+ window.open(link, '_blank', 'noopener,noreferrer');
+ return true;
+ }
+ } catch (_) {
+ // Do nothing if the url is invalid.
+ return false;
+ }
+
+ return false;
}
diff --git a/spec/features/wiki/wiki_page_external_link_spec.rb b/spec/features/wiki/wiki_page_external_link_spec.rb
new file mode 100644
index 00000000000..9913570a5a2
--- /dev/null
+++ b/spec/features/wiki/wiki_page_external_link_spec.rb
@@ -0,0 +1,69 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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 'Wiki page external link', js: true do
+ shared_let(:admin) { create(:admin) }
+ current_user { admin }
+
+ let(:project) { create(:project, enabled_module_names: %w[wiki]) }
+ let!(:wiki_page) do
+ create(:wiki_page,
+ wiki: project.wiki,
+ author: admin,
+ title: 'Wiki Page No. 55',
+ text: 'A link to <a href="http://0.0.0.0:3001/">OpenProject</a>.')
+ end
+
+ it 'opens that link in a new window or tab' do
+ visit project_wiki_path(project, wiki_page)
+
+ link = page.find('a[href^="http://0.0.0.0:3001/"]')
+ new_window = window_opened_by { link.click }
+ within_window new_window do
+ expect(page.current_url).to start_with 'http://0.0.0.0:3001/'
+ end
+ new_window.close
+ end
+
+ context 'when the link contains an invalid url' do
+ before do
+ wiki_page.update(text: 'A link to <a href="https:///">OpenProject</a>.')
+ end
+
+ it 'does not opens that link in a new window or tab' do
+ visit project_wiki_path(project, wiki_page)
+
+ link = page.find('a[href^="https:///"]')
+ expect do
+ window_opened_by { link.click }
+ end.to raise_error Capybara::WindowError, /window_opened_by opened 0 windows instead of 1/
+ end
+ end
+end