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

add noopener tag to all anchor tags to prevent tabnabbing

ulferts8511aca9394d
diff --git a/lib/open_project/text_formatting/filters/autolink_filter.rb b/lib/open_project/text_formatting/filters/autolink_filter.rb
index 797372f8a70..32afb495439 100644
--- a/lib/open_project/text_formatting/filters/autolink_filter.rb
+++ b/lib/open_project/text_formatting/filters/autolink_filter.rb
@@ -51,7 +51,7 @@ module OpenProject::TextFormatting
def default_autolink_options
{
enabled: true,
- classes: 'rinku-autolink',
+ classes: 'rinku-autolink'
}
end
end
diff --git a/lib/open_project/text_formatting/filters/macro_filter.rb b/lib/open_project/text_formatting/filters/macro_filter.rb
index 12543fd232f..3de0b142e76 100644
--- a/lib/open_project/text_formatting/filters/macro_filter.rb
+++ b/lib/open_project/text_formatting/filters/macro_filter.rb
@@ -72,7 +72,6 @@ module OpenProject::TextFormatting
data: { macro_name: macro_class.identifier }
end
-
def macro_placeholder(macro_class)
ApplicationController.helpers.content_tag :macro,
I18n.t('macros.placeholder', macro_name: macro_class.identifier),
diff --git a/lib/open_project/text_formatting/filters/relative_link_filter.rb b/lib/open_project/text_formatting/filters/relative_link_filter.rb
index 5e71e823987..9a2d0aa466d 100644
--- a/lib/open_project/text_formatting/filters/relative_link_filter.rb
+++ b/lib/open_project/text_formatting/filters/relative_link_filter.rb
@@ -31,7 +31,6 @@
module OpenProject::TextFormatting
module Filters
class RelativeLinkFilter < HTML::Pipeline::Filter
-
def call
# We only care for absolute rendering
unless context[:only_path] == false
diff --git a/lib/open_project/text_formatting/filters/sanitization_filter.rb b/lib/open_project/text_formatting/filters/sanitization_filter.rb
index bea52dfd0aa..6d4a52edbe1 100644
--- a/lib/open_project/text_formatting/filters/sanitization_filter.rb
+++ b/lib/open_project/text_formatting/filters/sanitization_filter.rb
@@ -35,7 +35,17 @@ module OpenProject::TextFormatting
super.merge(whitelist: WHITELIST.merge(
elements: WHITELIST[:elements] + ['macro'],
# Whitelist class and data-* attributes on all macros
- attributes: WHITELIST[:attributes].merge('macro' => ['class', :data])
+ attributes: WHITELIST[:attributes].merge('macro' => ['class', :data]),
+ transformers: WHITELIST[:transformers] + [
+ # Add rel attribute to prevent tabnabbing
+ lambda { |env|
+ name = env[:node_name]
+ node = env[:node]
+ if name == 'a'
+ node['rel'] = 'noopener noreferrer'
+ end
+ }
+ ]
))
end
end
diff --git a/lib/open_project/text_formatting/helpers/link_rewriter.rb b/lib/open_project/text_formatting/helpers/link_rewriter.rb
index bb761a8d145..2b111985bb6 100644
--- a/lib/open_project/text_formatting/helpers/link_rewriter.rb
+++ b/lib/open_project/text_formatting/helpers/link_rewriter.rb
@@ -30,7 +30,6 @@
module OpenProject::TextFormatting
module Helpers
-
##
# Rewrite relative URLs to their absolute paths
# when only_path is false.
diff --git a/spec/lib/open_project/text_formatting/markdown/markdown_formatting_spec.rb b/spec/lib/open_project/text_formatting/markdown/markdown_formatting_spec.rb
index 0b547fce9ee..0bffc3a1b4f 100644
--- a/spec/lib/open_project/text_formatting/markdown/markdown_formatting_spec.rb
+++ b/spec/lib/open_project/text_formatting/markdown/markdown_formatting_spec.rb
@@ -50,12 +50,19 @@ describe OpenProject::TextFormatting::Formats::Markdown::Formatter do
)
end
- it 'should escaping' do
+ it 'escapes script tags' do
assert_html_output(
'this is a <script>' => 'this is a &lt;script&gt;'
)
end
+ it 'limits `a` tags and hardens them against tabnabbing' do
+ assert_html_output(
+ 'this is a <a style="display:none;" href="http://malicious">' =>
+ 'this is a <a href="http://malicious" rel="noopener noreferrer">'
+ )
+ end
+
it 'should use of backslashes followed by numbers in headers' do
html = <<-HTML.strip_heredoc
<h1 id="20090209">
@@ -128,7 +135,7 @@ describe OpenProject::TextFormatting::Formats::Markdown::Formatter do
assert_html_output(
{
'Link to [relative path](/foo/bar)' =>
- %(Link to <a href="/foo/bar">relative path</a>),
+ %(Link to <a href="/foo/bar" rel="noopener noreferrer">relative path</a>),
'An inline image ![](/attachments/123/foobar.png)' =>
%(An inline image <img src="/attachments/123/foobar.png" alt="" />)
},
@@ -152,7 +159,7 @@ describe OpenProject::TextFormatting::Formats::Markdown::Formatter do
assert_html_output(
{
'Link to [relative path](/foo/bar)' =>
- %(Link to <a href="http://openproject.org/foo/bar">relative path</a>),
+ %(Link to <a href="http://openproject.org/foo/bar" rel="noopener noreferrer">relative path</a>),
'An inline image ![](/attachments/123/foobar.png)' =>
%(An inline image <img src="http://openproject.org/attachments/123/foobar.png" alt="" />)
},
diff --git a/spec/requests/api/v3/render_resource_spec.rb b/spec/requests/api/v3/render_resource_spec.rb
index dbf505fa563..74759135b6f 100644
--- a/spec/requests/api/v3/render_resource_spec.rb
+++ b/spec/requests/api/v3/render_resource_spec.rb
@@ -70,7 +70,7 @@ describe 'API v3 Render resource', type: :request do
it_behaves_like 'valid response' do
let(:text) do
'<p>Hello World! This <em>is</em> markdown with a ' +
- '<a href="http://community.openproject.org">link</a> ' +
+ '<a href="http://community.openproject.org" rel="noopener noreferrer">link</a> ' +
'and ümläutß.</p>'
end
end