Skip to content

Commit 421c2ea

Browse files
committed
add function to check for
overlapping mitre id type and subtypes
1 parent b778778 commit 421c2ea

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

contentctl/objects/detection_tags.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class DetectionTags(BaseModel):
4444
asset_type: AssetType = Field(...)
4545
group: list[str] = []
4646

47-
mitre_attack_id: List[MITRE_ATTACK_ID_TYPE] = []
47+
mitre_attack_id: list[MITRE_ATTACK_ID_TYPE] = []
4848
nist: list[NistCategory] = []
4949

5050
product: list[SecurityContentProductName] = Field(..., min_length=1)
@@ -165,6 +165,39 @@ def addAttackEnrichments(cls, v:list[MitreAttackEnrichment], info:ValidationInfo
165165
return enrichments
166166
"""
167167

168+
@field_validator("mitre_attack_id", mode="after")
169+
@classmethod
170+
def sameTypeAndSubtypeNotPresent(
171+
cls, mitre_ids: list[MITRE_ATTACK_ID_TYPE]
172+
) -> list[MITRE_ATTACK_ID_TYPE]:
173+
id_types: list[str] = [
174+
f"{mitre_id}." for mitre_id in mitre_ids if "." not in mitre_id
175+
]
176+
id_subtypes: list[MITRE_ATTACK_ID_TYPE] = [
177+
mitre_id for mitre_id in mitre_ids if "." in mitre_id
178+
]
179+
subtype_and_parent_exist_exceptions: list[ValueError] = []
180+
181+
for id_subtype in id_subtypes:
182+
for id_type in id_types:
183+
if id_subtype.startswith(id_type):
184+
subtype_and_parent_exist_exceptions.append(
185+
ValueError(
186+
f" Tactic : {id_type.split('.')[0]}\n"
187+
f" Subtactic: {id_subtype}\n"
188+
)
189+
)
190+
191+
if len(subtype_and_parent_exist_exceptions):
192+
error_string = "\n".join(
193+
str(e) for e in subtype_and_parent_exist_exceptions
194+
)
195+
raise ValueError(
196+
f"Overlapping MITRE Attack ID Tactics and Subtactics may not be defined:\n{error_string}"
197+
)
198+
199+
return mitre_ids
200+
168201
@field_validator("analytic_story", mode="before")
169202
@classmethod
170203
def mapStoryNamesToStoryObjects(

0 commit comments

Comments
 (0)