88import time
99import traceback
1010import uuid
11- from typing import Any , Dict , List , Optional , Union
11+ from typing import Any , Dict , List , Optional , Tuple , Union
1212
1313import datefinder
1414import dateutil .parser
3232 STIX_CORE_OBJECTS ,
3333 STIX_CYBER_OBSERVABLE_MAPPING ,
3434 STIX_META_OBJECTS ,
35+ OpenCTIStix2Utils ,
3536)
3637
3738datefinder .ValueError = ValueError , OverflowError
@@ -196,7 +197,7 @@ def import_bundle_from_file(
196197 file_path : str ,
197198 update : bool = False ,
198199 types : List = None ,
199- ) -> Optional [List ]:
200+ ) -> Optional [Tuple [ list , list ] ]:
200201 """import a stix2 bundle from a file
201202
202203 :param file_path: valid path to the file
@@ -221,7 +222,8 @@ def import_bundle_from_json(
221222 update : bool = False ,
222223 types : List = None ,
223224 work_id : str = None ,
224- ) -> List :
225+ objects_max_refs : int = 0 ,
226+ ) -> Tuple [list , list ]:
225227 """import a stix2 bundle from JSON data
226228
227229 :param json_data: JSON data
@@ -231,11 +233,13 @@ def import_bundle_from_json(
231233 :param types: list of stix2 types, defaults to None
232234 :type types: list, optional
233235 :param work_id work_id: str, optional
234- :return: list of imported stix2 objects
235- :rtype: List
236+ :param objects_max_refs: max deps amount of objects, reject object import if larger than configured amount
237+ :type objects_max_refs: int, optional
238+ :return: list of imported stix2 objects and a list of stix2 objects with too many deps
239+ :rtype: Tuple[List,List]
236240 """
237241 data = json .loads (json_data )
238- return self .import_bundle (data , update , types , work_id )
242+ return self .import_bundle (data , update , types , work_id , objects_max_refs )
239243
240244 def resolve_author (self , title : str ) -> Optional [Identity ]:
241245 if "fireeye" in title .lower () or "mandiant" in title .lower ():
@@ -3060,7 +3064,8 @@ def import_bundle(
30603064 update : bool = False ,
30613065 types : List = None ,
30623066 work_id : str = None ,
3063- ) -> List :
3067+ objects_max_refs : int = 0 ,
3068+ ) -> Tuple [list , list ]:
30643069 # Check if the bundle is correctly formatted
30653070 if "type" not in stix_bundle or stix_bundle ["type" ] != "bundle" :
30663071 raise ValueError ("JSON data type is not a STIX2 bundle" )
@@ -3094,12 +3099,27 @@ def import_bundle(
30943099
30953100 # Import every element in a specific order
30963101 imported_elements = []
3102+ too_large_elements_bundles = []
30973103 for bundle in bundles :
30983104 for item in bundle ["objects" ]:
3099- self .import_item (item , update , types , 0 , work_id )
3100- imported_elements .append ({"id" : item ["id" ], "type" : item ["type" ]})
3105+ # If item is considered too large, meaning that it has a number of refs higher than inputted objects_max_refs, do not import it
3106+ nb_refs = OpenCTIStix2Utils .compute_object_refs_number (item )
3107+ if 0 < objects_max_refs <= nb_refs :
3108+ self .opencti .work .report_expectation (
3109+ work_id ,
3110+ {
3111+ "error" : "Too large element in bundle" ,
3112+ "source" : "Element "
3113+ + item ["id" ]
3114+ + " is too large and couldn't be processed" ,
3115+ },
3116+ )
3117+ too_large_elements_bundles .append (item )
3118+ else :
3119+ self .import_item (item , update , types , 0 , work_id )
3120+ imported_elements .append ({"id" : item ["id" ], "type" : item ["type" ]})
31013121
3102- return imported_elements
3122+ return imported_elements , too_large_elements_bundles
31033123
31043124 @staticmethod
31053125 def put_attribute_in_extension (
0 commit comments