Enforce account write permissions for provider links
Fork pre-flight for we-promise/sure#3548, which fixes we-promise/sure#3534.
Same branch, same commit. Both PRs are at 9c9f8b9c. They point at the same ref in the same repository, so they move together and cannot diverge. Nothing is cherry-picked. This PR exists so the change carries this fork's CI and Gatekeeper history, which is otherwise invisible from upstream.
Why this is opening late
This PR should have existed when #3548 was raised on 13 September. It did not. The cross-link audit under rule 8 found the gap: #3548's body named its issue but no pre-flight, so it read as correctly linked. I am opening it now rather than leaving the obligation half-met.
It was also blocked by a separate problem. mirror/upstream-main had been stale since 23 September (#227), so this branch compared as 228 files against it instead of its actual 51. With the mirror back at upstream main, the comparison is the same 51-file set as the upstream PR itself.
What the change does
Every provider that can attach an existing account to a connection checked that the account belonged to the family. None checked that the current user could write it. A household member could therefore link a provider feed to an account they have no business modifying.
The fix puts one authorization path behind all of them:
app/controllers/concerns/provider_account_linking.rb(new):require_linkable_account!, used byselect_existing_accountandlink_existing_accountin every provider controller. It also backs the relink dialog check viaAccount.writable_by.- 23 provider controllers routed through it: Akahu, Binance, Brex, Coinbase, CoinSpot, Enable Banking, Fio, IBKR, Indexa, Kraken, Lunchflow, Mercury, Monobank, Plaid, Questrade, Redbark, SimpleFIN, SnapTrade, Sophtron, Trade Republic, Trading212, Up, Wise.
- Setup-form dropdowns in IBKR, Trade Republic, Trading 212 and SnapTrade offer only accounts the user can write, using
.writable_by(Current.user). They post to the guardedlink_existing_account, so without the filter they offered choices the action then refused. test/support/provider_link_authorization_tests.rb(new, 250 lines): one shared test module that each provider includes, so a new provider inherits the cases rather than re-deriving them.test/controllers/provider_link_authorization_coverage_test.rb(new): a guard that fails when a provider controller exposes those actions without calling the shared tests.
The coverage guard deserves a reviewer's attention. It caught Fio, which upstream added after this branch was cut. The guard failed, the gap was real, and a later commit closes it. A coverage test that can actually fail on a provider nobody remembered is the difference between fixing 23 controllers and fixing the class of defect.
Review follow-ups at 9c9f8b9c
Three commits address validated cubic findings. They were pushed as a fast-forward on 0053248d, with no rebase or force-push.
3fcea919Offer only writable accounts in provider setup link forms. Adds the setup-form filter above in all four controllers. It also adds an optionalsetup_url:to the shared tests, which asserts two things:- Owned and
full_controlaccounts are offered. - No-share,
read_onlyandread_writeaccounts are not.
- Owned and
597630a5Use the shared member-account helper in Sophtron tests.- Replaces the duplicate
sophtron_member_accountwith the sharedprovider_link_member_account, which asserts that the share took effect. - Drops
response.bodyassertions that followed a redirect. That body is empty, so they could not fail.
- Replaces the duplicate
9c9f8b9cStop a comment satisfying the provider link coverage guard. The guard now requires the call to open a line, so a comment naming it no longer counts.
The cubic thread claiming the shared tests cannot pass for Plaid was a false positive. The two tests it names are generated only with relinks: true, and Plaid does not pass it.
Verification
Fail-first and mutation checks:
- Setup forms: all 4 new setup-form tests failed before the fix, each on a refused account's
<option>being present. Swappingwritable_byforaccessible_byin Trade Republic makes the test fail again. - Sophtron helper: I stubbed
Account#share_with!to do nothing.- With the old Sophtron helper, the
read_writerefusal test still passed, for the wrong reason (no share at all). - With the shared helper, it fails with
Expected: :read_write, Actual: nil.
- With the old Sophtron helper, the
- Coverage guard: the new matcher test failed against the old substring check. Commenting out Fio's call now makes the guard report
["fio_items"].
Local runs:
- The 23 provider controller test files, the coverage test and
transfer_matches: 587 runs, 0 failures. rubocopon the changed files: clean.brakeman: no warnings.
Diff against mirror/upstream-main: 51 files, +1,042/−91.
CI:
- At the previous head
0053248d, fork CI was green on all sixci/*jobs, and upstream CI was green includingsecurity-scan. - The fork and upstream runs at
9c9f8b9care this PR's and #3548's current check runs; read the result there. security-scandoes not run on this PR.pipelock.ymlfires only forpull_requesttomain, and this PR's base ismirror/upstream-main.
Codacy: owner decision required
Codacy reports action_required, with 6 findings (3 critical / 3 high) against a "≤ 0 issues" gate. They sit on exactly 3 lines, and each line is flagged by two rules: "calling find(...) with user-controlled input" and "unscoped find(...) with user-controllable input".
app/controllers/questrade_items_controller.rb:196app/controllers/questrade_items_controller.rb:217app/controllers/sophtron_items_controller.rb:1035
Each line is Current.family.accounts.find(params[:account_id]). That lookup is family-scoped through the association, not unscoped, and each is followed at once by require_linkable_account!. The same expression appears 33 times across 18 controllers on the base; Codacy flags these three because they are new lines.
There is no .codacy.yml in the repository, and upstream does not use Codacy. A config file on this branch would ride upstream, so none was added. The options:
- Mark the 6 findings as false positives in app.codacy.com.
- Relax the gate or the two rules for this repository.
- Accept
action_requiredon this pre-flight.
Note on the base
CodeRabbit skipped this PR because it is a draft. #3548 carries CodeRabbit's review upstream, which is where this change lands.
Review follow-up
Pushed on 2026-09-29 with the owner's go-ahead: ef523362f on top of 269c7398. This branch is the head of we-promise/sure#3548, so that PR carries the same commit (same branch, same commit).
Behaviour change: family members who hold read_write on a shared account (what auto_share_with_family! grants) can no longer link a provider to it or relink one away from it. Only the owner or a full_control holder can. They see a not_authorized flash; the owner can grant full_control to restore it.
Correction to the sections above. The live head is 269c7398, not 9c9f8b9c. On 2026-09-26 at 23:58 -07:00, upstream maintainer Juan José Mata rebased the branch onto upstream main 186f97c9 (the committer on all 14 PR commits). As a result, 3fcea919, 597630a5 and 9c9f8b9c are now 476ebce9, f8f27f36 and 269c7398. The PR's own change is unchanged. git patch-id --stable gives 66f8fbae… both for 7cc6aa01..9c9f8b9c and for 186f97c9..269c7398: 51 files, +1,042/−91. Only two PR files differ in content, trade_republic_items_controller{,_test}.rb, and both differences come from upstream #3738 in the new base.
| Finding (source) | Status |
|---|---|
read_write members lose the ability to link providers, and operators are not told (PRR 1, MEDIUM, MUST VERIFY 2) | Added to this body (the Behaviour change paragraph above); #3548's body is upstream and left to the owner. The line: "Behaviour change: family members who hold read_write on a shared account (what auto_share_with_family! grants) can no longer link a provider to it or relink one away from it. Only the owner or a full_control holder can. They see a not_authorized flash; the owner can grant full_control to restore it." |
The PR's recorded base (45ab75ae) is stale, so the page shows 111 files instead of 51 (PRR 2, MUST VERIFY 1; Gatekeeper 2026-09-27 item 3) | Addressed by the push, which re-records the base. 269c7398 merges cleanly into the current mirror 1052443a (git merge-tree), and the three-dot diff is the 51-file set. |
| SimpleFIN stale-account move and delete are gated by role only (PRR 3, LOW, SHOULD IMPROVE 3) | Fixed in ef523362f, folded into #3548 as recommended (same authorisation class); the owner can still split it out. The gap was wider than reported: delete destroys the stale account, and move writes into the target and then destroys the source, both with no account check. Both now require writable_account_ids on the stale account, and move also requires it on the target. A refusal counts as a stale-action error through the existing flash. The setup form offers only writable move targets. Six new tests: refusal for read_write on delete, on a move's source and on a move's target; the allowed full_control delete and move; and the form's options on both sides. Fail-first: the 3 refusal tests failed before the fix (Account.count dropped by 1), and the form test failed with the read_write option present. Mutations: removing the source check was caught by 2 tests; removing the target check by 1; accessible_by in place of writable_by by 2 in the controller and 1 in the form. |
AccountsController#select_provider offers providers without the write check (PRR 4, LOW) | Declined here: out of scope. It leads only to the guarded actions, so a user is offered the link and then refused. The same class is open as #211. |
No-Referer fallback in redirect_target_url (PRR 5, LOW) | No action. The reviewer confirmed path is always app-generated (accounts_path). |
Codacy action_required (PRR 6; Gatekeeper items, all three passes) | OWNER DECISION. Options are unchanged: mark as false positives in app.codacy.com, relax the gate, or accept it. There are now 7 annotations at 269c7398: the 6 already described, plus a gitleaks "generic-api-key" warning at test/models/trade_republic_client_test.rb:821 (the constant PRICE_BACKFILL_ATTEMPTED_AT_KEY). That file is not in this PR's 51-file diff. It shows only because of the stale recorded base (upstream #3669/#3738). Codacy is the only non-green check, so it is what makes mergeable_state unstable. |
Codacy at ef523362: new warning "redirect uses user input" at snaptrade_items_controller.rb:505; the gitleaks warning is gone now the push re-recorded the base | False positive, recorded. Line 505 is redirect_to account_path(account): a route helper for an account found through Current.family.accounts.find_by, with no user-supplied URL. It is flagged only because this PR re-indents the block (the same line is on mirror/upstream-main). The 6 remaining annotations (3 critical, 3 high: "unscoped find with user-controllable input") are on lines this PR adds: questrade_items_controller.rb:196 and :217 and sophtron_items_controller.rb:1035. They are false positives: each is Current.family.accounts.find(params[:account_id]), scoped to the signed-in family, and is followed immediately by the require_linkable_account! write-access check this PR introduces. Codacy stays action_required until they are marked false positives in app.codacy.com (owner action) |
| Still a draft (Gatekeeper 2026-09-27 item 1) | Done: marked ready for review on 2026-09-29, once CI was green, per the owner's instruction. |
security-scan does not run on this PR | Already accepted as not applicable. pipelock.yml fires only for PRs to main. |
| cubic threads (5) | All resolved. The four valid ones were fixed in what are now 476ebce9, f8f27f36 and 269c7398. The Plaid thread was a false positive. |
Verification at ef523362f (Ruby 3.3.6, locally). Ran every controller test that includes the shared provider-link module, the coverage test, transfer_matches and accounts: 667 runs, 0 failures. Rubocop is clean on the changed files. Brakeman reports no warnings.
Head 87eb8330: two commits since ef523362f
This section was added after the 2026-09-29 14:41 Gatekeeper re-gate, which approved 87eb8330 and pointed out that the body stopped at ef523362f. The diff against mirror/upstream-main is now 51 files, +1,243/−99. The earlier +1,042/−91 figures describe older heads.
21e28dd7Merge upstream/main. There were two conflicts, both in the Trade Republic controller and its test, where upstream refactored the lines this branch guards:- Upstream moved the accountable-type check into
TradeRepublicAccount#linkable_to?. The resolution keeps it, with this branch'srequire_linkable_account!running first. - Upstream made
@linkable_accountsa local, grouped per provider account. The resolution keeps the local and applies this branch's.writable_by(Current.user)to it.
- Upstream moved the accountable-type check into
87eb8330Authorize the holder the relink actually moves, under the lock. This is a security fix.- The problem:
require_relinkable_provider_account!read the provider's account mapping before the provider row was locked. A concurrent request could re-point the provider in between. The relink would then move, and in several providers queue for deletion, an account the requester holds no write permission on. - The fix: every relinking path now runs inside
ProviderAccountLinking#relinking. It opens a transaction, runslock!on the provider row, re-reads the holders, and authorizes each one for:writebefore the mutation. A refusal raisesActiveRecord::Rollback, so nothing moves and noDestroyJobis enqueued. - Where it applies: Binance, Coinbase, Enable Banking, SimpleFIN and SnapTrade. SnapTrade previously had no lock at all.
- What stays: the early
require_relinkable_provider_account!check remains as a cheap refusal before any work. It is not what protects the mutation. - Test: one shared test covers all five providers. It changes the holder to an unwritable account and has the pre-lock check return true, then asserts the request refuses, leaves the mapping alone and enqueues no
DestroyJob. Removing the in-lock authorization makes it fail for all five.
- The problem:
- CI at
87eb8330: all sixci/*jobs pass. Codacy is stillaction_requiredfor the same false positives described above; that remains the owner's decision. cubic shows neutral, because its monthly limit is reached until 1 October. - we-promise/sure#3548 is the same branch, so it carries both commits too. Its body is upstream and left to the owner.