2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
module Banzai
|
|
|
|
module Filter
|
2019-09-30 21:07:59 +05:30
|
|
|
# Sanitize HTML produced by Markdown.
|
2015-09-11 14:41:01 +05:30
|
|
|
#
|
2019-09-30 21:07:59 +05:30
|
|
|
# Extends Banzai::Filter::BaseSanitizationFilter with specific rules.
|
|
|
|
class SanitizationFilter < Banzai::Filter::BaseSanitizationFilter
|
|
|
|
# Styles used by Markdown for table alignment
|
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
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
def customize_allowlist(allowlist)
|
|
|
|
# Allow table alignment; we allow specific text-align values in a
|
2017-09-10 17:25:29 +05:30
|
|
|
# transformer below
|
2021-04-17 20:07:23 +05:30
|
|
|
allowlist[:attributes]['th'] = %w[style]
|
|
|
|
allowlist[:attributes]['td'] = %w[style]
|
2021-03-08 18:12:59 +05:30
|
|
|
allowlist[:css] = { properties: ['text-align'] }
|
2015-09-11 14:41:01 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
# Allow the 'data-sourcepos' from CommonMark on all elements
|
2021-03-08 18:12:59 +05:30
|
|
|
allowlist[:attributes][:all].push('data-sourcepos')
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
# Remove any `style` properties not required for table alignment
|
2021-03-08 18:12:59 +05:30
|
|
|
allowlist[:transformers].push(self.class.remove_unsafe_table_style)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
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
|
2021-03-08 18:12:59 +05:30
|
|
|
allowlist[:attributes]['a'].push('id')
|
2021-04-17 20:07:23 +05:30
|
|
|
allowlist[:attributes]['li'] = %w[id]
|
2021-03-08 18:12:59 +05:30
|
|
|
allowlist[:transformers].push(self.class.remove_non_footnote_ids)
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
allowlist
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
class << self
|
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')
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
if Feature.enabled?(:use_cmark_renderer)
|
|
|
|
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
|
|
|
|
else
|
|
|
|
return if node.name == 'a' && node['id'] =~ Banzai::Filter::FootnoteFilter::FOOTNOTE_LINK_REFERENCE_PATTERN_OLD
|
|
|
|
return if node.name == 'li' && node['id'] =~ Banzai::Filter::FootnoteFilter::FOOTNOTE_LI_REFERENCE_PATTERN_OLD
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
node.remove_attribute('id')
|
|
|
|
end
|
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
Banzai::Filter::SanitizationFilter.prepend_mod_with('Banzai::Filter::SanitizationFilter')
|