Skip to content

SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.

SpiderFootHelpers: Add validLEI function (#1190)

bcolese890ec7ce641
diff --git a/spiderfoot/helpers.py b/spiderfoot/helpers.py
index 42609655..802c61a0 100644
--- a/spiderfoot/helpers.py
+++ b/spiderfoot/helpers.py
@@ -280,3 +280,24 @@ class SpiderFootHelpers():
return {}
return {"name": root, "children": get_children(root, data)}
+
+ def validLEI(lei):
+ """Check if the provided string is a valid Legal Entity Identifier (LEI).
+
+ Args:
+ lei (str): The LEI number to check.
+
+ Returns:
+ bool: string is a valid LEI
+
+ Note:
+ ISO 17442 has been withdrawn and is not accurate
+ https://www.gleif.org/en/about-lei/iso-17442-the-lei-code-structure
+ """
+ if not isinstance(lei, str):
+ return False
+
+ if not re.match(r'^[A-Z0-9]{18}[0-9]{2}$', lei, re.IGNORECASE):
+ return False
+
+ return True
diff --git a/test/unit/test_spiderfoothelpers.py b/test/unit/test_spiderfoothelpers.py
index 8d25399e..b69f35b0 100644
--- a/test/unit/test_spiderfoothelpers.py
+++ b/test/unit/test_spiderfoothelpers.py
@@ -121,3 +121,22 @@ class TestSpiderFootHelpers(unittest.TestCase):
tree = SpiderFootHelpers.dataParentChildToTree({"test": {"123": "456"}})
self.assertIsInstance(tree, dict)
+
+ def test_validLEI_should_return_a_boolean(self):
+ """
+ Test validLEI(self, lei)
+ """
+ invalid_types = [None, "", list(), dict(), int()]
+ for invalid_type in invalid_types:
+ with self.subTest(invalid_type=invalid_type):
+ valid_phone = SpiderFootHelpers.validLEI(invalid_type)
+ self.assertIsInstance(valid_phone, bool)
+ self.assertFalse(valid_phone)
+
+ valid_lei = SpiderFootHelpers.validLEI('7ZW8QJWVPR4P1J1KQYZZ')
+ self.assertIsInstance(valid_lei, bool)
+ self.assertFalse(valid_lei)
+
+ valid_lei = SpiderFootHelpers.validLEI('7ZW8QJWVPR4P1J1KQY45')
+ self.assertIsInstance(valid_lei, bool)
+ self.assertTrue(valid_lei)