rails_with_xss_plugin.rb 12.0 KB
Newer Older
1 2
abort "Please run using test/test.rb" unless defined? BrakemanTester

3 4 5 6
RailsWithXssPlugin = BrakemanTester.run_scan(
  "rails_with_xss_plugin",
  "RailsWithXssPlugin",
  :absolute_paths => true,
7 8
  :run_all_checks => true,
  :collapse_mass_assignment => true
9
)
10 11 12 13 14 15 16 17 18

class RailsWithXssPluginTests < Test::Unit::TestCase
  include BrakemanTester::FindWarning
  include BrakemanTester::CheckExpected

  def expected
    @expected ||= {
      :controller => 1,
      :model => 3,
19
      :template => 4,
20
      :generic => 28 }
21 22 23 24 25
  end

  def report
    RailsWithXssPlugin
  end
26 27

  def test_default_routes_1
28 29 30 31 32 33 34 35 36
    assert_warning :type => :warning,
      :warning_type => "Default Routes",
      :line => 52,
      :message => /^All\ public\ methods\ in\ controllers\ are\ av/,
      :confidence => 0,
      :file => /routes\.rb/
  end


37
  def test_command_injection_2
38 39 40 41 42 43 44 45 46
    assert_warning :type => :warning,
      :warning_type => "Command Injection",
      :line => 48,
      :message => /^Possible\ command\ injection/,
      :confidence => 0,
      :file => /users_controller\.rb/
  end


47
  def test_command_injection_3
48 49 50 51 52 53 54 55 56
    assert_warning :type => :warning,
      :warning_type => "Command Injection",
      :line => 68,
      :message => /^Possible\ command\ injection/,
      :confidence => 0,
      :file => /users_controller\.rb/
  end


57
  def test_command_injection_4
58 59 60 61 62 63 64 65 66
    assert_warning :type => :warning,
      :warning_type => "Command Injection",
      :line => 102,
      :message => /^Possible\ command\ injection/,
      :confidence => 0,
      :file => /users_controller\.rb/
  end


67
  def test_mass_assignment_5
68 69 70 71 72 73 74 75 76
    assert_warning :type => :warning,
      :warning_type => "Mass Assignment",
      :line => 47,
      :message => /^Unprotected\ mass\ assignment/,
      :confidence => 0,
      :file => /posts_controller\.rb/
  end


77
  def test_mass_assignment_6
78 79 80 81 82 83 84 85 86
    assert_warning :type => :warning,
      :warning_type => "Mass Assignment",
      :line => 47,
      :message => /^Unprotected\ mass\ assignment/,
      :confidence => 0,
      :file => /users_controller\.rb/
  end


87
  def test_mass_assignment_7
88 89 90 91 92 93 94 95 96
    assert_warning :type => :warning,
      :warning_type => "Mass Assignment",
      :line => 67,
      :message => /^Unprotected\ mass\ assignment/,
      :confidence => 0,
      :file => /posts_controller\.rb/
  end


97
  def test_mass_assignment_8
98 99 100 101 102 103 104 105 106
    assert_warning :type => :warning,
      :warning_type => "Mass Assignment",
      :line => 71,
      :message => /^Unprotected\ mass\ assignment/,
      :confidence => 0,
      :file => /users_controller\.rb/
  end


107 108
  def test_redirect_to_model_instance
    assert_no_warning :type => :warning,
109 110 111 112 113 114 115 116
      :warning_type => "Redirect",
      :line => 68,
      :message => /^Possible\ unprotected\ redirect/,
      :confidence => 2,
      :file => /posts_controller\.rb/
  end


117 118
  def test_another_redirect_to_model_instance
    assert_no_warning :type => :warning,
119 120 121 122 123 124 125 126
      :warning_type => "Redirect",
      :line => 72,
      :message => /^Possible\ unprotected\ redirect/,
      :confidence => 2,
      :file => /users_controller\.rb/
  end


127
  def test_redirect_11
128 129
    assert_warning :type => :warning,
      :warning_type => "Redirect",
130
      :line => 95,
131 132 133 134 135 136
      :message => /^Possible\ unprotected\ redirect/,
      :confidence => 0,
      :file => /users_controller\.rb/
  end


137 138 139 140 141 142 143 144
  def test_rails_cve_2012_2660
    assert_warning :type => :warning,
      :warning_type => "SQL Injection",
      :message => /CVE-2012-2660/,
      :confidence => 0,
      :file => /Gemfile/
  end

J
Justin Collins 已提交
145 146 147 148 149 150 151 152
  def test_rails_cve_2012_2695
    assert_warning :type => :warning,
      :warning_type => "SQL Injection",
      :message => /CVE-2012-2695/,
      :confidence => 0,
      :file => /Gemfile/
  end

153
  def test_sql_injection_12
154 155 156 157 158 159 160 161 162
    assert_warning :type => :warning,
      :warning_type => "SQL Injection",
      :line => 126,
      :message => /^Possible\ SQL\ injection/,
      :confidence => 0,
      :file => /users_controller\.rb/
  end


163
  def test_cross_site_scripting_13
164 165 166 167 168 169 170 171 172
    assert_warning :type => :warning,
      :warning_type => "Cross Site Scripting",
      #noline,
      :message => /^Rails\ 2\.3\.x\ using\ the\ rails_xss\ plugin\ h/,
      :confidence => 1,
      :file => /Gemfile/
  end


173
  def test_cross_site_scripting_14
174 175 176 177 178 179 180 181
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 13,
      :message => /^Unescaped\ model\ attribute/,
      :confidence => 0,
      :file => /show\.html\.erb/
  end

182 183 184 185 186 187 188
  def test_cross_site_scripting_single_quotes_CVE_2012_3464
    assert_no_warning :type => :warning,
      :warning_type => "Cross Site Scripting",
      :message => /^All\ Rails\ 2\.x\ versions\ do\ not\ escape\ sin/,
      :confidence => 1,
      :file => /environment\.rb/
  end
189

190
  def test_dynamic_render_path_15
J
Justin Collins 已提交
191
    assert_no_warning :type => :template,
192 193 194 195 196 197 198 199
      :warning_type => "Dynamic Render Path",
      :line => 8,
      :message => /^Render\ path\ is\ dynamic/,
      :confidence => 0,
      :file => /results\.html\.erb/
  end


200
  def test_sql_injection_16
201
    assert_no_warning :type => :template,
202 203 204 205 206 207 208 209
      :warning_type => "SQL Injection",
      :line => 4,
      :message => /^Possible\ SQL\ injection/,
      :confidence => 0,
      :file => /results\.html\.erb/
  end


210
  def test_sql_injection_17
211
    assert_no_warning :type => :template,
212 213 214 215 216 217 218
      :warning_type => "SQL Injection",
      :line => 7,
      :message => /^Possible\ SQL\ injection/,
      :confidence => 0,
      :file => /results\.html\.erb/
  end

219 220 221 222 223 224 225 226 227 228 229
  def test_sql_injection_select_value
    assert_warning :type => :warning,
      :warning_code => 0,
      :fingerprint => "e725c387439202f28c1983bf225323d93b5891695c91b9389740e2da3d74855e",
      :warning_type => "SQL Injection",
      :line => 134,
      :message => /^Possible\ SQL\ injection/,
      :confidence => 0,
      :relative_path => "app/controllers/users_controller.rb",
      :user_input => s(:call, s(:params), :[], s(:lit, :name))
  end
230

231
  def test_cross_site_request_forgery_18
232 233 234 235 236 237 238 239
    assert_warning :type => :controller,
      :warning_type => "Cross-Site Request Forgery",
      #noline,
      :message => /^'protect_from_forgery'\ should\ be\ called\ /,
      :confidence => 0,
      :file => /application_controller\.rb/
  end

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
  def test_cross_site_scripting
    assert_warning :type => :template,
      :warning_code => 58,
      :fingerprint => "3ec8749301aa7cdb1d3ec5610120492138060f05d65af0aa53dbb1a3b7c493ac",
      :warning_type => "Cross Site Scripting",
      :line => 1,
      :message => /^Rails\ 2\.3\.14\ has\ a\ vulnerability\ in\ sani/,
      :confidence => 0,
      :relative_path => "app/views/users/test_sanitize.html.erb",
      :user_input => nil
  end

  def test_cross_site_scripting_sanitize_dupe
    assert_no_warning :type => :template,
      :warning_code => 58,
      :fingerprint => "9d90d446941026c42502e1213ef6d9122a2ad587266cdb002d9f30bb3c77523d",
      :warning_type => "Cross Site Scripting",
      :line => 1,
      :message => /^Rails\ 2\.3\.14\ has\ a\ vulnerability\ in\ sani/,
      :confidence => 0,
      :relative_path => "app/views/users/test_sanitize.html.erb",
      :user_input => nil
  end
263

264
  def test_attribute_restriction_19
265 266 267 268 269 270 271 272 273
    assert_warning :type => :model,
      :warning_type => "Attribute Restriction",
      #noline,
      :message => /^Mass\ assignment\ is\ not\ restricted\ using\ /,
      :confidence => 0,
      :file => /post,\ user\.rb/
  end


274
  def test_format_validation_20
275 276 277 278 279 280 281 282 283
    assert_warning :type => :model,
      :warning_type => "Format Validation",
      :line => 5,
      :message => /^Insufficient\ validation\ for\ 'user_name'\ /,
      :confidence => 0,
      :file => /user\.rb/
  end


284
  def test_format_validation_21
285 286 287 288 289 290 291 292
    assert_warning :type => :model,
      :warning_type => "Format Validation",
      :line => 7,
      :message => /^Insufficient\ validation\ for\ 'display_nam/,
      :confidence => 0,
      :file => /user\.rb/
  end

J
Justin Collins 已提交
293
  def test_strip_tags_CVE_2012_3465
294 295 296 297 298 299
    assert_warning :type => :warning,
      :warning_type => "Cross Site Scripting",
      :message => /^All\ Rails\ 2\.x\ versions\ have\ a\ vulnerabil/,
      :confidence => 0,
      :file => /Gemfile/
  end
300

J
Justin Collins 已提交
301 302 303
  def test_sql_injection_CVE_2012_5664
    assert_warning :type => :warning,
      :warning_type => "SQL Injection",
304
      :message => /CVE-2012-5664/,
J
Justin Collins 已提交
305 306 307 308
      :confidence => 0,
      :file => /Gemfile/
  end

309 310 311 312 313 314 315
  def test_to_json
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 1,
      :message => /^Unescaped parameter value in JSON hash/,
      :confidence => 0,
      :file => /users\/to_json\.html\.erb/
316
  end
J
Justin Collins 已提交
317 318 319 320 321 322 323 324 325

  def test_session_secret_token
    assert_warning :type => :warning,
      :warning_type => "Session Setting",
      :line => 9,
      :message => /^Session\ secret\ should\ not\ be\ included\ in/,
      :confidence => 0,
      :file => /session_store\.rb/
  end
J
Justin Collins 已提交
326

327
  def test_absolute_paths
J
Justin Collins 已提交
328
    assert report[:generic_warnings].all? { |w| w.file.start_with? "/" }
329 330
  end

331 332 333 334 335 336 337 338 339 340 341 342
  def test_cross_site_scripting_CVE_2012_1099
    assert_warning :type => :template,
      :warning_code => 22,
      :fingerprint => "d54bacec90be92ad8ca58164cdfd505114eae34db2fb5b03f7bc2a8fd93f1edb",
      :warning_type => "Cross Site Scripting",
      :line => 18,
      :message => /^Upgrade\ to\ Rails\ 3\ or\ use\ options_for_se/,
      :confidence => 1,
      :relative_path => "app/views/users/index.html.erb",
      :user_input => nil
  end

