Check Lunch Flow's return path the way the provider kit does
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_to | Before | After |
|---|---|---|
/accounts?tab=all | kept | kept |
/accounts (surrounding whitespace) | rejected | kept, stripped |
//evil.example | kept: Cancel link off the site; a successful link raised OpenRedirectError (500) | rejected (fallback path) |
/%2Fevil.example, /%2fevil.example, /%5Cevil.example | kept | rejected |
/ (bare slash) | kept | rejected |
/\evil.example, /<tab>/evil.example, https://evil.example/... | rejected | rejected |
Brex: unchanged; it already used this check.
What changed
- New
app/controllers/concerns/safe_return_path.rb:safe_return_to_pathandencoded_path_separator?, moved verbatim fromProviderItemsController. ProviderItemsControllerandLunchflowItemsControllerinclude it; Lunch Flow's ownsafe_return_to_pathis deleted.
Demo data
No demo data needed.
Migration notes
No migration.
Verification
- Tests (fail-first): new
LunchflowItemsReturnToTest(11 tests) reads the Cancel link'shreffrom the selection dialog for each row above, and posts a real link withreturn_to=//evil.example, asserting oneAccountProvideris 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.parserejects a raw tab. The//second-character check does not: the host check catches//host, butURI.parseleaves the host nil for///host, so only the second-character check stops it. A///evil.examplecase now pins it (added after full review 6945212463), with return-path cases forlink_accountsandselect_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 8ReleaseNotesWorkflowTesterrors (nonodein the test image).bin/rubocopon 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_pathis read in Lunch Flow'sselect_accounts,link_accounts,select_existing_accountandlink_existing_account, and in the kit controller's same actions; all treatnilas "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_tois a second, regex-based validator inApplicationController. The two differ at the edges (/%2Fis 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_errorpartial declaresreturn_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.