Class: Govspeak::HtmlSanitizer::ImageSourceWhitelister

Inherits:
Object
  • Object
show all
Defined in:
lib/govspeak/html_sanitizer.rb

Instance Method Summary collapse

Constructor Details

#initialize(allowed_image_hosts) ⇒ ImageSourceWhitelister

Returns a new instance of ImageSourceWhitelister.



5
6
7
# File 'lib/govspeak/html_sanitizer.rb', line 5

def initialize(allowed_image_hosts)
  @allowed_image_hosts = allowed_image_hosts
end

Instance Method Details

#call(sanitize_context) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/govspeak/html_sanitizer.rb', line 9

def call(sanitize_context)
  return unless sanitize_context[:node_name] == "img"

  node = sanitize_context[:node]
  image_uri = Addressable::URI.parse(node["src"])
  unless image_uri.relative? || @allowed_image_hosts.include?(image_uri.host)
    node.unlink # the node isn't sanitary. Remove it from the document.
  end
end