Skip to content

fix: only follow same-site redirect_to targets - #54

Merged
vishaltps merged 2 commits into
vishaltps:mainfrom
phmunhoz:fix/safe-redirect-to
Nov 28, 2026
Merged

vishaltps merged 2 commits into
vishaltps:mainfrom
phmunhoz:fix/safe-redirect-to

Conversation

@phmunhoz

@phmunhoz phmunhoz commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Retry/Discard (failed jobs), Execute (scheduled jobs) and Pause/Resume (queues) pass params[:redirect_to] straight to redirect_to, so a crafted link or form controls where the user lands after the action:

  • With Rails' open-redirect protection (default since load_defaults 7.0), the user gets a 500 after the action has run.
  • Without it, it's an open redirect to another site. Since CSRF protection is opt-in (csrf_protection_enabled), another site can submit that form for a logged-in user.

Fix

def redirect_to_return_path(fallback)
  target = url_from(params[:redirect_to])
  target = nil unless target&.start_with?("#{request.script_name}/")
  redirect_to(target || fallback)
end
  • url_from (Rails ≥ 7.0) drops other hosts, // and /\ URLs, other schemes, and paths Rails can't parse (/a|b, /café).
  • The mount-point check only follows paths inside the dashboard, which is all the views ever send.
  • Anything else falls back to the page's usual redirect.

Tests

New redirect_to_spec.rb (52 examples) covers each action: one safe path is followed, the unsafe forms fall back, and a mounted engine is tested too. 428 examples, 0 failures; rubocop clean.

Bulk actions redirect to a fixed page and aren't affected; a follow-up will send them back to the page the user came from, using this helper.

@phmunhoz
phmunhoz requested a review from vishaltps as a code owner November 17, 2026 11:51
vishaltps
vishaltps previously approved these changes Nov 24, 2026
@vishaltps
vishaltps merged commit 7817ea3 into vishaltps:main Nov 28, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants