You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sanitization_filter.rb 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. module Redmine
  19. module WikiFormatting
  20. module CommonMark
  21. # sanitizes rendered HTML using the Sanitize gem
  22. class SanitizationFilter < HTML::Pipeline::SanitizationFilter
  23. include Redmine::Helpers::URL
  24. RELAXED_PROTOCOL_ATTRS = {
  25. "a" => %w(href).freeze,
  26. }.freeze
  27. ALLOWED_CSS_PROPERTIES = %w[
  28. color background-color
  29. width min-width max-width
  30. height min-height max-height
  31. padding padding-left padding-right padding-top padding-bottom
  32. margin margin-left margin-right margin-top margin-bottom
  33. border border-left border-right border-top border-bottom border-radius border-style border-collapse border-spacing
  34. font font-style font-variant font-weight font-stretch font-size line-height font-family
  35. text-align
  36. float
  37. ].freeze
  38. def allowlist
  39. @allowlist ||= customize_allowlist(super.deep_dup)
  40. end
  41. private
  42. # customizes the allowlist defined in
  43. # https://github.com/jch/html-pipeline/blob/master/lib/html/pipeline/sanitization_filter.rb
  44. def customize_allowlist(allowlist)
  45. # Disallow `name` attribute globally, allow on `a`
  46. allowlist[:attributes][:all].delete("name")
  47. allowlist[:attributes]["a"].push("name")
  48. allowlist[:attributes][:all].push("style")
  49. allowlist[:css] = { properties: ALLOWED_CSS_PROPERTIES }
  50. # allow class on code tags (this holds the language info from fenced
  51. # code bocks and has the format language-foo)
  52. allowlist[:attributes]["code"] = %w(class)
  53. allowlist[:transformers].push lambda{|env|
  54. node = env[:node]
  55. return unless node.name == "code"
  56. return unless node.has_attribute?("class")
  57. unless /\Alanguage-(\S+)\z/.match?(node["class"])
  58. node.remove_attribute("class")
  59. end
  60. }
  61. # Allow table cell alignment by style attribute
  62. #
  63. # Only necessary if we used the TABLE_PREFER_STYLE_ATTRIBUTES
  64. # commonmarker option (which we do not, currently).
  65. # By default, the align attribute is used (which is allowed on all
  66. # elements).
  67. # allowlist[:attributes]["th"] = %w(style)
  68. # allowlist[:attributes]["td"] = %w(style)
  69. # allowlist[:css] = { properties: ["text-align"] }
  70. # Allow `id` in a and li elements for footnotes
  71. # and remove any `id` properties not matching for footnotes
  72. allowlist[:attributes]["a"].push "id"
  73. allowlist[:attributes]["li"] = %w(id)
  74. allowlist[:transformers].push lambda{|env|
  75. node = env[:node]
  76. return unless node.name == "a" || node.name == "li"
  77. return unless node.has_attribute?("id")
  78. return if node.name == "a" && node["id"] =~ /\Afnref-\d+\z/
  79. return if node.name == "li" && node["id"] =~ /\Afn-\d+\z/
  80. node.remove_attribute("id")
  81. }
  82. # https://github.com/rgrove/sanitize/issues/209
  83. allowlist[:protocols].delete("a")
  84. allowlist[:transformers].push lambda{|env|
  85. node = env[:node]
  86. return if node.type != Nokogiri::XML::Node::ELEMENT_NODE
  87. name = env[:node_name]
  88. return unless RELAXED_PROTOCOL_ATTRS.include?(name)
  89. RELAXED_PROTOCOL_ATTRS[name].each do |attr|
  90. next unless node.has_attribute?(attr)
  91. node[attr] = node[attr].strip
  92. unless !node[attr].empty? && uri_with_link_safe_scheme?(node[attr])
  93. node.remove_attribute(attr)
  94. end
  95. end
  96. }
  97. allowlist
  98. end
  99. end
  100. end
  101. end
  102. end