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

JA
6d 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 (#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 jaysbeekay/sure#670 (Track B, controller-layer convergence for Mercury, owner 2026-11-16). Stacked on B8a (jaysbeekay/sure#740) in fork stack #687.

Upstream draft PR: https://github.com/we-promise/sure/pull/5251 (same branch, same commit), in native stack we-promise/sure#5268 on refactor/provider-kit.

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 (#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 #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 #733), each left for its provider's move.