/spec/whitewash_spec.rb

https://github.com/bejbus/whitewash · Ruby · 99 lines · 84 code · 14 blank · 1 comment · 14 complexity · 050665c0f0fca247d7cf2dd9571bef32 MD5 · raw file

  1. require File.expand_path('spec/spec_helper')
  2. describe Whitewash do
  3. it "loads default whitelist" do
  4. whitelist = Whitewash.default_whitelist
  5. whitelist.should be_a_kind_of Hash
  6. whitelist.should include '_css'
  7. end
  8. it "drops <html> and <body> elements" do
  9. w = Whitewash.new
  10. input = '<html><head></head><body><p>test</p></body>'
  11. output = w.sanitize(input)
  12. output.should == '<p>test</p>'
  13. end
  14. it "understands fragments with multiple root elements" do
  15. w = Whitewash.new
  16. input = '<p>foo</p><p>bar</p>'
  17. output = w.sanitize(input)
  18. output.should == '<p>foo</p><p>bar</p>'
  19. end
  20. it "removes <script/> element" do
  21. w = Whitewash.new
  22. input = '<p>foo <script type="text/javascript" src="test.js">bar</script> buzz</p>'
  23. output = w.sanitize(input)
  24. output.should == '<p>foo <![CDATA[bar]]> buzz</p>'
  25. end
  26. it "removes onclick attribute" do
  27. w = Whitewash.new
  28. input = '<p>foo <span onlick="test()">bar</span> buzz</p>'
  29. output = w.sanitize(input)
  30. output.should == '<p>foo <span>bar</span> buzz</p>'
  31. end
  32. it "removes background CSS property" do
  33. w = Whitewash.new
  34. input = '<p>foo <span style="background: url(//test/t.js)">bar</span> buzz</p>'
  35. output = w.sanitize(input)
  36. output.should == '<p>foo <span>bar</span> buzz</p>'
  37. end
  38. it "rewrites HTML when supplied with a block" do
  39. w = Whitewash.new
  40. input = '<p>foo <img src="in.jpg"/> buzz</p>'
  41. output = w.sanitize(input) do |xml|
  42. if xml.name == 'img'
  43. xml['src'] = 'out.jpg'
  44. end
  45. end
  46. output.should == '<p>foo <img src="out.jpg" /> buzz</p>'
  47. end
  48. it "fixes up invalid markup" do
  49. w = Whitewash.new
  50. input = '<p>foo <strong><em>bar</strong></em> buzz</p>'
  51. output = w.sanitize(input)
  52. output.should == '<p>foo <strong><em>bar</em></strong> buzz</p>'
  53. end
  54. # http://ha.ckers.org/xss.html
  55. it "catches javascript: in img/src" do
  56. w = Whitewash.new
  57. input = %q{<IMG SRC=JaVaScRiPt:alert('XSS')>}
  58. output = w.sanitize(input)
  59. output.should == %q{<img />}
  60. end
  61. it "handles strings with null in the middle" do
  62. w = Whitewash.new
  63. input = %q{<IMG SRC=java\0script:alert("XSS")>}
  64. output = w.sanitize(input)
  65. output.should == %q{<img />}
  66. end
  67. it "handles extra open brackets" do
  68. w = Whitewash.new
  69. input = %q{<<SCRIPT>alert("XSS");//<</SCRIPT>}
  70. output = w.sanitize(input)
  71. output.should == '<p>alert("XSS");//</p>'
  72. end
  73. it "removes remote stylesheet link" do
  74. w = Whitewash.new
  75. input = %q{<P><STYLE>@import'http://ha.ckers.org/xss.css';</STYLE></P>}
  76. output = w.sanitize(input)
  77. output.should == '<p></p>'
  78. end
  79. it "removes XML data island with CDATA obfuscation" do
  80. w = Whitewash.new
  81. input = %{<XML ID=I><X><C><![CDATA[<IMG SRC="javas]]><![CDATA[cript:alert('XSS');">]]> </C></X></xml><SPAN DATASRC=#I DATAFLD=C DATAFORMATAS=HTML></SPAN>}
  82. output = w.sanitize(input)
  83. output.should == ']]&gt; <span></span>'
  84. end
  85. end