Require write permission for API v1 transaction update and destroy

Merged
MA
18d ago
+286-21

Problem

Api::V1::TransactionsController#set_transaction scopes show, update and destroy with Account.accessible_by. A family member who only has read_only (or read_write) access to a shared account can therefore PATCH or DELETE its transactions over the API. The web UI already blocks this: full control changes anything, read_write only annotates (notes, category, merchant, tags), and read_only changes nothing (TransactionsController#permitted_entry_params).

This is the same class of gap as #3786 fixes for trades and valuations, and it was noted as out of scope there.

Behavior after this change

  • show is unchanged (any access reads).
  • destroy needs owner or full control, else 403.
  • update by owner or full control is unchanged. A read_write member may send only notes, category_id, merchant_id and tag_ids (the AccountShare annotate attribute lists the web bulk edit already uses); anything else (amount, name, description, date, currency, nature, user_modified) gets a 403. A read_only member always gets a 403.
  • A refused request is a 403 with error: forbidden, not a silently trimmed update, so an API client can tell nothing was applied. The record is visible to that user through show, so 404 would be misleading here.
  • A read_write client that echoes unchanged non-annotate fields (name, amount) along with its change gets a 403, because those keys are in the permitted set.
  • A request with no transaction object now returns 400 (it was a 500).
  • Keys outside the permitted transaction params (for example account_id) are ignored on update for every role, as before. They cannot change anything, so they do not turn an annotate-only request into a 403.

How it is built

  • AccountPolicy already described the write and annotate tiers but nothing called it. It is now the shared tier predicate for require_account_permission! and the API helper: the annotate tier comes from AccountShare::ANNOTATE_PERMISSIONS, and permits?(level) maps :write, :annotate and :owner for controllers. Each check evaluates one tier, so an update or destroy makes at most one indexed share lookup, and none for owners.
  • AccountAuthorizable#require_account_permission! delegates to the policy; its web rendering is unchanged.
  • Api::V1::BaseController#require_api_account_permission!(account, level = :write, resource: nil) renders { error: "forbidden", message: } with a 403 and returns false unless the credentials' owner passes that level; it returns true otherwise. With resource: "transaction" the message is "You do not have permission to delete|change this transaction" (verb from the action); without it, it is "You do not have write access to this account". Transactions pass resource:; Api::V1::SplitsController uses the default, so its message is unchanged. Trades and valuations in #3786 reuse this helper, so visible-but-not-writable is a 403 across the API.

Validation

  • New AccountPolicyTest covers owner, full_control, read_write, read_only and no share for both predicates, plus permits? (:owner is owner-only, unknown levels and strings are denied).
  • API transaction tests cover read_only, read_write (category, merchant and tags allowed; amount, name, description, date and user_modified refused; a { notes, account_id } payload applies the note and leaves the account alone; delete refused) and full_control.
  • I broke each guard in turn (update? admitting read_write, annotate? using the write tier, permits? mishandling :owner or unknown levels, the helper always passing, the helper ignoring resource: or its delete/change verb, the annotate-only check always true, destroy treated as annotate) and each one turned at least one of the new tests red.
  • rswag docs-only 403 responses added for update and destroy, openapi.yaml regenerated (13 lines).
  • Rebased onto current main. Full bin/rails test: 16318 runs, 0 failures, 0 errors, 9 skips. RuboCop and Brakeman clean.
  • Upstream's newer tag, category and merchant ownership checks in update still run after the permission check, so an annotate-only change from a read_write member still has its ids checked.

Summary by CodeRabbit

  • Bug Fixes
    • Transaction updates and deletions now follow shared-account permissions. Read-only members cannot modify transactions; read-write members can update notes and selected details but cannot change financial or identifying fields or delete transactions. Members with full control retain broader access.
    • API requests that exceed account permissions now receive a 403 response, including attempts to write to shared accounts without the required access.
  • Documentation
    • Added API documentation for forbidden responses when shared-account permissions do not allow transaction updates or deletion.