Feature #44585 » 0002-Sanitize-the-HTML-generated-by-the-Textile-formatter.patch
| lib/redmine/wiki_formatting/textile/formatter.rb | ||
|---|---|---|
|
Redmine::WikiFormatting::TablesortScrubber.new
|
||
|
]
|
||
|
SANITIZER = SanitizationFilter.new
|
||
|
class Formatter
|
||
|
include Redmine::WikiFormatting::SectionHelper
|
||
| ... | ... | |
|
def to_html(*rules)
|
||
|
html = @filter.to_html(rules)
|
||
|
fragment = Loofah.html5_fragment(html)
|
||
|
SANITIZER.call(fragment)
|
||
|
scrubbers = SCRUBBERS + post_processor_scrubbers
|
||
|
scrubber = Loofah::Scrubber.new do |node|
|
||
| lib/redmine/wiki_formatting/textile/sanitization_filter.rb | ||
|---|---|---|
|
# frozen_string_literal: true
|
||
|
# Redmine - project management software
|
||
|
# Copyright (C) 2006- Jean-Philippe Lang
|
||
|
#
|
||
|
# 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.
|
||
|
module Redmine
|
||
|
module WikiFormatting
|
||
|
module Textile
|
||
|
# Sanitizes the HTML generated by the Textile formatter
|
||
|
class SanitizationFilter < Redmine::WikiFormatting::SanitizationFilter
|
||
|
ALLOWED_CLASS_RE = /\A(wiki-class-\S+|external|email|footnote)\z/
|
||
|
ALLOWED_ID_RE = /\A(wiki-id-\S+|fn\d+)\z/
|
||
|
# Same properties as the ones accepted by RedCloth3::STYLES_RE
|
||
|
STYLE_PROPERTY_RE = /\A(color|(min-|max-)?(width|height)|border|background|padding|margin|font|text|float)(-[a-z]+)*\z/
|
||
|
STYLE_PROPERTIES =
|
||
|
(Sanitize::Config::RELAXED[:css][:properties].grep(STYLE_PROPERTY_RE) + %w[vertical-align]).freeze
|
||
|
private
|
||
|
def default_allowlist
|
||
|
allowlist = super
|
||
|
allowlist[:attributes][:all] += %w[class id]
|
||
|
allowlist[:css][:properties] |= STYLE_PROPERTIES
|
||
|
allowlist
|
||
|
end
|
||
|
def add_transformers
|
||
|
super
|
||
|
allowlist[:transformers].push method(:remove_disallowed_classes_and_ids)
|
||
|
end
|
||
|
def remove_disallowed_classes_and_ids(env)
|
||
|
node = env[:node]
|
||
|
return unless node.element?
|
||
|
# The class of <code> holds the language for syntax highlighting
|
||
|
if node['class'] && node.name != 'code'
|
||
|
allowed_classes = node['class'].split.grep(ALLOWED_CLASS_RE)
|
||
|
if allowed_classes.any?
|
||
|
node['class'] = allowed_classes.join(' ')
|
||
|
else
|
||
|
node.remove_attribute('class')
|
||
|
end
|
||
|
end
|
||
|
node.remove_attribute('id') if node['id'] && !ALLOWED_ID_RE.match?(node['id'])
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
| test/helpers/application_helper_test.rb | ||
|---|---|---|
|
'<a class="external" href="http://example.net/path!602815048C7B5C20!302.html">' \
|
||
|
'http://example.net/path!602815048C7B5C20!302.html</a>',
|
||
|
# escaping
|
||
|
'http://foo"bar' => '<a class="external" href="http://foo"bar">http://foo"bar</a>',
|
||
|
'http://foo"bar' => '<a class="external" href="http://foo%22bar">http://foo"bar</a>',
|
||
|
# wrap in angle brackets
|
||
|
'<http://foo.bar>' => '<<a class="external" href="http://foo.bar">http://foo.bar</a>>',
|
||
|
# invalid urls
|
||
| ... | ... | |
|
'"a link":http://example.net/path!602815048C7B5C20!302.html' =>
|
||
|
'<a href="http://example.net/path!602815048C7B5C20!302.html" class="external">a link</a>',
|
||
|
# escaping
|
||
|
'"test":http://foo"bar' => '<a href="http://foo"bar" class="external">test</a>',
|
||
|
'"test":http://foo"bar' => '<a href="http://foo%22bar" class="external">test</a>',
|
||
|
# ends with a hyphen
|
||
|
'(see "inline link":http://www.foo.bar/Test-)' =>
|
||
|
'(see <a href="http://www.foo.bar/Test-" class="external">inline link</a>)',
|
||
| test/unit/lib/redmine/wiki_formatting/textile/sanitization_filter_test.rb | ||
|---|---|---|
|
# frozen_string_literal: true
|
||
|
# Redmine - project management software
|
||
|
# Copyright (C) 2006- Jean-Philippe Lang
|
||
|
#
|
||
|
# 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.
|
||
|
require_relative '../../../../../test_helper'
|
||
|
class Redmine::WikiFormatting::Textile::SanitizationFilterTest < ActiveSupport::TestCase
|
||
|
def filter(html)
|
||
|
fragment = Redmine::WikiFormatting::HtmlParser.parse(html)
|
||
|
sanitizer = Redmine::WikiFormatting::Textile::SanitizationFilter.new
|
||
|
sanitizer.call(fragment)
|
||
|
fragment.to_s
|
||
|
end
|
||
|
def test_should_filter_tags
|
||
|
input = %(<script>alert(1)</script><iframe src="https://example.com/"></iframe><input type="text"> <b>foo</b>)
|
||
|
assert_equal %( <b>foo</b>), filter(input)
|
||
|
end
|
||
|
def test_should_sanitize_attributes
|
||
|
input = %(<img src="foo.png" onerror="alert(1)"> <a href="foo" onclick="alert(1)">link</a>)
|
||
|
assert_equal %(<img src="foo.png"> <a href="foo">link</a>), filter(input)
|
||
|
end
|
||
|
def test_should_remove_links_with_unsafe_scheme
|
||
|
input = %(<a href="javascript:alert(1)">link</a>)
|
||
|
assert_equal %(<a>link</a>), filter(input)
|
||
|
end
|
||
|
def test_should_allow_classes_and_ids_generated_by_textile
|
||
|
[
|
||
|
%(<p class="wiki-class-foo wiki-class-bar" id="wiki-id-baz">foo</p>),
|
||
|
%(<a class="external" href="https://example.com/">foo</a>),
|
||
|
%(<a class="email" href="mailto:foo@example.com">foo</a>),
|
||
|
%(<p id="fn1" class="footnote"><sup>1</sup> foo</p>),
|
||
|
%(<pre><code class="ruby">foo</code></pre>),
|
||
|
].each do |input|
|
||
|
assert_equal input, filter(input)
|
||
|
end
|
||
|
end
|
||
|
def test_should_remove_other_classes_and_ids
|
||
|
input = %(<p class="wiki-class-foo flash error" id="main-menu">foo</p>)
|
||
|
assert_equal %(<p class="wiki-class-foo">foo</p>), filter(input)
|
||
|
end
|
||
|
def test_should_allow_styles_generated_by_textile
|
||
|
[
|
||
|
%(<p style="vertical-align:top;">foo</p>),
|
||
|
%(<p style="background:#fdd;">foo</p>),
|
||
|
%(<p style="border-top-left-radius: 10px 5px;">foo</p>),
|
||
|
].each do |input|
|
||
|
assert_equal input, filter(input)
|
||
|
end
|
||
|
end
|
||
|
end
|
||
| test/unit/lib/redmine/wiki_formatting/textile_formatter_test.rb | ||
|---|---|---|
|
raw =
|
||
|
'!/images/comment.png"onclick=alert('XSS');"!'
|
||
|
expected =
|
||
|
'<p><img src="/images/comment.png"onclick=' \
|
||
|
'<p><img src="/images/comment.png%22onclick=' \
|
||
|
'&#x61;&#x6c;&#x65;&#x72;&#x74;&#x28;' \
|
||
|
'&#x27;&#x58;&#x53;&#x53;&#x27;&#x29;;&#x22;" alt=""></p>'
|
||
|
assert_equal expected.gsub(%r{\s+}, ''), to_html(raw).gsub(%r{\s+}, '')
|
||
| ... | ... | |
|
payload = %{https://example.com/?a:"onmouseover=alert(document.domain)//"}
|
||
|
html = to_html(payload)
|
||
|
assert_includes html, '"', 'quotes inside the href must stay escaped'
|
||
|
assert_includes html, 'href="https://example.com/?a:%22onmouseover', 'quotes inside the href must stay escaped'
|
||
|
assert_empty(
|
||
|
Nokogiri::HTML.fragment(html).css('[onmouseover]'),
|
||
|
"restore_redmine_links injected an attribute into the rendered HTML: #{html}"
|
||