Skip to content

Commit f1ee22c

Browse files
committed
[client] Prevent file without data to be imported (OpenCTI-Platform#577)
1 parent 866968f commit f1ee22c

File tree

1 file changed

+76
-64
lines changed

1 file changed

+76
-64
lines changed

pycti/utils/opencti_stix2.py

+76-64
Original file line numberDiff line numberDiff line change
@@ -554,14 +554,17 @@ def extract_embedded_relationships(
554554
)["id"]
555555
if "x_opencti_files" in external_reference:
556556
for file in external_reference["x_opencti_files"]:
557-
self.opencti.external_reference.add_file(
558-
id=external_reference_id,
559-
file_name=file["name"],
560-
version=file.get("version", None),
561-
data=base64.b64decode(file["data"]),
562-
mime_type=file["mime_type"],
563-
no_trigger_import=file.get("no_trigger_import", False),
564-
)
557+
if "data" in file:
558+
self.opencti.external_reference.add_file(
559+
id=external_reference_id,
560+
file_name=file["name"],
561+
version=file.get("version", None),
562+
data=base64.b64decode(file["data"]),
563+
mime_type=file["mime_type"],
564+
no_trigger_import=file.get(
565+
"no_trigger_import", False
566+
),
567+
)
565568
if (
566569
self.opencti.get_attribute_in_extension(
567570
"files", external_reference
@@ -571,14 +574,17 @@ def extract_embedded_relationships(
571574
for file in self.opencti.get_attribute_in_extension(
572575
"files", external_reference
573576
):
574-
self.opencti.external_reference.add_file(
575-
id=external_reference_id,
576-
file_name=file["name"],
577-
version=file.get("version", None),
578-
data=base64.b64decode(file["data"]),
579-
mime_type=file["mime_type"],
580-
no_trigger_import=file.get("no_trigger_import", False),
581-
)
577+
if "data" in file:
578+
self.opencti.external_reference.add_file(
579+
id=external_reference_id,
580+
file_name=file["name"],
581+
version=file.get("version", None),
582+
data=base64.b64decode(file["data"]),
583+
mime_type=file["mime_type"],
584+
no_trigger_import=file.get(
585+
"no_trigger_import", False
586+
),
587+
)
582588
self.mapping_cache[generated_ref_id] = generated_ref_id
583589
external_references_ids.append(external_reference_id)
584590
if stix_object["type"] in [
@@ -722,29 +728,31 @@ def extract_embedded_relationships(
722728
)["id"]
723729
if "x_opencti_files" in external_reference:
724730
for file in external_reference["x_opencti_files"]:
725-
self.opencti.external_reference.add_file(
726-
id=external_reference_id,
727-
file_name=file["name"],
728-
version=file.get("version", None),
729-
data=base64.b64decode(file["data"]),
730-
mime_type=file["mime_type"],
731-
no_trigger_import=file.get("no_trigger_import", False),
732-
)
731+
if "data" in file:
732+
self.opencti.external_reference.add_file(
733+
id=external_reference_id,
734+
file_name=file["name"],
735+
version=file.get("version", None),
736+
data=base64.b64decode(file["data"]),
737+
mime_type=file["mime_type"],
738+
no_trigger_import=file.get("no_trigger_import", False),
739+
)
733740
if (
734741
self.opencti.get_attribute_in_extension("files", external_reference)
735742
is not None
736743
):
737744
for file in self.opencti.get_attribute_in_extension(
738745
"files", external_reference
739746
):
740-
self.opencti.external_reference.add_file(
741-
id=external_reference_id,
742-
file_name=file["name"],
743-
version=file.get("version", None),
744-
data=base64.b64decode(file["data"]),
745-
mime_type=file["mime_type"],
746-
no_trigger_import=file.get("no_trigger_import", False),
747-
)
747+
if "data" in file:
748+
self.opencti.external_reference.add_file(
749+
id=external_reference_id,
750+
file_name=file["name"],
751+
version=file.get("version", None),
752+
data=base64.b64decode(file["data"]),
753+
mime_type=file["mime_type"],
754+
no_trigger_import=file.get("no_trigger_import", False),
755+
)
748756
self.mapping_cache[generated_ref_id] = generated_ref_id
749757
external_references_ids.append(external_reference_id)
750758
# Granted refs
@@ -948,29 +956,31 @@ def import_object(
948956
# Add files
949957
if "x_opencti_files" in stix_object:
950958
for file in stix_object["x_opencti_files"]:
951-
self.opencti.stix_domain_object.add_file(
952-
id=stix_object_result["id"],
953-
file_name=file["name"],
954-
version=file.get("version", None),
955-
data=base64.b64decode(file["data"]),
956-
mime_type=file["mime_type"],
957-
no_trigger_import=file.get("no_trigger_import", False),
958-
)
959+
if "data" in file:
960+
self.opencti.stix_domain_object.add_file(
961+
id=stix_object_result["id"],
962+
file_name=file["name"],
963+
version=file.get("version", None),
964+
data=base64.b64decode(file["data"]),
965+
mime_type=file["mime_type"],
966+
no_trigger_import=file.get("no_trigger_import", False),
967+
)
959968
if (
960969
self.opencti.get_attribute_in_extension("files", stix_object)
961970
is not None
962971
):
963972
for file in self.opencti.get_attribute_in_extension(
964973
"files", stix_object
965974
):
966-
self.opencti.stix_domain_object.add_file(
967-
id=stix_object_result["id"],
968-
file_name=file["name"],
969-
version=file.get("version", None),
970-
data=base64.b64decode(file["data"]),
971-
mime_type=file["mime_type"],
972-
no_trigger_import=file.get("no_trigger_import", False),
973-
)
975+
if "data" in file:
976+
self.opencti.stix_domain_object.add_file(
977+
id=stix_object_result["id"],
978+
file_name=file["name"],
979+
version=file.get("version", None),
980+
data=base64.b64decode(file["data"]),
981+
mime_type=file["mime_type"],
982+
no_trigger_import=file.get("no_trigger_import", False),
983+
)
974984
return stix_object_results
975985

976986
def import_observable(
@@ -1071,29 +1081,31 @@ def import_observable(
10711081
# Add files
10721082
if "x_opencti_files" in stix_object:
10731083
for file in stix_object["x_opencti_files"]:
1074-
self.opencti.stix_cyber_observable.add_file(
1075-
id=stix_observable_result["id"],
1076-
file_name=file["name"],
1077-
version=file.get("version", None),
1078-
data=base64.b64decode(file["data"]),
1079-
mime_type=file["mime_type"],
1080-
no_trigger_import=file.get("no_trigger_import", False),
1081-
)
1084+
if "data" in file:
1085+
self.opencti.stix_cyber_observable.add_file(
1086+
id=stix_observable_result["id"],
1087+
file_name=file["name"],
1088+
version=file.get("version", None),
1089+
data=base64.b64decode(file["data"]),
1090+
mime_type=file["mime_type"],
1091+
no_trigger_import=file.get("no_trigger_import", False),
1092+
)
10821093
if (
10831094
self.opencti.get_attribute_in_extension("files", stix_object)
10841095
is not None
10851096
):
10861097
for file in self.opencti.get_attribute_in_extension(
10871098
"files", stix_object
10881099
):
1089-
self.opencti.stix_cyber_observable.add_file(
1090-
id=stix_observable_result["id"],
1091-
file_name=file["name"],
1092-
version=file.get("version", None),
1093-
data=base64.b64decode(file["data"]),
1094-
mime_type=file["mime_type"],
1095-
no_trigger_import=file.get("no_trigger_import", False),
1096-
)
1100+
if "data" in file:
1101+
self.opencti.stix_cyber_observable.add_file(
1102+
id=stix_observable_result["id"],
1103+
file_name=file["name"],
1104+
version=file.get("version", None),
1105+
data=base64.b64decode(file["data"]),
1106+
mime_type=file["mime_type"],
1107+
no_trigger_import=file.get("no_trigger_import", False),
1108+
)
10971109
if "id" in stix_object:
10981110
self.mapping_cache[stix_object["id"]] = {
10991111
"id": stix_observable_result["id"],

0 commit comments

Comments
 (0)