The personal finance app for everyone (by everyone)
Ruby
fix(imports): require account write access to publish or revert an import (#3998)
GFR435cba3fb0e9
diff --git a/app/controllers/imports_controller.rb b/app/controllers/imports_controller.rbindex 3569561682..6a322c4f03 100644--- a/app/controllers/imports_controller.rb+++ b/app/controllers/imports_controller.rb@@ -3,6 +3,7 @@ class ImportsController < ApplicationControllerbefore_action :set_import, only: %i[show update publish destroy revert apply_template cancel summary]before_action :require_statement_import_permission!, only: %i[update publish destroy revert apply_template cancel]+ before_action :require_writable_import_accounts!, only: %i[publish revert]def update# Handle both pdf_import[account_id] and import[account_id] param formats@@ -179,6 +180,15 @@ class ImportsController < ApplicationControllerredirect_back_or_to redirect_target, alert: t("accounts.not_authorized")end+ # Imports are family-scoped, so a member can reach an import configured by+ # someone else, or one whose target account was unshared since. Publishing+ # or reverting writes into its accounts, so require write access to all.+ def require_writable_import_accounts!+ return if @import.accounts_writable_by?(Current.user)++ redirect_back_or_to import_path(@import), alert: t("accounts.not_authorized")+ end+def create_pdf_import(file)return redirect_to new_import_path, alert: t("accounts.not_authorized") unless AccountStatement.statement_manager?(Current.user)return redirect_to new_import_path, alert: t("imports.create.pdf_too_large", max_size: Import::MAX_PDF_SIZE / 1.megabyte) if file.size > Import::MAX_PDF_SIZEdiff --git a/app/models/import.rb b/app/models/import.rbindex 731a0a06d8..4ea1f277b3 100644--- a/app/models/import.rb+++ b/app/models/import.rb@@ -472,6 +472,27 @@ class Import < ApplicationRecordcomplete? || revert_failed?end+ # Whether the user may write to every pre-existing account this import+ # targets (its own account, account mappings) or has already written into+ # (entries). Accounts the import created itself are excluded.+ def accounts_writable_by?(user)+ ids = [ account_id ]+ ids += mappings.accounts.where.not(mappable_id: nil).pluck(:mappable_id)+ ids += entries.distinct.pluck(:account_id)+ ids += preexisting_target_account_ids+ ids = ids.compact.uniq - accounts.pluck(:id)+ return true if ids.empty?+ return false if user.nil?++ (ids - family.accounts.writable_by(user).where(id: ids).pluck(:id)).empty?+ end++ # Existing family accounts the import resolves on its own at publish time+ # (e.g. by name). Subclasses that do this override it.+ def preexisting_target_account_ids+ []+ end+def has_unassigned_account?mappings.accounts.where(key: "").any?enddiff --git a/app/models/qif_import.rb b/app/models/qif_import.rbindex fd52edcc1c..efbf47f739 100644--- a/app/models/qif_import.rb+++ b/app/models/qif_import.rb@@ -102,6 +102,16 @@ class QifImport < Importqif_accounts.any?end+ # Embedded QIF accounts and per-row account names are matched to existing+ # family accounts by name when publishing (see find_or_create_qif_account).+ def preexisting_target_account_ids+ names = qif_accounts.map(&:name) + rows.where.not(account: [ nil, "" ]).distinct.pluck(:account)+ names = names.compact_blank.uniq+ return [] if names.empty?++ family.accounts.where(name: names).pluck(:id)+ end+# Unique categories used across all rows (blank entries excluded).def row_categories(rows.distinct.pluck(:category) + selected_split_categories).reject(&:blank?).uniq.sortdiff --git a/test/controllers/imports_controller_test.rb b/test/controllers/imports_controller_test.rbindex 3593ff56e3..26438b898d 100644--- a/test/controllers/imports_controller_test.rb+++ b/test/controllers/imports_controller_test.rb@@ -401,6 +401,78 @@ class ImportsControllerTest < ActionDispatch::IntegrationTestassert_redirected_to import_path(import)end+ test "member cannot publish import targeting a read only shared account" do+ import = imports(:transaction)+ import.update!(account: accounts(:credit_card))+ TransactionImport.any_instance.expects(:publish_later).never++ sign_in users(:family_member)+ post publish_import_url(import)++ assert_redirected_to import_path(import)+ assert_equal I18n.t("accounts.not_authorized"), flash[:alert]+ end++ test "member cannot publish import with account mapping to an unshared account" do+ import = imports(:transaction)+ import.mappings.create!(type: "Import::AccountMapping", key: "Brokerage", mappable: accounts(:investment))+ TransactionImport.any_instance.expects(:publish_later).never++ sign_in users(:family_member)+ post publish_import_url(import)++ assert_redirected_to import_path(import)+ assert_equal I18n.t("accounts.not_authorized"), flash[:alert]+ end++ test "member can publish import targeting a full control shared account" do+ import = imports(:transaction)+ import.update!(account: accounts(:depository))+ import.mappings.create!(type: "Import::AccountMapping", key: "Checking", mappable: accounts(:depository))+ TransactionImport.any_instance.expects(:publish_later).once++ sign_in users(:family_member)+ post publish_import_url(import)++ assert_redirected_to import_path(import)+ assert_equal I18n.t("imports.publish.started"), flash[:notice]+ end++ test "member cannot revert import whose entries are in a read only shared account" do+ import = imports(:transaction)+ import.update!(status: :complete)+ entries(:transaction).update!(import: import, account: accounts(:credit_card))+ TransactionImport.any_instance.expects(:revert_later).never++ sign_in users(:family_member)+ put revert_import_url(import)++ assert_redirected_to import_path(import)+ assert_equal I18n.t("accounts.not_authorized"), flash[:alert]+ end++ test "member cannot publish QIF import whose embedded account matches a read only shared account" do+ import = qif_import_with_embedded_account(accounts(:credit_card).name)+ QifImport.any_instance.expects(:publish_later).never++ sign_in users(:family_member)+ post publish_import_url(import)++ assert_redirected_to import_path(import)+ assert_equal I18n.t("accounts.not_authorized"), flash[:alert]+ end++ test "member can publish QIF import whose embedded account is new" do+ import = qif_import_with_embedded_account("Brand New QIF Account")+ QifImport.any_instance.expects(:publish_later).once++ sign_in users(:family_member)+ post publish_import_url(import)++ assert_redirected_to import_path(import)+ assert_equal I18n.t("imports.publish.started"), flash[:notice]+ end+test "destroys import" doimport = imports(:transaction)@@ -591,4 +663,23 @@ class ImportsControllerTest < ActionDispatch::IntegrationTestassert_select 'select[name="import[account_id]"] option', text: "IOU (personal debt to friend)", count: 0assert_select 'select[name="import[account_id]"] option', text: "Plaid Depository Account", count: 0end++ private+ def qif_import_with_embedded_account(name)+ import = QifImport.create!(family: families(:dylan_family))+ import.update!(raw_file_str: <<~QIF)+ !Account+ N#{name}+ TBank+ ^+ !Type:Bank+ D1/ 1'24+ U-25.00+ T-25.00+ PCoffee Shop+ ^+ QIF+ import.generate_rows_from_csv+ import.reload+ endend