romanbsd-tarantula 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (126) hide show
  1. data/CHANGELOG +47 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +162 -0
  4. data/Rakefile +69 -0
  5. data/VERSION.yml +4 -0
  6. data/examples/example_helper.rb +48 -0
  7. data/examples/relevance/core_extensions/ellipsize_example.rb +19 -0
  8. data/examples/relevance/core_extensions/file_example.rb +8 -0
  9. data/examples/relevance/core_extensions/response_example.rb +29 -0
  10. data/examples/relevance/core_extensions/test_case_example.rb +20 -0
  11. data/examples/relevance/tarantula/attack_form_submission_example.rb +79 -0
  12. data/examples/relevance/tarantula/attack_handler_example.rb +29 -0
  13. data/examples/relevance/tarantula/crawler_example.rb +386 -0
  14. data/examples/relevance/tarantula/form_example.rb +50 -0
  15. data/examples/relevance/tarantula/form_submission_example.rb +71 -0
  16. data/examples/relevance/tarantula/html_document_handler_example.rb +43 -0
  17. data/examples/relevance/tarantula/html_report_helper_example.rb +46 -0
  18. data/examples/relevance/tarantula/html_reporter_example.rb +82 -0
  19. data/examples/relevance/tarantula/invalid_html_handler_example.rb +33 -0
  20. data/examples/relevance/tarantula/io_reporter_example.rb +11 -0
  21. data/examples/relevance/tarantula/link_example.rb +67 -0
  22. data/examples/relevance/tarantula/log_grabber_example.rb +26 -0
  23. data/examples/relevance/tarantula/rails_integration_proxy_example.rb +88 -0
  24. data/examples/relevance/tarantula/result_example.rb +85 -0
  25. data/examples/relevance/tarantula/tidy_handler_example.rb +58 -0
  26. data/examples/relevance/tarantula/transform_example.rb +20 -0
  27. data/examples/relevance/tarantula/w3c_validator_example.rb +71 -0
  28. data/examples/relevance/tarantula_example.rb +23 -0
  29. data/laf/images/button_active.png +0 -0
  30. data/laf/images/button_hover.png +0 -0
  31. data/laf/images/button_inactive.png +0 -0
  32. data/laf/images/header_bg.jpg +0 -0
  33. data/laf/images/logo.png +0 -0
  34. data/laf/images/tagline.png +0 -0
  35. data/laf/javascripts/jquery-1.2.3.js +3408 -0
  36. data/laf/javascripts/jquery-ui-tabs.js +890 -0
  37. data/laf/javascripts/jquery.tablesorter.js +861 -0
  38. data/laf/javascripts/niftyLayout.js +11 -0
  39. data/laf/javascripts/niftycube-details.js +298 -0
  40. data/laf/javascripts/niftycube.js +298 -0
  41. data/laf/javascripts/tarantula.js +10 -0
  42. data/laf/stylesheets/tarantula.css +345 -0
  43. data/laf/v2/detail.html +59 -0
  44. data/laf/v2/images/button_active.png +0 -0
  45. data/laf/v2/images/button_hover.png +0 -0
  46. data/laf/v2/images/button_inactive.png +0 -0
  47. data/laf/v2/images/header_bg.jpg +0 -0
  48. data/laf/v2/images/logo.png +0 -0
  49. data/laf/v2/images/tagline.png +0 -0
  50. data/laf/v2/index.html +77 -0
  51. data/laf/v2/stylesheets/tarantula.v2.css +324 -0
  52. data/lib/relevance/core_extensions/ellipsize.rb +34 -0
  53. data/lib/relevance/core_extensions/file.rb +9 -0
  54. data/lib/relevance/core_extensions/metaclass.rb +78 -0
  55. data/lib/relevance/core_extensions/response.rb +9 -0
  56. data/lib/relevance/core_extensions/string_chars_fix.rb +11 -0
  57. data/lib/relevance/core_extensions/test_case.rb +19 -0
  58. data/lib/relevance/tarantula/attack.rb +15 -0
  59. data/lib/relevance/tarantula/attack_form_submission.rb +75 -0
  60. data/lib/relevance/tarantula/attack_handler.rb +37 -0
  61. data/lib/relevance/tarantula/crawler.rb +264 -0
  62. data/lib/relevance/tarantula/detail.html.erb +82 -0
  63. data/lib/relevance/tarantula/form.rb +21 -0
  64. data/lib/relevance/tarantula/form_submission.rb +70 -0
  65. data/lib/relevance/tarantula/html_document_handler.rb +36 -0
  66. data/lib/relevance/tarantula/html_report_helper.rb +39 -0
  67. data/lib/relevance/tarantula/html_reporter.rb +105 -0
  68. data/lib/relevance/tarantula/index.html.erb +37 -0
  69. data/lib/relevance/tarantula/invalid_html_handler.rb +18 -0
  70. data/lib/relevance/tarantula/io_reporter.rb +34 -0
  71. data/lib/relevance/tarantula/link.rb +56 -0
  72. data/lib/relevance/tarantula/log_grabber.rb +16 -0
  73. data/lib/relevance/tarantula/rails_integration_proxy.rb +68 -0
  74. data/lib/relevance/tarantula/recording.rb +12 -0
  75. data/lib/relevance/tarantula/response.rb +13 -0
  76. data/lib/relevance/tarantula/result.rb +66 -0
  77. data/lib/relevance/tarantula/test_report.html.erb +32 -0
  78. data/lib/relevance/tarantula/tidy_handler.rb +32 -0
  79. data/lib/relevance/tarantula/transform.rb +17 -0
  80. data/lib/relevance/tarantula/w3c_validator.rb +33 -0
  81. data/lib/relevance/tarantula.rb +59 -0
  82. data/tasks/tarantula_tasks.rake +36 -0
  83. data/template/tarantula_test.rb +22 -0
  84. data/vendor/w3c_validators/CHANGELOG +14 -0
  85. data/vendor/w3c_validators/LICENSE +60 -0
  86. data/vendor/w3c_validators/README +120 -0
  87. data/vendor/w3c_validators/README.svn +4 -0
  88. data/vendor/w3c_validators/lib/w3c_validators/constants.rb +80 -0
  89. data/vendor/w3c_validators/lib/w3c_validators/css_validator.rb +149 -0
  90. data/vendor/w3c_validators/lib/w3c_validators/exceptions.rb +4 -0
  91. data/vendor/w3c_validators/lib/w3c_validators/feed_validator.rb +110 -0
  92. data/vendor/w3c_validators/lib/w3c_validators/markup_validator.rb +227 -0
  93. data/vendor/w3c_validators/lib/w3c_validators/message.rb +82 -0
  94. data/vendor/w3c_validators/lib/w3c_validators/results.rb +62 -0
  95. data/vendor/w3c_validators/lib/w3c_validators/validator.rb +157 -0
  96. data/vendor/w3c_validators/lib/w3c_validators.rb +5 -0
  97. data/vendor/w3c_validators/rakefile.rb +53 -0
  98. data/vendor/w3c_validators/test/fixtures/invalid_css.css +2 -0
  99. data/vendor/w3c_validators/test/fixtures/invalid_encoding.html +10 -0
  100. data/vendor/w3c_validators/test/fixtures/invalid_feed.xml +19 -0
  101. data/vendor/w3c_validators/test/fixtures/invalid_html5.html +16 -0
  102. data/vendor/w3c_validators/test/fixtures/invalid_markup.html +11 -0
  103. data/vendor/w3c_validators/test/fixtures/valid_css.css +2 -0
  104. data/vendor/w3c_validators/test/fixtures/valid_feed.xml +20 -0
  105. data/vendor/w3c_validators/test/fixtures/valid_html5.html +16 -0
  106. data/vendor/w3c_validators/test/fixtures/valid_markup.html +11 -0
  107. data/vendor/w3c_validators/test/test_css_validator.rb +51 -0
  108. data/vendor/w3c_validators/test/test_exceptions.rb +35 -0
  109. data/vendor/w3c_validators/test/test_feed_validator.rb +61 -0
  110. data/vendor/w3c_validators/test/test_helper.rb +6 -0
  111. data/vendor/w3c_validators/test/test_html5_validator.rb +64 -0
  112. data/vendor/w3c_validators/test/test_markup_validator.rb +94 -0
  113. data/vendor/xss-shield/MIT-LICENSE +20 -0
  114. data/vendor/xss-shield/README +76 -0
  115. data/vendor/xss-shield/init.rb +16 -0
  116. data/vendor/xss-shield/lib/xss_shield/erb_hacks.rb +111 -0
  117. data/vendor/xss-shield/lib/xss_shield/haml_hacks.rb +42 -0
  118. data/vendor/xss-shield/lib/xss_shield/safe_string.rb +47 -0
  119. data/vendor/xss-shield/lib/xss_shield/secure_helpers.rb +40 -0
  120. data/vendor/xss-shield/lib/xss_shield.rb +6 -0
  121. data/vendor/xss-shield/test/test_actionview_integration.rb +40 -0
  122. data/vendor/xss-shield/test/test_erb.rb +44 -0
  123. data/vendor/xss-shield/test/test_haml.rb +43 -0
  124. data/vendor/xss-shield/test/test_helpers.rb +25 -0
  125. data/vendor/xss-shield/test/test_safe_string.rb +55 -0
  126. metadata +218 -0
