Check Mercury's return path the way the provider kit does

Merged
JA
2d ago
+103-25

Summary

Mercury controller convergence, C2d: Mercury's return_to check now matches the provider kit's, as Lunch Flow's does since C1d (jaysbeekay/sure#733). The old check accepted protocol-relative paths, an open redirect: ?return_to=//evil.example was rendered as the selection dialog's Cancel link, and after a successful link redirect_to raised OpenRedirectError (a 500). MercuryItemsController now includes the kit's SafeReturnPath concern and its own copy goes.

Part of #5003 (Track B, controller-layer convergence for Mercury, decided 2026-11-16).

Stacked on #5250. Fork pre-flight: jaysbeekay/sure#741 (same branch, same commit; its CI, Gatekeeper review and screenshot history live there).

Behaviour delta (Mercury)

return_toBeforeAfter
/accounts?tab=allkeptkept
/accounts (surrounding whitespace)rejectedkept, stripped
//evil.examplekept: Cancel link off the site; a successful link raised OpenRedirectError (500)rejected (fallback path)
/%2Fevil.example, /%2fevil.example, /%5Cevil.examplekeptrejected
/ (bare slash)keptrejected
/\evil.example, /<tab>/evil.example, https://evil.example/...rejectedrejected

What changed

  • MercuryItemsController includes SafeReturnPath; its private safe_return_to_path is deleted.

Demo data

No demo data needed.

Migration notes

No migration.

Verification

  • Tests (fail-first): new MercuryItemsReturnToTest, the 11 cases of Lunch Flow's (jaysbeekay/sure#733) against Mercury's selection dialog and link action. Before the change 7 fail (whitespace, //, three encoded forms, bare slash, and the link's OpenRedirectError); the rest pass before and after as guards.
  • Mutation, caught: Mercury keeping a lax check of its own (8 failures). The concern's own layers were mutated in jaysbeekay/sure#733.
  • Existing tests unedited: MercuryItemsControllerTest passes.
  • bin/rails test (full, at the C2d commit): 16,434 runs, 0 failures; the same 8 ReleaseNotesWorkflowTest errors (no node in the test image).
  • bin/rubocop on changed files: no offenses. bin/brakeman: no warnings.
  • Size: non-test +2 / -25.
  • Checked against upstream AGENTS.md (blob 1a5dbebd, 2026-11-17): no deviations.

Blast radius

  • safe_return_to_path is read in Mercury's select_accounts, link_accounts, select_existing_account and link_existing_account; all treat nil as "use the fallback".
  • Siblings: Sophtron, Wise, Questrade, Redbark and Kraken keep lax copies (listed in jaysbeekay/sure#733), each left for its provider's move.

Summary by cubic

Mercury's return_to validation now matches the provider kit's SafeReturnPath concern, closing an open redirect where ?return_to=//evil.example rendered an off-site Cancel link and a successful link raised OpenRedirectError. MercuryItemsController includes the shared concern and its lax safe_return_to_path copy is removed; absolute, protocol-relative, backslash, percent-encoded, control-character, and bare-slash paths are rejected, and surrounding whitespace is stripped.

  • MercuryItemsReturnToTest covers all 11 return-path cases against every Mercury action that reads return_to: the selection dialogs and link actions, plus triple-slash and control-character cases, each proven by mutation.
  • Sibling providers (Sophtron, Wise, Questrade, Redbark, Kraken) keep their lax copies and are left for their own migration.