Scope member access in categories and the trade/valuation API

Merged
MA
17d ago
+302-19

Problem

A few lookups used the family as the only boundary, so a family member could reach accounts that were never shared with them. I reported this privately to the maintainers first and am opening the fix here at their request.

  • Creating a category with a transaction_id assigned that category to any family transaction.
  • The category dropdown rendered for any family transaction.
  • API v1 trades and valuations listed, read and changed records on any family account.

The Reports gains card and the linked-transfer check on trades were part of the original version of this PR. Both have since landed on main (#4409 and #3816), so they are no longer in this diff.

Behavior after this change

  • Category create with a transaction_id requires an account the member can annotate; the dropdown requires an accessible account. Both look the transaction up the same way TransactionsController#accessible_transactions does.
  • API v1 trades and valuations load every record through Account.accessible_by, so a record on an account that is not shared with the caller is a 404 (index, show, update, destroy).
  • Updating or deleting a trade, or updating a valuation, on an account the caller can see but not change is a 403, through the shared require_api_account_permission! helper. The caller can already read that record, so a 404 would be misleading.
  • Creating a trade or valuation for an account_id the caller cannot write stays a 404, the same as transactions#create and transfer_account_id.

Owners and members with write access see no change. A read_write share cannot change trades or valuations over the API, including notes, which matches the web app (TradesController and ValuationsController already require write access there).

Stacked on #3869

The API 403 rule uses the helper and the AccountPolicy change from #3869, so this branch is built on top of it. Until #3869 merges, this PR's diff also shows its four commits; the commits specific to this PR are the last three. I will rebase once #3869 lands.

Validation

  • Controller tests for each path: category create (private account and read-only share refused, read_write share accepted), the category dropdown (a private account is a 404; the existing upstream test at dropdowns_controller_test.rb:83 already covers a read-only share still getting the menu, which stays a 200 on purpose) and API trades and valuations (unshared 404, read-only share 403 on update (and destroy for trades), full_control success on update (and destroy for trades), create 404).
  • I broke each new guard once to confirm its test fails, then restored it: removing the write-permission before_action turns the read-only tests red, and loading through all family accounts instead of accessible_by turns the unshared 404 tests red.
  • Swapping annotatable_by for writable_by in category create turns the new read_write test red.
  • Documentation-only rswag 403 responses for trades update/destroy and valuations update; docs/api/openapi.yaml regenerated. The rswag trades and valuations specs now create their account with owner: user, so their examples are not 404s once the owner check applies; rake rswag:specs:swaggerize: 482 examples, 0 failures, and regenerating leaves openapi.yaml unchanged.
  • Rebased on #3869, which sits on current main. Trades update now runs inside Entry.transaction (from #4734); the 403 check runs before it, so a refused update never opens a transaction.
  • bin/rails test: full suite green locally on the rebased branch (16325 runs, 0 failures, 0 errors, 9 skips).
  • bin/rubocop on the changed files: no offenses. bin/brakeman: no warnings.

Not included

API v1 transactions#update and #destroy are handled in #3869.

Summary by CodeRabbit

  • Bug Fixes
    • Trade, valuation, transaction, and category access now respects account-sharing permissions.
    • Listings and lookups exclude records on accounts you can’t access. Read-only access allows viewing; write permissions govern changes, while annotation permissions allow permitted transaction annotations.
    • Requests to change records without the required permission are rejected.
  • Documentation
    • API documentation now describes forbidden responses for changes that shared-account permissions don’t allow.