@@ -0,0 +1,64 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ # Test cases for the HTML5Validator.
4
+ class HTML5ValidatorTests < Test::Unit::TestCase
5
+ include W3CValidators
6
+ def setup
7
+ @v = NuValidator.new
8
+ sleep 1
9
+ end
10
+
11
+ def test_getting_request_data
12
+ r = @v.validate_uri('http://code.dunae.ca/w3c_validators/test/valid_html5.html')
13
+ assert_equal :html5, r.doctype
14
+ assert_equal 'http://code.dunae.ca/w3c_validators/test/valid_html5.html', r.uri
15
+ assert_equal 0, r.errors.length
16
+ assert_equal 0, r.warnings.length
17
+ assert r.is_valid?
18
+ end
19
+
20
+ def test_validating_uri
21
+ r = @v.validate_uri('http://code.dunae.ca/w3c_validators/test/invalid_html5.html')
22
+ assert_equal 1, r.errors.length
23
+ assert_equal 1, r.warnings.length
24
+ assert !r.is_valid?
25
+ end
26
+
27
+ def test_validating_file
28
+ file = File.dirname(__FILE__) + '/fixtures/invalid_html5.html'
29
+ r = @v.validate_file(file)
30
+ assert_equal 1, r.errors.length
31
+ end
32
+
33
+ def test_validating_text
34
+ valid_fragment = <<-EOV
35
+ <!DOCTYPE html>
36
+ <html lang="en-ca">
37
+ <head>
38
+ <title>HTML 5 Example</title>
39
+ </head>
40
+ <body>
41
+ <!-- should have one error (missing </section>) -->
42
+ <p>This is a sample HTML 5 document.</p>
43
+ <section>
44
+ <h1>Example of paragraphs</h1>
45
+ This is the <em>first</em> paragraph in this example.
46
+ <p>This is the second.</p>
47
+ <p>Test<br>test</p>
48
+ </body>
49
+ </html>
50
+ EOV
51
+
52
+ r = @v.validate_text(valid_fragment)
53
+ assert_equal 1, r.errors.length
54
+ end
55
+
56
+ #def test_validating_text_via_file
57
+ # fh = File.new(File.dirname(__FILE__) + '/fixtures/invalid_html5.html', 'r+')
58
+ # r = @v.validate_file(fh)
59
+ # fh.close
60
+ # assert_equal 1, r.errors.length
61
+ #end
62
+
63
+
64
+ end
@@ -0,0 +1,94 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ # Test cases for the MarkupValidator.
4
+ class MarkupValidatorTests < Test::Unit::TestCase
5
+ include W3CValidators
6
+ def setup
7
+ @v = MarkupValidator.new
8
+ sleep 1
9
+ end
10
+
11
+ def test_overriding_doctype
12
+ @v.set_doctype!(:html32, false)
13
+ r = @v.validate_uri('http://code.dunae.ca/w3c_validators/test/invalid_markup.html')
14
+ assert_equal '-//W3C//DTD HTML 3.2 Final//EN', r.doctype
15
+ end
16
+
17
+ def test_overriding_doctype_for_fallback_only
18
+ @v.set_doctype!(:html32, true)
19
+ r = @v.validate_uri('http://code.dunae.ca/w3c_validators/test/invalid_markup.html')
20
+ assert_not_equal '-//W3C//DTD HTML 3.2 Final//EN', r.doctype
21
+ end
22
+
23
+ def test_overriding_charset
24
+ @v.set_charset!(:utf_16, false)
25
+ r = @v.validate_uri('http://code.dunae.ca/w3c_validators/test/invalid_markup.html')
26
+ assert_equal 'utf-16', r.charset
27
+ end
28
+
29
+ def test_overriding_charset_for_fallback_only
30
+ @v.set_doctype!(:utf_16, true)
31
+ r = @v.validate_uri('http://code.dunae.ca/w3c_validators/test/invalid_markup.html')
32
+ assert_not_equal 'utf-16', r.charset
33
+ end
34
+
35
+ def test_validating_uri_with_head_request
36
+ r = @v.validate_uri_quickly('http://code.dunae.ca/w3c_validators/test/invalid_markup.html')
37
+ assert_equal 1, r.errors.length
38
+ assert_equal 0, r.warnings.length
39
+ end
40
+
41
+ def test_validating_uri_with_soap
42
+ r = @v.validate_uri('http://code.dunae.ca/w3c_validators/test/invalid_markup.html')
43
+ assert_equal 1, r.errors.length
44
+ assert_equal 0, r.warnings.length
45
+ end
46
+
47
+ def test_debugging_uri
48
+ @v.set_debug!
49
+ r = @v.validate_uri('http://code.dunae.ca/w3c_validators/test/invalid_markup.html')
50
+ assert r.debug_messages.length > 0
51
+ end
52
+
53
+ def test_validating_file
54
+ file = File.dirname(__FILE__) + '/fixtures/invalid_markup.html'
55
+ r = @v.validate_file(file)
56
+ assert_equal 1, r.errors.length
57
+
58
+ assert r.uri =~ /fixtures\/invalid_markup\.html$/
59
+ end
60
+
61
+ def test_validating_text
62
+ valid_fragment = <<-EOV
63
+ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
64
+ <title>Test</title>
65
+ <body>
66
+ <div class="example">This is a test</div>
67
+ </body>
68
+ EOV
69
+
70
+ r = @v.validate_text(valid_fragment)
71
+ assert_equal 0, r.errors.length
72
+ assert_equal 0, r.warnings.length
73
+ end
74
+
75
+ def test_validating_text_via_file
76
+ fh = File.new(File.dirname(__FILE__) + '/fixtures/invalid_markup.html', 'r+')
77
+ r = @v.validate_file(fh)
78
+ fh.close
79
+ assert_equal 1, r.errors.length
80
+ end
81
+
82
+
83
+ def test_validator_abort
84
+ @v.set_debug!
85
+ assert_nothing_raised do
86
+ r = @v.validate_uri('http://code.dunae.ca/w3c_validators/test/invalid_encoding.html')
87
+ assert !r.is_valid?
88
+ assert_equal 1, r.errors.length
89
+ assert_equal [], r.warnings
90
+ end
91
+ end
92
+
93
+
94
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Trampoline Systems
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,76 @@
1
+ FIXME: THIS README IS NOT UP-TO-DATE.
2
+
3
+ This plugin provides XSS protection for views coded in HAML and RHTML.
4
+
5
+ ERB templates are sometimes used for HTML, and sometimes for
6
+ other kinds of languages (SQL, email templates, YAML etc.).
7
+ XSS Shield protects only those templates with .rhtml extension,
8
+ leaving templates with .erb extension unprotected.
9
+
10
+ === Quick start ===
11
+
12
+ Assuming you're using HAML for all your templates.
13
+
14
+ * Install plugin.
15
+ * Edit all your layout files and change:
16
+ = @content_for_layout
17
+ = yield(:foo) # Foo being usually :js or :css
18
+ to:
19
+ = @content_for_layout.mark_as_xss_protected
20
+ = yield(:foo).mark_as_xss_protected
21
+ * By this point your application should be runnanble,
22
+ but might need some tweaking here and there to avoid potential
23
+ double-escaping.
24
+
25
+ === How it works ===
26
+
27
+ It works by subclassing String into SafeString.
28
+ When HAML engine seems a "= foo" fragment it check if result of executing "foo"
29
+ is a SafeString. If it is - it copies it to the output, if it's anything else
30
+ (String, Integer, nil and so on) it HTML-escapes it first.
31
+
32
+ To avoid double-escaping output of h is a SafeString, as is everything you
33
+ mark as XSS-protected.
34
+ = h(@foo)
35
+ = @foo # fully equivalent to h(@foo)
36
+ = "X <br /> Y".mark_as_xss_protected
37
+
38
+ It would be cumbersome to require mark_as_xss_protected every time you use
39
+ some helper like render :partial or link_to, so some helpers are modified
40
+ to return SafeString.
41
+
42
+ = render :partial => "foo"
43
+ = link_to "Bar", :action => :bar
44
+
45
+ If you trust your helpers, make them as XSS-protected:
46
+
47
+ module Some::Module
48
+ mark_helpers_as_xss_protected :text_field, :check_box
49
+ end
50
+
51
+ Because it is not possible to alter syntactic keywords like yield
52
+ or instance variables like @content_for_layout to mark them automatically
53
+ as secure, layout files need some manual tweaking.
54
+
55
+ === Other template engines ===
56
+
57
+ If a templates uses some templating engine other than HAML or ERB,
58
+ or it uses ERB but has extension .erb not .rhtml, XSS Shield does not protect it.
59
+
60
+ However some helpers like link_to and button_to are patched by XSS Shield to
61
+ make them more secure, and this extra security will be there even when used
62
+ in an otherwise unprotected context.
63
+
64
+ For example with XSS shield
65
+ link_to "A & B", "/foo"
66
+ will return (marked as safe):
67
+ '<a href="/foo">A &amp; B</a>'
68
+ not (plain String):
69
+ '<a href="/foo">A & B</a>'
70
+
71
+ Also - RHTML protection only works with default ERB engine (erb.rb from Ruby base).
72
+ If you use some alternative ERB engine it probably won't work.
73
+
74
+ Adding support for alternative templating engine should be relatively straightforward.
75
+ It's mostly a matter of changing to_s to to_s_xss_protected in a few places
76
+ in their source.
@@ -0,0 +1,16 @@
1
+ unless ENV['DISABLE_XSS_SHIELD']
2
+ puts "Loading XSS Shield"
3
+ require 'xss_shield'
4
+ else
5
+ class ::String
6
+ def mark_as_xss_protected
7
+ self
8
+ end
9
+ end
10
+
11
+ class ::NilClass
12
+ def mark_as_xss_protected
13
+ self
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,111 @@
1
+ class XSSProtectedERB < ERB
2
+ class Compiler < ::ERB::Compiler
3
+ def compile(s)
4
+ out = Buffer.new(self)
5
+
6
+ content = ''
7
+ scanner = make_scanner(s)
8
+ scanner.scan do |token|
9
+ if scanner.stag.nil?
10
+ case token
11
+ when PercentLine
12
+ out.push("#{@put_cmd} #{content.dump}") if content.size > 0
13
+ content = ''
14
+ out.push(token.to_s)
15
+ out.cr
16
+ when :cr
17
+ out.cr
18
+ when '<%', '<%=', '<%#'
19
+ scanner.stag = token
20
+ out.push("#{@put_cmd} #{content.dump}") if content.size > 0
21
+ content = ''
22
+ when "\n"
23
+ content << "\n"
24
+ out.push("#{@put_cmd} #{content.dump}")
25
+ out.cr
26
+ content = ''
27
+ when '<%%'
28
+ content << '<%'
29
+ else
30
+ content << token
31
+ end
32
+ else
33
+ case token
34
+ when '%>'
35
+ case scanner.stag
36
+ when '<%'
37
+ if content[-1] == ?\n
38
+ content.chop!
39
+ out.push(content)
40
+ out.cr
41
+ else
42
+ out.push(content)
43
+ end
44
+ when '<%='
45
+ # NOTE: Changed lines
46
+ out.push("#{@insert_cmd}((#{content}).to_s_xss_protected)")
47
+ # NOTE: End changed lines
48
+ when '<%#'
49
+ # out.push("# #{content.dump}")
50
+ end
51
+ scanner.stag = nil
52
+ content = ''
53
+ when '%%>'
54
+ content << '%>'
55
+ else
56
+ content << token
57
+ end
58
+ end
59
+ end
60
+ out.push("#{@put_cmd} #{content.dump}") if content.size > 0
61
+ out.close
62
+ out.script
63
+ end
64
+ end
65
+
66
+ def initialize(str, safe_level=nil, trim_mode=nil, eoutvar='_erbout')
67
+ @safe_level = safe_level
68
+ compiler = XSSProtectedERB::Compiler.new(trim_mode)
69
+ set_eoutvar(compiler, eoutvar)
70
+ @src = compiler.compile(str)
71
+ @filename = nil
72
+ end
73
+ end
74
+
75
+ module ActionView
76
+ class Base
77
+ private
78
+ def create_template_source(extension, template, render_symbol, locals)
79
+ if template_requires_setup?(extension)
80
+ body = case extension.to_sym
81
+ when :rxml, :builder
82
+ content_type_handler = (controller.respond_to?(:response) ? "controller.response" : "controller")
83
+ "#{content_type_handler}.content_type ||= Mime::XML\n" +
84
+ "xml = Builder::XmlMarkup.new(:indent => 2)\n" +
85
+ template +
86
+ "\nxml.target!\n"
87
+ when :rjs
88
+ "controller.response.content_type ||= Mime::JS\n" +
89
+ "update_page do |page|\n#{template}\nend"
90
+ end
91
+ # NOTE: Changed lines
92
+ elsif extension.to_sym == :rhtml
93
+ body = XSSProtectedERB.new(template, nil, @@erb_trim_mode).src
94
+ # NOTE: End changed lines
95
+ else
96
+ body = ERB.new(template, nil, @@erb_trim_mode).src
97
+ end
98
+
99
+ @@template_args[render_symbol] ||= {}
100
+ locals_keys = @@template_args[render_symbol].keys | locals
101
+ @@template_args[render_symbol] = locals_keys.inject({}) { |h, k| h[k] = true; h }
102
+
103
+ locals_code = ""
104
+ locals_keys.each do |key|
105
+ locals_code << "#{key} = local_assigns[:#{key}]\n"
106
+ end
107
+
108
+ "def #{render_symbol}(local_assigns)\n#{locals_code}#{body}\nend"
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,42 @@
1
+ raise "Haml not loaded" unless Haml::Engine.instance_method(:push_script)
2
+
3
+ module Haml
4
+ class Engine
5
+ def push_script(text, flattened)
6
+ unless options[:suppress_eval]
7
+ push_silent("haml_temp = #{text}", true)
8
+ push_silent("haml_temp = haml_temp.to_s_xss_protected", true)
9
+ out = "haml_temp = _hamlout.push_script(haml_temp, #{@output_tabs}, #{flattened})\n"
10
+ if @block_opened
11
+ push_and_tabulate([:loud, out])
12
+ else
13
+ @precompiled << out
14
+ end
15
+ end
16
+ end
17
+
18
+ def build_attributes(attributes = {})
19
+ # We ignore @options[:attr_wrapper] because ERB::Util.h does not espace ' to &apos;
20
+ # making ' as attribute quote not workable
21
+ result = attributes.map do |a,v|
22
+ v = v.to_s_xss_protected
23
+ unless v.blank?
24
+ " #{a}=\"#{v}\""
25
+ end
26
+ end
27
+ result.sort.join
28
+ end
29
+ end
30
+
31
+ class Buffer
32
+ def build_attributes(attributes = {})
33
+ result = attributes.map do |a,v|
34
+ v = v.to_s_xss_protected
35
+ unless v.blank?
36
+ " #{a}=\"#{v}\""
37
+ end
38
+ end
39
+ result.sort.join
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,47 @@
1
+ class SafeString < String
2
+ def to_s
3
+ self
4
+ end
5
+ def to_s_xss_protected
6
+ self
7
+ end
8
+ end
9
+
10
+ class String
11
+ def mark_as_xss_protected
12
+ SafeString.new(self)
13
+ end
14
+ end
15
+
16
+ class NilClass
17
+ def mark_as_xss_protected
18
+ self
19
+ end
20
+ end
21
+
22
+ # ERB::Util.h and (include ERB::Util; h) are different methods
23
+ module ERB::Util
24
+ class <<self
25
+ def h_with_xss_protection(*args)
26
+ h_without_xss_protection(*args).mark_as_xss_protected
27
+ end
28
+ alias_method_chain :h, :xss_protection
29
+ end
30
+
31
+ def h_with_xss_protection(*args)
32
+ h_without_xss_protection(*args).mark_as_xss_protected
33
+ end
34
+ alias_method_chain :h, :xss_protection
35
+ end
36
+
37
+ class Object
38
+ def to_s_xss_protected
39
+ ERB::Util.h(to_s).mark_as_xss_protected
40
+ end
41
+ end
42
+
43
+ class Array
44
+ def join_xss_protected(sep="")
45
+ map(&:to_s_xss_protected).join(sep.to_s_xss_protected).mark_as_xss_protected
46
+ end
47
+ end
@@ -0,0 +1,40 @@
1
+ class Module
2
+ def mark_helpers_as_xss_protected(*ms)
3
+ ms.each do |m|
4
+ begin
5
+ instance_method("#{m}_with_xss_protection")
6
+ rescue NameError
7
+ define_method :"#{m}_with_xss_protection" do |*args|
8
+ send(:"#{m}_without_xss_protection", *args).mark_as_xss_protected
9
+ end
10
+ alias_method_chain m, :xss_protection
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ class ActionView::Base
17
+ mark_helpers_as_xss_protected :javascript_include_tag,
18
+ :stylesheet_link_tag,
19
+ :render,
20
+ :text_field_tag,
21
+ :submit_tag,
22
+ :radio_button,
23
+ :text_area,
24
+ :auto_discovery_link_tag,
25
+ :image_tag
26
+
27
+ def link_to_with_xss_protection(text, *args)
28
+ link_to_without_xss_protection(text.to_s_xss_protected, *args).mark_as_xss_protected
29
+ end
30
+ alias_method_chain :link_to, :xss_protection
31
+
32
+ def button_to_with_xss_protection(text, *args)
33
+ button_to_without_xss_protection(text.to_s_xss_protected, *args).mark_as_xss_protected
34
+ end
35
+ alias_method_chain :button_to, :xss_protection
36
+ end
37
+
38
+ module ActionView::Helpers::FormHelper
39
+ mark_helpers_as_xss_protected :text_field, :check_box
40
+ end
@@ -0,0 +1,6 @@
1
+ require 'xss_shield/safe_string'
2
+ # Tarantula doesn't use haml
3
+ # require 'xss_shield/haml_hacks'
4
+ # ERB hacks blow up Rails
5
+ # require 'xss_shield/erb_hacks'
6
+ require 'xss_shield/secure_helpers'
@@ -0,0 +1,40 @@
1
+ # Run from your Rails main directory
2
+ require 'test/test_helper'
3
+
4
+ class TestActionViewIntegration < Test::Unit::TestCase
5
+ def assert_renders(expected, input, extension)
6
+ base = ActionView::Base.new
7
+ actual = base.render_template(extension, input, "foo.#{extension}")
8
+ assert_equal expected, actual
9
+ end
10
+
11
+ def test_erb
12
+ assert_renders <<OUT, <<IN, :erb
13
+ A & B
14
+ A & B
15
+ OUT
16
+ <%= "A & B" %>
17
+ <%= "A & B".mark_as_xss_protected %>
18
+ IN
19
+ end
20
+
21
+ def test_rhtml
22
+ assert_renders <<OUT, <<IN, :rhtml
23
+ A &amp; B
24
+ A & B
25
+ OUT
26
+ <%= "A & B" %>
27
+ <%= "A & B".mark_as_xss_protected %>
28
+ IN
29
+ end
30
+
31
+ def test_haml
32
+ assert_renders <<OUT, <<IN, :haml
33
+ A &amp; B
34
+ A & B
35
+ OUT
36
+ = "A & B"
37
+ = "A & B".mark_as_xss_protected
38
+ IN
39
+ end
40
+ end
@@ -0,0 +1,44 @@
1
+ # Run from your Rails main directory
2
+ require 'test/test_helper'
3
+
4
+ class TestERB < Test::Unit::TestCase
5
+ def assert_renders_erb(expected, input, shield=true)
6
+ erb_class = shield ? XSSProtectedERB : ERB
7
+
8
+ actual = eval(erb_class.new(input).src)
9
+
10
+ assert_equal expected, actual
11
+ end
12
+
13
+ def test_erb_with_shield
14
+ assert_renders_erb <<OUT, <<IN, true
15
+ Foo &amp;amp; Bar
16
+ Foo &amp;amp; Bar
17
+ Foo &amp; Bar
18
+ Foo &amp; Bar
19
+ Foo &amp; Bar
20
+ OUT
21
+ <%= "Foo &amp; Bar" %>
22
+ <%= h("Foo &amp; Bar") %>
23
+ <%= "Foo &amp; Bar".mark_as_xss_protected %>
24
+ <%= h("Foo & Bar") %>
25
+ <%= "Foo & Bar" %>
26
+ IN
27
+ end
28
+
29
+ def test_erb_without_shield
30
+ assert_renders_erb <<OUT, <<IN, false
31
+ Foo &amp;amp; Bar
32
+ Foo &amp; Bar
33
+ Foo &amp; Bar
34
+ Foo &amp; Bar
35
+ Foo & Bar
36
+ OUT
37
+ <%= h("Foo &amp; Bar") %>
38
+ <%= "Foo &amp; Bar" %>
39
+ <%= "Foo &amp; Bar".mark_as_xss_protected %>
40
+ <%= h("Foo & Bar") %>
41
+ <%= "Foo & Bar" %>
42
+ IN
43
+ end
44
+ end
@@ -0,0 +1,43 @@
1
+ # Run from your Rails main directory
2
+ require 'test/test_helper'
3
+
4
+ class TestHaml < Test::Unit::TestCase
5
+ def setup
6
+ @base = ActionView::Base.new
7
+ end
8
+
9
+ def assert_haml_renders(expected, input)
10
+ actual = Haml::Engine.new(input).to_html(@base)
11
+ assert_equal expected, actual
12
+ end
13
+
14
+ def test_haml_engine
15
+ assert_haml_renders <<OUT, <<IN
16
+ A & B
17
+ C &amp; D
18
+ E &amp; F
19
+ G & H
20
+ I &amp; J
21
+ OUT
22
+ A & B
23
+ = "C & D"
24
+ = h("E & F")
25
+ = "G & H".mark_as_xss_protected
26
+ = "I & J".to_s_xss_protected
27
+ IN
28
+ end
29
+
30
+ def test_attribute_escaping_in_haml
31
+ @base.instance_eval {
32
+ @foo = "A < & > ' \" B"
33
+ }
34
+ assert_haml_renders <<OUT, <<IN
35
+ <div foo="A &lt; &amp; &gt; ' &quot; B" />
36
+ <div foo="A < & > ' " B" />
37
+ OUT
38
+ %div{:foo => @foo}/
39
+ %div{:foo => @foo.mark_as_xss_protected}/
40
+ IN
41
+ # Note that '/" explicitly marked as XSS-protected can break validity
42
+ end
43
+ end
@@ -0,0 +1,25 @@
1
+ # Run from your Rails main directory
2
+ require 'test/test_helper'
3
+
4
+ class TestHelpers < Test::Unit::TestCase
5
+ def setup
6
+ @base = ActionView::Base.new
7
+ end
8
+
9
+ def assert_haml_renders(expected, input)
10
+ actual = Haml::Engine.new(input).to_html(@base)
11
+ assert_equal expected, actual
12
+ end
13
+
14
+ def test_link_to
15
+ assert_haml_renders <<OUT, <<IN
16
+ <a href="/bar">Foo</a>
17
+ <a href="/bar">Foo &amp; Bar</a>
18
+ <a href="/bar">Foo & Bar</a>
19
+ OUT
20
+ = link_to "Foo", "/bar"
21
+ = link_to "Foo & Bar", "/bar"
22
+ = link_to "Foo & Bar".mark_as_xss_protected, "/bar"
23
+ IN
24
+ end
25
+ end