List and open only the imports whose statement the user can view

Merged
DG
11d ago
+663-58

GET /api/v1/imports, /imports/:id and /imports/:id/rows looked imports up by family only. A member with a read key could list a PDF import made from a statement on an account not shared with them, and read the transactions extracted from it. A guest could do the same with a statement that is not linked to any account yet.

The web had the same gap outside the import page: the wizard steps (rows, clean, confirm, upload, configuration, mappings, QIF categories) found the import by family, and the imports list and the pending import on the new import page showed it.

Import.viewable_by(user) now expresses the rule in SQL: an import made from a statement follows AccountStatement.viewable_by(user), and any other import stays visible to the whole family, as before. Import#viewable_by? uses that scope, so the lookup and the check cannot drift. The API controller, ImportsController and the wizard controllers (through a small ViewableImports concern) all look imports up through it. A hidden import answers 404, like an unknown id.

AccountStatement.viewable_by(user) matches AccountStatement#viewable_by?. It replaces the three hand-written copies of that rule in the statements page, GET /api/v1/account_statements and list_account_statements. Those three already require a statement manager, so they return the same rows as before.

import_bank_statement builds a TransactionImport from the statement's transactions. That import now carries the statement, so it is as private as the statement, and the tool refuses a statement the user can view but not manage, as the web does for its import.

Imports not made from a statement keep today's behaviour, including a CSV import into an account the user cannot see. The comment on require_writable_import_accounts! describes that as intended, so this PR leaves it alone.


Summary by cubic

Fixes an access gap where imports were looked up by family only: the imports API and the web wizard could list PDF imports of statements the user can't view, and guests could see unlinked statements.

Import.viewable_by(user) now defines visibility in SQL — statement-backed imports follow AccountStatement.viewable_by(user), other imports stay visible to the family — and every lookup (API index/show/rows, imports list, wizard steps, pending import) goes through it. Hidden imports answer 404. import_bank_statement only offers accounts the user can write to, refuses statements the user can only read, and attaches the statement to the import it creates so the extracted transactions keep the statement's privacy.

A statement can no longer be deleted while an import carries it: the database refuses the DELETE (previously the foreign key nullified the reference, making the import visible to the whole family), and the statements controller reports that refusal like the model's check. The foreign key is added NOT VALID and validated in a separate migration, so the schema lock lasts only for the swap. The financial data reset now destroys imports before statements, and the OpenAPI docs state which imports the API lists and opens.

Summary by CodeRabbit

  • Access Controls
    • Account statements and statement-backed imports are visible only to people with access to the associated statement. Imports without a statement are limited to the viewer’s family.
    • Inaccessible imports and their rows return not found.
    • People who can view a statement but cannot manage it cannot import it.
  • Bug Fixes
    • Statements referenced by imports are protected from deletion, preserving the associated imports and showing a failure notice.
  • Documentation
    • Updated API documentation to describe import visibility and not-found responses.