wipe.rb 5.7 KB

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