fix(api): require a writable account when creating or preflighting an import
Merged
GF
17d ago+172+172+012345678901234567890123456789-10-10-01234567890123456789
Fixes #2064
Before: POST /api/v1/imports and POST /api/v1/imports/preflight accepted any account_id from the user's family. A member with a read_only share, or with no share at all, could create an import that targets that account. An unknown account_id was even stored on the import as-is.
After:
- The
account_idmust be an account the API user can see. Unknown, malformed, cross-family and unshared ids are answered like a missing account: 422 "Account not found" on create, 404 on preflight. The response does not reveal which accounts exist. - An account the user can see but not write to (a
read_onlyorread_writeshare) gets a 403 fromrequire_api_account_permission!, the same answer the API now gives for transaction update and destroy. - Owners and members with
full_controlsee no change.
Relation to other work
- #3869 (merged) covers transaction update and destroy, and #3786 covers trades and valuations, both with the 403 style used here. The earlier version of this PR also changed those endpoints; that part is removed so the PRs do not overlap.
- #5269 refuses to publish an import into accounts the user cannot write. This PR stops the import from being created or preflighted against such an account in the first place. The two changes touch different lines and work together.
Impact
- API clients using an owner or
full_controlkey: unchanged. - Members with
read_only/read_writeshares or unshared accounts: can no longer create or preflight imports into those accounts through the API. - Syncs, balances, reports, web UI: unchanged. No migrations.
Tests
test/controllers/api/v1/imports_controller_test.rb: create and preflight are refused for an unshared account (422 / 404) and aread_onlyshare (403), and still work with afull_controlshare; unknown and malformed ids create nothing.- These tests fail on current main (the import is created, preflight returns 200) and pass with the change.
- rswag: 403 documented for preflight,
account_iddescriptions updated,docs/api/openapi.yamlregenerated. - Full
bin/rails test, RuboCop and Brakeman pass. No ERB or JS changed.
Generated by Claude Code