From 348bc6e5198a6b4d77611bf62e9120085749c501 Mon Sep 17 00:00:00 2001 From: Herculino Trotta Date: Sat, 14 Nov 2026 03:01:49 -0300 Subject: [PATCH] fix(transactions): scope installment plan and recurring transaction lookups InstallmentPlan and RecurringTransaction declare the unfiltered all_objects manager first and set no default_manager_name, so get_object_or_404(Model, id=...) bypassed account visibility. Any logged-in user could read, refresh, pause, finish or delete another user's plans (and their transactions) by id. Resolve the nine per-object views through the filtered objects manager, as the list views, the API and the transaction views already do. Refs GHSA-vcr4-g82g-wcq8 --- app/apps/transactions/tests/test_views.py | 60 +++++++++++++++++++ .../transactions/views/installment_plans.py | 8 +-- .../views/recurring_transactions.py | 10 ++-- 3 files changed, 69 insertions(+), 9 deletions(-) diff --git a/app/apps/transactions/tests/test_views.py b/app/apps/transactions/tests/test_views.py index 8a9f00db..660111b5 100644 --- a/app/apps/transactions/tests/test_views.py +++ b/app/apps/transactions/tests/test_views.py @@ -380,3 +380,63 @@ class TransactionConvertViewTests(TestCase): self.assertEqual( self._get("transaction_convert_to_installment_plan").status_code, 404 ) + + +@override_settings( + STORAGES={ + "default": {"BACKEND": "django.core.files.storage.FileSystemStorage"}, + "staticfiles": {"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage"}, + }, + WHITENOISE_AUTOREFRESH=True, +) +class InstallmentRecurringIsolationTests(TestCase): + """Per-object plan views must 404 for objects on another user's private account.""" + + def setUp(self): + User = get_user_model() + owner = User.objects.create_user(email="owner@test.com", password="pw") + User.objects.create_user(email="other@test.com", password="pw") + self.client.login(username="other@test.com", password="pw") + + currency = Currency.objects.create(code="USD", name="US Dollar", decimal_places=2) + account = Account.all_objects.create(name="Private", currency=currency, owner=owner) + self.plan = InstallmentPlan.all_objects.create( + account=account, + type=Transaction.Type.EXPENSE, + description="Owner plan", + number_of_installments=3, + start_date=date(2025, 1, 1), + installment_amount=100, + ) + self.recurring = RecurringTransaction.all_objects.create( + account=account, + type=Transaction.Type.EXPENSE, + amount=50, + description="Owner recurring", + start_date=date(2025, 1, 1), + recurrence_type=RecurringTransaction.RecurrenceType.MONTH, + recurrence_interval=1, + ) + + def test_other_user_gets_404(self): + cases = [ + ("get", "installment_plan_transactions", "installment_plan_id", self.plan), + ("get", "installment_plan_edit", "installment_plan_id", self.plan), + ("get", "installment_plan_refresh", "installment_plan_id", self.plan), + ("delete", "installment_plan_delete", "installment_plan_id", self.plan), + ("get", "recurring_transaction_transactions", "recurring_transaction_id", self.recurring), + ("get", "recurring_transaction_edit", "recurring_transaction_id", self.recurring), + ("get", "recurring_transaction_toggle_pause", "recurring_transaction_id", self.recurring), + ("get", "recurring_transaction_finish", "recurring_transaction_id", self.recurring), + ("delete", "recurring_transaction_delete", "recurring_transaction_id", self.recurring), + ] + for method, name, kwarg, obj in cases: + with self.subTest(name=name): + url = reverse(name, kwargs={kwarg: obj.id}) + response = getattr(self.client, method)(url, HTTP_HX_REQUEST="true") + self.assertEqual(response.status_code, 404) + + self.assertTrue(InstallmentPlan.all_objects.filter(id=self.plan.id).exists()) + self.recurring.refresh_from_db() + self.assertFalse(self.recurring.is_paused) + self.assertIsNone(self.recurring.end_date) diff --git a/app/apps/transactions/views/installment_plans.py b/app/apps/transactions/views/installment_plans.py index 5f1fd192..c89b9cd5 100644 --- a/app/apps/transactions/views/installment_plans.py +++ b/app/apps/transactions/views/installment_plans.py @@ -66,7 +66,7 @@ def finished_installment_plans_list(request): @login_required @require_http_methods(["GET"]) def installment_plan_transactions(request, installment_plan_id): - installment_plan = get_object_or_404(InstallmentPlan, id=installment_plan_id) + installment_plan = get_object_or_404(InstallmentPlan.objects, id=installment_plan_id) transactions = installment_plan.transactions.prefetch_related("sub_transactions").order_by("reference_date", "id") return render( @@ -106,7 +106,7 @@ def installment_plan_add(request): @login_required @require_http_methods(["GET", "POST"]) def installment_plan_edit(request, installment_plan_id): - installment_plan = get_object_or_404(InstallmentPlan, id=installment_plan_id) + installment_plan = get_object_or_404(InstallmentPlan.objects, id=installment_plan_id) if request.method == "POST": form = InstallmentPlanForm(request.POST, instance=installment_plan) @@ -134,7 +134,7 @@ def installment_plan_edit(request, installment_plan_id): @login_required @require_http_methods(["GET"]) def installment_plan_refresh(request, installment_plan_id): - installment_plan = get_object_or_404(InstallmentPlan, id=installment_plan_id) + installment_plan = get_object_or_404(InstallmentPlan.objects, id=installment_plan_id) installment_plan.update_transactions() messages.success(request, _("Installment Plan refreshed successfully")) @@ -151,7 +151,7 @@ def installment_plan_refresh(request, installment_plan_id): @login_required @require_http_methods(["DELETE"]) def installment_plan_delete(request, installment_plan_id): - installment_plan = get_object_or_404(InstallmentPlan, id=installment_plan_id) + installment_plan = get_object_or_404(InstallmentPlan.objects, id=installment_plan_id) installment_plan.delete() diff --git a/app/apps/transactions/views/recurring_transactions.py b/app/apps/transactions/views/recurring_transactions.py index bca4fb18..e41386e7 100644 --- a/app/apps/transactions/views/recurring_transactions.py +++ b/app/apps/transactions/views/recurring_transactions.py @@ -88,7 +88,7 @@ def finished_recurring_transactions_list(request): @require_http_methods(["GET"]) def recurring_transaction_transactions(request, recurring_transaction_id): recurring_transaction = get_object_or_404( - RecurringTransaction, id=recurring_transaction_id + RecurringTransaction.objects, id=recurring_transaction_id ) transactions = recurring_transaction.transactions.collapse_transfers().prefetch_related("sub_transactions").order_by( "reference_date", "id" @@ -132,7 +132,7 @@ def recurring_transaction_add(request): @require_http_methods(["GET", "POST"]) def recurring_transaction_edit(request, recurring_transaction_id): recurring_transaction = get_object_or_404( - RecurringTransaction, id=recurring_transaction_id + RecurringTransaction.objects, id=recurring_transaction_id ) if request.method == "POST": @@ -162,7 +162,7 @@ def recurring_transaction_edit(request, recurring_transaction_id): @require_http_methods(["GET"]) def recurring_transaction_toggle_pause(request, recurring_transaction_id): recurring_transaction = get_object_or_404( - RecurringTransaction, id=recurring_transaction_id + RecurringTransaction.objects, id=recurring_transaction_id ) current_paused = recurring_transaction.is_paused recurring_transaction.is_paused = not current_paused @@ -201,7 +201,7 @@ def recurring_transaction_toggle_pause(request, recurring_transaction_id): @require_http_methods(["GET"]) def recurring_transaction_finish(request, recurring_transaction_id): recurring_transaction: RecurringTransaction = get_object_or_404( - RecurringTransaction, id=recurring_transaction_id + RecurringTransaction.objects, id=recurring_transaction_id ) today = timezone.localdate(timezone.now()) - relativedelta(days=1) @@ -227,7 +227,7 @@ def recurring_transaction_finish(request, recurring_transaction_id): @require_http_methods(["DELETE"]) def recurring_transaction_delete(request, recurring_transaction_id): recurring_transaction = get_object_or_404( - RecurringTransaction, id=recurring_transaction_id + RecurringTransaction.objects, id=recurring_transaction_id ) recurring_transaction.delete()