Skip to content

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

sflib: move parseRobotsTxt to SpiderFootHelpers (#1208)

bcolese58898a614d0
diff --git a/modules/sfp_spider.py b/modules/sfp_spider.py
index 3829c8dc..19d24e80 100644
--- a/modules/sfp_spider.py
+++ b/modules/sfp_spider.py
@@ -14,7 +14,7 @@
import json
import time
-from spiderfoot import SpiderFootEvent, SpiderFootPlugin
+from spiderfoot import SpiderFootEvent, SpiderFootPlugin, SpiderFootHelpers
class sfp_spider(SpiderFootPlugin):
@@ -323,7 +323,7 @@ class sfp_spider(SpiderFootPlugin):
verify=False)
if robotsTxt['content'] is not None:
self.sf.debug('robots.txt contents: ' + robotsTxt['content'])
- self.robotsRules[targetBase] = self.sf.parseRobotsTxt(robotsTxt['content'])
+ self.robotsRules[targetBase] = SpiderFootHelpers.parseRobotsTxt(robotsTxt['content'])
if self.checkForStop():
return
diff --git a/spiderfoot/helpers.py b/spiderfoot/helpers.py
index 802c61a0..386c98f5 100644
--- a/spiderfoot/helpers.py
+++ b/spiderfoot/helpers.py
@@ -8,6 +8,7 @@ from networkx.readwrite.gexf import GEXFWriter
class SpiderFootHelpers():
"""SpiderFoot helper functions."""
+ @staticmethod
def targetType(target):
"""Return the scan target seed data type for the specified scan target input.
@@ -42,6 +43,7 @@ class SpiderFootHelpers():
return None
+ @staticmethod
def buildGraphGexf(root, title, data, flt=[]):
"""Convert supplied raw data into GEXF format (e.g. for Gephi)
@@ -93,6 +95,7 @@ class SpiderFootHelpers():
gexf = GEXFWriter(graph=graph)
return str(gexf).encode('utf-8')
+ @staticmethod
def buildGraphJson(root, data, flt=[]):
"""Convert supplied raw data into JSON format for SigmaJS.
@@ -165,6 +168,7 @@ class SpiderFootHelpers():
return json.dumps(ret)
+ @staticmethod
def buildGraphData(data, flt=list()):
"""Return a format-agnostic collection of tuples to use as the
basis for building graphs in various formats.
@@ -230,6 +234,7 @@ class SpiderFootHelpers():
mapping.add((entity, next_parent))
return mapping
+ @staticmethod
def dataParentChildToTree(data):
"""Converts a dictionary of k -> array to a nested
tree that can be digested by d3 for visualizations.
@@ -281,6 +286,7 @@ class SpiderFootHelpers():
return {"name": root, "children": get_children(root, data)}
+ @staticmethod
def validLEI(lei):
"""Check if the provided string is a valid Legal Entity Identifier (LEI).
@@ -301,3 +307,31 @@ class SpiderFootHelpers():
return False
return True
+
+ def parseRobotsTxt(robotsTxtData):
+ """Parse the contents of robots.txt.
+
+ Args:
+ robotsTxtData (str): robots.txt file contents
+
+ Returns:
+ list: list of patterns which should not be followed
+
+ Todo:
+ Check and parse User-Agent.
+
+ Fix whitespace parsing; ie, " " is not a valid disallowed path
+ """
+
+ returnArr = list()
+
+ if not isinstance(robotsTxtData, str):
+ return returnArr
+
+ for line in robotsTxtData.splitlines():
+ if line.lower().startswith('disallow:'):
+ m = re.match(r'disallow:\s*(.[^ #]*)', line, re.IGNORECASE)
+ if m:
+ returnArr.append(m.group(1))
+
+ return returnArr
diff --git a/test/unit/test_spiderfoot.py b/test/unit/test_spiderfoot.py
index b40265db..d9c0efb1 100644
--- a/test/unit/test_spiderfoot.py
+++ b/test/unit/test_spiderfoot.py
@@ -920,26 +920,6 @@ class TestSpiderFoot(unittest.TestCase):
sf.safeSSLSocket(None, None, None, None)
self.assertEqual('TBD', 'TBD')
- def test_parse_robots_txt_should_return_list(self):
- """
- Test parseRobotsTxt(self, robotsTxtData)
- """
- sf = SpiderFoot(self.default_options)
-
- invalid_types = [None, "", list(), dict()]
- for invalid_type in invalid_types:
- with self.subTest(invalid_type=invalid_type):
- robots_txt = sf.parseRobotsTxt(invalid_type)
- 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_parseHashes_should_return_a_list(self):
"""
Test parseHashes(self, data)
diff --git a/test/unit/test_spiderfoothelpers.py b/test/unit/test_spiderfoothelpers.py
index b69f35b0..e03873dc 100644
--- a/test/unit/test_spiderfoothelpers.py
+++ b/test/unit/test_spiderfoothelpers.py
@@ -124,7 +124,7 @@ class TestSpiderFootHelpers(unittest.TestCase):
def test_validLEI_should_return_a_boolean(self):
"""
- Test validLEI(self, lei)
+ Test validLEI(lei)
"""
invalid_types = [None, "", list(), dict(), int()]
for invalid_type in invalid_types:
@@ -140,3 +140,21 @@ class TestSpiderFootHelpers(unittest.TestCase):
valid_lei = SpiderFootHelpers.validLEI('7ZW8QJWVPR4P1J1KQY45')
self.assertIsInstance(valid_lei, bool)
self.assertTrue(valid_lei)
+
+ def test_parse_robots_txt_should_return_list(self):
+ """
+ Test parseRobotsTxt(robotsTxtData)
+ """
+ invalid_types = [None, "", list(), dict()]
+ for invalid_type in invalid_types:
+ with self.subTest(invalid_type=invalid_type):
+ robots_txt = SpiderFootHelpers.parseRobotsTxt(invalid_type)
+ self.assertIsInstance(robots_txt, list)
+
+ robots_txt = SpiderFootHelpers.parseRobotsTxt("disallow:")
+ self.assertIsInstance(robots_txt, list)
+ self.assertFalse(robots_txt)
+
+ robots_txt = SpiderFootHelpers.parseRobotsTxt("disallow: /disallowed/path\n")
+ self.assertIsInstance(robots_txt, list)
+ self.assertIn("/disallowed/path", robots_txt)