Skip to content

Commit

Permalink
sflib: Add validPhoneNumber function
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoles committed Oct 24, 2020
1 parent 5c29849 commit 6b2831f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
23 changes: 21 additions & 2 deletions sflib.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import cryptography
import dns.resolver
import netaddr
import phonenumbers
import OpenSSL
import requests
import urllib3
Expand Down Expand Up @@ -804,6 +805,7 @@ def targetType(self, target):
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"},
Expand Down Expand Up @@ -1310,16 +1312,33 @@ def validEmail(self, email):
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
Expand Down
25 changes: 25 additions & 0 deletions test/unit/test_spiderfoot.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,31 @@ def test_valid_email_should_return_a_boolean(self):
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)
Expand Down

0 comments on commit 6b2831f

Please sign in to comment.