Skip to content

Commit 25da904

Browse files
committed
test cases for rb/csrf-protection-disabled
1 parent 4666024 commit 25da904

File tree

10 files changed

+70
-0
lines changed

10 files changed

+70
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| railsapp/app/controllers/users_controller.rb:4:3:4:47 | call to skip_before_action | Potential CSRF vulnerability due to forgery protection being disabled. |
2+
| railsapp/config/application.rb:15:5:15:53 | call to allow_forgery_protection= | Potential CSRF vulnerability due to forgery protection being disabled. |
3+
| railsapp/config/environments/production.rb:5:3:5:51 | call to allow_forgery_protection= | Potential CSRF vulnerability due to forgery protection being disabled. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/security/cwe-352/CSRFProtectionDisabled.ql
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class ApplicationController < ActionController::Base
2+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class UsersController < ApplicationController
2+
3+
# BAD: Disabling forgery protection may open the application to CSRF attacks
4+
skip_before_action :verify_authenticity_token
5+
6+
def change_email
7+
user = User.find_by(name: params[:user_name])
8+
user.email = params[:new_email]
9+
user.save!
10+
end
11+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require_relative 'boot'
2+
3+
require 'rails/all'
4+
5+
# Require the gems listed in Gemfile, including any gems
6+
# you've limited to :test, :development, or :production.
7+
Bundler.require(*Rails.groups)
8+
9+
module Railsapp
10+
class Application < Rails::Application
11+
# Initialize configuration defaults for originally generated Rails version.
12+
config.load_defaults 6.0
13+
14+
# BAD: Disabling forgery protection may open the application to CSRF attacks
15+
config.action_controller.allow_forgery_protection = false
16+
end
17+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Load the Rails application.
2+
require_relative 'application'
3+
4+
# Initialize the Rails application.
5+
Rails.application.initialize!
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Rails.application.configure do
2+
# Settings specified here will take precedence over those in config/application.rb.
3+
4+
# GOOD: disabling CSRF protection in the development environment should not be flagged
5+
config.action_controller.allow_forgery_protection = false
6+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Rails.application.configure do
2+
# Settings specified here will take precedence over those in config/application.rb.
3+
4+
# BAD: Disabling forgery protection may open the application to CSRF attacks
5+
config.action_controller.allow_forgery_protection = false
6+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# The test environment is used exclusively to run your application's
2+
# test suite. You never need to work with it otherwise. Remember that
3+
# your test database is "scratch space" for the test suite and is wiped
4+
# and recreated between test runs. Don't rely on the data there!
5+
6+
Rails.application.configure do
7+
# Settings specified here will take precedence over those in config/application.rb.
8+
9+
# GOOD: disabling CSRF protection in the test environment should not be flagged
10+
config.action_controller.allow_forgery_protection = false
11+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require "test_helper"
2+
3+
class UsersControllerTest < ActiveSupport::TestCase
4+
setup do
5+
# GOOD: disabling CSRF protection in tests should not be flagged
6+
config.action_controller.allow_forgery_protection = false
7+
end
8+
end

0 commit comments

Comments
 (0)