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

[#56694] escape potentially malicious HTML

Tobias Dillmannde9e6e146446
diff --git a/app/helpers/search_helper.rb b/app/helpers/search_helper.rb
index a8b4a262704..c178e06ae48 100644
--- a/app/helpers/search_helper.rb
+++ b/app/helpers/search_helper.rb
@@ -73,12 +73,18 @@ module SearchHelper
doc = Nokogiri::HTML::DocumentFragment.parse(html)
tokens.each do |token|
+ escaped_token = Regexp.escape(token)
+
doc.traverse do |node|
next unless node.text?
+ escaped_text = CGI.escapeHTML(node.content)
+
t = (tokens.index(node.content.downcase) || 0) % 4
- highlighted_text = node.content.gsub(/(#{Regexp.escape(token)})/i,
- "<span class='search-highlight token-#{t}'>\\1</span>")
+ highlighted_text = escaped_text.gsub(/(#{escaped_token})/i) do
+ %{<span class="search-highlight token-#{t}">#{$1}</span>}
+ end
+
node.replace(Nokogiri::HTML::DocumentFragment.parse(highlighted_text))
end
end