2015-08-27 23:02:49 +00:00
|
|
|
require 'gitlab/markdown'
|
2015-04-27 22:56:37 +00:00
|
|
|
require 'html/pipeline/filter'
|
|
|
|
require 'html/pipeline/sanitization_filter'
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Markdown
|
|
|
|
# Sanitize HTML
|
|
|
|
#
|
|
|
|
# Extends HTML::Pipeline::SanitizationFilter with a custom whitelist.
|
|
|
|
class SanitizationFilter < HTML::Pipeline::SanitizationFilter
|
|
|
|
def whitelist
|
2015-05-29 23:01:12 +00:00
|
|
|
# Descriptions are more heavily sanitized, allowing only a few elements.
|
|
|
|
# See http://git.io/vkuAN
|
|
|
|
if pipeline == :description
|
|
|
|
whitelist = LIMITED
|
2015-06-02 17:41:12 +00:00
|
|
|
whitelist[:elements] -= %w(pre code img ol ul li)
|
2015-05-29 23:01:12 +00:00
|
|
|
else
|
|
|
|
whitelist = super
|
|
|
|
end
|
|
|
|
|
|
|
|
customize_whitelist(whitelist)
|
|
|
|
|
|
|
|
whitelist
|
|
|
|
end
|
2015-04-27 22:56:37 +00:00
|
|
|
|
2015-05-29 23:01:12 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def pipeline
|
|
|
|
context[:pipeline] || :default
|
|
|
|
end
|
|
|
|
|
|
|
|
def customized?(transformers)
|
|
|
|
transformers.last.source_location[0] == __FILE__
|
|
|
|
end
|
|
|
|
|
|
|
|
def customize_whitelist(whitelist)
|
2015-05-21 00:55:11 +00:00
|
|
|
# Only push these customizations once
|
2015-05-29 23:01:12 +00:00
|
|
|
return if customized?(whitelist[:transformers])
|
2015-04-27 22:56:37 +00:00
|
|
|
|
2015-05-29 23:01:12 +00:00
|
|
|
# Allow code highlighting
|
|
|
|
whitelist[:attributes]['pre'] = %w(class)
|
|
|
|
whitelist[:attributes]['span'] = %w(class)
|
2015-04-27 22:56:37 +00:00
|
|
|
|
2015-05-29 23:01:12 +00:00
|
|
|
# Allow table alignment
|
|
|
|
whitelist[:attributes]['th'] = %w(style)
|
|
|
|
whitelist[:attributes]['td'] = %w(style)
|
2015-04-27 22:56:37 +00:00
|
|
|
|
2015-05-29 23:01:12 +00:00
|
|
|
# Allow span elements
|
|
|
|
whitelist[:elements].push('span')
|
2015-04-27 22:56:37 +00:00
|
|
|
|
2015-05-29 23:01:12 +00:00
|
|
|
# Remove `rel` attribute from `a` elements
|
|
|
|
whitelist[:transformers].push(remove_rel)
|
|
|
|
|
|
|
|
# Remove `class` attribute from non-highlight spans
|
|
|
|
whitelist[:transformers].push(clean_spans)
|
2015-05-08 16:17:54 +00:00
|
|
|
|
2015-04-27 22:56:37 +00:00
|
|
|
whitelist
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_rel
|
|
|
|
lambda do |env|
|
|
|
|
if env[:node_name] == 'a'
|
|
|
|
env[:node].remove_attribute('rel')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-05-08 16:17:54 +00:00
|
|
|
|
|
|
|
def clean_spans
|
|
|
|
lambda do |env|
|
2015-09-05 21:50:47 +00:00
|
|
|
node = env[:node]
|
2015-05-08 16:17:54 +00:00
|
|
|
|
2015-09-05 21:50:47 +00:00
|
|
|
return unless node.name == 'span'
|
|
|
|
return unless node.has_attribute?('class')
|
|
|
|
|
|
|
|
unless has_ancestor?(node, 'pre')
|
|
|
|
node.remove_attribute('class')
|
2015-05-08 16:17:54 +00:00
|
|
|
end
|
2015-09-05 21:50:47 +00:00
|
|
|
|
|
|
|
{node_whitelist: [node]}
|
2015-05-08 16:17:54 +00:00
|
|
|
end
|
|
|
|
end
|
2015-04-27 22:56:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|