Skip to content
This repository was archived by the owner on Dec 5, 2025. It is now read-only.

Commit 89b0ee9

Browse files
[client] Self-referencing reports cause a recursion error (#267)
1 parent e59621d commit 89b0ee9

File tree

3 files changed

+74
-14
lines changed

3 files changed

+74
-14
lines changed

pycti/utils/opencti_stix2_splitter.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,24 @@ def enlist_element(self, item_id, raw_data):
1818
item = raw_data[item_id]
1919
for key, value in item.items():
2020
if key.endswith("_refs"):
21+
to_keep = []
2122
for element_ref in item[key]:
22-
nb_deps += self.enlist_element(element_ref, raw_data)
23+
if element_ref != item_id:
24+
nb_deps += self.enlist_element(element_ref, raw_data)
25+
to_keep.append(element_ref)
26+
item[key] = to_keep
2327
elif key.endswith("_ref"):
24-
# Need to handle the special case of recursive ref for created by ref
25-
is_created_by_ref = key == "created_by_ref"
26-
if is_created_by_ref:
27-
is_marking = item["id"].startswith("marking-definition--")
28-
if is_marking is False:
29-
nb_deps += self.enlist_element(value, raw_data)
28+
if item[key] == item_id:
29+
item[key] = None
3030
else:
31-
nb_deps += self.enlist_element(value, raw_data)
31+
# Need to handle the special case of recursive ref for created by ref
32+
is_created_by_ref = key == "created_by_ref"
33+
if is_created_by_ref:
34+
is_marking = item["id"].startswith("marking-definition--")
35+
if is_marking is False:
36+
nb_deps += self.enlist_element(value, raw_data)
37+
else:
38+
nb_deps += self.enlist_element(value, raw_data)
3239
# Get the final dep counting and add in cache
3340
item["nb_deps"] = nb_deps
3441
self.elements.append(item)

tests/01-unit/utils/test_opencti_stix2_splitter.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ def test_split_bundle():
1111
content = file.read()
1212
bundles = stix_splitter.split_bundle(content)
1313
assert len(bundles) == 7029
14-
#
15-
# with open("./tests/data/test.pdf", 'w') as file:
16-
# content = file.read()
17-
# with pytest.raises(Exception):
18-
# stix_splitter.split_bundle(content)
1914

2015

21-
def test_crate_bundle():
16+
def test_split_cyclic_bundle():
17+
stix_splitter = OpenCTIStix2Splitter()
18+
with open("./tests/data/cyclic-bundle.json") as file:
19+
content = file.read()
20+
bundles = stix_splitter.split_bundle(content)
21+
assert len(bundles) == 3
22+
23+
24+
def test_create_bundle():
2225
stix_splitter = OpenCTIStix2Splitter()
2326
report = Report(
2427
report_types=["campaign"],

tests/data/cyclic-bundle.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"type": "bundle",
3+
"id": "bundle--8c939929-688f-4a72-badb-3dd1bd6af0fa",
4+
"objects": [
5+
{
6+
"id": "identity--7b82b010-b1c0-4dae-981f-7756374a17df",
7+
"type": "identity",
8+
"spec_version": "2.1",
9+
"name": "ANSSI",
10+
"identity_class": "organization",
11+
"labels": ["identity"],
12+
"created": "2020-02-23T23:40:53.575Z",
13+
"modified": "2020-02-27T08:45:39.351Z",
14+
"x_opencti_organization_type": "CSIRT",
15+
"created_by_ref": "identity--7b82b010-b1c0-4dae-981f-7756374a17df"
16+
},
17+
{
18+
"id": "marking-definition--78ca4366-f5b8-4764-83f7-34ce38198e27",
19+
"type": "marking-definition",
20+
"spec_version": "2.1",
21+
"definition_type": "TLP",
22+
"definition": {
23+
"TLP": "TLP:TEST"
24+
},
25+
"created": "2020-02-25T09:02:29.040Z",
26+
"modified": "2020-02-25T09:02:29.040Z",
27+
"created_by_ref": "marking-definition--78ca4366-f5b8-4764-83f7-34ce38198e27"
28+
},
29+
{
30+
"id": "report--a445d22a-db0c-4b5d-9ec8-e9ad0b6dbdd7",
31+
"type": "report",
32+
"spec_version": "2.1",
33+
"name": "A demo report for testing purposes",
34+
"labels": ["report"],
35+
"description": "Report for testing purposes (random data).",
36+
"published": "2020-03-01T14:02:48.111Z",
37+
"created": "2020-03-01T14:02:55.327Z",
38+
"modified": "2020-03-01T14:09:48.078Z",
39+
"report_types": ["threat-report"],
40+
"x_opencti_report_status": 2,
41+
"confidence": 3,
42+
"created_by_ref": "identity--7b82b010-b1c0-4dae-981f-7756374a17df",
43+
"object_marking_refs": ["marking-definition--78ca4366-f5b8-4764-83f7-34ce38198e27"],
44+
"object_refs": [
45+
"observed-data--7d258c31-9a26-4543-aecb-2abc5ed366be",
46+
"report--a445d22a-db0c-4b5d-9ec8-e9ad0b6dbdd7"
47+
]
48+
}
49+
]
50+
}

0 commit comments

Comments
 (0)