Class: Scryer::Rules::CsrfProtectionRule
- Inherits:
-
Scryer::Rule
- Object
- Scryer::Rule
- Scryer::Rules::CsrfProtectionRule
- Defined in:
- lib/scryer/rules/csrf_protection_rule.rb
Overview
Flags a controller class named *Controller that calls
skip_before_action :verify_authenticity_token without that same file
(or, best-effort, without any protect_from_forgery call visible in it)
— skipping CSRF verification on a controller that isn't clearly API-only
(no < ActionController::API / ActionController::Base used alongside
explicit null_session) is a common way to accidentally disable CSRF
protection app-wide for that controller's actions.
Instance Attribute Summary
Attributes inherited from Scryer::Rule
Instance Method Summary collapse
Methods inherited from Scryer::Rule
Constructor Details
This class inherits a constructor from Scryer::Rule
Instance Method Details
#scan ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/scryer/rules/csrf_protection_rule.rb', line 16 def scan findings = [] Ast.each_node(sexp) do |node| next unless Ast.tagged?(node, :class) class_name = Ast.ident_text(node[1].is_a?(Array) ? node[1][1] : nil) next unless class_name.to_s.end_with?("Controller") body = node[3] skip_node = find_skip_verify(body) next unless skip_node has_null_session_pattern = each_descendant_call_names(body).any? do |name| name == "protect_from_forgery" end next if has_null_session_pattern # they've explicitly configured an alternative line = Ast.line_of(skip_node) findings << finding( line: line, message: "`#{class_name}` skips CSRF token verification (`skip_before_action " \ ":verify_authenticity_token`) without declaring its own " \ "`protect_from_forgery` policy — if this controller renders any HTML forms " \ "or is reachable with a browser session cookie, this leaves it open to " \ "cross-site request forgery.", suggested_fix: "If this is a true JSON/API-only controller, make that explicit with " \ "`protect_from_forgery with: :null_session` (or inherit from a base " \ "class that does) rather than bypassing verification silently. If it's " \ "not API-only, remove the `skip_before_action` and let the app's normal " \ "CSRF handling apply." ) end findings end |