The personal finance app for everyone (by everyone)
Ruby
Limit import_bank_statement to imports and accounts the user can reach (#5695)
David Gil0089985d90e9
diff --git a/app/controllers/imports_controller.rb b/app/controllers/imports_controller.rbindex 87de110c18..cc7b2a3552 100644--- a/app/controllers/imports_controller.rb+++ b/app/controllers/imports_controller.rb@@ -188,7 +188,7 @@ class ImportsController < ApplicationControllerprivatedef set_import@import = Current.family.imports.includes(:account, :account_statement).find(params[:id])- raise ActiveRecord::RecordNotFound if @import.account_statement.present? && [email protected]_statement.viewable_by?(Current.user)+ raise ActiveRecord::RecordNotFound unless @import.viewable_by?(Current.user)enddef import_paramsdiff --git a/app/models/assistant/function/import_bank_statement.rb b/app/models/assistant/function/import_bank_statement.rbindex 3981ee5526..7f58847684 100644--- a/app/models/assistant/function/import_bank_statement.rb+++ b/app/models/assistant/function/import_bank_statement.rb@@ -55,7 +55,7 @@ class Assistant::Function::ImportBankStatement < Assistant::Functionenddef call(params = {})- pdf_import = family.imports.find_by(id: params["pdf_import_id"], type: "PdfImport")+ pdf_import = find_pdf_import(params["pdf_import_id"])unless pdf_importreturn {@@ -80,17 +80,17 @@ class Assistant::Function::ImportBankStatement < Assistant::Functionsuccess: false,error: "account_required",message: "Please specify which account to import transactions into",- available_accounts: family.accounts.visible.depository.map { |a| { id: a.id, name: a.name } }+ available_accounts: account_choices}end- account = family.accounts.find_by(id: params["account_id"])+ account = writable_accounts.find_by(id: params["account_id"]) if valid_uuid?(params["account_id"])unless accountreturn {success: false,error: "account_not_found",- message: "Account not found",- available_accounts: family.accounts.visible.depository.map { |a| { id: a.id, name: a.name } }+ message: "No writable account matched that account_id",+ available_accounts: account_choices}end@@ -167,6 +167,22 @@ class Assistant::Function::ImportBankStatement < Assistant::Functionendprivate+ # Same visibility as the web's import page: another member's private+ # statement is not found, so it is neither sent to the model nor echoed back.+ def find_pdf_import(id)+ pdf_import = family.imports.find_by(id: id, type: "PdfImport")+ pdf_import if pdf_import&.viewable_by?(user)+ end++ # The import writes transactions into the account, so the user needs write+ # access to it, as the web requires to publish and the API to create.+ def writable_accounts+ family.accounts.writable_by(user)+ end++ def account_choices+ writable_accounts.visible.depositories.map { |a| { id: a.id, name: a.name } }+ end# The refusal every AI gate on this path returns: the router's reason and its message.def ai_unavailable(availability)diff --git a/app/models/import.rb b/app/models/import.rbindex 94bf09df2e..cf05b971e2 100644--- a/app/models/import.rb+++ b/app/models/import.rb@@ -494,6 +494,12 @@ class Import < ApplicationRecordFAMILY_CONFIG_TYPES.include?(type)end+ # A statement-backed import is as private as its statement; any other import+ # is visible to the whole family.+ def viewable_by?(user)+ account_statement.blank? || account_statement.viewable_by?(user)+ end+# Guests may view family configuration but not change it, so they may only# edit an import that doesn't write family configuration and isn't being# (or already) published.diff --git a/test/controllers/mcp_controller_test.rb b/test/controllers/mcp_controller_test.rbindex 6df8b0ae1c..b7c1a63cd9 100644--- a/test/controllers/mcp_controller_test.rb+++ b/test/controllers/mcp_controller_test.rb@@ -868,6 +868,28 @@ class McpControllerTest < ActionDispatch::IntegrationTestendend+ test "tools/call import_bank_statement offers and accepts only accounts the user can write to" do+ use_ai_routes+ @user = users(:family_member)+ pdf_import = PdfImport.create!(family: @user.family, document_type: "bank_statement")+ Provider::Registry.expects(:preferred_llm_provider).never++ with_mcp_env do+ post "/mcp", params: jsonrpc_request("tools/call", { name: "import_bank_statement", arguments: { pdf_import_id: pdf_import.id } }).to_json,+ headers: mcp_headers(@token)+ listed = JSON.parse(JSON.parse(response.body).dig("result", "content", 0, "text"))+ assert_equal "account_required", listed["error"]+ assert_not_includes listed["available_accounts"].map { |a| a["id"] }, accounts(:connected).id++ assert_no_difference "Import.count" do+ post "/mcp", params: jsonrpc_request("tools/call", { name: "import_bank_statement", arguments: { pdf_import_id: pdf_import.id, account_id: accounts(:connected).id } }).to_json,+ headers: mcp_headers(@token)+ end+ refused = JSON.parse(JSON.parse(response.body).dig("result", "content", 0, "text"))+ assert_equal "account_not_found", refused["error"]+ end+ end+# -- JSON-RPC id preservation --test "preserves request id in successful response" dodiff --git a/test/models/assistant/function/import_bank_statement_test.rb b/test/models/assistant/function/import_bank_statement_test.rbindex 5bdd6f5163..5faf68d25d 100644--- a/test/models/assistant/function/import_bank_statement_test.rb+++ b/test/models/assistant/function/import_bank_statement_test.rb@@ -38,4 +38,101 @@ class Assistant::Function::ImportBankStatementTest < ActiveSupport::TestCaseassert_equal "not_configured", result[:reason]assert result[:message].present?, "the refusal carries the reason worded for the user"end++ # --- Accounts: the ones the user can write to, as on the web and the API ---++ test "without an account it lists the depository accounts the user can write to" do+ member = users(:family_member)+ pdf_import = PdfImport.create!(family: member.family, document_type: "bank_statement")++ result = Assistant::Function::ImportBankStatement.new(member).call("pdf_import_id" => pdf_import.id)++ assert_equal "account_required", result[:error]+ ids = result[:available_accounts].map { |a| a[:id] }+ assert_includes ids, accounts(:depository).id+ assert_not_includes ids, accounts(:connected).id, "listed another member's private account"+ end++ test "an unknown account id is refused with the same list" do+ member = users(:family_member)+ pdf_import = PdfImport.create!(family: member.family, document_type: "bank_statement")++ result = Assistant::Function::ImportBankStatement.new(member).call(+ "pdf_import_id" => pdf_import.id, "account_id" => SecureRandom.uuid+ )++ assert_equal "account_not_found", result[:error]+ ids = result[:available_accounts].map { |a| a[:id] }+ assert_includes ids, accounts(:depository).id+ assert_not_includes ids, accounts(:connected).id+ end++ test "another member's private account is refused before any extraction" do+ member = users(:family_member)+ pdf_import = PdfImport.create!(family: member.family, document_type: "bank_statement")+ use_ai_routes+ Provider::Registry.expects(:preferred_llm_provider).never++ assert_no_difference -> { Import.count } do+ result = Assistant::Function::ImportBankStatement.new(member).call(+ "pdf_import_id" => pdf_import.id, "account_id" => accounts(:connected).id+ )++ assert_equal "account_not_found", result[:error]+ end+ end++ test "an account shared read-only is refused" do+ member = users(:family_member)+ read_only = accounts(:credit_card)+ assert_equal :read_only, read_only.permission_for(member)+ pdf_import = PdfImport.create!(family: member.family, document_type: "bank_statement")+ use_ai_routes+ Provider::Registry.expects(:preferred_llm_provider).never++ result = Assistant::Function::ImportBankStatement.new(member).call(+ "pdf_import_id" => pdf_import.id, "account_id" => read_only.id+ )++ assert_equal "account_not_found", result[:error]+ end++ test "an account shared with full control goes on to extraction" do+ use_ai_routes+ member = users(:family_member)+ pdf_import = PdfImport.create!(family: member.family, document_type: "bank_statement")+ Provider::Registry.stubs(:preferred_llm_provider).returns(nil)++ result = Assistant::Function::ImportBankStatement.new(member).call(+ "pdf_import_id" => pdf_import.id, "account_id" => accounts(:depository).id+ )++ assert_equal "ai_unavailable", result[:error]+ end++ # The web answers 404 for a statement-backed import the user cannot view, so+ # the statement is neither sent to the model nor summarised back.+ test "an import of a statement the user cannot view is not found" do+ member = users(:family_member)+ statement = AccountStatement.create_from_upload!(+ family: member.family,+ account: accounts(:investment),+ file: uploaded_file(+ filename: "statement.pdf",+ content_type: "application/pdf",+ content: file_fixture("imports/sample_bank_statement.pdf").binread+ )+ )+ pdf_import = PdfImport.create_from_statement!(statement: statement)+ pdf_import.update!(document_type: "bank_statement")+ assert_not statement.viewable_by?(member)+ use_ai_routes+ Provider::Registry.expects(:preferred_llm_provider).never++ result = Assistant::Function::ImportBankStatement.new(member).call(+ "pdf_import_id" => pdf_import.id, "account_id" => accounts(:depository).id+ )++ assert_equal "PDF import not found", result[:error]+ endenddiff --git a/test/models/import_test.rb b/test/models/import_test.rbindex bec5e5da42..4eccb24236 100644--- a/test/models/import_test.rb+++ b/test/models/import_test.rb@@ -245,4 +245,20 @@ class ImportTest < ActiveSupport::TestCaseassert_equal [ "'+44 Taxis", "+44 Taxis" ], csv["name"]end++ test "an import is viewable by the family unless its statement is private" do+ member = users(:family_member)+ assert TransactionImport.create!(family: member.family).viewable_by?(member)++ statement = AccountStatement.create_from_upload!(+ family: member.family,+ account: accounts(:investment),+ file: uploaded_file(filename: "statement.pdf", content_type: "application/pdf",+ content: file_fixture("imports/sample_bank_statement.pdf").binread)+ )+ pdf_import = PdfImport.create_from_statement!(statement: statement)++ assert pdf_import.viewable_by?(users(:family_admin))+ assert_not pdf_import.viewable_by?(member)+ endend