fix(rules): only run rules owned by or shared with the acting user

Rule selection went through SharedObjectManager, which includes every
user's public rules, so a public rule ran against other users'
transactions. Public rules stay readable by everyone; shared rules keep
running for the users they are shared with.

Fixes GHSA-hjfv-p3g6-mwch
This commit is contained in:
Herculino Trotta committed 2026-11-20 20:16:38 -03:00
1 parent f558d52ac6
commit c0eca55489
2 files changed
+65 -4

No files matched your search

+11 -4
View File
@@ -12,6 +12,7 @@ from typing import Literal
from cachalot.api import cachalot_disabled
from dateutil.relativedelta import relativedelta
from django.contrib.auth import get_user_model
from django.db.models import Q
from django.forms import model_to_dict
from procrastinate.contrib.django import app
from simpleeval import EvalWithCompoundTypes
@@ -676,23 +677,29 @@ def check_for_transaction_rules(
if signal == "transaction_updated" and old_data:
simple.names.update(_get_names(old_data, "old_"))
# Public rules are readable by everyone but only run for their owner
# and the users they are explicitly shared with.
runnable_rules = TransactionRule.all_objects.filter(
Q(owner=user) | Q(shared_with=user) | Q(owner=None)
).distinct()
# Select rules based on the signal type
if dry_run and rule_id:
rules = TransactionRule.objects.filter(id=rule_id)
elif signal == "transaction_created":
rules = TransactionRule.objects.filter(
rules = runnable_rules.filter(
active=True, on_create=True
).order_by("order", "id")
elif signal == "transaction_updated":
rules = TransactionRule.objects.filter(
rules = runnable_rules.filter(
active=True, on_update=True
).order_by("order", "id")
elif signal == "transaction_deleted":
rules = TransactionRule.objects.filter(
rules = runnable_rules.filter(
active=True, on_delete=True
).order_by("order", "id")
else:
rules = TransactionRule.objects.filter(active=True).order_by(
rules = runnable_rules.filter(active=True).order_by(
"order", "id"
)
+54
View File
@@ -228,3 +228,57 @@ class SubTransactionRuleTests(TransactionTestCase):
list(copy.sub_transactions.values_list("name", "amount")),
[("A", Decimal("3")), ("Fee", Decimal("1"))],
)
class RuleScopeTests(TransactionTestCase):
def setUp(self):
User = get_user_model()
self.rule_owner = User.objects.create_user(email="owner@example.com", password="x")
self.other = User.objects.create_user(email="other@example.com", password="x")
currency = Currency.objects.create(code="USD", name="US Dollar", decimal_places=2)
self.account = Account.all_objects.create(
name="Other's account", currency=currency, owner=self.other
)
def _run_rule(self, **rule_kwargs):
rule = TransactionRule.all_objects.create(
active=True,
on_update=True,
name="Rewrite",
trigger="True",
owner=self.rule_owner,
**rule_kwargs,
)
TransactionRuleAction.objects.create(
rule=rule, field="description", value="'Rewritten'"
)
with patch("apps.rules.signals.check_for_transaction_rules.defer"):
transaction = Transaction.userless_all_objects.create(
account=self.account,
type=Transaction.Type.EXPENSE,
amount=Decimal("10.00"),
date=date(2026, 5, 4),
reference_date=date(2026, 5, 1),
description="Original",
owner=self.other,
)
return rule, transaction
def _trigger(self, transaction):
with patch("apps.rules.signals.check_for_transaction_rules.defer"):
run_check_for_transaction_rules_without_worker_wrapper(
instance_id=transaction.id,
user_id=self.other.id,
signal="transaction_updated",
)
transaction.refresh_from_db()
return transaction.description
def test_public_rule_does_not_run_on_other_users_transactions(self):
_, transaction = self._run_rule(visibility="public")
self.assertEqual(self._trigger(transaction), "Original")
def test_shared_rule_runs_for_users_it_is_shared_with(self):
rule, transaction = self._run_rule()
rule.shared_with.add(self.other)
self.assertEqual(self._trigger(transaction), "Rewritten")