Skip to content

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

sflib: Add validPhoneNumber function

Brendan Coles6b2831f41d17
diff --git a/sflib.py b/sflib.py
index e09ced01..536b7640 100644
--- a/sflib.py
+++ b/sflib.py
@@ -34,6 +34,7 @@ from datetime import datetime
import cryptography
import dns.resolver
import netaddr
+import phonenumbers
import OpenSSL
import requests
import urllib3
@@ -804,6 +805,7 @@ class SpiderFoot:
if not target:
return None
+ # NOTE: the regex order is important
regexToType = [
{r"^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$": "IP_ADDRESS"},
{r"^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/\d+$": "NETBLOCK_OWNER"},
@@ -1310,16 +1312,33 @@ class SpiderFoot:
if len(email) < 6:
return False
- # Handle messed up encodings
+ # Skip strings with messed up URL encoding
if "%" in email:
return False
- # Handle truncated emails
+ # Skip strings which may have been truncated
if "..." in email:
return False
return True
+ def validPhoneNumber(self, phone):
+ """Check if the provided string is a valid phone number.
+
+ Args:
+ phone (str): The phone number to check.
+
+ Returns:
+ bool: string is a valid phone number
+ """
+ if not isinstance(phone, str):
+ return False
+
+ try:
+ return phonenumbers.is_valid_number(phonenumbers.parse(phone))
+ except Exception:
+ return False
+
def sanitiseInput(self, cmd):
"""Verify input command is safe to execute
diff --git a/test/unit/test_spiderfoot.py b/test/unit/test_spiderfoot.py
index c2f5ec5a..3a21952a 100644
--- a/test/unit/test_spiderfoot.py
+++ b/test/unit/test_spiderfoot.py
@@ -806,6 +806,31 @@ class TestSpiderFoot(unittest.TestCase):
self.assertIsInstance(valid_email, bool)
self.assertTrue(valid_email)
+ def test_validPhoneNumber_should_return_a_boolean(self):
+ """
+ Test validPhoneNumber(self, phone)
+ """
+ sf = SpiderFoot(dict())
+
+ invalid_types = [None, "", list(), dict(), int()]
+ for invalid_type in invalid_types:
+ with self.subTest(invalid_type=invalid_type):
+ valid_phone = sf.validPhoneNumber(invalid_type)
+ self.assertIsInstance(valid_phone, bool)
+ self.assertFalse(valid_phone)
+
+ valid_phone = sf.validPhoneNumber('+1234567890')
+ self.assertIsInstance(valid_phone, bool)
+ self.assertFalse(valid_phone)
+
+ valid_phone = sf.validPhoneNumber('+12345678901234567890')
+ self.assertIsInstance(valid_phone, bool)
+ self.assertFalse(valid_phone)
+
+ valid_phone = sf.validPhoneNumber('+12345678901')
+ self.assertIsInstance(valid_phone, bool)
+ self.assertTrue(valid_phone)
+
def test_normalize_dns(self):
"""
Test normalizeDNS(self, res)