Skip to content

Use protect_from_forgery with: :exception#6926

Merged
compwron merged 1 commit intorubyforgood:mainfrom
mvanhorn:security/csrf-with-exception-6920
May 5, 2026
Merged

Use protect_from_forgery with: :exception#6926
compwron merged 1 commit intorubyforgood:mainfrom
mvanhorn:security/csrf-with-exception-6920

Conversation

@mvanhorn
Copy link
Copy Markdown
Contributor

@mvanhorn mvanhorn commented May 3, 2026

What github issue is this PR for, if any?

Resolves #6920

What changed, and why?

app/controllers/application_controller.rb:7 was calling protect_from_forgery with no with: argument, which downgrades the failure mode to with: :null_session -- weaker than the Rails 5+ default of with: :exception that ActionController::Base applies on its own. CodeQL flagged this as alert #60 (rb/csrf-protection-disabled, CWE-352).

This PR takes option 2 from the issue: keep the call but make the strategy explicit. That resolves the alert while leaving a self-documenting CSRF setting in place rather than relying on the framework default.

-  protect_from_forgery
+  protect_from_forgery with: :exception

How is this tested? (please write rspec and jest tests!) 💖💪

No new spec is added. The change is the recommended fix from a CodeQL static-analysis alert and brings the call in line with the Rails 5+ default ActionController::Base would otherwise apply, so existing controller / request / system specs continue to exercise the CSRF flow with the same effective strategy. CodeQL alert #60 should auto-close after the next scan on main.

Screenshots please :)

n/a -- this is a one-line application_controller.rb change with no UI surface.

CodeQL alert rubyforgood#60 (rb/csrf-protection-disabled, CWE-352): calling
protect_from_forgery with no with: argument downgrades the failure
mode to with: :null_session, weaker than the Rails 5+ default of
with: :exception that ActionController::Base would otherwise apply.

Make the strategy explicit so the call no longer weakens the default,
matching option 2 from the issue (more self-documenting than removing
the call entirely).
@github-actions github-actions Bot added the ruby Touches Ruby code label May 3, 2026
@compwron compwron merged commit 63311c5 into rubyforgood:main May 5, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ruby Touches Ruby code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CodeQL: weakened CSRF protection in ApplicationController (rb/csrf-protection-disabled)

2 participants