343 344 345
  def test_sql_injection_CVE_2013_0155
    assert_warning :type => :warning,
      :warning_type => "SQL Injection",
346
      :message => /CVE-2013-0155/,
347 348 349 350
      :confidence => 0,
      :file => /Gemfile/
  end

J
Justin Collins 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364
  def test_parsing_disable_CVE_2013_0156
    assert_no_warning :type => :warning,
      :warning_type => "Remote Code Execution",
      :message => /^Rails\ 2\.3\.14\ has\ a\ remote\ code\ execution/,
      :confidence => 0,
      :file => /Gemfile/
  end

  def test_remote_code_execution_CVE_2013_0156
    assert_warning :type => :warning,
      :warning_type => "Remote Code Execution",
      :message => /^Parsing\ YAML\ request\ parameters\ enables\ /,
      :confidence => 0
  end
J
Justin Collins 已提交
365

366 367 368 369 370 371 372 373
  def test_denial_of_service_CVE_2013_0269
    assert_warning :type => :warning,
      :warning_type => "Denial of Service",
      :message => /^json\ gem\ version\ 1\.1\.0\ has\ a\ symbol\ crea/,
      :confidence => 2,
      :file => /Gemfile/
  end

J
Justin Collins 已提交
374 375 376 377 378 379 380
  def test_json_parsing_workaround_CVE_2013_0333
    assert_no_warning :type => :warning,
      :warning_type => "Remote Code Execution",
      :message => /^Rails\ 2\.3\.14\ has\ a\ serious\ JSON\ parsing\ /,
      :confidence => 0,
      :file => /Gemfile/
  end
J
Justin Collins 已提交
381 382 383 384 385 386 387 388

  def test_denial_of_service_CVE_2013_1854
    assert_warning :type => :warning,
      :warning_type => "Denial of Service",
      :message => /^Rails\ 2\.3\.14\ has\ a\ denial\ of\ service\ vul/,
      :confidence => 1,
      :file => /Gemfile/
  end
389

390 391 392 393 394
  def test_sql_injection_CVE_2013_6417
    assert_warning :type => :warning,
      :warning_code => 69,
      :fingerprint => "e1b66f4311771d714a13be519693c540d7e917511a758827d9b2a0a7f958e40f",
      :warning_type => "SQL Injection",
395 396
      :line => 3,
      :file => /Gemfile/,
397 398 399 400 401 402
      :message => /^Rails\ 2\.3\.14\ contains\ a\ SQL\ injection\ vu/,
      :confidence => 0,
      :relative_path => "Gemfile",
      :user_input => nil
  end

J
Justin Collins 已提交
403
  def test_number_to_currency_CVE_2014_0081
404
    assert_warning :type => :warning,
J
Justin Collins 已提交
405 406
      :warning_code => 73,
      :fingerprint => "f6981b9c24727ef45040450a1f4b158ae3bc31b4b0343efe853fe12c64881695",
407 408 409 410 411 412 413
      :warning_type => "Cross Site Scripting",
      :line => nil,
      :message => /^Rails\ 2\.3\.14\ has\ a\ vulnerability\ in\ numb/,
      :confidence => 1,
      :relative_path => "Gemfile",
      :user_input => nil
  end
J
Justin Collins 已提交
414 415 416 417 418 419 420 421 422 423 424 425

  def test_remote_code_execution_CVE_2014_0130
    assert_warning :type => :warning,
      :warning_code => 77,
      :fingerprint => "93393e44a0232d348e4db62276b18321b4cbc9051b702d43ba2fd3287175283c",
      :warning_type => "Remote Code Execution",
      :line => nil,
      :message => /^Rails\ 2\.3\.14\ with\ globbing\ routes\ is\ vul/,
      :confidence => 0,
      :relative_path => "config/routes.rb",
      :user_input => nil
  end
426
end