The personal finance app for everyone (by everyone)
Ruby
fix: keep accessible_by from clobbering destination filters on recurring transactions (#4991)
Chris Werner Rauc8b496ddb107
diff --git a/app/models/recurring_transaction.rb b/app/models/recurring_transaction.rbindex 813d15b9dd..4aa5c98115 100644--- a/app/models/recurring_transaction.rb+++ b/app/models/recurring_transaction.rb@@ -477,12 +477,18 @@ class RecurringTransaction < ApplicationRecord# * its destination_account_id is also accessible OR null (so a recurring# transfer never leaks into the list of a user without access to BOTH# endpoints).- where(account_id: accessible_account_ids)- .or(where(account_id: nil))- .merge(- where(destination_account_id: accessible_account_ids)- .or(where(destination_account_id: nil))- )+ #+ # Written as one raw SQL predicate on purpose. ActiveRecord's+ # `merge` replaces an existing hash predicate on the same column instead of+ # AND-ing it, so a hash-style `where(destination_account_id: ...)` here would+ # silently wipe out a caller's own destination filter when composed through+ # `.merge(RecurringTransaction.accessible_by(user))`.+ # Both endpoints (ignoring NULLs) must be inside the accessible set, so the+ # set is evaluated once instead of once per column.+ where(+ "array_remove(ARRAY[recurring_transactions.account_id, recurring_transactions.destination_account_id], NULL) <@ ARRAY(?)",+ accessible_account_ids+ )}# Class methods for identification and cleanupdiff --git a/test/models/recurring_transaction/paycheck_planner_test.rb b/test/models/recurring_transaction/paycheck_planner_test.rbindex 00af9356be..42d9576529 100644--- a/test/models/recurring_transaction/paycheck_planner_test.rb+++ b/test/models/recurring_transaction/paycheck_planner_test.rb@@ -25,6 +25,22 @@ class RecurringTransaction::PaycheckPlannerTest < ActiveSupport::TestCase"a recurring one-cent transfer is income by sign and a paycheck by nothing"end+ test "recurring transfers between own depository accounts are not bills; debt transfers are" do+ create_series(name: "Paycheck", amount: -2000, due: Date.current + 3, income: true)+ expense = create_series(name: "Rent", amount: 500, due: Date.current + 5)+ to_loan = create_series(name: "Mortgage payment", amount: 300, due: Date.current + 6)+ to_loan.update!(destination_account: accounts(:loan))+ to_savings = create_series(name: "To savings", amount: 700, due: Date.current + 7)+ to_savings.update!(destination_account: accounts(:connected))++ plan = Planner.new(@family, user: @user).plan(periods_limit: 2)+ series_ids = plan.flat_map(&:items).map { |item| item.occurrence.recurring_transaction_id }.uniq++ assert_includes series_ids, expense.id+ assert_includes series_ids, to_loan.id+ assert_not_includes series_ids, to_savings.id+ end+test "periods slice by paycheck and apportion bills to their due dates" dofirst_payday = Date.current + 3create_series(name: "Paycheck", amount: -1840, due: first_payday, preset: "weekly", income: true)diff --git a/test/models/recurring_transaction_test.rb b/test/models/recurring_transaction_test.rbindex af51c1cf6a..975265a3fa 100644--- a/test/models/recurring_transaction_test.rb+++ b/test/models/recurring_transaction_test.rb@@ -1139,6 +1139,45 @@ class RecurringTransactionTest < ActiveSupport::TestCaseassert_not RecurringTransaction.exists?(auto_recurring.id)end+ test "accessible_by hides a transfer whose destination the user cannot access" do+ member = users(:family_member)+ visible = create_transfer_series("To shared", accounts(:credit_card))+ hidden = create_transfer_series("To unshared", accounts(:investment))++ results = @family.recurring_transactions.accessible_by(member)++ assert_includes results, visible+ assert_not_includes results, hidden+ end++ test "accessible_by composes with an earlier destination_account_id filter instead of replacing it" do+ admin = users(:family_admin)+ to_depository = create_transfer_series("To savings", accounts(:connected))+ to_loan = create_transfer_series("Mortgage payment", accounts(:loan))+ expense = create_transfer_series("Rent", nil)++ debt_accounts = Account.where(family_id: @family.id, accountable_type: %w[CreditCard Loan]).select(:id)+ results = RecurringTransaction+ .where(destination_account_id: nil)+ .or(RecurringTransaction.where(destination_account_id: debt_accounts))+ .merge(RecurringTransaction.accessible_by(admin))++ assert_includes results, expense+ assert_includes results, to_loan+ assert_not_includes results, to_depository+ end++ test "accessible_by composes with an earlier id filter" do+ admin = users(:family_admin)+ kept = create_transfer_series("Rent", nil)+ other = create_transfer_series("Gym", nil)++ results = RecurringTransaction.where(id: kept.id).merge(RecurringTransaction.accessible_by(admin))++ assert_equal [ kept ], results.to_a+ assert_not_includes results, other+ end+# Account access scoping teststest "accessible_by scope returns only recurring transactions from accessible accounts" doadmin = users(:family_admin)@@ -1823,6 +1862,14 @@ class RecurringTransactionTest < ActiveSupport::TestCaseprivate+ def create_transfer_series(name, destination)+ @family.recurring_transactions.create!(+ name: name, account: @account, destination_account: destination, amount: 100, currency: "USD",+ expected_day_of_month: 5, last_occurrence_date: Date.current,+ next_expected_date: 5.days.from_now.to_date, status: "active", manual: true+ )+ end+def stale_hint_seriesstale = 45.days.ago.to_dateseries = @family.recurring_transactions.create!(