fix(security): reject immutable field mutation in runtime updates and eliminate superuser trigger escalation
This commit is contained in:
parent
a97fde2d59
commit
2d41c964d2
4 changed files with 87 additions and 14 deletions
|
|
@ -90,14 +90,17 @@ class TestSignalReentrancyGuard:
|
||||||
],
|
],
|
||||||
"edges": [{"source": "start", "target": "upd"}],
|
"edges": [{"source": "start", "target": "upd"}],
|
||||||
}
|
}
|
||||||
|
from django.contrib.auth.models import User
|
||||||
|
system_user, _ = User.objects.get_or_create(username="reentrancy_owner")
|
||||||
FlowDefinition.objects.create(
|
FlowDefinition.objects.create(
|
||||||
name="SelfUpdateFlow",
|
name="SelfUpdateFlow",
|
||||||
trigger_type="record_updated",
|
trigger_type="record_updated",
|
||||||
trigger_object="Account",
|
trigger_object="Account",
|
||||||
is_active=True,
|
is_active=True,
|
||||||
|
created_by=system_user,
|
||||||
graph=update_self_dag,
|
graph=update_self_dag,
|
||||||
)
|
)
|
||||||
acct = Account.objects.create(name="Original")
|
acct = Account.objects.create(name="Original", owner=system_user)
|
||||||
|
|
||||||
# This must complete without RecursionError or hitting Django's recursion limit
|
# This must complete without RecursionError or hitting Django's recursion limit
|
||||||
import threading
|
import threading
|
||||||
|
|
|
||||||
|
|
@ -244,6 +244,78 @@ class TestTenantAccessIsolation:
|
||||||
acct_a.refresh_from_db()
|
acct_a.refresh_from_db()
|
||||||
assert acct_a.name == 'Alice Unmodified'
|
assert acct_a.name == 'Alice Unmodified'
|
||||||
|
|
||||||
|
def test_flow_cannot_rewrite_primary_key_or_authority_fields(self):
|
||||||
|
from workflows.models import FlowDefinition
|
||||||
|
acct_alice = Account.objects.create(name='Alice Original', owner=self.user_a)
|
||||||
|
acct_bob = Account.objects.create(name='Bob Original', owner=self.user_b)
|
||||||
|
|
||||||
|
# Bob attempts to rewrite primary key to overwrite Alice's account
|
||||||
|
id_rewrite_graph = {
|
||||||
|
'nodes': [
|
||||||
|
{'id': 'start', 'type': 'trigger', 'data': {}},
|
||||||
|
{'id': 'update', 'type': 'update_record', 'data': {
|
||||||
|
'target_model': 'Account',
|
||||||
|
'field_updates': {'id': acct_alice.id, 'name': 'OVERWRITTEN'}
|
||||||
|
}}
|
||||||
|
],
|
||||||
|
'edges': [{'source': 'start', 'target': 'update'}]
|
||||||
|
}
|
||||||
|
flow = FlowDefinition.objects.create(
|
||||||
|
name='Bob ID Rewrite Flow',
|
||||||
|
created_by=self.user_b,
|
||||||
|
graph=id_rewrite_graph
|
||||||
|
)
|
||||||
|
self.client_b.force_authenticate(user=self.user_b)
|
||||||
|
res = self.client_b.post(f'/api/workflows/{flow.id}/execute/', {
|
||||||
|
'context': {'record_id': acct_bob.id, 'record_type': 'Account'}
|
||||||
|
}, format='json')
|
||||||
|
assert res.status_code == 400
|
||||||
|
assert 'Permission denied' in res.data['error']
|
||||||
|
|
||||||
|
# Both Alice's and Bob's accounts must remain completely unchanged
|
||||||
|
acct_alice.refresh_from_db()
|
||||||
|
acct_bob.refresh_from_db()
|
||||||
|
assert acct_alice.name == 'Alice Original'
|
||||||
|
assert acct_alice.owner == self.user_a
|
||||||
|
assert acct_bob.name == 'Bob Original'
|
||||||
|
assert acct_bob.owner == self.user_b
|
||||||
|
|
||||||
|
def test_ownerless_trigger_flow_fails_closed_without_superuser_elevation(self):
|
||||||
|
from workflows.models import FlowDefinition
|
||||||
|
from django.core.exceptions import PermissionDenied
|
||||||
|
acct_alice = Account.objects.create(name='Alice Safe', owner=self.user_a)
|
||||||
|
|
||||||
|
# Create trigger flow with no created_by
|
||||||
|
trigger_graph = {
|
||||||
|
'nodes': [
|
||||||
|
{'id': 'start', 'type': 'trigger', 'data': {}},
|
||||||
|
{'id': 'update', 'type': 'update_record', 'data': {
|
||||||
|
'target_model': 'Account',
|
||||||
|
'field': 'name',
|
||||||
|
'value': 'Trigger Hack'
|
||||||
|
}}
|
||||||
|
],
|
||||||
|
'edges': [{'source': 'start', 'target': 'update'}]
|
||||||
|
}
|
||||||
|
FlowDefinition.objects.create(
|
||||||
|
name='Ownerless Trigger Flow',
|
||||||
|
trigger_type='record_created',
|
||||||
|
trigger_object='Account',
|
||||||
|
is_active=True,
|
||||||
|
created_by=None,
|
||||||
|
graph=trigger_graph
|
||||||
|
)
|
||||||
|
from workflows.dispatcher import dispatch_flow
|
||||||
|
# If dispatched with no user, must raise PermissionDenied and not elevate to superuser
|
||||||
|
with pytest.raises(PermissionDenied):
|
||||||
|
dispatch_flow(
|
||||||
|
FlowDefinition.objects.get(name='Ownerless Trigger Flow'),
|
||||||
|
context={'record_id': acct_alice.id, 'record_type': 'Account'},
|
||||||
|
user=None
|
||||||
|
)
|
||||||
|
acct_alice.refresh_from_db()
|
||||||
|
assert acct_alice.name == 'Alice Safe'
|
||||||
|
|
||||||
def test_idempotency_key_does_not_leak_other_user_execution(self):
|
def test_idempotency_key_does_not_leak_other_user_execution(self):
|
||||||
from workflows.models import FlowDefinition, FlowExecutionLog
|
from workflows.models import FlowDefinition, FlowExecutionLog
|
||||||
dag = {
|
dag = {
|
||||||
|
|
|
||||||
|
|
@ -17,16 +17,6 @@ def _get_active_flow_depth():
|
||||||
def _set_active_flow_depth(val):
|
def _set_active_flow_depth(val):
|
||||||
_thread_locals.active_flow_depth = val
|
_thread_locals.active_flow_depth = val
|
||||||
|
|
||||||
def _get_system_user():
|
|
||||||
from django.contrib.auth.models import User
|
|
||||||
admin = User.objects.filter(is_superuser=True).order_by('id').first()
|
|
||||||
if not admin:
|
|
||||||
admin, _ = User.objects.get_or_create(
|
|
||||||
username='system_internal',
|
|
||||||
defaults={'is_superuser': True, 'is_staff': True}
|
|
||||||
)
|
|
||||||
return admin
|
|
||||||
|
|
||||||
MAX_SIGNAL_REENTRANCY_DEPTH = 1
|
MAX_SIGNAL_REENTRANCY_DEPTH = 1
|
||||||
|
|
||||||
def dispatch_flow(flow, context=None, idempotency_key=None, user=None):
|
def dispatch_flow(flow, context=None, idempotency_key=None, user=None):
|
||||||
|
|
@ -127,9 +117,8 @@ def on_record_post_save(sender, instance, created, **kwargs):
|
||||||
# an unhandled failure in a synchronous record-triggered flow must fail
|
# an unhandled failure in a synchronous record-triggered flow must fail
|
||||||
# the transaction and roll back the triggering record modification.
|
# the transaction and roll back the triggering record modification.
|
||||||
exec_user = getattr(flow, 'created_by', None) or owner
|
exec_user = getattr(flow, 'created_by', None) or owner
|
||||||
# If flow has no owner and record has no owner (e.g. system seed), use system execution identity
|
# If neither flow nor record has an owner, dispatch_flow passes user=None
|
||||||
if not exec_user:
|
# and any attempt to perform protected mutations in execute_flow_dag will fail closed.
|
||||||
exec_user = _get_system_user()
|
|
||||||
dispatch_flow(flow, context=context, user=exec_user)
|
dispatch_flow(flow, context=context, user=exec_user)
|
||||||
finally:
|
finally:
|
||||||
_set_active_flow_depth(depth)
|
_set_active_flow_depth(depth)
|
||||||
|
|
|
||||||
|
|
@ -282,6 +282,15 @@ def execute_flow_dag(flow_or_graph, initial_context=None, idempotency_key=None,
|
||||||
else:
|
else:
|
||||||
inst = qs.first()
|
inst = qs.first()
|
||||||
if inst:
|
if inst:
|
||||||
|
IMMUTABLE_FIELDS = {
|
||||||
|
'id', 'pk', 'owner', 'owner_id', 'created_by', 'created_by_id',
|
||||||
|
'assigned_to', 'assigned_to_id', 'created_at', 'updated_at'
|
||||||
|
}
|
||||||
|
# Check for any attempts to rewrite primary key or ownership/authority fields
|
||||||
|
forbidden_fields = set(updates.keys()) & IMMUTABLE_FIELDS
|
||||||
|
if forbidden_fields:
|
||||||
|
raise PermissionDenied(f"Permission denied: cannot mutate protected authority fields {forbidden_fields}")
|
||||||
|
|
||||||
for f_name, f_val in updates.items():
|
for f_name, f_val in updates.items():
|
||||||
if hasattr(inst, f_name):
|
if hasattr(inst, f_name):
|
||||||
setattr(inst, f_name, f_val)
|
setattr(inst, f_name, f_val)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue