fix(security): lock authority-bearing fields, enforce fail-closed runtime checks, and scope Activity mutations
This commit is contained in:
parent
eb3a85586a
commit
a97fde2d59
7 changed files with 103 additions and 12 deletions
|
|
@ -42,7 +42,7 @@ class AccountSerializer(OwnerScopedRelationMixin, CustomFieldsValidationMixin, s
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Account
|
model = Account
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
read_only_fields = ['id', 'created_at', 'updated_at']
|
read_only_fields = ['id', 'owner', 'created_at', 'updated_at']
|
||||||
|
|
||||||
|
|
||||||
class ContactSerializer(OwnerScopedRelationMixin, CustomFieldsValidationMixin, serializers.ModelSerializer):
|
class ContactSerializer(OwnerScopedRelationMixin, CustomFieldsValidationMixin, serializers.ModelSerializer):
|
||||||
|
|
@ -51,7 +51,7 @@ class ContactSerializer(OwnerScopedRelationMixin, CustomFieldsValidationMixin, s
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Contact
|
model = Contact
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
read_only_fields = ['id', 'created_at', 'updated_at']
|
read_only_fields = ['id', 'owner', 'created_at', 'updated_at']
|
||||||
|
|
||||||
|
|
||||||
class LeadSerializer(OwnerScopedRelationMixin, CustomFieldsValidationMixin, serializers.ModelSerializer):
|
class LeadSerializer(OwnerScopedRelationMixin, CustomFieldsValidationMixin, serializers.ModelSerializer):
|
||||||
|
|
@ -61,7 +61,7 @@ class LeadSerializer(OwnerScopedRelationMixin, CustomFieldsValidationMixin, seri
|
||||||
model = Lead
|
model = Lead
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
read_only_fields = [
|
read_only_fields = [
|
||||||
'id', 'is_converted', 'converted_date',
|
'id', 'owner', 'is_converted', 'converted_date',
|
||||||
'converted_account', 'converted_contact',
|
'converted_account', 'converted_contact',
|
||||||
'converted_opportunity', 'created_at', 'updated_at'
|
'converted_opportunity', 'created_at', 'updated_at'
|
||||||
]
|
]
|
||||||
|
|
@ -73,14 +73,14 @@ class OpportunitySerializer(OwnerScopedRelationMixin, CustomFieldsValidationMixi
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Opportunity
|
model = Opportunity
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
read_only_fields = ['id', 'expected_revenue', 'created_at', 'updated_at']
|
read_only_fields = ['id', 'owner', 'expected_revenue', 'created_at', 'updated_at']
|
||||||
|
|
||||||
|
|
||||||
class ActivitySerializer(OwnerScopedRelationMixin, serializers.ModelSerializer):
|
class ActivitySerializer(OwnerScopedRelationMixin, serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Activity
|
model = Activity
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
read_only_fields = ['id', 'completed_at', 'created_at', 'updated_at']
|
read_only_fields = ['id', 'assigned_to', 'completed_at', 'created_at', 'updated_at']
|
||||||
|
|
||||||
|
|
||||||
class LeadConvertSerializer(serializers.Serializer):
|
class LeadConvertSerializer(serializers.Serializer):
|
||||||
|
|
|
||||||
|
|
@ -33,14 +33,14 @@ class CustomObjectDefinitionSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = CustomObjectDefinition
|
model = CustomObjectDefinition
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
read_only_fields = ['id', 'created_at', 'updated_at']
|
read_only_fields = ['id', 'created_by', 'created_at', 'updated_at']
|
||||||
|
|
||||||
|
|
||||||
class CustomRecordSerializer(serializers.ModelSerializer):
|
class CustomRecordSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = CustomRecord
|
model = CustomRecord
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
read_only_fields = ['id', 'custom_object', 'created_at', 'updated_at']
|
read_only_fields = ['id', 'custom_object', 'owner', 'created_at', 'updated_at']
|
||||||
|
|
||||||
def validate(self, attrs):
|
def validate(self, attrs):
|
||||||
view = self.context.get('view')
|
view = self.context.get('view')
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ class EmailTemplateSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = EmailTemplate
|
model = EmailTemplate
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
read_only_fields = ['id', 'created_at', 'updated_at']
|
read_only_fields = ['id', 'created_by', 'created_at', 'updated_at']
|
||||||
|
|
||||||
|
|
||||||
class CampaignRecipientSerializer(serializers.ModelSerializer):
|
class CampaignRecipientSerializer(serializers.ModelSerializer):
|
||||||
|
|
@ -32,7 +32,7 @@ class CampaignSerializer(OwnerScopedRelationMixin, serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Campaign
|
model = Campaign
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
read_only_fields = ['id', 'created_at', 'updated_at']
|
read_only_fields = ['id', 'created_by', 'status', 'sent_at', 'created_at', 'updated_at']
|
||||||
|
|
||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
user_subject = validated_data.pop('subject', None)
|
user_subject = validated_data.pop('subject', None)
|
||||||
|
|
|
||||||
|
|
@ -174,6 +174,76 @@ class TestTenantAccessIsolation:
|
||||||
acct_alice.refresh_from_db()
|
acct_alice.refresh_from_db()
|
||||||
assert acct_alice.name == 'Alice Account'
|
assert acct_alice.name == 'Alice Account'
|
||||||
|
|
||||||
|
def test_flow_cannot_patch_created_by_to_forge_staff_identity(self):
|
||||||
|
from workflows.models import FlowDefinition
|
||||||
|
flow = FlowDefinition.objects.create(
|
||||||
|
name='Bob Own Flow',
|
||||||
|
created_by=self.user_b,
|
||||||
|
graph={'nodes': [{'id': 'start', 'type': 'trigger', 'data': {}}], 'edges': []}
|
||||||
|
)
|
||||||
|
staff_user = User.objects.create_user('staff_victim', 'staff@test.com', 'pw', is_staff=True)
|
||||||
|
res = self.client_b.patch(f'/api/workflows/{flow.id}/', {'created_by': staff_user.id}, format='json')
|
||||||
|
# created_by is read-only, so PATCHing it cannot alter authority
|
||||||
|
flow.refresh_from_db()
|
||||||
|
assert flow.created_by == self.user_b
|
||||||
|
|
||||||
|
def test_flow_cannot_modify_foreign_activity(self):
|
||||||
|
from workflows.models import FlowDefinition
|
||||||
|
from crm_core.models import Activity
|
||||||
|
from django.contrib.contenttypes.models import ContentType
|
||||||
|
ct = ContentType.objects.get_for_model(Account)
|
||||||
|
acct_a = Account.objects.create(name='Alice Acct For Act', owner=self.user_a)
|
||||||
|
act_alice = Activity.objects.create(
|
||||||
|
subject='Alice Secret Task',
|
||||||
|
activity_type='task',
|
||||||
|
content_type=ct,
|
||||||
|
object_id=acct_a.id,
|
||||||
|
assigned_to=self.user_a
|
||||||
|
)
|
||||||
|
|
||||||
|
hack_graph = {
|
||||||
|
'nodes': [
|
||||||
|
{'id': 'start', 'type': 'trigger', 'data': {}},
|
||||||
|
{'id': 'upd', 'type': 'update_record', 'data': {
|
||||||
|
'target_model': 'Activity',
|
||||||
|
'field_updates': {'subject': 'Hacked Activity Task'}
|
||||||
|
}}
|
||||||
|
],
|
||||||
|
'edges': [{'source': 'start', 'target': 'upd'}]
|
||||||
|
}
|
||||||
|
flow = FlowDefinition.objects.create(
|
||||||
|
name='Bob Hack Activity',
|
||||||
|
created_by=self.user_b,
|
||||||
|
graph=hack_graph
|
||||||
|
)
|
||||||
|
|
||||||
|
res = self.client_b.post(f'/api/workflows/{flow.id}/execute/', {
|
||||||
|
'context': {'record_id': act_alice.id, 'record_type': 'Activity'}
|
||||||
|
}, format='json')
|
||||||
|
assert res.status_code == 400
|
||||||
|
assert 'Permission denied' in res.data.get('error', '')
|
||||||
|
act_alice.refresh_from_db()
|
||||||
|
assert act_alice.subject == 'Alice Secret Task'
|
||||||
|
|
||||||
|
def test_execute_flow_dag_with_missing_user_fails_closed(self):
|
||||||
|
from workflows.engine import execute_flow_dag
|
||||||
|
from django.core.exceptions import PermissionDenied
|
||||||
|
acct_a = Account.objects.create(name='Alice Unmodified', owner=self.user_a)
|
||||||
|
dag = {
|
||||||
|
'nodes': [
|
||||||
|
{'id': 'start', 'type': 'trigger', 'data': {}},
|
||||||
|
{'id': 'upd', 'type': 'update_record', 'data': {
|
||||||
|
'target_model': 'Account',
|
||||||
|
'field_updates': {'name': 'Forged No-User Update'}
|
||||||
|
}}
|
||||||
|
],
|
||||||
|
'edges': [{'source': 'start', 'target': 'upd'}]
|
||||||
|
}
|
||||||
|
with pytest.raises(PermissionDenied):
|
||||||
|
execute_flow_dag(dag, initial_context={'record_id': acct_a.id, 'record_type': 'Account'}, user=None)
|
||||||
|
acct_a.refresh_from_db()
|
||||||
|
assert acct_a.name == 'Alice Unmodified'
|
||||||
|
|
||||||
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,6 +17,16 @@ 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):
|
||||||
|
|
@ -117,6 +127,9 @@ 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 not exec_user:
|
||||||
|
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)
|
||||||
|
|
|
||||||
|
|
@ -261,13 +261,21 @@ def execute_flow_dag(flow_or_graph, initial_context=None, idempotency_key=None,
|
||||||
if not model_cls:
|
if not model_cls:
|
||||||
model_cls = apps.get_model('custom_objects', record_type)
|
model_cls = apps.get_model('custom_objects', record_type)
|
||||||
if model_cls:
|
if model_cls:
|
||||||
# Authorize record lookup by user principal if user is provided and not staff.
|
if not user:
|
||||||
|
# Fail-closed: unauthenticated / missing identity cannot perform DB writes
|
||||||
|
raise PermissionDenied("Missing execution identity for update_record.")
|
||||||
|
|
||||||
qs = model_cls.objects.filter(pk=record_id)
|
qs = model_cls.objects.filter(pk=record_id)
|
||||||
if user and not (user.is_superuser or user.is_staff):
|
if not (user.is_superuser or user.is_staff):
|
||||||
if hasattr(model_cls, 'owner'):
|
if hasattr(model_cls, 'owner'):
|
||||||
qs = qs.filter(owner=user)
|
qs = qs.filter(owner=user)
|
||||||
|
elif hasattr(model_cls, 'assigned_to'):
|
||||||
|
qs = qs.filter(assigned_to=user)
|
||||||
elif hasattr(model_cls, 'created_by'):
|
elif hasattr(model_cls, 'created_by'):
|
||||||
qs = qs.filter(created_by=user)
|
qs = qs.filter(created_by=user)
|
||||||
|
else:
|
||||||
|
raise PermissionDenied(f"Permission denied: no ownership policy defined for model {record_type}")
|
||||||
|
|
||||||
inst = qs.first()
|
inst = qs.first()
|
||||||
if not inst:
|
if not inst:
|
||||||
raise PermissionDenied(f"Permission denied: cannot update unauthorized record {record_type}:{record_id}")
|
raise PermissionDenied(f"Permission denied: cannot update unauthorized record {record_type}:{record_id}")
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ class FlowDefinitionSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = FlowDefinition
|
model = FlowDefinition
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
read_only_fields = ['id', 'created_at', 'updated_at']
|
read_only_fields = ['id', 'created_by', 'created_at', 'updated_at']
|
||||||
|
|
||||||
def validate_graph(self, value):
|
def validate_graph(self, value):
|
||||||
validate_flow_dag(value)
|
validate_flow_dag(value)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue