rails2.rb 43.0 KB
Newer Older
J
Justin Collins 已提交
1 2
abort "Please run using test/test.rb" unless defined? BrakemanTester

J
Justin Collins 已提交
3 4 5 6
Rails2 = BrakemanTester.run_scan "rails2", "Rails 2"

class Rails2Tests < Test::Unit::TestCase
  include BrakemanTester::FindWarning
7 8 9
  include BrakemanTester::CheckExpected

  def expected
J
Justin Collins 已提交
10
    if Brakeman::Scanner::RUBY_1_9
11 12
      @expected ||= {
        :controller => 1,
J
Justin Collins 已提交
13
        :model => 3,
14
        :template => 45,
15
        :generic => 49 }
16 17 18
    else
      @expected ||= {
        :controller => 1,
J
Justin Collins 已提交
19
        :model => 3,
20
        :template => 45,
21
        :generic => 50 }
22
    end
23
  end
J
Justin Collins 已提交
24 25 26 27 28 29 30 31 32

  def report
    Rails2
  end

  def test_no_errors
    assert_equal 0, report[:errors].length
  end

33 34 35 36
  def test_config_sanity
    assert_equal 'UTC', report[:config][:rails][:time_zone].value
  end

J
Justin Collins 已提交
37 38
  def test_eval
    assert_warning :warning_type => "Dangerous Eval",
39
      :line => 40,
J
Justin Collins 已提交
40
      :message => /^User input in eval/,
41 42 43
      :format_code => /eval\(params\[:dangerous_input\]\)/,
      :file => /home_controller.rb/,
      :relative_path => "app/controllers/home_controller.rb"
J
Justin Collins 已提交
44 45 46 47
  end

  def test_default_routes
    assert_warning :warning_type => "Default Routes",
48
      :line => 54,
J
Justin Collins 已提交
49
      :message => /All public methods in controllers are available as actions/,
50 51
      :file => /routes\.rb/,
      :relative_path => "config/routes.rb"
J
Justin Collins 已提交
52 53 54 55 56 57 58
  end

  def test_command_injection_interpolate
    assert_warning :type => :warning,
      :warning_type => "Command Injection",
      :line => 34,
      :message => /^Possible command injection/,
59
      :confidence => 0,
60 61
      :file => /home_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
J
Justin Collins 已提交
62 63 64 65 66
  end

  def test_command_injection_direct
    assert_warning :type => :warning,
      :warning_type => "Command Injection",
67
      :line => 36,
J
Justin Collins 已提交
68 69 70
      :message => /^Possible command injection/,
      :confidence => 0,
      :file => /home_controller\.rb/,
71 72
      :relative_path => "app/controllers/home_controller.rb",
      :format_code => /params\[:user_input\]/
J
Justin Collins 已提交
73 74 75 76 77 78 79 80
  end

  def test_file_access_concatenation
    assert_warning :type => :warning,
      :warning_type => "File Access",
      :line => 24,
      :message => /^Parameter value used in file name/,
      :confidence => 0,
81 82
      :file => /home_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
J
Justin Collins 已提交
83 84 85 86 87 88 89 90
  end

  def test_mass_assignment
    assert_warning :type => :warning,
      :warning_type => "Mass Assignment",
      :line => 54,
      :message => /^Unprotected mass assignment/,
      :confidence => 0,
91 92
      :file => /home_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
J
Justin Collins 已提交
93 94
  end

95 96 97 98 99 100
  def test_update_attribute_no_mass_assignment
    assert_no_warning :type => :warning,
      :warning_type => "Mass Assignment",
      :line => 26,
      :message => /^Unprotected mass assignment/,
      :confidence => 0,
101 102
      :file => /other_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
103 104
  end

105 106 107 108 109 110
  def test_mass_assignment_with_or_equals_in_filter
    assert_warning :type => :warning,
      :warning_type => "Mass Assignment",
      :line => 127,
      :message => /^Unprotected\ mass\ assignment/,
      :confidence => 0,
111 112
      :file => /home_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
113 114
  end

J
Justin Collins 已提交
115 116 117
  def test_redirect
    assert_warning :type => :warning,
      :warning_type => "Redirect",
118
      :line => 45,
J
Justin Collins 已提交
119 120
      :message => /^Possible unprotected redirect/,
      :confidence => 0,
121 122
      :file => /home_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
123 124 125

    assert_warning :type => :warning,
      :warning_type => "Redirect",
126
      :line => 182,
127 128
      :message => /^Possible unprotected redirect/,
      :confidence => 0,
129 130
      :file => /home_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
J
Justin Collins 已提交
131 132 133 134 135
  end

  def test_dynamic_render_path
    assert_warning :type => :warning,
      :warning_type => "Dynamic Render Path",
136 137
      :line => 59,
      :message => /^Render path contains parameter value near line 59: render/,
138
      :confidence => 1,
139 140
      :file => /home_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
141 142 143 144 145
  end

  def test_dynamic_render_path_high_confidence
    assert_warning :type => :warning,
      :warning_type => "Dynamic Render Path",
146 147
      :line => 77,
      :message => /^Render path contains parameter value near line 77: render/,
J
Justin Collins 已提交
148
      :confidence => 0,
149 150
      :file => /home_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
J
Justin Collins 已提交
151 152 153 154 155
  end

  def test_file_access
    assert_warning :type => :warning,
      :warning_type => "File Access",
156
      :line => 21,
J
Justin Collins 已提交
157 158
      :message => /^Parameter value used in file name/,
      :confidence => 0,
159 160
      :file => /other_controller\.rb/,
      :relative_path => "app/controllers/other_controller.rb"
J
Justin Collins 已提交
161 162 163 164 165
  end

  def test_file_access_with_load
    assert_warning :type => :warning,
      :warning_type => "File Access",
166
      :line => 63,
J
Justin Collins 已提交
167 168
      :message => /^Parameter value used in file name/,
      :confidence => 0,
169 170
      :file => /home_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
J
Justin Collins 已提交
171 172 173 174 175
  end

  def test_file_access_load_false
    warnings = find :type => :warning,
      :warning_type => "File Access",
176
      :line => 64,
J
Justin Collins 已提交
177 178
      :message => /^Parameter value used in file name/,
      :confidence => 0,
179 180
      :file => /home_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
J
Justin Collins 已提交
181 182 183 184 185 186 187 188

    assert_equal 0, warnings.length, "False positive found."
  end

  def test_session_secret
    assert_warning :type => :warning,
      :warning_type => "Session Setting",
      :line => 9,
J
Justin Collins 已提交
189
      :message => /^Session\ secret\ should\ not\ be\ included\ in/,
J
Justin Collins 已提交
190
      :confidence => 0,
191 192
      :file => /session_store\.rb/,
      :relative_path => "config/initializers/session_store.rb"
J
Justin Collins 已提交
193 194 195 196 197 198 199 200
  end

  def test_session_cookies
    assert_warning :type => :warning,
      :warning_type => "Session Setting",
      :line => 10,
      :message => /^Session cookies should be set to HTTP on/,
      :confidence => 0,
201 202
      :file => /session_store\.rb/,
      :relative_path => "config/initializers/session_store.rb"
J
Justin Collins 已提交
203 204
  end

205 206 207 208 209 210 211
  def test_rails_cve_2012_2660
    assert_warning :type => :warning,
      :warning_type => "SQL Injection",
      :message => /CVE-2012-2660/,
      :confidence => 0
  end

J
Justin Collins 已提交
212 213 214 215 216 217 218
  def test_rails_cve_2012_2695
    assert_warning :type => :warning,
      :warning_type => "SQL Injection",
      :message => /CVE-2012-2695/,
      :confidence => 0
  end

J
Justin Collins 已提交
219 220 221 222 223 224
  def test_sql_injection_find_by_sql
    assert_warning :type => :warning,
      :warning_type => "SQL Injection",
      :line => 28,
      :message => /^Possible SQL injection/,
      :confidence => 1,
225 226
      :file => /home_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
J
Justin Collins 已提交
227 228 229 230 231 232 233 234
  end

  def test_sql_injection_conditions_local
    assert_warning :type => :warning,
      :warning_type => "SQL Injection",
      :line => 29,
      :message => /^Possible SQL injection/,
      :confidence => 1,
235 236
      :file => /home_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
J
Justin Collins 已提交
237 238 239 240 241 242 243 244
  end

  def test_sql_injection_params
    assert_warning :type => :warning,
      :warning_type => "SQL Injection",
      :line => 30,
      :message => /^Possible SQL injection/,
      :confidence => 0,
245 246
      :file => /home_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
J
Justin Collins 已提交
247 248
  end

249 250 251 252 253 254
  def test_sql_injection_named_scope
    assert_warning :type => :warning,
      :warning_type => "SQL Injection",
      :line => 4,
      :message => /^Possible SQL injection near line 4: named_scope\(:phooey/,
      :confidence => 0,
255 256
      :file => /user\.rb/,
      :relative_path => "app/models/user.rb"
257 258 259 260 261 262 263 264
  end

  def test_sql_injection_named_scope_lambda
    assert_warning :type => :warning,
      :warning_type => "SQL Injection",
      :line => 2,
      :message => /^Possible SQL injection near line 2: named_scope\(:dah, lambda/,
      :confidence => 1,
265 266
      :file => /user\.rb/,
      :relative_path => "app/models/user.rb"
267 268 269 270 271 272 273 274
  end

  def test_sql_injection_named_scope_conditional
    assert_warning :type => :warning,
      :warning_type => "SQL Injection",
      :line => 6,
      :message => /^Possible SQL injection near line 6: named_scope\(:with_state, lambda/,
      :confidence => 1,
275 276
      :file => /user\.rb/,
      :relative_path => "app/models/user.rb"
277 278
  end

J
Justin Collins 已提交
279 280 281 282 283 284
  def test_sql_injection_in_self_call
    assert_warning :type => :warning,
      :warning_type => "SQL Injection",
      :line => 15,
      :message => /^Possible SQL injection near line 15: self\.find/,
      :confidence => 1,
285 286
      :file => /user\.rb/,
      :relative_path => "app/models/user.rb"
J
Justin Collins 已提交
287 288
  end

J
Justin Collins 已提交
289 290 291 292 293 294
  def test_sql_user_input_in_find_by
    assert_no_warning :type => :warning,
      :warning_type => "SQL Injection",
      :line => 116,
      :message => /^Possible SQL injection near line 116: User.find_or_create_by_name/,
      :confidence => 0,
295 296
      :file => /home_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
J
Justin Collins 已提交
297 298
  end

O
oreoshake 已提交
299 300 301 302 303 304 305 306
  # ensure that the warning is generated for the line which contains the input, not
  # the line of the beginning of the string
  def test_sql_user_input_multiline
    assert_warning :type => :warning,
      :warning_type => "SQL Injection",
      :line => 121,
      :message => /^Possible SQL injection near line 121: User.find_by_sql/,
      :confidence => 0,
307 308 309
      :file => /home_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
  end
O
oreoshake 已提交
310

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
  def test_sql_injection_false_positive_quote_value
    assert_no_warning :type => :warning,
      :warning_code => 0,
      :fingerprint => "6ea8fe3abe8eac86e5ecb790b53fb064b1152b2574b14d9354a40d07269a952e",
      :warning_type => "SQL Injection",
      :line => 30,
      :message => /^Possible\ SQL\ injection/,
      :confidence => 1,
      :relative_path => "app/models/user.rb",
      :user_input => s(:call, s(:call, s(:str, "DELETE FROM cool_table WHERE cool_id="), :+, s(:call, nil, :quote_value, s(:call, s(:self), :cool_id))), :+, s(:str, "  AND my_id="))
  end

  def test_sql_injection_sanitize_sql
    assert_no_warning :type => :warning,
      :warning_code => 0,
      :fingerprint => "7481ff666ae949b8442400cf516615ce8b04b87f7e11e33e29d4ad1303d24dd0",
      :warning_type => "SQL Injection",
      :line => 26,
      :message => /^Possible\ SQL\ injection/,
      :confidence => 1,
      :relative_path => "app/models/user.rb",
      :user_input => s(:call, s(:str, "select * from cool_table where stuff = "), :+, s(:call, s(:self), :sanitize_sql, s(:lvar, :input)))
  end

J
Justin Collins 已提交
335 336 337 338 339
  def test_csrf_protection
    assert_warning :type => :controller,
      :warning_type => "Cross-Site Request Forgery",
      :message => /^'protect_from_forgery' should be called /,
      :confidence => 0,
340 341
      :file => /application_controller\.rb/,
      :relative_path => "app/controllers/application_controller.rb"
J
Justin Collins 已提交
342 343 344 345 346
  end

  def test_attribute_restriction
    assert_warning :type => :model,
      :warning_type => "Attribute Restriction",
347
      :warning_code => Brakeman::WarningCodes::Codes[:no_attr_accessible],
J
Justin Collins 已提交
348 349
      :message => /^Mass assignment is not restricted using /,
      :confidence => 0,
350
      :file => /account, user\.rb/,
351
      :relative_path => "app/models/account, user.rb" #TODO: kinda broken
J
Justin Collins 已提交
352 353 354 355 356 357 358 359
  end

  def test_format_validation
    assert_warning :type => :model,
      :warning_type => "Format Validation",
      :line => 2,
      :message => /^Insufficient validation for 'name' using/,
      :confidence => 0,
360 361
      :file => /account\.rb/,
      :relative_path => "app/models/account.rb"
J
Justin Collins 已提交
362 363 364 365 366 367 368 369
  end

  def test_unescaped_parameter
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 3,
      :message => /^Unescaped parameter value/,
      :confidence => 0,
370 371
      :file => /index\.html\.erb/,
      :relative_path => "app/views/home/index.html.erb"
J
Justin Collins 已提交
372 373
  end

374 375 376 377 378 379
  def test_unescaped_request_env
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 1,
      :message => /^Unescaped request value/,
      :confidence => 0,
380 381
      :file => /test_env\.html\.erb/,
      :relative_path => "app/views/other/test_env.html.erb"
382 383
  end

J
Justin Collins 已提交
384 385 386 387 388 389
  def test_params_from_controller
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 4,
      :message => /^Unescaped parameter value/,
      :confidence => 0,
390 391
      :file => /test_params\.html\.erb/,
      :relative_path => "app/views/home/test_params.html.erb"
J
Justin Collins 已提交
392 393
  end

394
  def test_unrendered_sanitized_params_from_controller
395 396 397 398 399
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 1,
      :message => /^Unescaped parameter value/,
      :confidence => 0,
400 401
      :file => /test_sanitized_param\.html\.erb/,
      :relative_path => "app/views/home/test_sanitized_param.html.erb"
402 403 404 405 406 407 408 409
  end

  def test_sanitized_params_from_controller
    assert_no_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 5,
      :message => /^Unescaped parameter value/,
      :confidence => 0,
410 411
      :file => /test_sanitized_param\.html\.erb/,
      :relative_path => "app/views/home/test_sanitized_param.html.erb"
412 413
  end

J
Justin Collins 已提交
414 415 416 417 418 419
  def test_indirect_xss
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 6,
      :message => /^Unescaped parameter value/,
      :confidence => 2,
420 421
      :file => /test_params\.html\.erb/,
      :relative_path => "app/views/home/test_params.html.erb"
J
Justin Collins 已提交
422 423 424 425 426 427 428 429
  end

  def test_model_attribute_from_controller
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 3,
      :message => /^Unescaped model attribute/,
      :confidence => 0,
430 431
      :file => /test_model\.html\.erb/,
      :relative_path => "app/views/home/test_model.html.erb"
J
Justin Collins 已提交
432 433 434 435 436 437 438 439
  end

  def test_model_from_controller_indirect_bad
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 5,
      :message => /^Unescaped model attribute/,
      :confidence => 0,
440 441
      :file => /test_model\.html\.erb/,
      :relative_path => "app/views/home/test_model.html.erb"
J
Justin Collins 已提交
442 443 444 445 446 447 448 449
  end

  def test_model_in_link_to
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 7,
      :message => /^Unescaped model attribute in link_to/,
      :confidence => 0,
450 451
      :file => /test_model\.html\.erb/,
      :relative_path => "app/views/home/test_model.html.erb"
J
Justin Collins 已提交
452 453
  end

454 455 456 457 458 459 460 461 462 463 464
  def test_indirect_model_in_link_to
    assert_warning :type => :template,
      :warning_code => 3,
      :fingerprint => "8941c902e7c71d0df4ebb1888c8ed9ac99affaf385be657838452ac3eefe563c",
      :warning_type => "Cross Site Scripting",
      :line => 9,
      :message => /^Unescaped\ model\ attribute\ in\ l/,
      :confidence => 1,
      :relative_path => "app/views/home/test_link_to.html.erb"
  end

465 466 467 468 469 470
  def test_escaped_parameter_in_link_to
    assert_no_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 10,
      :message => /^Unescaped parameter value in link_to/,
      :confidence => 1,
471 472
      :file => /test_params\.html\.erb/,
      :relative_path => "app/views/home/test_params.html.erb"
473 474
  end

475 476 477 478 479 480
  def test_encoded_href_parameter_in_link_to
    assert_no_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 12,
      :message => /^Unsafe parameter value in link_to href/,
      :confidence => 0,
481 482 483
      :file => /test_params\.html\.erb/,
      :relative_path => "app/views/home/test_params.html.erb"
  end
484 485 486 487 488 489 490

  def test_href_parameter_in_link_to
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 14,
      :message => /^Unsafe parameter value in link_to href/,
      :confidence => 0,
491 492
      :file => /test_params\.html\.erb/,
      :relative_path => "app/views/home/test_params.html.erb"
493 494 495 496 497 498

    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 16,
      :message => /^Unsafe parameter value in link_to href/,
      :confidence => 1,
499 500
      :file => /test_params\.html\.erb/,
      :relative_path => "app/views/home/test_params.html.erb"
501 502 503 504 505 506

    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 18,
      :message => /^Unsafe parameter value in link_to href/,
      :confidence => 1,
507 508
      :file => /test_params\.html\.erb/,
      :relative_path => "app/views/home/test_params.html.erb"
509
  end
510

511 512 513 514 515 516
  def test_polymorphic_url_in_href
    assert_no_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 9,
      :message => /^Unsafe parameter value in link_to href/,
      :confidence => 1,
517 518
      :file => /test_model\.html\.erb/,
      :relative_path => "app/views/home/test_model.html.erb"
519 520 521 522 523 524

    assert_no_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 11,
      :message => /^Unsafe parameter value in link_to href/,
      :confidence => 1,
525 526
      :file => /test_model\.html\.erb/,
      :relative_path => "app/views/home/test_model.html.erb"
527 528
  end

J
Justin Collins 已提交
529 530 531 532 533 534
  def test_unescaped_body_in_link_to
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 7,
      :message => /^Unescaped parameter value in link_to/,
      :confidence => 0,
535 536
      :file => /test_link_to\.html\.erb/,
      :relative_path => "app/views/home/test_link_to.html.erb"
J
Justin Collins 已提交
537 538
  end

J
Justin Collins 已提交
539 540 541 542 543 544
  def test_filter
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 3,
      :message => /^Unescaped parameter value/,
      :confidence => 0,
545 546
      :file => /test_filter\.html\.erb/,
      :relative_path => "app/views/home/test_filter.html.erb"
J
Justin Collins 已提交
547 548 549 550 551 552 553 554
  end

  def test_unescaped_model
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 4,
      :message => /^Unescaped model attribute/,
      :confidence => 0,
555 556
      :file => /test_sql\.html\.erb/,
      :relative_path => "app/views/home/test_sql.html.erb"
J
Justin Collins 已提交
557 558 559 560 561 562 563 564
  end

  def test_param_from_filter
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 5,
      :message => /^Unescaped parameter value/,
      :confidence => 0,
565 566
      :file => /index\.html\.erb/,
      :relative_path => "app/views/home/index.html.erb"
J
Justin Collins 已提交
567 568 569 570 571 572 573 574
  end

  def test_params_from_locals_hash
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 4,
      :message => /^Unescaped parameter value/,
      :confidence => 0,
575 576
      :file => /app\/views\/other\/test_locals\.html\.erb/,
      :relative_path => "app/views/other/test_locals.html.erb"
J
Justin Collins 已提交
577 578 579 580 581 582 583 584
  end

  def test_model_attribute_from_collection
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 1,
      :message => /^Unescaped model attribute/,
      :confidence => 0,
585 586
      :file => /_user\.html\.erb/,
      :relative_path => "app/views/other/_user.html.erb"
J
Justin Collins 已提交
587 588 589 590 591 592 593 594
  end

  def test_model_attribute_from_iteration
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 3,
      :message => /^Unescaped model attribute/,
      :confidence => 0,
595 596
      :file => /test_iteration\.html\.erb/,
      :relative_path => "app/views/other/test_iteration.html.erb"
J
Justin Collins 已提交
597 598 599 600 601 602 603 604
  end

  def test_other_model_attribute_from_iteration
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 4,
      :message => /^Unescaped model attribute/,
      :confidence => 0,
605 606
      :file => /test_iteration\.html\.erb/,
      :relative_path => "app/views/other/test_iteration.html.erb"
J
Justin Collins 已提交
607 608 609
  end

  def test_sql_injection_in_template
J
Justin 已提交
610
    assert_no_warning :type => :template,
J
Justin Collins 已提交
611 612 613 614
      :warning_type => "SQL Injection",
      :line => 4,
      :message => /^Possible SQL injection/,
      :confidence => 0,
615 616
      :file => /test_sql\.html\.erb/,
      :relative_path => "app/views/home/test_sql.html.erb"
J
Justin Collins 已提交
617 618
  end

619 620 621 622 623 624
  def test_sql_injection_call_chain
    assert_warning :type => :warning,
      :warning_type => "SQL Injection",
      :line => 73,
      :message => /^Possible SQL injection near line 73: User.humans.alive.find/,
      :confidence => 0,
625 626
      :file => /home_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
627 628
  end

629 630 631 632 633 634
  def test_sql_injection_merge_conditions
    assert_no_warning :type => :warning,
      :warning_type => "SQL Injection",
      :line => 22,
      :message => /^Possible SQL injection near line 22: find/,
      :confidence => 0,
635 636
      :file => /user\.rb/,
      :relative_path => "app/models/user.rb"
637
  end
638 639 640 641 642 643 644 645 646 647 648 649
 
  def test_sql_injection_active_record_base_connection
    assert_warning :type => :warning,
      :warning_code => 0,
      :fingerprint => "37885d589fc5c41553dcc38b36b506c2e508d1f37ce040eb6dca92a958f858fb",
      :warning_type => "SQL Injection",
      :line => 26,
      :message => /^Possible\ SQL\ injection/,
      :confidence => 1,
      :relative_path => "app/models/user.rb",
      :user_input => s(:lvar, :value)
  end
650

J
Justin Collins 已提交
651 652 653 654 655 656
  def test_escape_once
    results = find :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 7,
      :message => /^Unescaped parameter value/,
      :confidence => 2,
657 658
      :file => /index\.html\.erb/,
      :relative_path => "app/views/home/index.html.erb"
J
Justin Collins 已提交
659 660 661 662 663 664 665 666 667 668

    assert_equal 0, results.length, "escape_once is a safe method"
  end

  def test_indirect_cookie
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 5,
      :message => /^Unescaped cookie value/,
      :confidence => 2,
669 670
      :file => /test_cookie\.html\.erb/,
      :relative_path => "app/views/home/test_cookie.html.erb"
J
Justin Collins 已提交
671 672
  end

673 674 675 676 677 678
  def test_cookie_from_controller
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 3,
      :message => /^Unescaped cookie value/,
      :confidence => 0,
679 680
      :file => /test_cookie\.html\.erb/,
      :relative_path => "app/views/home/test_cookie.html.erb"
681 682
  end

J
Justin Collins 已提交
683 684 685 686 687 688 689
  #Check for params that look like params[:x][:y]
  def test_params_multidimensional
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 8,
      :message => /^Unescaped parameter value/,
      :confidence => 0,
690 691
      :file => /test_params\.html\.erb/,
      :relative_path => "app/views/home/test_params.html.erb"
J
Justin Collins 已提交
692 693 694 695 696 697 698 699 700
  end

  #Check for cookies that look like cookies[:blah][:blah]
  def test_cookies_multidimensional
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 7,
      :message => /^Unescaped cookie value/,
      :confidence => 0,
701 702
      :file => /test_cookie\.html\.erb/,
      :relative_path => "app/views/home/test_cookie.html.erb"
J
Justin Collins 已提交
703
  end
704 705 706 707 708 709 710

  def test_xss_in_unused_template
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 1,
      :message => "Unescaped parameter value near line 1: params[:blah]",
      :confidence => 0,
711 712
      :file => /not_used\.html\.erb/,
      :relative_path => "app/views/other/not_used.html.erb"
713
  end
714 715 716 717

  def test_select_vulnerability
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
718 719 720
      :line => 3,
      :message => /^Upgrade\ to\ Rails\ 3\ or\ use\ options_for_se/,
      :confidence => 1,
721 722
      :file => /not_used\.html\.erb/,
      :relative_path => "app/views/other/not_used.html.erb"
723
  end
724

725 726 727
  def test_explicit_render_template
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
728 729
      :line => 1,
      :message => /^Unescaped parameter value near line 1: params\[:ba/,
730
      :confidence => 0,
731 732
      :file => /home\/test_render_template\.html\.haml/,
      :relative_path => "app/views/home/test_render_template.html.haml"
733 734
  end

735 736 737 738 739 740
  def test_xss_with_or_in_view
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 1,
      :message => /^Unescaped\ parameter\ value/,
      :confidence => 0,
741 742
      :file => /test_xss_with_or\.html\.erb/,
      :relative_path => "app/views/home/test_xss_with_or.html.erb"
743 744 745 746 747 748 749 750
  end

  def test_xss_with_or_from_action
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 3,
      :message => /^Unescaped\ parameter\ value/,
      :confidence => 0,
751 752
      :file => /test_xss_with_or\.html\.erb/,
      :relative_path => "app/views/home/test_xss_with_or.html.erb"
753 754 755 756 757 758 759 760
  end

  def test_xss_with_or_from_if_branches
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 5,
      :message => /^Unescaped\ parameter\ value/,
      :confidence => 0,
761 762
      :file => /test_xss_with_or\.html\.erb/,
      :relative_path => "app/views/home/test_xss_with_or.html.erb"
763 764 765 766 767 768 769 770
  end

  def test_xss_with_nested_or
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 7,
      :message => /^Unescaped\ parameter\ value/,
      :confidence => 0,
771 772
      :file => /test_xss_with_or\.html\.erb/,
      :relative_path => "app/views/home/test_xss_with_or.html.erb"
773 774
  end

775 776 777 778 779 780
  def test_xss_with_model_in_or
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 9,
      :message => /^Unescaped\ model\ attribute/,
      :confidence => 0,
781 782
      :file => /test_xss_with_or\.html\.erb/,
      :relative_path => "app/views/home/test_xss_with_or.html.erb"
783 784
  end

785 786 787 788 789 790
  def test_cross_site_scripting_strip_tags
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 3,
      :message => /^Unescaped\ parameter\ value/,
      :confidence => 0,
791 792
      :file => /test_strip_tags\.html\.erb/,
      :relative_path => "app/views/home/test_strip_tags.html.erb"
793 794
  end

J
Justin Collins 已提交
795 796 797 798 799 800
  def test_xss_content_tag_body
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 5,
      :message => /^Unescaped\ model\ attribute\ in\ content_tag/,
      :confidence => 0,
801 802
      :file => /test_content_tag\.html\.erb/,
      :relative_path => "app/views/home/test_content_tag.html.erb"
J
Justin Collins 已提交
803 804 805 806 807 808 809 810
  end

  def test_xss_content_tag_escaped
    assert_no_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 8,
      :message => /^Unescaped\ cookie\ value\ in\ content_tag/,
      :confidence => 0,
811 812
      :file => /test_content_tag\.html\.erb/,
      :relative_path => "app/views/home/test_content_tag.html.erb"
J
Justin Collins 已提交
813 814 815 816 817 818 819 820
  end

  def test_xss_content_tag_attribute_name
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 11,
      :message => /^Unescaped\ cookie\ value\ in\ content_tag/,
      :confidence => 0,
821 822
      :file => /test_content_tag\.html\.erb/,
      :relative_path => "app/views/home/test_content_tag.html.erb"
J
Justin Collins 已提交
823 824 825 826 827 828 829 830
  end

  def test_xss_content_tag_attribute_name_even_with_escape_set
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 17,
      :message => /^Unescaped\ model\ attribute\ in\ content_tag/,
      :confidence => 0,
831 832
      :file => /test_content_tag\.html\.erb/,
      :relative_path => "app/views/home/test_content_tag.html.erb"
J
Justin Collins 已提交
833 834 835 836 837 838 839 840
  end

  def test_cross_site_scripting_escaped_by_default
    assert_no_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 20,
      :message => /^Unescaped\ parameter\ value\ in\ content_tag/,
      :confidence => 0,
841 842
      :file => /test_content_tag\.html\.erb/,
      :relative_path => "app/views/home/test_content_tag.html.erb"
J
Justin Collins 已提交
843 844
  end

J
Justin Collins 已提交
845 846 847 848 849 850 851
  #Uh...maybe this shouldn't be a warning
  def test_cross_site_scripting_in_sanitize_method
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 5,
      :message => /^Unescaped\ parameter\ value/,
      :confidence => 2,
852 853
      :file => /not_used\.html\.erb/,
      :relative_path => "app/views/other/not_used.html.erb"
J
Justin Collins 已提交
854 855
  end

J
Justin Collins 已提交
856 857 858 859 860 861
  def test_xss_content_tag_unescaped_on_purpose
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 23,
      :message => /^Unescaped\ model\ attribute\ in\ content_tag/,
      :confidence => 0,
862 863
      :file => /test_content_tag\.html\.erb/,
      :relative_path => "app/views/home/test_content_tag.html.erb"
J
Justin Collins 已提交
864 865 866 867 868 869 870 871
  end

  def test_xss_content_tag_indirect_body
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 26,
      :message => /^Unescaped\ parameter\ value\ in\ content_tag/,
      :confidence => 1,
872 873
      :file => /test_content_tag\.html\.erb/,
      :relative_path => "app/views/home/test_content_tag.html.erb"
J
Justin Collins 已提交
874 875
  end

876 877 878 879 880
  def test_cross_site_scripting_single_quotes_CVE_2012_3464
    assert_warning :type => :warning,
      :warning_type => "Cross Site Scripting",
      :message => /^All\ Rails\ 2\.x\ versions\ do\ not\ escape\ sin/,
      :confidence => 1,
881 882
      :file => /environment\.rb/,
      :relative_path => "config/environment.rb"
883 884
  end

885 886
  def test_check_send
    assert_warning :type => :warning,
887
      :warning_type => "Dangerous Send",
888 889 890
      :line => 83,
      :message => /\AUser controlled method execution/,
      :confidence => 0,
891 892
      :file => /home_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
893

894 895 896 897 898 899 900 901
    assert_no_warning :type => :warning,
      :warning_code => 23,
      :warning_type => "Dangerous Send",
      :line => 84,
      :message => /^User\ controlled\ method\ execution/,
      :confidence => 0,
      :relative_path => "app/controllers/home_controller.rb"

902
    assert_no_warning :type => :warning,
903
      :warning_type => "Dangerous Send",
904 905 906
      :line => 90,
      :message => /\AUser defined target of method invocation/,
      :confidence => 1,
907 908
      :file => /home_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
909
  end
910 911 912 913 914 915

  def test_strip_tags_CVE_2011_2931
    assert_warning :type => :warning,
      :warning_type => "Cross Site Scripting",
      :message => /^Versions\ before\ 2\.3\.13\ have\ a\ vulnerabil/,
      :confidence => 0,
916 917
      :file => /environment\.rb/,
      :relative_path => "config/environment.rb"
918 919
  end

J
Justin Collins 已提交
920
  def test_strip_tags_CVE_2012_3465_high
921 922 923 924 925
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 3,
      :message => /^Unescaped\ parameter\ value/,
      :confidence => 0,
926 927
      :file => /test_strip_tags\.html\.erb/,
      :relative_path => "app/views/home/test_strip_tags.html.erb"
928
  end
929

J
Justin Collins 已提交
930 931 932
  def test_sql_injection_CVE_2012_5664
    assert_warning :type => :warning,
      :warning_type => "SQL Injection",
933
      :message => /CVE-2012-5664/,
J
Justin Collins 已提交
934
      :confidence => 0,
935 936
      :file => /environment\.rb/,
      :relative_path => "config/environment.rb"
J
Justin Collins 已提交
937 938
  end

939 940 941
  def test_sql_injection_CVE_2013_0155
    assert_warning :type => :warning,
      :warning_type => "SQL Injection",
942
      :message => /CVE-2013-0155/,
943
      :confidence => 0,
944 945
      :file => /environment\.rb/,
      :relative_path => "config/environment.rb"
946 947
  end

J
Justin Collins 已提交
948 949 950 951 952
  def test_remote_code_execution_CVE_2013_0156
    assert_warning :type => :warning,
      :warning_type => "Remote Code Execution",
      :message => /^Rails\ 2\.3\.11\ has\ a\ remote\ code\ execution/,
      :confidence => 0,
953 954
      :file => /environment\.rb/,
      :relative_path => "config/environment.rb"
J
Justin Collins 已提交
955 956
  end

J
Justin Collins 已提交
957 958 959 960 961
  def test_remote_code_execution_CVE_2013_0277
    assert_warning :type => :model,
      :warning_type => "Remote Code Execution",
      :message => /^Serialized\ attributes\ are\ vulnerable\ in\ /,
      :confidence => 0,
962 963
      :file => /unprotected\.rb/,
      :relative_path => "app/models/unprotected.rb"
J
Justin Collins 已提交
964 965
  end

J
Justin Collins 已提交
966 967 968 969 970
  def test_remote_code_execution_CVE_2013_0333
    assert_warning :type => :warning,
      :warning_type => "Remote Code Execution",
      :message => /^Rails\ 2\.3\.11\ has\ a\ serious\ JSON\ parsing\ /,
      :confidence => 0,
971 972
      :file => /environment\.rb/,
      :relative_path => "config/environment.rb"
J
Justin Collins 已提交
973 974
  end

J
Justin Collins 已提交
975 976 977 978 979 980
  def test_xss_sanitize_CVE_2013_1857
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 5,
      :message => /^Rails\ 2\.3\.11\ has\ a\ vulnerability\ in\ sani/,
      :confidence => 0,
981 982
      :file => /not_used\.html\.erb/,
      :relative_path => "app/views/other/not_used.html.erb"
J
Justin Collins 已提交
983 984
  end

J
Justin Collins 已提交
985 986 987 988 989
  def test_denial_of_service_CVE_2013_1854
    assert_warning :type => :warning,
      :warning_type => "Denial of Service",
      :message => /^Rails\ 2\.3\.11\ has\ a\ denial\ of\ service\ vul/,
      :confidence => 1,
990 991
      :file => /environment\.rb/,
      :relative_path => "config/environment.rb"
J
Justin Collins 已提交
992 993
  end

994 995 996 997 998 999 1000 1001 1002 1003
  def test_number_to_currency_CVE_2013_6415
    assert_warning :type => :warning,
      :warning_code => 65,
      :fingerprint => "1822c8179beeb0358b71c545bad0dd824104aed8b995fe0781c1b6e324417a91",
      :warning_type => "Cross Site Scripting",
      :message => /^Rails\ 2\.3\.11\ has\ a\ vulnerability\ in\ numb/,
      :confidence => 1,
      :relative_path => "config/environment.rb"
  end

1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
  def test_sql_injection_CVE_2013_6417
    assert_warning :type => :warning,
      :warning_code => 69,
      :fingerprint => "378978cda99add8404dd38db466f6ffa0b824ea8c57270d98869241a240d12a6",
      :warning_type => "SQL Injection",
      :line => nil,
      :message => /^Rails\ 2\.3\.11\ contains\ a\ SQL\ injection\ vu/,
      :confidence => 0,
      :relative_path => "config/environment.rb",
      :user_input => nil
  end

1016 1017 1018 1019 1020 1021
  def test_to_json
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 3,
      :message => /^Unescaped model attribute in JSON hash/,
      :confidence => 0,
1022 1023 1024
      :file => /test_to_json\.html\.erb/,
      :relative_path => "app/views/home/test_to_json.html.erb"

1025 1026 1027 1028 1029
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 7,
      :message => /^Unescaped parameter value in JSON hash/,
      :confidence => 0,
1030 1031 1032
      :file => /test_to_json\.html\.erb/,
      :relative_path => "app/views/home/test_to_json.html.erb"

1033 1034 1035 1036 1037
    assert_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 11,
      :message => /^Unescaped parameter value in JSON hash/,
      :confidence => 0,
1038 1039 1040
      :file => /test_to_json\.html\.erb/,
      :relative_path => "app/views/home/test_to_json.html.erb"

1041 1042 1043 1044 1045
    assert_no_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 14,
      :message => /^Unescaped parameter value in JSON hash/,
      :confidence => 0,
1046 1047
      :file => /test_to_json\.html\.erb/,
      :relative_path => "app/views/home/test_to_json.html.erb"
1048
  end
J
Justin Collins 已提交
1049

J
Justin Collins 已提交
1050 1051 1052 1053 1054 1055
  def test_xss_with_params_to_i
    assert_no_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 1,
      :message => /^Unescaped\ parameter\ value/,
      :confidence => 0,
1056 1057
      :file => /test_to_i\.html\.erb/,
      :relative_path => "app/views/home/test_to_i.html.erb"
J
Justin Collins 已提交
1058 1059 1060 1061 1062 1063 1064 1065
  end

  def test_xss_with_request_env_to_i
    assert_no_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 5,
      :message => /^Unescaped\ cookie\ value/,
      :confidence => 2,
1066 1067
      :file => /test_to_i\.html\.erb/,
      :relative_path => "app/views/home/test_to_i.html.erb"
J
Justin Collins 已提交
1068 1069 1070 1071 1072 1073 1074 1075
  end

  def test_xss_with_cookie_to_i
    assert_no_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 3,
      :message => /^Unescaped\ request\ value/,
      :confidence => 0,
1076 1077
      :file => /test_to_i\.html\.erb/,
      :relative_path => "app/views/home/test_to_i.html.erb"
J
Justin Collins 已提交
1078
  end
1079 1080 1081 1082 1083 1084 1085

  def test_xss_with_model_attribute_to_i
    assert_no_warning :type => :template,
      :warning_type => "Cross Site Scripting",
      :line => 7,
      :message => /^Unescaped\ model\ attribute/,
      :confidence => 1,
1086 1087
      :file => /test_to_i\.html\.erb/,
      :relative_path => "app/views/home/test_to_i.html.erb"
1088
  end
1089

1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
  def test_cross_site_scripting_unresolved_model_id
    assert_no_warning :type => :template,
      :warning_code => 2,
      :warning_type => "Cross Site Scripting",
      :line => 1,
      :message => /^Unescaped\ model\ attribute/,
      :confidence => 0,
      :file => /_models\.html\.erb/
  end

1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
  def test_cross_site_scripting_in_layout_for_dupe
    assert_warning :type => :template,
      :warning_code => 2,
      :fingerprint => "5d9a5790dbcd6ae68a11e8cdb791a8be9585bf0f75b18ef1f763c6965f55e431",
      :warning_type => "Cross Site Scripting",
      :line => 1,
      :message => /^Unescaped\ parameter\ value/,
      :confidence => 0,
      :relative_path => "app/views/layouts/thing.html.erb"
  end

  def test_cross_site_scripting_in_layout_weak_dupe
    assert_no_warning :type => :template,
      :warning_code => 5,
      :fingerprint => "56fa0dc161d310062ae4717dd70515269b776fe532352e59f72ed2cdc4932153",
      :warning_type => "Cross Site Scripting",
      :line => 1,
      :message => /^Unescaped\ parameter\ value/,
      :confidence => 2,
      :relative_path => "app/views/layouts/thing.html.erb"
  end

1122 1123 1124 1125 1126 1127
  def test_dangerous_send_try
    assert_warning :type => :warning,
      :warning_type => "Dangerous Send",
      :line => 155,
      :message => /^User\ controlled\ method\ execution/,
      :confidence => 0,
1128 1129
      :file => /home_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
1130 1131 1132 1133 1134 1135 1136 1137
  end

  def test_dangerous_send_underscore
    assert_warning :type => :warning,
      :warning_type => "Dangerous Send",
      :line => 156,
      :message => /^User\ controlled\ method\ execution/,
      :confidence => 0,
1138 1139
      :file => /home_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
1140 1141 1142 1143 1144 1145 1146 1147
  end

  def test_dangerous_public_send
    assert_warning :type => :warning,
      :warning_type => "Dangerous Send",
      :line => 157,
      :message => /^User\ controlled\ method\ execution/,
      :confidence => 0,
1148 1149
      :file => /home_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
1150 1151 1152
  end

  def test_dangerous_try_on_user_input
1153
    assert_no_warning :type => :warning,
1154 1155 1156 1157
      :warning_type => "Dangerous Send",
      :line => 160,
      :message => /^User\ defined\ target\ of\ method\ invocation/,
      :confidence => 1,
1158 1159
      :file => /home_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
1160
  end
1161 1162 1163 1164 1165

  def test_unsafe_reflection_constantize
    assert_warning :type => :warning,
      :warning_type => "Remote Code Execution",
      :line => 89,
1166
      :message => /^Unsafe\ reflection\ method\ constantize\ cal/,
1167
      :confidence => 0,
1168 1169
      :file => /home_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
1170 1171 1172 1173 1174 1175

    # This is same call, copied to template
    assert_no_warning :type => :template,
      :warning_code => 24,
      :warning_type => "Remote Code Execution",
      :line => 1,
1176
      :message => /^Unsafe\ reflection\ method\ constantize\ cal/,
1177 1178
      :confidence => 0,
      :relative_path => "app/views/home/test_send_target.html.erb"
1179 1180 1181 1182 1183 1184
  end

  def test_unsafe_reflection_constantize_2
    assert_warning :type => :warning,
      :warning_type => "Remote Code Execution",
      :line => 160,
1185
      :message => /^Unsafe\ reflection\ method\ constantize\ cal/,
1186
      :confidence => 0,
1187 1188
      :file => /home_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
1189
  end
1190 1191 1192 1193 1194 1195 1196 1197

  def test_unsafe_symbol_creation
    [40,41].each do |line|
      assert_warning :type => :warning,
        :warning_type => "Denial of Service",
        :line => line,
        :message => /^Symbol\ conversion\ from\ unsafe\ string/,
        :confidence => 0,
1198 1199
        :file => /application_controller\.rb/,
        :relative_path => "app/controllers/application_controller.rb"
1200 1201 1202 1203 1204 1205 1206 1207 1208
     end
  end

  def test_unsafe_symbol_creation_2
    assert_warning :type => :warning,
      :warning_type => "Denial of Service",
      :line => 83,
      :message => /^Symbol\ conversion\ from\ unsafe\ string/,
      :confidence => 0,
1209 1210
      :file => /home_controller\.rb/,
      :relative_path => "app/controllers/home_controller.rb"
1211 1212 1213 1214 1215 1216 1217 1218
  end

  def test_unsafe_symbol_creation_3
    assert_warning :type => :warning,
      :warning_type => "Denial of Service",
      :line => 29,
      :message => /^Symbol\ conversion\ from\ unsafe\ string/,
      :confidence => 1,
1219 1220
      :file => /application_controller\.rb/,
      :relative_path => "app/controllers/application_controller.rb"
1221 1222
  end

1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
  def test_unsafe_symbol_creation_from_param
    assert_warning :type => :warning,
      :warning_code => 59,
      :fingerprint => "b9c29fc37080f827527feb53f29d618b91d9a5aaac9047383baf46361f08c4cc",
      :warning_type => "Denial of Service",
      :line => 49,
      :message => /^Symbol\ conversion\ from\ unsafe\ string\ \(pa/,
      :confidence => 0,
      :relative_path => "app/controllers/other_controller.rb"
  end

  def test_to_sym_duplicate_as_argument
    assert_no_warning :type => :warning,
      :warning_code => 59,
      :fingerprint => "b9c29fc37080f827527feb53f29d618b91d9a5aaac9047383baf46361f08c4cc",
      :warning_type => "Denial of Service",
      :line => 53,
      :message => /^Symbol\ conversion\ from\ unsafe\ string\ \(pa/,
      :confidence => 0,
      :relative_path => "app/controllers/other_controller.rb"
  end

  def test_to_sym_duplicate_as_target
    assert_no_warning :type => :warning,
      :warning_code => 59,
      :fingerprint => "b9c29fc37080f827527feb53f29d618b91d9a5aaac9047383baf46361f08c4cc",
      :warning_type => "Denial of Service",
      :line => 54,
      :message => /^Symbol\ conversion\ from\ unsafe\ string\ \(pa/,
      :confidence => 0,
      :relative_path => "app/controllers/other_controller.rb"
  end

J
Justin Collins 已提交
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
  def test_ignored_sql_warning
    assert_no_warning :type => :template,
      :warning_code => 0,
      :fingerprint => "f2fa1da45eea252150f6920454822bda3ed5c83a2c376c1296a98037969dd45f",
      :warning_type => "SQL Injection",
      :line => 2,
      :message => /^Possible\ SQL\ injection/,
      :confidence => 0,
      :relative_path => "app/views/other/ignore_me.html.erb"
  end

  def test_ignored_xss_warning
    assert_no_warning :type => :template,
      :warning_code => 2,
      :fingerprint => "6300805e44167e6c3446efbd06b97206928855a2bfc6e1f3e61c097795956b13",
      :warning_type => "Cross Site Scripting",
      :line => 2,
      :message => /^Unescaped\ model\ attribute/,
      :confidence => 0,
      :relative_path => "app/views/other/ignore_me.html.erb"
  end
J
Justin Collins 已提交
1277
end
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289

Rails2WithOptions = BrakemanTester.run_scan "rails2", "Rails 2", :collapse_mass_assignment => false

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

  def expected
    if Brakeman::Scanner::RUBY_1_9
      @expected ||= {
        :controller => 1,
        :model => 4,
1290
        :template => 45,
1291
        :generic => 49 }
1292 1293 1294 1295
    else
      @expected ||= {
        :controller => 1,
        :model => 4,
1296
        :template => 45,
1297
        :generic => 50 }
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
    end
  end

  def report
    Rails2WithOptions
  end

  def test_no_errors
    assert_equal 0, report[:errors].length
  end

  def test_attribute_restriction
    assert_warning :type => :model,
      :warning_type => "Attribute Restriction",
      :warning_code => Brakeman::WarningCodes::Codes[:no_attr_accessible],
      :message => /^Mass assignment is not restricted using /,
      :confidence => 0,
      :file => /account\.rb/
    assert_warning :type => :model,
      :warning_type => "Attribute Restriction",
      :warning_code => Brakeman::WarningCodes::Codes[:no_attr_accessible],
      :message => /^Mass assignment is not restricted using /,
      :confidence => 0,
      :file => /user\.rb/
  end
end