strict.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class HtmlSanitizer
  3. class Strict < Base
  4. def initialize(no_images: false)
  5. super()
  6. @no_images = no_images
  7. end
  8. def sanitize(string, external: false, timeout: true)
  9. return run_sanitization(string, external) if !timeout
  10. with_timeout(string) do
  11. run_sanitization(string, external)
  12. end
  13. end
  14. private
  15. def run_sanitization(string, external)
  16. fragment = Loofah
  17. .fragment(string)
  18. .scrub!(HtmlSanitizer::Scrubber::TagRemove.new)
  19. .scrub!(HtmlSanitizer::Scrubber::QuoteContent.new)
  20. if @no_images
  21. fragment.scrub! HtmlSanitizer::Scrubber::TagRemove.new(tags: %w[img])
  22. end
  23. wipe_scrubber = HtmlSanitizer::Scrubber::Wipe.new
  24. string = loop_string(fragment.to_html, wipe_scrubber)
  25. link_scrubber = HtmlSanitizer::Scrubber::Link.new(web_app_url_prefix: web_app_url_prefix, external: external)
  26. Loofah.fragment(string).scrub!(link_scrubber).to_html
  27. end
  28. def web_app_url_prefix
  29. fqdn = Setting.get('fqdn')
  30. http_type = Setting.get('http_type')
  31. "#{http_type}://#{fqdn}/#".downcase
  32. end
  33. end
  34. end