From e8fdb20b98b88ec330ef41ee5b7454e68bad5f25 Mon Sep 17 00:00:00 2001 From: Nassim Tabchiche Date: Thu, 12 Dec 2024 15:36:43 +0100 Subject: [PATCH 1/2] Check that parent assets do not cause a cycle in the assets graph --- backend/core/serializers.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/backend/core/serializers.py b/backend/core/serializers.py index 0e3c95ae7..c741ef570 100644 --- a/backend/core/serializers.py +++ b/backend/core/serializers.py @@ -220,6 +220,20 @@ class Meta: model = Asset fields = "__all__" + def validate_parent_assets(self, parent_assets): + """ + Check that the assets graph will not contain cycles + """ + if not self.instance: + return parent_assets + if parent_assets: + for asset in parent_assets: + if self.instance in asset.ancestors_plus_self(): + raise serializers.ValidationError( + "errorAssetGraphMustNotContainCycles" + ) + return parent_assets + class AssetReadSerializer(AssetWriteSerializer): folder = FieldsRelatedField() From 45ce56b3172fd7b15a8d4035017fe53bd3ab8166 Mon Sep 17 00:00:00 2001 From: Nassim Tabchiche Date: Thu, 12 Dec 2024 15:36:54 +0100 Subject: [PATCH 2/2] Localize cyclic asset graph error --- frontend/messages/en.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frontend/messages/en.json b/frontend/messages/en.json index a58960796..50fd4a9b8 100644 --- a/frontend/messages/en.json +++ b/frontend/messages/en.json @@ -979,5 +979,6 @@ "attackPath": "Attack path", "attackPaths": "Attack paths", "currentCriticality": "Current criticality", - "residualCriticality": "Residual criticality" + "residualCriticality": "Residual criticality", + "errorAssetGraphMustNotContainCycles": "The asset graph must not contain cycles." }