Skip to content

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

Tests: Add sfwebui tests (#1674)

bcolesa5e6a428223a
diff --git a/sfwebui.py b/sfwebui.py
index 8c81740d..42c209cf 100644
--- a/sfwebui.py
+++ b/sfwebui.py
@@ -125,7 +125,7 @@ class SpiderFootWebUi:
"""Error page."""
cherrypy.response.status = 500
- if self.config['_debug']:
+ if self.config.get('_debug'):
cherrypy.response.body = _cperror.get_error_page(status=500, traceback=_cperror.format_exc())
else:
cherrypy.response.body = b"<html><body>Error</body></html>"
@@ -191,16 +191,20 @@ class SpiderFootWebUi:
return templ.render(message=message, docroot=self.docroot, version=__version__)
def cleanUserInput(self: 'SpiderFootWebUi', inputList: list) -> list:
- """Sanitize user input, poorly.
+ """Convert data to HTML entities; except quotes and ampersands.
Args:
- inputList (list): TBD
+ inputList (list): list of strings to sanitize
Returns:
list: sanitized input
Raises:
TypeError: inputList type was invalid
+
+ Todo:
+ Review all uses of this function, then remove it.
+ Use of this function is overloaded.
"""
if not isinstance(inputList, list):
raise TypeError(f"inputList is {type(inputList)}; expected list()")
@@ -212,8 +216,8 @@ class SpiderFootWebUi:
ret.append('')
continue
c = html.escape(item, True)
- c = c.replace("'", '&quot;')
- # We don't actually want & translated to &amp;
+
+ # Decode '&' and '"' HTML entities
c = c.replace("&amp;", "&").replace("&quot;", "\"")
ret.append(c)
@@ -674,14 +678,14 @@ class SpiderFootWebUi:
@cherrypy.expose
@cherrypy.tools.json_out()
- def scanopts(self: 'SpiderFootWebUi', id: str) -> str:
+ def scanopts(self: 'SpiderFootWebUi', id: str) -> dict:
"""Return configuration used for the specified scan as JSON.
Args:
id: scan ID
Returns:
- str: options as JSON string
+ dict: scan options for the specified scan
"""
dbh = SpiderFootDb(self.config)
ret = dict()
@@ -1195,11 +1199,11 @@ class SpiderFootWebUi:
@cherrypy.expose
@cherrypy.tools.json_out()
- def eventtypes(self: 'SpiderFootWebUi') -> str:
+ def eventtypes(self: 'SpiderFootWebUi') -> list:
"""List all event types.
Returns:
- str: list of event types
+ list: list of event types
"""
cherrypy.response.headers['Content-Type'] = "application/json; charset=utf-8"
@@ -1214,17 +1218,22 @@ class SpiderFootWebUi:
@cherrypy.expose
@cherrypy.tools.json_out()
- def modules(self: 'SpiderFootWebUi') -> str:
+ def modules(self: 'SpiderFootWebUi') -> list:
"""List all modules.
Returns:
- str: list of modules
+ list: list of modules
"""
cherrypy.response.headers['Content-Type'] = "application/json; charset=utf-8"
+ ret = list()
+
modinfo = list(self.config['__modules__'].keys())
+ if not modinfo:
+ return ret
+
modinfo.sort()
- ret = list()
+
for m in modinfo:
if "__" in m:
continue
@@ -1234,16 +1243,20 @@ class SpiderFootWebUi:
@cherrypy.expose
@cherrypy.tools.json_out()
- def correlationrules(self: 'SpiderFootWebUi') -> str:
+ def correlationrules(self: 'SpiderFootWebUi') -> list:
"""List all correlation rules.
Returns:
- str: list of correlation rules
+ list: list of correlation rules
"""
cherrypy.response.headers['Content-Type'] = "application/json; charset=utf-8"
- rules = self.config['__correlationrules__']
ret = list()
+
+ rules = self.config['__correlationrules__']
+ if not rules:
+ return ret
+
for r in rules:
ret.append({
'id': r['id'],
@@ -1308,7 +1321,8 @@ class SpiderFootWebUi:
Raises:
HTTPRedirect: redirect to new scan info page
"""
- [scanname, scantarget] = self.cleanUserInput([scanname, scantarget])
+ scanname = self.cleanUserInput([scanname])[0]
+ scantarget = self.cleanUserInput([scantarget])[0]
if not scanname:
if cherrypy.request.headers.get('Accept') and 'application/json' in cherrypy.request.headers.get('Accept'):
diff --git a/spiderfoot/templates/opts.tmpl b/spiderfoot/templates/opts.tmpl
index e8d207e5..20a534b5 100644
--- a/spiderfoot/templates/opts.tmpl
+++ b/spiderfoot/templates/opts.tmpl
@@ -46,7 +46,7 @@
<thead><tr> <th>Option</th> <th>Value</th></tr></thead><tbody>
% for opt in sorted(opts.keys()):
% if not opt.startswith("__"):
- <tr><td>${opts['__globaloptdescs__'][opt]}</td><td style='vertical-align: middle'>
+ <tr><td>${opts['__globaloptdescs__'].get(opt) or "No description available"}</td><td style='vertical-align: middle'>
% if type(opts[opt]) is int:
<div class='form-group'>
<input id='${opt}' class='form-control' type=text value="${opts[opt]}">
diff --git a/test/unit/test_spiderfootwebui.py b/test/unit/test_spiderfootwebui.py
index e433d0f4..8550ae88 100644
--- a/test/unit/test_spiderfootwebui.py
+++ b/test/unit/test_spiderfootwebui.py
@@ -74,9 +74,6 @@ class TestSpiderFootWebUi(unittest.TestCase):
self.assertIsInstance(error_page_404, str)
def test_clean_user_input_should_return_a_list(self):
- """
- Test cleanUserInput(self, inputList)
- """
opts = self.default_options
opts['__modules__'] = dict()
sfwebui = SpiderFootWebUi(self.web_default_options, opts)
@@ -84,9 +81,6 @@ class TestSpiderFootWebUi(unittest.TestCase):
self.assertIsInstance(clean_user_input, list)
def test_clean_user_input_invalid_input_should_raise(self):
- """
- Test cleanUserInput(self, inputList)
- """
opts = self.default_options
opts['__modules__'] = dict()
sfwebui = SpiderFootWebUi(self.web_default_options, opts)
@@ -97,6 +91,21 @@ class TestSpiderFootWebUi(unittest.TestCase):
with self.assertRaises(TypeError):
sfwebui.cleanUserInput(invalid_type)
+ def test_clean_user_input_should_clean_user_input(self):
+ opts = self.default_options
+ opts['__modules__'] = dict()
+ sfwebui = SpiderFootWebUi(self.web_default_options, opts)
+
+ clean_input = sfwebui.cleanUserInput([
+ "<p>some HTML with \"some quotes\" & some JavaScript\n<script>alert('JavaScript')</script></p>",
+ "Some more input. This function accepts a list"
+ ])
+ self.assertIsInstance(clean_input, list)
+ self.assertEqual(clean_input, [
+ '&lt;p&gt;some HTML with "some quotes" & some JavaScript\n&lt;script&gt;alert(&#x27;JavaScript&#x27;)&lt;/script&gt;&lt;/p&gt;',
+ "Some more input. This function accepts a list"
+ ])
+
def test_search_base_should_return_a_list(self):
"""
Test searchBase(self, id=None, eventType=None, value=None)
@@ -204,28 +213,21 @@ class TestSpiderFootWebUi(unittest.TestCase):
scan_viz_multi = sfwebui.scanvizmulti(None, None)
self.assertIsInstance(scan_viz_multi, str)
- @unittest.skip("todo")
- def test_scanopts(self):
- """
- Test scanopts(self, id)
- """
+ def test_scanopts_should_return_dict(self):
opts = self.default_options
opts['__modules__'] = dict()
sfwebui = SpiderFootWebUi(self.web_default_options, opts)
scan_opts = sfwebui.scanopts("example scan instance")
- self.assertIsInstance(scan_opts, str)
-
- self.assertEqual('TBD', 'TBD')
+ self.assertIsInstance(scan_opts, dict)
+ self.assertEqual(scan_opts, dict())
- def test_rerunscan(self):
- """
- Test rerunscan(self, id)
- """
+ def test_rerunscan_invalid_scan_id_should_return_error(self):
opts = self.default_options
opts['__modules__'] = dict()
sfwebui = SpiderFootWebUi(self.web_default_options, opts)
rerunscan = sfwebui.rerunscan("example scan instance")
self.assertIsInstance(rerunscan, str)
+ self.assertIn("Invalid scan ID", rerunscan)
@unittest.skip("todo")
def test_rerunscanmulti(self):
@@ -275,17 +277,16 @@ class TestSpiderFootWebUi(unittest.TestCase):
scan_info = sfwebui.scaninfo("example scan instance")
self.assertIsInstance(scan_info, str)
- @unittest.skip("todo")
def test_opts(self):
- """
- Test opts(self)
- """
- self.assertEqual('TBD', 'TBD')
+ opts = self.default_options
+ opts['__modules__'] = dict()
+ opts['__globaloptdescs__'] = dict()
+ sfwebui = SpiderFootWebUi(self.web_default_options, opts)
+ opts_page = sfwebui.opts()
+ self.assertIsInstance(opts_page, str)
+ self.assertIn('Settings', opts_page)
def test_optsexport_should_return_a_string(self):
- """
- Test optsexport(self, pattern)
- """
opts = self.default_options
opts['__modules__'] = dict()
sfwebui = SpiderFootWebUi(self.web_default_options, opts)
@@ -293,9 +294,6 @@ class TestSpiderFootWebUi(unittest.TestCase):
self.assertIsInstance(opts_export, str)
def test_optsraw_should_return_a_list(self):
- """
- Test optsraw(self)
- """
opts = self.default_options
opts['__modules__'] = dict()
sfwebui = SpiderFootWebUi(self.web_default_options, opts)
@@ -304,9 +302,6 @@ class TestSpiderFootWebUi(unittest.TestCase):
self.assertEqual(opts_raw[0], 'SUCCESS')
def test_error(self):
- """
- Test error(self, message)
- """
opts = self.default_options
opts['__modules__'] = dict()
sfwebui = SpiderFootWebUi(self.web_default_options, opts)
@@ -327,20 +322,41 @@ class TestSpiderFootWebUi(unittest.TestCase):
self.assertIsInstance(scan_delete, dict)
self.assertEqual("Scan example scan id does not exist", scan_delete.get('error').get('message'))
- @unittest.skip("todo")
- def test_savesettings(self):
+ def test_savesettings_invalid_csrf_token_should_return_an_error(self):
"""
Test savesettings(self, allopts, token, configFile=None)
"""
+ opts = self.default_options
+ opts['__modules__'] = dict()
+ sfwebui = SpiderFootWebUi(self.web_default_options, opts)
+ save_settings = sfwebui.savesettings(None, "invalid token", None)
+ self.assertIsInstance(save_settings, str)
+ self.assertIn("Invalid token", save_settings)
+
+ @unittest.skip("todo")
+ def test_savesettings(self):
self.assertEqual('TBD', 'TBD')
+ def test_savesettingsraw_invalid_csrf_token_should_return_an_error(self):
+ opts = self.default_options
+ opts['__modules__'] = dict()
+ sfwebui = SpiderFootWebUi(self.web_default_options, opts)
+ save_settings_raw = sfwebui.savesettingsraw(None, "invalid token")
+ self.assertIsInstance(save_settings_raw, bytes)
+ self.assertIn(b"Invalid token", save_settings_raw)
+
@unittest.skip("todo")
def test_savesettingsraw(self):
- """
- Test savesettingsraw(self, allopts, token)
- """
self.assertEqual('TBD', 'TBD')
+ def test_reset_settings_should_return_true(self):
+ opts = self.default_options
+ opts['__modules__'] = dict()
+ sfwebui = SpiderFootWebUi(self.web_default_options, opts)
+ reset_settings = sfwebui.reset_settings()
+ self.assertIsInstance(reset_settings, bool)
+ self.assertTrue(reset_settings)
+
@unittest.skip("todo")
def test_result_set_fp(self):
"""
@@ -349,9 +365,6 @@ class TestSpiderFootWebUi(unittest.TestCase):
self.assertEqual('TBD', 'TBD')
def test_eventtypes_should_return_list(self):
- """
- Test eventtypes(self)
- """
opts = self.default_options
opts['__modules__'] = dict()
sfwebui = SpiderFootWebUi(self.web_default_options, opts)
@@ -359,15 +372,19 @@ class TestSpiderFootWebUi(unittest.TestCase):
self.assertIsInstance(event_types, list)
def test_modules_should_return_list(self):
- """
- Test modules(self)
- """
opts = self.default_options
opts['__modules__'] = dict()
sfwebui = SpiderFootWebUi(self.web_default_options, opts)
- modules = sfwebui.eventtypes()
+ modules = sfwebui.modules()
self.assertIsInstance(modules, list)
+ def test_correlationrules_should_return_list(self):
+ opts = self.default_options
+ opts['__modules__'] = dict()
+ sfwebui = SpiderFootWebUi(self.web_default_options, opts)
+ correlationrules = sfwebui.correlationrules()
+ self.assertIsInstance(correlationrules, list)
+
def test_ping_should_return_list(self):
"""
Test ping(self)