wipe.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. # Copyright (C) 2012-2023 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|
  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. node['style'].present? && node['style']&.include?("#{key}:")
  69. end
  70. def node_init_style(node)
  71. if node['style'].blank?
  72. node['style'] = ''
  73. else
  74. node['style'] += ';'
  75. end
  76. end
  77. def node_set_style(node, key, value)
  78. node_init_style(node)
  79. value += 'px' if !value.match?(%r{%|px|em}i)
  80. node['style'] += "#{key}:#{value}"
  81. end
  82. def clear_css_classes(node)
  83. return if !node['class']
  84. classes = node['class'].gsub(%r{\t|\n|\r}, '').split
  85. class_new = ''
  86. classes.each do |local_class|
  87. next if classes_allowlist.exclude?(local_class.to_s.strip)
  88. if class_new != ''
  89. class_new += ' '
  90. end
  91. class_new += local_class
  92. end
  93. if class_new == ''
  94. node.delete('class')
  95. else
  96. node['class'] = class_new
  97. end
  98. end
  99. def remove_unsafe_src(node)
  100. return if !node['src']
  101. src = cleanup_target(CGI.unescape(node['src']))
  102. return if src !~ %r{(javascript|livescript|vbscript):}i && !src.downcase.start_with?('http', 'ftp', '//')
  103. node.remove
  104. true
  105. end
  106. def clear_tags_allowlist(node)
  107. return if tags_allowlist.include?(node.name)
  108. node.replace node.children.to_s
  109. true
  110. end
  111. def tags_allowlist
  112. @tags_allowlist ||= Rails.configuration.html_sanitizer_tags_allowlist
  113. end
  114. def attributes_allowlist
  115. @attributes_allowlist ||= Rails.configuration.html_sanitizer_attributes_allowlist
  116. end
  117. def css_properties_allowlist
  118. @css_properties_allowlist ||= Rails.configuration.html_sanitizer_css_properties_allowlist
  119. end
  120. def css_values_blocklist
  121. @css_values_blocklist ||= Rails.application.config.html_sanitizer_css_values_blocklist
  122. end
  123. # We allowlist yahoo_quoted because Yahoo Mail marks quoted email content using
  124. # <div class='yahoo_quoted'> and we rely on this class to identify quoted messages
  125. def classes_allowlist
  126. %w[js-signatureMarker yahoo_quoted]
  127. end
  128. def attributes_2_css
  129. %w[width height]
  130. end
  131. def cleanup_target(string, **options)
  132. cleaned_string = string.utf8_encode(fallback: :read_as_sanitized_binary)
  133. cleaned_string = cleaned_string.gsub(%r{[[:space:]]}, '') if !options[:keep_spaces]
  134. cleaned_string = cleaned_string.strip
  135. .delete("\t\n\r\u0000")
  136. .gsub(%r{/\*.*?\*/}, '')
  137. .gsub(%r{<!--.*?-->}, '')
  138. sanitize_attachment_disposition(cleaned_string)
  139. end
  140. def sanitize_attachment_disposition(url)
  141. @fqdn ||= Setting.get('fqdn')
  142. uri = URI(url)
  143. if uri.host == @fqdn && uri.query.present?
  144. params = CGI.parse(uri.query || '')
  145. .tap { |p| p.merge!('disposition' => 'attachment') if p.include?('disposition') }
  146. uri.query = URI.encode_www_form(params)
  147. end
  148. uri.to_s
  149. rescue
  150. url
  151. end
  152. end
  153. end
  154. end