require "test_helper" class HtmlSanitizerTest < Minitest::Test test "disallow a script tag" do html = "" assert_equal "", Govspeak::HtmlSanitizer.new(html).sanitize end test "disallow a javascript protocol in an attribute" do html = 'an example' assert_equal "an example", Govspeak::HtmlSanitizer.new(html).sanitize end test "disallow on* attributes" do html = %q{Link} assert_equal "Link", Govspeak::HtmlSanitizer.new(html).sanitize end test "allow non-JS HTML content" do html = "" assert_equal "", Govspeak::HtmlSanitizer.new(html).sanitize end test "keep things that should be HTML entities" do html = "Fortnum & Mason" assert_equal "Fortnum & Mason", Govspeak::HtmlSanitizer.new(html).sanitize end test "allow govspeak button markup" do html = "" assert_equal( "", Govspeak::HtmlSanitizer.new(html).sanitize, ) end test "allow data attributes on links" do html = "Test Link" assert_equal( "Test Link", Govspeak::HtmlSanitizer.new(html).sanitize, ) end test "allows images on whitelisted domains" do html = "" sanitized_html = Govspeak::HtmlSanitizer.new(html, allowed_image_hosts: ["allowed.com"]).sanitize assert_equal "", sanitized_html end test "removes images not on whitelisted domains" do html = "" assert_equal "", Govspeak::HtmlSanitizer.new(html, allowed_image_hosts: ["allowed.com"]).sanitize end test "allows table cells and table headings without a style attribute" do html = "
thing
thing
" assert_equal html, Govspeak::HtmlSanitizer.new(html).sanitize end test "strips table cells and headings that appear outside a table" do html = "thingthing" assert_equal "thingthing", Govspeak::HtmlSanitizer.new(html).sanitize end test "normalizes table tags to inject missing rows and bodies like a browser does" do html = "
thingthing
" assert_equal "
thingthing
", Govspeak::HtmlSanitizer.new(html).sanitize end test "allows valid text-align properties on the style attribute for table cells and table headings" do %w[left right center].each do |alignment| html = "
thing
thing
" assert_equal html, Govspeak::HtmlSanitizer.new(html).sanitize end [ "width: 10000px", "text-align: middle", "text-align: left; width: 10px", "background-image: url(javascript:alert('XSS'))", "expression(alert('XSS'));", ].each do |style| html = "
thing
thing
" assert_equal "
thing
thing
", Govspeak::HtmlSanitizer.new(html).sanitize end end end