wipe.rb 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class HtmlSanitizer
  3. module Scrubber
  4. class Wipe < Base
  5. def initialize # rubocop:disable Lint/MissingSuper
  6. @direction = :bottom_up
  7. end
  8. def scrub(node)
  9. return STOP if clear_tags_allowlist(node)
  10. return STOP if remove_unsafe_src(node)
  11. clear_css_classes(node)
  12. move_attrs_to_css(node)
  13. clear_style(node)
  14. remove_invalid_links(node)
  15. remove_attributes_not_in_allowlist(node)
  16. end
  17. private
  18. def remove_attributes_not_in_allowlist(node)
  19. node.each do |attribute, _value| # rubocop:disable Style/HashEachMethods
  20. attribute_name = attribute.downcase
  21. next if attributes_allowlist[:all].include?(attribute_name) || attributes_allowlist[node.name]&.include?(attribute_name)
  22. node.delete(attribute)
  23. end
  24. end
  25. def remove_invalid_links(node)
  26. %w[href style].each do |attribute_name|
  27. next if !node[attribute_name]
  28. href = cleanup_target(node[attribute_name])
  29. next if !href.match?(%r{(javascript|livescript|vbscript):}i)
  30. node.delete(attribute_name)
  31. end
  32. end
  33. def clear_style(node)
  34. return if !node['style']
  35. style = clear_style_pairs(node)
  36. .each_with_object('') do |elem, memo|
  37. memo << "#{elem};" if clear_style_pair_valid?(node, elem)
  38. end
  39. node['style'] = style
  40. node.delete('style') if style.blank?
  41. end
  42. def clear_style_pairs(node)
  43. node['style'].downcase.gsub(%r{\t|\n|\r}, '').split(';')
  44. end
  45. def clear_style_pair_valid?(node, pair)
  46. prop = pair.split(':')
  47. return if prop.first.blank?
  48. return if !clear_style_allowed?(node, prop)
  49. return if clear_style_blocked?(node, pair)
  50. true
  51. end
  52. def clear_style_allowed?(node, prop)
  53. return if css_properties_allowlist.exclude?(node.name)
  54. return if css_properties_allowlist[node.name].exclude?(prop.first.strip)
  55. true
  56. end
  57. def clear_style_blocked?(node, pair)
  58. css_values_blocklist[node.name]&.include?(pair.gsub(%r{[[:space:]]}, '').strip)
  59. end
  60. def move_attrs_to_css(node)
  61. attributes_2_css.each do |key|
  62. next if !node[key]
  63. value = node[key]
  64. node.delete(key)
  65. next if value.blank?
  66. next if node_has_css?(node, key)
  67. node_set_style(node, key, value)
  68. end
  69. end
  70. def node_has_css?(node, key)
  71. return false if node['style'].blank?
  72. return false if node['style'].split(';').blank?
  73. node['style'].split(';').filter_map { |attr| attr.split(':')&.first&.strip }.include?(key)
  74. end
  75. def node_init_style(node)
  76. if node['style'].blank?
  77. node['style'] = ''
  78. else
  79. node['style'] += ';'
  80. end
  81. end
  82. def node_set_style(node, key, value)
  83. node_init_style(node)
  84. value += 'px' if !value.match?(%r{%|px|em}i)
  85. node['style'] += "#{key}:#{value}"
  86. end
  87. def clear_css_classes(node)
  88. return if !node['class']
  89. classes = node['class'].gsub(%r{\t|\n|\r}, '').split
  90. class_new = ''
  91. classes.each do |local_class|
  92. next if classes_allowlist.exclude?(local_class.to_s.strip)
  93. if class_new != ''
  94. class_new += ' '
  95. end
  96. class_new += local_class
  97. end
  98. if class_new == ''
  99. node.delete('class')
  100. else
  101. node['class'] = class_new
  102. end
  103. end
  104. def remove_unsafe_src(node)
  105. return if !node['src']
  106. src = cleanup_target(CGI.unescape(node['src']))
  107. return if src !~ %r{(javascript|livescript|vbscript):}i && !src.downcase.start_with?('http', 'ftp', '//')
  108. node.remove
  109. true
  110. end
  111. def clear_tags_allowlist(node)
  112. return if tags_allowlist.include?(node.name)
  113. node.before(node.children)
  114. node.remove
  115. true
  116. end
  117. def tags_allowlist
  118. @tags_allowlist ||= Rails.configuration.html_sanitizer_tags_allowlist
  119. end
  120. def attributes_allowlist
  121. @attributes_allowlist ||= Rails.configuration.html_sanitizer_attributes_allowlist
  122. end
  123. def css_properties_allowlist
  124. @css_properties_allowlist ||= Rails.configuration.html_sanitizer_css_properties_allowlist
  125. end
  126. def css_values_blocklist
  127. @css_values_blocklist ||= Rails.application.config.html_sanitizer_css_values_blocklist
  128. end
  129. # We allowlist yahoo_quoted because Yahoo Mail marks quoted email content using
  130. # <div class='yahoo_quoted'> and we rely on this class to identify quoted messages
  131. def classes_allowlist
  132. %w[js-signatureMarker yahoo_quoted]
  133. end
  134. def attributes_2_css
  135. %w[width height]
  136. end
  137. def cleanup_target(string, **options)
  138. cleaned_string = string.utf8_encode(fallback: :read_as_sanitized_binary)
  139. cleaned_string = cleaned_string.gsub(%r{[[:space:]]}, '') if !options[:keep_spaces]
  140. cleaned_string = cleaned_string.strip
  141. .delete("\t\n\r\u0000")
  142. .gsub(%r{/\*.*?\*/}, '')
  143. .gsub(%r{<!--.*?-->}, '')
  144. sanitize_attachment_disposition(cleaned_string)
  145. end
  146. def sanitize_attachment_disposition(url)
  147. @fqdn ||= Setting.get('fqdn')
  148. uri = URI(url)
  149. if uri.host == @fqdn && uri.query.present?
  150. params = CGI.parse(uri.query || '')
  151. .tap { |p| p.merge!('disposition' => 'attachment') if p.include?('disposition') }
  152. uri.query = URI.encode_www_form(params)
  153. end
  154. uri.to_s
  155. rescue
  156. url
  157. end
  158. end
  159. end
  160. end