Skip to content

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

sfdb: validate search() search criteria

Brendan Coles3296b619d714
diff --git a/sfdb.py b/sfdb.py
index 7fbc5533..b230966b 100644
--- a/sfdb.py
+++ b/sfdb.py
@@ -375,16 +375,29 @@ class SpiderFootDb:
TypeError: arg type was invalid
ValueError: arg value was invalid
IOError: database I/O failed
-
- Todo:
- Raise errors upon invalid args
"""
-
if not isinstance(criteria, dict):
raise TypeError(f"criteria is {type(criteria)}; expected dict()")
- if list(criteria.values()).count(None) >= 3:
- raise ValueError("Invalid number of search criteria provided; expected at least 2")
+ valid_criteria = ['scan_id', 'type', 'value', 'regex']
+
+ for key in list(criteria.keys()):
+ if key not in valid_criteria:
+ criteria.pop(key, None)
+ continue
+
+ if not isinstance(criteria.get(key), str):
+ raise TypeError(f"criteria[{key}] is {type(criteria.get(key))}; expected str()")
+
+ if not criteria[key]:
+ criteria.pop(key, None)
+ continue
+
+ if len(criteria) == 0:
+ raise ValueError(f"No valid search criteria provided; expected: {', '.join(valid_criteria)}")
+
+ if len(criteria) == 1:
+ raise ValueError(f"Only one search criteria provided; expected at least two")
qvars = list()
qry = "SELECT ROUND(c.generated) AS generated, c.data, \
diff --git a/test/unit/test_spiderfootdb.py b/test/unit/test_spiderfootdb.py
index d1b49c86..93a1c0ac 100644
--- a/test/unit/test_spiderfootdb.py
+++ b/test/unit/test_spiderfootdb.py
@@ -90,10 +90,10 @@ class TestSpiderFootDb(unittest.TestCase):
sfdb = SpiderFootDb(self.default_options, False)
criteria = {
- 'scan_id': "",
- 'type': "",
- 'value': "",
- 'regex': ""
+ 'scan_id': "example scan id",
+ 'type': "example type",
+ 'value': "example value",
+ 'regex': "example regex"
}
search_results = sfdb.search(criteria, False)
@@ -106,23 +106,48 @@ class TestSpiderFootDb(unittest.TestCase):
"""
sfdb = SpiderFootDb(self.default_options, False)
- invalid_types = [None, "", list()]
+ invalid_types = [None, "", list(), int()]
for invalid_type in invalid_types:
with self.subTest(invalid_type=invalid_type):
with self.assertRaises(TypeError) as cm:
search_results = sfdb.search(invalid_type, False)
- def test_search_argument_criteria_invalid_value_should_raise_ValueError(self):
+ def test_search_argument_criteria_key_of_invalid_type_should_raise_TypeError(self):
+ """
+ Test search(self, criteria, filterFp=False)
+ """
+ sfdb = SpiderFootDb(self.default_options, False)
+
+ criteria = {
+ 'type': "example type",
+ 'value': "example value",
+ 'regex': []
+ }
+
+ with self.assertRaises(TypeError) as cm:
+ search_results = sfdb.search(criteria, False)
+
+ def test_search_argument_criteria_no_valid_criteria_should_raise_ValueError(self):
+ """
+ Test search(self, criteria, filterFp=False)
+ """
+ sfdb = SpiderFootDb(self.default_options, False)
+
+ criteria = {
+ 'invalid_criteria': "example invalid criteria"
+ }
+
+ with self.assertRaises(ValueError) as cm:
+ search_results = sfdb.search(criteria, False)
+
+ def test_search_argument_criteria_one_criteria_should_raise_ValueError(self):
"""
Test search(self, criteria, filterFp=False)
"""
sfdb = SpiderFootDb(self.default_options, False)
criteria = {
- 'scan_id': None,
- 'type': None,
- 'value': None,
- 'regex': None
+ 'type': "example type"
}
with self.assertRaises(ValueError) as cm: