feat: opt-in CSRF protection for dashboard actions#40
Open
TakuroOnoda wants to merge 2 commits into
Open
Conversation
Add `config.csrf_protection_enabled` (default false) so hosts with a session store can enable Rails' CSRF protection for the dashboard's destructive POST actions, on par with Sidekiq::Web. When enabled, `verify_authenticity_token` is no longer skipped, all forms embed an authenticity token via the `csrf_token_field` helper, and the layout renders `csrf_meta_tags`. Disabled by default to keep working in session-less / API-only hosts.
Author
|
Hi @vishaltps 👋 Whenever you get a chance, I'd love a review on this PR. |
vishaltps
approved these changes
Jun 23, 2026
vishaltps
left a comment
Owner
There was a problem hiding this comment.
Nice work on this, tested it locally against a session-backed app and the opt-in flow behaves exactly as described. 👍
One small, purely cosmetic note: the two helpers are both conditional on csrf_protection_enabled, but only one carries the _if_enabled suffix- csrf_meta_tags_if_enabled vs csrf_token_field. Would be nice to unify the naming so the pair reads consistently. Either direction works:
- drop the suffix →
csrf_meta_tags_if_enabledbecomescsrf_meta_tags(but that would shadow Rails' built-in helper, so probably not this one), or - keep both explicit → e.g.
csrf_token_field→csrf_token_field_if_enabled.
Given the shadowing concern, leaning toward making both explicit feels cleanest. Totally non-blocking - just a readability nit.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds opt-in CSRF protection for the dashboard's destructive
POSTactions (retry / discard / pause / resume / execute / reject / remove / prune), gated behind a newcsrf_protection_enabledconfig (defaultfalse).Why opt-in (default off)
The gem renders raw HTML forms and doesn't assume a session store (it supports API-only apps).
Rails' CSRF tokens require a session, so enabling it by default would break session-less hosts.
Off by default = fully backward compatible.
Changes
SolidQueueMonitor.csrf_protection_enabled(defaultfalse).skip_before_action :verify_authenticity_tokenis now skipped only when protection is disabled. When enabled, unverified POSTs return422; safe methods pass through.csrf_token_fieldhelper; the layout addscsrf_meta_tags. Both render nothing when disabled.Enable
Requires a session store, same-origin mount, and a non-
api_onlyapp.Thanks for building and maintaining such a clean, lightweight gem.
I've tried to keep this change minimal and fully backward compatible.
Hope it's a useful addition, and I'd love to hear your thoughts. 🙂