Skip to content

Commit c1a51d9

Browse files
committed
Ruby: add test for protect_from_forgery without exception strategy
1 parent d09f48e commit c1a51d9

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
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/development.rb:5:3:5:51 | call to allow_forgery_protection= | Potential CSRF vulnerability due to forgery protection being disabled. |
4-
| railsapp/config/environments/production.rb:5:3:5:51 | call to allow_forgery_protection= | Potential CSRF vulnerability due to forgery protection being disabled. |
1+
| railsapp/app/controllers/application_controller.rb:5:3:5:22 | call to protect_from_forgery | Potential CSRF vulnerability due to forgery protection being disabled or weakened. |
2+
| railsapp/app/controllers/users_controller.rb:4:3:4:47 | call to skip_before_action | Potential CSRF vulnerability due to forgery protection being disabled or weakened. |
3+
| railsapp/config/application.rb:15:5:15:53 | call to allow_forgery_protection= | Potential CSRF vulnerability due to forgery protection being disabled or weakened. |
4+
| railsapp/config/environments/development.rb:5:3:5:51 | call to allow_forgery_protection= | Potential CSRF vulnerability due to forgery protection being disabled or weakened. |
5+
| railsapp/config/environments/production.rb:5:3:5:51 | call to allow_forgery_protection= | Potential CSRF vulnerability due to forgery protection being disabled or weakened. |
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,20 @@
11
class ApplicationController < ActionController::Base
2+
3+
# BAD: `protect_from_forgery` without `with: :exception` can expose an
4+
# application to CSRF attacks in some circumstances
5+
protect_from_forgery
6+
7+
before_action authz_guard
8+
9+
def current_user
10+
@current_user ||= User.find_by_id(session[:user_id])
11+
end
12+
13+
def logged_in?
14+
!current_user.nil?
15+
end
16+
17+
def authz_guard
18+
render(plain: "not logged in") unless logged_in?
19+
end
220
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class ArticlesController < ApplicationController
2+
prepend_before_action :user_authored_article?, only: [:delete_authored_article]
3+
4+
def delete_authored_article
5+
article.destroy
6+
end
7+
8+
def article
9+
@article ||= Article.find(params[:article_id])
10+
end
11+
12+
def user_authored_article?
13+
@article.author_id = current_user.id
14+
end
15+
end

ruby/ql/test/query-tests/security/cwe-352/railsapp/app/controllers/users_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class UsersController < ApplicationController
44
skip_before_action :verify_authenticity_token
55

66
def change_email
7-
user = User.find_by(name: params[:user_name])
7+
user = current_user
88
user.email = params[:new_email]
99
user.save!
1010
end

0 commit comments

Comments
 (0)