From faafc74f035e60bfd0557fff157c6b2d1e76b771 Mon Sep 17 00:00:00 2001 From: jplang Date: Thu, 6 Apr 2017 16:41:52 +0000 Subject: [PATCH] Filter arbitrary class names and ids in rendered HTML output (#25503). * Disallow setting arbitrary classes and ids via Textile syntax * Only allow valid/supported languages for syntax highlighted code blocks Patch by Jan Schulz-Hofen. git-svn-id: https://svn.redmine.org/redmine/trunk@16502 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- .../wiki_formatting/markdown/formatter.rb | 2 +- .../wiki_formatting/textile/formatter.rb | 10 +++- .../wiki_formatting/textile/redcloth3.rb | 12 ++++- test/unit/helpers/application_helper_test.rb | 13 ++--- .../markdown_formatter_test.rb | 9 ++++ .../wiki_formatting/textile_formatter_test.rb | 48 +++++++++++++++++-- 6 files changed, 80 insertions(+), 14 deletions(-) diff --git a/lib/redmine/wiki_formatting/markdown/formatter.rb b/lib/redmine/wiki_formatting/markdown/formatter.rb index 4afbc2fdd..bfb04774c 100644 --- a/lib/redmine/wiki_formatting/markdown/formatter.rb +++ b/lib/redmine/wiki_formatting/markdown/formatter.rb @@ -35,7 +35,7 @@ module Redmine end def block_code(code, language) - if language.present? + if language.present? && Redmine::SyntaxHighlighting.language_supported?(language) "
" +
               Redmine::SyntaxHighlighting.highlight_by_language(code, language) +
               "
" diff --git a/lib/redmine/wiki_formatting/textile/formatter.rb b/lib/redmine/wiki_formatting/textile/formatter.rb index 5862a1c62..8ff623a73 100644 --- a/lib/redmine/wiki_formatting/textile/formatter.rb +++ b/lib/redmine/wiki_formatting/textile/formatter.rb @@ -121,8 +121,14 @@ module Redmine text.gsub!(//) do content = @pre_list[$1.to_i] if content.match(/\s?(.+)/m) - content = "" + - Redmine::SyntaxHighlighting.highlight_by_language($2, $1) + language = $1 + text = $2 + if Redmine::SyntaxHighlighting.language_supported?(language) + content = "" + + Redmine::SyntaxHighlighting.highlight_by_language(text, language) + else + content = "#{ERB::Util.h(text)}" + end end content end diff --git a/lib/redmine/wiki_formatting/textile/redcloth3.rb b/lib/redmine/wiki_formatting/textile/redcloth3.rb index bcb796ec6..d0bd217d3 100644 --- a/lib/redmine/wiki_formatting/textile/redcloth3.rb +++ b/lib/redmine/wiki_formatting/textile/redcloth3.rb @@ -494,7 +494,15 @@ class RedCloth3 < String style << "text-align:#{ h_align( $& ) };" if text =~ A_HLGN cls, id = $1, $2 if cls =~ /^(.*?)#(.*)$/ - + + # add wiki-class- and wiki-id- to classes and ids to prevent setting of + # arbitrary classes and ids + cls = cls.split(/\s+/).map do |c| + c.starts_with?('wiki-class-') ? c : "wiki-class-#{c}" + end.join(' ') if cls + + id = id.starts_with?('wiki-id-') ? id : "wiki-id-#{id}" if id + atts = '' atts << " style=\"#{ style.join }\"" unless style.empty? atts << " class=\"#{ cls }\"" unless cls.to_s.empty? @@ -1097,7 +1105,7 @@ class RedCloth3 < String first.match(/<#{ OFFTAGS }([^>]*)>/) tag = $1 $2.to_s.match(/(class\=("[^"]+"|'[^']+'))/i) - tag << " #{$1}" if $1 + tag << " #{$1}" if $1 && tag == 'code' @pre_list << "<#{ tag }>#{ aftertag }" end elsif $1 and codepre > 0 diff --git a/test/unit/helpers/application_helper_test.rb b/test/unit/helpers/application_helper_test.rb index a0d88eb5d..500fbd86e 100644 --- a/test/unit/helpers/application_helper_test.rb +++ b/test/unit/helpers/application_helper_test.rb @@ -117,7 +117,8 @@ class ApplicationHelperTest < Redmine::HelperTest to_test = { '!http://foo.bar/image.jpg!' => '', 'floating !>http://foo.bar/image.jpg!' => 'floating ', - 'with class !(some-class)http://foo.bar/image.jpg!' => 'with class ', + 'with class !(some-class)http://foo.bar/image.jpg!' => 'with class ', + 'with class !(wiki-class-foo)http://foo.bar/image.jpg!' => 'with class ', 'with style !{width:100px;height:100px}http://foo.bar/image.jpg!' => 'with style ', 'with title !http://foo.bar/image.jpg(This is a title)!' => 'with title This is a title', 'with title !http://foo.bar/image.jpg(This is a double-quoted "title")!' => 'with title This is a double-quoted "title"', @@ -911,11 +912,11 @@ RAW "
content
" => "
<div>content</div>
", "HTML comment: " => "

HTML comment: <!-- no comments -->

", "