Require write permission for API v1 transaction update and destroy
Merged
MA
18d ago+286+286+012345678901234567890123456789-21-21-01234567890123456789
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
showis unchanged (any access reads).destroyneeds owner or full control, else 403.updateby owner or full control is unchanged. Aread_writemember may send onlynotes,category_id,merchant_idandtag_ids(theAccountShareannotate attribute lists the web bulk edit already uses); anything else (amount, name, description, date, currency, nature, user_modified) gets a 403. Aread_onlymember 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 throughshow, 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
transactionobject 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
AccountPolicyalready described the write and annotate tiers but nothing called it. It is now the shared tier predicate forrequire_account_permission!and the API helper: the annotate tier comes fromAccountShare::ANNOTATE_PERMISSIONS, andpermits?(level)maps:write,:annotateand:ownerfor 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 returnsfalseunless the credentials' owner passes that level; it returnstrueotherwise. Withresource: "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 passresource:;Api::V1::SplitsControlleruses 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
AccountPolicyTestcovers owner, full_control, read_write, read_only and no share for both predicates, pluspermits?(:owneris 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_modifiedrefused; 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:owneror unknown levels, the helper always passing, the helper ignoringresource: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.yamlregenerated (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
updatestill run after the permission check, so an annotate-only change from aread_writemember 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.