Skip to content

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

sflib: raise ValueError if target type is invalid

Brendan Coles350c1e4fed0d
diff --git a/sflib.py b/sflib.py
index 9273895c..ec05578c 100644
--- a/sflib.py
+++ b/sflib.py
@@ -2517,16 +2517,15 @@ class SpiderFootTarget(object):
targetAliases = list()
def __init__(self, targetValue, typeName):
- if typeName in self._validTypes:
- self.targetType = typeName
- if type(targetValue) != str:
- self.targetValue = str(targetValue).lower()
- else:
- self.targetValue = targetValue
- self.targetAliases = list()
+ if typeName not in self._validTypes:
+ raise ValueError("Invalid target type %s; expected %s" % (typeName, self._validTypes))
+
+ self.targetType = typeName
+ if type(targetValue) != str:
+ self.targetValue = str(targetValue).lower()
else:
- print("Internal Error: Invalid target type.")
- sys.exit(-1)
+ self.targetValue = targetValue
+ self.targetAliases = list()
def getType(self):
return self.targetType
diff --git a/sfscan.py b/sfscan.py
index 17aa689f..5255b856 100644
--- a/sfscan.py
+++ b/sfscan.py
@@ -113,8 +113,14 @@ class SpiderFootScanner():
self.dbh.scanInstanceCreate(self.scanId,
self.scanName, self.targetValue)
self.setStatus("STARTING", time.time() * 1000, None)
+
# Create our target
- target = SpiderFootTarget(self.targetValue, self.targetType)
+ try:
+ target = SpiderFootTarget(self.targetValue, self.targetType)
+ except BaseException as e:
+ self.sf.status("Scan [%s] failed: %s" % (self.scanId, e))
+ self.setStatus("ERROR-FAILED", None, time.time() * 1000)
+ return None
# Save the config current set for this scan
self.config['_modulesenabled'] = self.moduleList
diff --git a/test/unit/test_spiderfoottarget.py b/test/unit/test_spiderfoottarget.py
index 9e73815e..49e5f10c 100644
--- a/test/unit/test_spiderfoottarget.py
+++ b/test/unit/test_spiderfoottarget.py
@@ -12,18 +12,16 @@ class TestSpiderFootTarget(unittest.TestCase):
'EMAILADDR', 'HUMAN_NAME', 'BGP_AS_OWNER', 'PHONE_NUMBER', "USERNAME"
]
- def test_init_unsupported_target_type_should_exit(self):
+ def test_init_unsupported_target_type_should_raise(self):
"""
Test __init__(self, targetValue, typeName)
"""
target_value = 'example target value'
- with self.assertRaises(SystemExit) as cm:
+ with self.assertRaises(ValueError) as cm:
target_type = 'example target type'
target = SpiderFootTarget(target_value, target_type)
- self.assertEqual(cm.exception.code, -1)
-
def test_init_supported_target_types(self):
"""
Test __init__(self, targetValue, typeName)