Skip to content

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

sflib: Ensure parseRobotsTxt returns a list

Brendan Coles7f6dab9d19d2
diff --git a/sflib.py b/sflib.py
index ffa24e34..6aaeedb3 100644
--- a/sflib.py
+++ b/sflib.py
@@ -1464,14 +1464,18 @@ class SpiderFoot:
returnArr = list()
+ if not isinstance(robotsTxtData, str):
+ return returnArr
+
# We don't check the User-Agent rule yet.. probably should at some stage
for line in robotsTxtData.splitlines():
if line.lower().startswith('disallow:'):
+ # todo: fix whitespace parsing; ie, " " is not a valid disallowed path
m = re.match('disallow:\s*(.[^ #]*)', line, re.IGNORECASE)
- self.debug('robots.txt parsing found disallow: ' + m.group(1))
- returnArr.append(m.group(1))
- continue
+ if m:
+ self.debug('robots.txt parsing found disallow: ' + m.group(1))
+ returnArr.append(m.group(1))
return returnArr
diff --git a/test/unit/test_spiderfoot.py b/test/unit/test_spiderfoot.py
index 98da0cfc..99b15674 100644
--- a/test/unit/test_spiderfoot.py
+++ b/test/unit/test_spiderfoot.py
@@ -82,7 +82,7 @@ class TestSpiderFoot(unittest.TestCase):
sf = SpiderFoot(self.default_options)
test_string = "example string"
- opt_data = sf.optValueToData(test_string, fatal=True, splitLines=True)
+ opt_data = sf.optValueToData(test_string, fatal=False, splitLines=True)
self.assertIsInstance(opt_data, str)
self.assertEqual(test_string, opt_data)
@@ -92,10 +92,10 @@ class TestSpiderFoot(unittest.TestCase):
"""
sf = SpiderFoot(self.default_options)
- opt_data = sf.optValueToData(None, fatal=True, splitLines=True)
+ opt_data = sf.optValueToData(None, fatal=False, splitLines=True)
self.assertEqual(None, opt_data)
- opt_data = sf.optValueToData([], fatal=True, splitLines=True)
+ opt_data = sf.optValueToData([], fatal=False, splitLines=True)
self.assertEqual(None, opt_data)
def test_build_graph_data_should_return_a_set(self):
@@ -337,7 +337,6 @@ class TestSpiderFoot(unittest.TestCase):
target_type = sf.targetType('""')
self.assertEqual(None, target_type)
-
def test_modules_producing(self):
"""
Test modulesProducing(self, events)
@@ -726,6 +725,20 @@ class TestSpiderFoot(unittest.TestCase):
robots_txt = sf.parseRobotsTxt(None)
self.assertIsInstance(robots_txt, list)
+ robots_txt = sf.parseRobotsTxt("")
+ self.assertIsInstance(robots_txt, list)
+
+ robots_txt = sf.parseRobotsTxt([])
+ self.assertIsInstance(robots_txt, list)
+
+ robots_txt = sf.parseRobotsTxt("disallow:")
+ self.assertIsInstance(robots_txt, list)
+ self.assertFalse(robots_txt)
+
+ robots_txt = sf.parseRobotsTxt("disallow: /disallowed/path\n")
+ self.assertIsInstance(robots_txt, list)
+ self.assertIn("/disallowed/path", robots_txt)
+
def test_parse_emails_should_return_list(self):
"""
Test parseEmails(self, data)