CodeQL flagged a high-severity warning on main: code-scanning alert #60 — rb/csrf-protection-disabled (CWE-352).
Where
app/controllers/application_controller.rb:7
class ApplicationController < ActionController::Base
...
protect_from_forgery
...
end
Why CodeQL flags it
Calling protect_from_forgery with no with: argument downgrades the failure mode to with: :null_session instead of the safer Rails 5+ default of with: :exception. From the CodeQL guidance:
Care should be taken when using the Rails protect_from_forgery method to prevent CSRF. The default behaviour of this method is to null the session when an invalid CSRF token is provided. This may not be sufficient to avoid a CSRF vulnerability — for example if parts of the session are memoized. … Note this remains true even in Rails version 5 and later: these versions automatically run protect_from_forgery with: :exception by default, but manually calling protect_from_forgery with no with argument will still downgrade protection to provide an empty session rather than raise an exception.
See also: Veracode — When Rails' protect_from_forgery Fails.
Suggested fix
One of:
- Remove the explicit call —
ActionController::Base already enables protect_from_forgery with: :exception by default in Rails 5+, so the line on its own is actively weakening the default.
- Or be explicit — change to
protect_from_forgery with: :exception if we want the call to remain for clarity.
Option 1 is the smaller diff; option 2 is more self-documenting. Either resolves the alert.
Verification
CodeQL flagged a high-severity warning on
main: code-scanning alert #60 —rb/csrf-protection-disabled(CWE-352).Where
app/controllers/application_controller.rb:7Why CodeQL flags it
Calling
protect_from_forgerywith nowith:argument downgrades the failure mode towith: :null_sessioninstead of the safer Rails 5+ default ofwith: :exception. From the CodeQL guidance:See also: Veracode — When Rails' protect_from_forgery Fails.
Suggested fix
One of:
ActionController::Basealready enablesprotect_from_forgery with: :exceptionby default in Rails 5+, so the line on its own is actively weakening the default.protect_from_forgery with: :exceptionif we want the call to remain for clarity.Option 1 is the smaller diff; option 2 is more self-documenting. Either resolves the alert.
Verification
app/controllers/application_controller.rbmain