PageRenderTime 23ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/rails/actionpack/test/controller/html-scanner/sanitizer_test.rb

https://github.com/henrikhodne/gitscm
Ruby | 259 lines | 211 code | 45 blank | 3 comment | 2 complexity | c1321495b637e2e4bbae57c0272813a0 MD5 | raw file
  1. require 'abstract_unit'
  2. class SanitizerTest < Test::Unit::TestCase
  3. def setup
  4. @sanitizer = nil # used by assert_sanitizer
  5. end
  6. def test_strip_tags
  7. sanitizer = HTML::FullSanitizer.new
  8. assert_equal("<<<bad html", sanitizer.sanitize("<<<bad html"))
  9. assert_equal("<<", sanitizer.sanitize("<<<bad html>"))
  10. assert_equal("Dont touch me", sanitizer.sanitize("Dont touch me"))
  11. assert_equal("This is a test.", sanitizer.sanitize("<p>This <u>is<u> a <a href='test.html'><strong>test</strong></a>.</p>"))
  12. assert_equal("Weirdos", sanitizer.sanitize("Wei<<a>a onclick='alert(document.cookie);'</a>/>rdos"))
  13. assert_equal("This is a test.", sanitizer.sanitize("This is a test."))
  14. assert_equal(
  15. %{This is a test.\n\n\nIt no longer contains any HTML.\n}, sanitizer.sanitize(
  16. %{<title>This is <b>a <a href="" target="_blank">test</a></b>.</title>\n\n<!-- it has a comment -->\n\n<p>It no <b>longer <strong>contains <em>any <strike>HTML</strike></em>.</strong></b></p>\n}))
  17. assert_equal "This has a here.", sanitizer.sanitize("This has a <!-- comment --> here.")
  18. [nil, '', ' '].each { |blank| assert_equal blank, sanitizer.sanitize(blank) }
  19. end
  20. def test_strip_links
  21. sanitizer = HTML::LinkSanitizer.new
  22. assert_equal "Dont touch me", sanitizer.sanitize("Dont touch me")
  23. assert_equal "on my mind\nall day long", sanitizer.sanitize("<a href='almost'>on my mind</a>\n<A href='almost'>all day long</A>")
  24. assert_equal "0wn3d", sanitizer.sanitize("<a href='http://www.rubyonrails.com/'><a href='http://www.rubyonrails.com/' onlclick='steal()'>0wn3d</a></a>")
  25. assert_equal "Magic", sanitizer.sanitize("<a href='http://www.rubyonrails.com/'>Mag<a href='http://www.ruby-lang.org/'>ic")
  26. assert_equal "FrrFox", sanitizer.sanitize("<href onlclick='steal()'>FrrFox</a></href>")
  27. assert_equal "My mind\nall <b>day</b> long", sanitizer.sanitize("<a href='almost'>My mind</a>\n<A href='almost'>all <b>day</b> long</A>")
  28. assert_equal "all <b>day</b> long", sanitizer.sanitize("<<a>a href='hello'>all <b>day</b> long<</A>/a>")
  29. assert_equal "<a<a", sanitizer.sanitize("<a<a")
  30. end
  31. def test_sanitize_form
  32. assert_sanitized "<form action=\"/foo/bar\" method=\"post\"><input></form>", ''
  33. end
  34. def test_sanitize_plaintext
  35. raw = "<plaintext><span>foo</span></plaintext>"
  36. assert_sanitized raw, "<span>foo</span>"
  37. end
  38. def test_sanitize_script
  39. assert_sanitized "a b c<script language=\"Javascript\">blah blah blah</script>d e f", "a b cd e f"
  40. end
  41. # fucked
  42. def test_sanitize_js_handlers
  43. raw = %{onthis="do that" <a href="#" onclick="hello" name="foo" onbogus="remove me">hello</a>}
  44. assert_sanitized raw, %{onthis="do that" <a name="foo" href="#">hello</a>}
  45. end
  46. def test_sanitize_javascript_href
  47. raw = %{href="javascript:bang" <a href="javascript:bang" name="hello">foo</a>, <span href="javascript:bang">bar</span>}
  48. assert_sanitized raw, %{href="javascript:bang" <a name="hello">foo</a>, <span>bar</span>}
  49. end
  50. def test_sanitize_image_src
  51. raw = %{src="javascript:bang" <img src="javascript:bang" width="5">foo</img>, <span src="javascript:bang">bar</span>}
  52. assert_sanitized raw, %{src="javascript:bang" <img width="5">foo</img>, <span>bar</span>}
  53. end
  54. HTML::WhiteListSanitizer.allowed_tags.each do |tag_name|
  55. define_method "test_should_allow_#{tag_name}_tag" do
  56. assert_sanitized "start <#{tag_name} title=\"1\" onclick=\"foo\">foo <bad>bar</bad> baz</#{tag_name}> end", %(start <#{tag_name} title="1">foo bar baz</#{tag_name}> end)
  57. end
  58. end
  59. def test_should_allow_anchors
  60. assert_sanitized %(<a href="foo" onclick="bar"><script>baz</script></a>), %(<a href="foo"></a>)
  61. end
  62. # RFC 3986, sec 4.2
  63. def test_allow_colons_in_path_component
  64. assert_sanitized("<a href=\"./this:that\">foo</a>")
  65. end
  66. %w(src width height alt).each do |img_attr|
  67. define_method "test_should_allow_image_#{img_attr}_attribute" do
  68. assert_sanitized %(<img #{img_attr}="foo" onclick="bar" />), %(<img #{img_attr}="foo" />)
  69. end
  70. end
  71. def test_should_handle_non_html
  72. assert_sanitized 'abc'
  73. end
  74. def test_should_handle_blank_text
  75. assert_sanitized nil
  76. assert_sanitized ''
  77. end
  78. def test_should_allow_custom_tags
  79. text = "<u>foo</u>"
  80. sanitizer = HTML::WhiteListSanitizer.new
  81. assert_equal(text, sanitizer.sanitize(text, :tags => %w(u)))
  82. end
  83. def test_should_allow_only_custom_tags
  84. text = "<u>foo</u> with <i>bar</i>"
  85. sanitizer = HTML::WhiteListSanitizer.new
  86. assert_equal("<u>foo</u> with bar", sanitizer.sanitize(text, :tags => %w(u)))
  87. end
  88. def test_should_allow_custom_tags_with_attributes
  89. text = %(<blockquote cite="http://example.com/">foo</blockquote>)
  90. sanitizer = HTML::WhiteListSanitizer.new
  91. assert_equal(text, sanitizer.sanitize(text))
  92. end
  93. def test_should_allow_custom_tags_with_custom_attributes
  94. text = %(<blockquote foo="bar">Lorem ipsum</blockquote>)
  95. sanitizer = HTML::WhiteListSanitizer.new
  96. assert_equal(text, sanitizer.sanitize(text, :attributes => ['foo']))
  97. end
  98. [%w(img src), %w(a href)].each do |(tag, attr)|
  99. define_method "test_should_strip_#{attr}_attribute_in_#{tag}_with_bad_protocols" do
  100. assert_sanitized %(<#{tag} #{attr}="javascript:bang" title="1">boo</#{tag}>), %(<#{tag} title="1">boo</#{tag}>)
  101. end
  102. end
  103. def test_should_flag_bad_protocols
  104. sanitizer = HTML::WhiteListSanitizer.new
  105. %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|
  106. assert sanitizer.send(:contains_bad_protocols?, 'src', "#{proto}://bad")
  107. end
  108. end
  109. def test_should_accept_good_protocols
  110. sanitizer = HTML::WhiteListSanitizer.new
  111. HTML::WhiteListSanitizer.allowed_protocols.each do |proto|
  112. assert !sanitizer.send(:contains_bad_protocols?, 'src', "#{proto}://good")
  113. end
  114. end
  115. def test_should_reject_hex_codes_in_protocol
  116. assert_sanitized %(<a href="&#37;6A&#37;61&#37;76&#37;61&#37;73&#37;63&#37;72&#37;69&#37;70&#37;74&#37;3A&#37;61&#37;6C&#37;65&#37;72&#37;74&#37;28&#37;22&#37;58&#37;53&#37;53&#37;22&#37;29">1</a>), "<a>1</a>"
  117. 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")
  118. end
  119. def test_should_block_script_tag
  120. assert_sanitized %(<SCRIPT\nSRC=http://ha.ckers.org/xss.js></SCRIPT>), ""
  121. end
  122. [%(<IMG SRC="javascript:alert('XSS');">),
  123. %(<IMG SRC=javascript:alert('XSS')>),
  124. %(<IMG SRC=JaVaScRiPt:alert('XSS')>),
  125. %(<IMG """><SCRIPT>alert("XSS")</SCRIPT>">),
  126. %(<IMG SRC=javascript:alert(&quot;XSS&quot;)>),
  127. %(<IMG SRC=javascript:alert(String.fromCharCode(88,83,83))>),
  128. %(<IMG SRC=&#106;&#97;&#118;&#97;&#115;&#99;&#114;&#105;&#112;&#116;&#58;&#97;&#108;&#101;&#114;&#116;&#40;&#39;&#88;&#83;&#83;&#39;&#41;>),
  129. %(<IMG SRC=&#0000106&#0000097&#0000118&#0000097&#0000115&#0000099&#0000114&#0000105&#0000112&#0000116&#0000058&#0000097&#0000108&#0000101&#0000114&#0000116&#0000040&#0000039&#0000088&#0000083&#0000083&#0000039&#0000041>),
  130. %(<IMG SRC=&#x6A&#x61&#x76&#x61&#x73&#x63&#x72&#x69&#x70&#x74&#x3A&#x61&#x6C&#x65&#x72&#x74&#x28&#x27&#x58&#x53&#x53&#x27&#x29>),
  131. %(<IMG SRC="jav\tascript:alert('XSS');">),
  132. %(<IMG SRC="jav&#x09;ascript:alert('XSS');">),
  133. %(<IMG SRC="jav&#x0A;ascript:alert('XSS');">),
  134. %(<IMG SRC="jav&#x0D;ascript:alert('XSS');">),
  135. %(<IMG SRC=" &#14; javascript:alert('XSS');">),
  136. %(<IMG SRC=`javascript:alert("RSnake says, 'XSS'")`>)].each_with_index do |img_hack, i|
  137. define_method "test_should_not_fall_for_xss_image_hack_#{i+1}" do
  138. assert_sanitized img_hack, "<img>"
  139. end
  140. end
  141. def test_should_sanitize_tag_broken_up_by_null
  142. assert_sanitized %(<SCR\0IPT>alert(\"XSS\")</SCR\0IPT>), "alert(\"XSS\")"
  143. end
  144. def test_should_sanitize_invalid_script_tag
  145. assert_sanitized %(<SCRIPT/XSS SRC="http://ha.ckers.org/xss.js"></SCRIPT>), ""
  146. end
  147. def test_should_sanitize_script_tag_with_multiple_open_brackets
  148. assert_sanitized %(<<SCRIPT>alert("XSS");//<</SCRIPT>), "&lt;"
  149. assert_sanitized %(<iframe src=http://ha.ckers.org/scriptlet.html\n<a), %(&lt;a)
  150. end
  151. def test_should_sanitize_unclosed_script
  152. assert_sanitized %(<SCRIPT SRC=http://ha.ckers.org/xss.js?<B>), "<b>"
  153. end
  154. def test_should_sanitize_half_open_scripts
  155. assert_sanitized %(<IMG SRC="javascript:alert('XSS')"), "<img>"
  156. end
  157. def test_should_not_fall_for_ridiculous_hack
  158. img_hack = %(<IMG\nSRC\n=\n"\nj\na\nv\na\ns\nc\nr\ni\np\nt\n:\na\nl\ne\nr\nt\n(\n'\nX\nS\nS\n'\n)\n"\n>)
  159. assert_sanitized img_hack, "<img>"
  160. end
  161. # fucked
  162. def test_should_sanitize_attributes
  163. assert_sanitized %(<SPAN title="'><script>alert()</script>">blah</SPAN>), %(<span title="'&gt;&lt;script&gt;alert()&lt;/script&gt;">blah</span>)
  164. end
  165. def test_should_sanitize_illegal_style_properties
  166. raw = %(display:block; position:absolute; left:0; top:0; width:100%; height:100%; z-index:1; background-color:black; background-image:url(http://www.ragingplatypus.com/i/cam-full.jpg); background-x:center; background-y:center; background-repeat:repeat;)
  167. expected = %(display: block; width: 100%; height: 100%; background-color: black; background-image: ; background-x: center; background-y: center;)
  168. assert_equal expected, sanitize_css(raw)
  169. end
  170. def test_should_sanitize_with_trailing_space
  171. raw = "display:block; "
  172. expected = "display: block;"
  173. assert_equal expected, sanitize_css(raw)
  174. end
  175. def test_should_sanitize_xul_style_attributes
  176. raw = %(-moz-binding:url('http://ha.ckers.org/xssmoz.xml#xss'))
  177. assert_equal '', sanitize_css(raw)
  178. end
  179. def test_should_sanitize_invalid_tag_names
  180. assert_sanitized(%(a b c<script/XSS src="http://ha.ckers.org/xss.js"></script>d e f), "a b cd e f")
  181. end
  182. def test_should_sanitize_non_alpha_and_non_digit_characters_in_tags
  183. assert_sanitized('<a onclick!#$%&()*~+-_.,:;?@[/|\]^`=alert("XSS")>foo</a>', "<a>foo</a>")
  184. end
  185. def test_should_sanitize_invalid_tag_names_in_single_tags
  186. assert_sanitized('<img/src="http://ha.ckers.org/xss.js"/>', "<img />")
  187. end
  188. def test_should_sanitize_img_dynsrc_lowsrc
  189. assert_sanitized(%(<img lowsrc="javascript:alert('XSS')" />), "<img />")
  190. end
  191. def test_should_sanitize_div_background_image_unicode_encoded
  192. raw = %(background-image:\0075\0072\006C\0028'\006a\0061\0076\0061\0073\0063\0072\0069\0070\0074\003a\0061\006c\0065\0072\0074\0028.1027\0058.1053\0053\0027\0029'\0029)
  193. assert_equal '', sanitize_css(raw)
  194. end
  195. def test_should_sanitize_div_style_expression
  196. raw = %(width: expression(alert('XSS'));)
  197. assert_equal '', sanitize_css(raw)
  198. end
  199. def test_should_sanitize_img_vbscript
  200. assert_sanitized %(<img src='vbscript:msgbox("XSS")' />), '<img />'
  201. end
  202. protected
  203. def assert_sanitized(input, expected = nil)
  204. @sanitizer ||= HTML::WhiteListSanitizer.new
  205. if input
  206. assert_dom_equal expected || input, @sanitizer.sanitize(input)
  207. else
  208. assert_nil @sanitizer.sanitize(input)
  209. end
  210. end
  211. def sanitize_css(input)
  212. (@sanitizer ||= HTML::WhiteListSanitizer.new).sanitize_css(input)
  213. end
  214. end