fix(api): require a writable account when creating or preflighting an import

Merged
GF
17d ago
+172-10

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_id must 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_only or read_write share) gets a 403 from require_api_account_permission!, the same answer the API now gives for transaction update and destroy.
  • Owners and members with full_control see 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_control key: unchanged.
  • Members with read_only / read_write shares 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 a read_only share (403), and still work with a full_control share; 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_id descriptions updated, docs/api/openapi.yaml regenerated.
  • Full bin/rails test, RuboCop and Brakeman pass. No ERB or JS changed.

Generated by Claude Code