Check Lunch Flow's return path the way the provider kit does

Merged
JA
2d ago
+125-56

Summary

Lunch Flow controller convergence, C1d: Lunch Flow's return_to check now matches the provider kit's. 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 ActionController::Redirecting::OpenRedirectError (a 500). The kit's check moves out of ProviderItemsController into a SafeReturnPath concern that both controllers include, so the later conversion stays a pure move.

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

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

Behaviour delta (Lunch Flow)

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

Brex: unchanged; it already used this check.

What changed

  • New app/controllers/concerns/safe_return_path.rb: safe_return_to_path and encoded_path_separator?, moved verbatim from ProviderItemsController.
  • ProviderItemsController and LunchflowItemsController include it; Lunch Flow's own safe_return_to_path is deleted.

Demo data

No demo data needed.

Migration notes

No migration.

Verification

  • Tests (fail-first): new LunchflowItemsReturnToTest (11 tests) reads the Cancel link's href from the selection dialog for each row above, and posts a real link with return_to=//evil.example, asserting one AccountProvider is created, the success notice, and the redirect to /accounts. Before the change 7 fail: the whitespace, //, three encoded and bare-slash cases, and the link (OpenRedirectError). The kept-path, backslash, control-character and absolute-URL cases pass before and after, as guards.
  • Mutations, each caught: no whitespace strip; the encoded-separator check gone; encoded backslash allowed; bare slash allowed; Lunch Flow keeping a lax check of its own; the second-character and host checks both gone. The control-character check survives alone because URI.parse rejects a raw tab. The // second-character check does not: the host check catches //host, but URI.parse leaves the host nil for ///host, so only the second-character check stops it. A ///evil.example case now pins it (added after full review 6945212463), with return-path cases for link_accounts and select_existing_account.
  • Existing tests unedited: LunchflowItemsControllerTest, the Lunch Flow cache test and Brex's controller and provider-kit tests pass (107 runs, 0 failures).
  • bin/rails test (full, at the C1d commit): 16,351 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 +42 / -56 (the concern is a move out of the kit controller).
  • Checked against upstream AGENTS.md (blob 1a5dbebd, 2026-11-17): no deviations.

Blast radius

  • safe_return_to_path is read in Lunch Flow's select_accounts, link_accounts, select_existing_account and link_existing_account, and in the kit controller's same actions; all treat nil as "use the fallback".
  • Siblings with their own copies (grep -n "def safe_return_to_path" app/controllers): Mercury converges in its own slice (same flaw). Sophtron, Wise, Questrade, Redbark and Kraken have lax copies; Up, Akahu, Monobank, Fio and Coinspot have strict copies. All are left for their provider's move.

Review notes

A code review of the diff before commit raised nine points. Taken: the link test now proves the success path ran (an AccountProvider is created and the success notice is shown), and the encoded backslash, lowercase encoding, control character and bare slash are pinned. Noted, not changed:

  • StoreLocation#safe_return_to is a second, regex-based validator in ApplicationController. The two differ at the edges (/%2F is accepted there). Consolidating them changes the kit's behaviour too, so it is a follow-up rather than part of this convergence.
  • /%09/evil (an encoded tab after the slash) passes, as it does on the kit today. Rails keeps it as a literal path, so it doesn't redirect off the site; worth tightening with the consolidation above.
  • Lunch Flow's _api_error partial declares return_path: but never renders it, so the hardened value is unused on the error path. This predates the change, and the selection convergence (C1e) replaces that code.

Summary by cubic

Lunch Flow's return_to check now goes through the same SafeReturnPath concern as the provider kit, closing an open redirect where protocol-relative paths like //evil.example rendered an off-site Cancel link and made successful account links raise OpenRedirectError (a 500). The concern moves out of ProviderItemsController and was tightened to match the strict check other provider controllers use.

Behavior changes for Lunch Flow: surrounding whitespace is stripped, and bare slashes, backslashes, percent-encoded separators (/%2F, /%5C), malformed percent-encoding, non-routing paths, and protocol-relative inputs including ///evil.example are rejected along with absolute URLs. Tests now pin every action that reads safe_return_to_path, including fallback redirects for link_accounts and select_existing_account. Brex is unaffected: its return_to values come from route helpers, so they always route.