This is a test.
")) assert_equal("Weirdos", sanitizer.sanitize("Wei<a onclick='alert(document.cookie);'/>rdos")) assert_equal("This is a test.", sanitizer.sanitize("This is a test.")) assert_equal( %{This is a test.\n\n\nIt no longer contains any HTML.\n}, sanitizer.sanitize( %{It no longer contains any HTML.
foo) sanitizer = HTML::WhiteListSanitizer.new assert_equal(text, sanitizer.sanitize(text)) end def test_should_allow_custom_tags_with_custom_attributes text = %(
Lorem ipsum) sanitizer = HTML::WhiteListSanitizer.new assert_equal(text, sanitizer.sanitize(text, :attributes => ['foo'])) end def test_should_raise_argument_error_if_tags_is_not_enumerable sanitizer = HTML::WhiteListSanitizer.new e = assert_raise(ArgumentError) do sanitizer.sanitize('', :tags => 'foo') end assert_equal "You should pass :tags as an Enumerable", e.message end def test_should_raise_argument_error_if_attributes_is_not_enumerable sanitizer = HTML::WhiteListSanitizer.new e = assert_raise(ArgumentError) do sanitizer.sanitize('', :attributes => 'foo') end assert_equal "You should pass :attributes as an Enumerable", e.message end [%w(img src), %w(a href)].each do |(tag, attr)| define_method "test_should_strip_#{attr}_attribute_in_#{tag}_with_bad_protocols" do assert_sanitized %(<#{tag} #{attr}="javascript:bang" title="1">boo#{tag}>), %(<#{tag} title="1">boo#{tag}>) end end def test_should_flag_bad_protocols sanitizer = HTML::WhiteListSanitizer.new %w(about chrome data disk hcp help javascript livescript lynxcgi lynxexec ms-help ms-its mhtml mocha opera res resource shell vbscript view-source vnd.ms.radio wysiwyg).each do |proto| assert sanitizer.send(:contains_bad_protocols?, 'src', "#{proto}://bad") end end def test_should_accept_good_protocols_ignoring_case sanitizer = HTML::WhiteListSanitizer.new HTML::WhiteListSanitizer.allowed_protocols.each do |proto| assert !sanitizer.send(:contains_bad_protocols?, 'src', "#{proto.capitalize}://good") end end def test_should_accept_good_protocols_ignoring_space sanitizer = HTML::WhiteListSanitizer.new HTML::WhiteListSanitizer.allowed_protocols.each do |proto| assert !sanitizer.send(:contains_bad_protocols?, 'src', " #{proto}://good") end end def test_should_accept_good_protocols sanitizer = HTML::WhiteListSanitizer.new HTML::WhiteListSanitizer.allowed_protocols.each do |proto| assert !sanitizer.send(:contains_bad_protocols?, 'src', "#{proto}://good") end end def test_should_reject_hex_codes_in_protocol assert_sanitized %(1), "1" assert @sanitizer.send(:contains_bad_protocols?, 'src', "%6A%61%76%61%73%63%72%69%70%74%3A%61%6C%65%72%74%28%22%58%53%53%22%29") end def test_should_block_script_tag assert_sanitized %(), "" end [%(), %(), %(), %(">), %(), %(), %(), %(), %(), %(), %(), %(), %(), %(), %(), %()].each_with_index do |img_hack, i| define_method "test_should_not_fall_for_xss_image_hack_#{i+1}" do assert_sanitized img_hack, "" end end def test_should_sanitize_tag_broken_up_by_null assert_sanitized %(