debian-mirror-gitlab/lib/banzai/filter/sanitization_filter.rb

112 lines
3.6 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2015-12-23 02:04:40 +05:30
module Banzai
module Filter
2015-09-11 14:41:01 +05:30
# Sanitize HTML
#
# Extends HTML::Pipeline::SanitizationFilter with a custom whitelist.
class SanitizationFilter < HTML::Pipeline::SanitizationFilter
2018-11-08 19:23:39 +05:30
include Gitlab::Utils::StrongMemoize
2019-07-31 22:56:46 +05:30
extend Gitlab::Utils::SanitizeNodeLink
2018-11-08 19:23:39 +05:30
2019-03-02 22:35:43 +05:30
TABLE_ALIGNMENT_PATTERN = /text-align: (?<alignment>center|left|right)/.freeze
2016-06-02 11:05:42 +05:30
2015-09-11 14:41:01 +05:30
def whitelist
2018-11-08 19:23:39 +05:30
strong_memoize(:whitelist) do
customize_whitelist(super.deep_dup)
end
2015-09-11 14:41:01 +05:30
end
private
def customize_whitelist(whitelist)
2018-06-27 16:04:02 +05:30
# Allow table alignment; we whitelist specific text-align values in a
2017-09-10 17:25:29 +05:30
# transformer below
2015-09-11 14:41:01 +05:30
whitelist[:attributes]['th'] = %w(style)
whitelist[:attributes]['td'] = %w(style)
2018-06-27 16:04:02 +05:30
whitelist[:css] = { properties: ['text-align'] }
2015-09-11 14:41:01 +05:30
# Allow span elements
whitelist[:elements].push('span')
2017-09-10 17:25:29 +05:30
# Allow data-math-style attribute in order to support LaTeX formatting
whitelist[:attributes]['code'] = %w(data-math-style)
whitelist[:attributes]['pre'] = %w(data-math-style)
2017-08-17 22:00:37 +05:30
# Allow html5 details/summary elements
whitelist[:elements].push('details')
whitelist[:elements].push('summary')
2016-04-02 18:10:28 +05:30
# Allow abbr elements with title attribute
whitelist[:elements].push('abbr')
whitelist[:attributes]['abbr'] = %w(title)
2019-03-02 22:35:43 +05:30
# Allow the 'data-sourcepos' from CommonMark on all elements
whitelist[:attributes][:all].push('data-sourcepos')
2018-03-17 18:26:18 +05:30
# Disallow `name` attribute globally, allow on `a`
2017-09-10 17:25:29 +05:30
whitelist[:attributes][:all].delete('name')
2018-03-17 18:26:18 +05:30
whitelist[:attributes]['a'].push('name')
2017-09-10 17:25:29 +05:30
2019-03-02 22:35:43 +05:30
# Allow any protocol in `a` elements
# and then remove links with unsafe protocols
2015-11-26 14:37:03 +05:30
whitelist[:protocols].delete('a')
2019-07-31 22:56:46 +05:30
whitelist[:transformers].push(self.class.method(:remove_unsafe_links))
2015-11-26 14:37:03 +05:30
2015-09-11 14:41:01 +05:30
# Remove `rel` attribute from `a` elements
2016-09-29 09:46:39 +05:30
whitelist[:transformers].push(self.class.remove_rel)
2015-09-11 14:41:01 +05:30
2017-09-10 17:25:29 +05:30
# Remove any `style` properties not required for table alignment
whitelist[:transformers].push(self.class.remove_unsafe_table_style)
2019-03-02 22:35:43 +05:30
# Allow `id` in a and li elements for footnotes
# and remove any `id` properties not matching for footnotes
whitelist[:attributes]['a'].push('id')
whitelist[:attributes]['li'] = %w(id)
whitelist[:transformers].push(self.class.remove_non_footnote_ids)
2015-09-11 14:41:01 +05:30
whitelist
end
2016-09-29 09:46:39 +05:30
class << self
def remove_rel
lambda do |env|
if env[:node_name] == 'a'
env[:node].remove_attribute('rel')
end
2015-09-11 14:41:01 +05:30
end
end
2017-09-10 17:25:29 +05:30
def remove_unsafe_table_style
lambda do |env|
node = env[:node]
return unless node.name == 'th' || node.name == 'td'
return unless node.has_attribute?('style')
if node['style'] =~ TABLE_ALIGNMENT_PATTERN
node['style'] = "text-align: #{$~[:alignment]}"
else
node.remove_attribute('style')
end
end
end
2019-03-02 22:35:43 +05:30
def remove_non_footnote_ids
lambda do |env|
node = env[:node]
return unless node.name == 'a' || node.name == 'li'
return unless node.has_attribute?('id')
return if node.name == 'a' && node['id'] =~ Banzai::Filter::FootnoteFilter::FOOTNOTE_LINK_REFERENCE_PATTERN
return if node.name == 'li' && node['id'] =~ Banzai::Filter::FootnoteFilter::FOOTNOTE_LI_REFERENCE_PATTERN
node.remove_attribute('id')
end
end
2015-09-11 14:41:01 +05:30
end
end
end
end