wipe.rb 5.2 KB

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