2018-07-23 09:32:32 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-15 14:51:16 +00:00
|
|
|
module Banzai
|
|
|
|
module Filter
|
2019-07-03 08:53:00 +00:00
|
|
|
# Sanitize HTML produced by Markdown.
|
2015-04-27 22:56:37 +00:00
|
|
|
#
|
2019-07-03 08:53:00 +00:00
|
|
|
# Extends Banzai::Filter::BaseSanitizationFilter with specific rules.
|
|
|
|
class SanitizationFilter < Banzai::Filter::BaseSanitizationFilter
|
|
|
|
# Styles used by Markdown for table alignment
|
2019-01-17 03:13:44 +00:00
|
|
|
TABLE_ALIGNMENT_PATTERN = /text-align: (?<alignment>center|left|right)/.freeze
|
2016-02-24 01:40:31 +00:00
|
|
|
|
2015-05-29 23:01:12 +00:00
|
|
|
def customize_whitelist(whitelist)
|
2018-06-15 12:05:26 +00:00
|
|
|
# Allow table alignment; we whitelist specific text-align values in a
|
2017-08-31 15:27:34 +00:00
|
|
|
# transformer below
|
2015-05-29 23:01:12 +00:00
|
|
|
whitelist[:attributes]['th'] = %w(style)
|
|
|
|
whitelist[:attributes]['td'] = %w(style)
|
2018-06-15 12:05:26 +00:00
|
|
|
whitelist[:css] = { properties: ['text-align'] }
|
2015-04-27 22:56:37 +00:00
|
|
|
|
2018-12-21 02:22:27 +00:00
|
|
|
# Allow the 'data-sourcepos' from CommonMark on all elements
|
|
|
|
whitelist[:attributes][:all].push('data-sourcepos')
|
|
|
|
|
2017-08-31 15:27:34 +00:00
|
|
|
# Remove any `style` properties not required for table alignment
|
|
|
|
whitelist[:transformers].push(self.class.remove_unsafe_table_style)
|
|
|
|
|
2019-01-05 02:54:02 +00:00
|
|
|
# Allow `id` in a and li elements for footnotes
|
2019-01-17 03:13:44 +00:00
|
|
|
# and remove any `id` properties not matching for footnotes
|
2019-01-05 02:54:02 +00:00
|
|
|
whitelist[:attributes]['a'].push('id')
|
|
|
|
whitelist[:attributes]['li'] = %w(id)
|
|
|
|
whitelist[:transformers].push(self.class.remove_non_footnote_ids)
|
|
|
|
|
2015-04-27 22:56:37 +00:00
|
|
|
whitelist
|
|
|
|
end
|
|
|
|
|
2016-09-21 13:55:04 +00:00
|
|
|
class << self
|
2017-08-31 15:27:34 +00:00
|
|
|
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-01-05 02:54:02 +00:00
|
|
|
|
|
|
|
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')
|
|
|
|
|
2019-01-17 03:13:44 +00:00
|
|
|
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
|
2019-01-05 02:54:02 +00:00
|
|
|
|
|
|
|
node.remove_attribute('id')
|
|
|
|
end
|
|
|
|
end
|
2015-05-08 16:17:54 +00:00
|
|
|
end
|
2015-04-27 22:56:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|