From: Lance McNearney Date: Fri, 22 Nov 2013 18:30:47 +0000 (-0800) Subject: Extend Yahoo plugin to support disposable e-mail addresses X-Git-Tag: v0.3.3~2^2~2 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=15828c28c81e7d4acdca39ea527ac65917e4ac8b;p=flanker.git Extend Yahoo plugin to support disposable e-mail addresses --- diff --git a/flanker/addresslib/plugins/yahoo.py b/flanker/addresslib/plugins/yahoo.py index afcb424..ce0039f 100644 --- a/flanker/addresslib/plugins/yahoo.py +++ b/flanker/addresslib/plugins/yahoo.py @@ -3,7 +3,7 @@ ''' Email address validation plugin for yahoo.com email addresses. - Notes: + Notes for primary e-mail: 4-32 characters must start with letter @@ -25,6 +25,20 @@ 2. Length of local-part must be no more than 32 characters, and no less than 4 characters. + Notes for disposable e-mails using "AddressGuard": + + example: base-keyword@yahoo.com + + base and keyword may each be up to 32 characters + base may contain letters, numbers, underscores + base must start with a letter + keyword may contain letters and numbers + a single hyphen (-) connects the base and keyword + + Grammar: + + local-part -> alpha { [ alpha | num | underscore ] } hyphen { [ alpha | num ] } + ''' import re @@ -50,35 +64,43 @@ UNDERSCORE = re.compile(r''' \_ ''', re.MULTILINE | re.VERBOSE) +HYPHEN = re.compile(r''' + \- + ''', re.MULTILINE | re.VERBOSE) + def validate(localpart): # check string exists and not empty if not localpart: return False - # length check - l = len(localpart) - if l < 4 or l > 32: - return False - - # no more than one dot (.) - if localpart.count('.') > 1: - return False - # must start with letter - if ALPHA.match(localpart[0]) is None: + if len(localpart) < 1 or ALPHA.match(localpart[0]) is None: return False # must end with letter or digit if ALPHANUM.match(localpart[-1]) is None: return False - # grammar check - return _validate(localpart) + # only disposable addresses may contain hyphens + if HYPHEN.search(localpart): + return _validate_disposable(localpart) + # otherwise, normal validation + return _validate_primary(localpart) -def _validate(localpart): - "Grammar: local-part -> alpha { [ dot | underscore ] ( alpha | num ) }" + +def _validate_primary(localpart): + # length check + l = len(localpart) + if l < 4 or l > 32: + return False + + # no more than one dot (.) + if localpart.count('.') > 1: + return False + + # Grammar: local-part -> alpha { [ dot | underscore ] ( alpha | num ) }" stream = TokenStream(localpart) # local-part must being with alpha @@ -100,3 +122,46 @@ def _validate(localpart): return False return True + +def _validate_disposable(localpart): + # length check (base + hyphen + keyword) + l = len(localpart) + if l < 3 or l > 65: + return False + + # single hyphen + if localpart.count('-') != 1: + return False + + # Grammar: local-part -> alpha { [ alpha | num | underscore ] } hyphen { [ alpha | num ] } + stream = TokenStream(localpart) + + # must being with alpha + begin = stream.get_token(ALPHA) + if begin is None: + return False + + while True: + # alpha, num, underscore + base = stream.get_token(ALPHANUM) or stream.get_token(UNDERSCORE) + + if base is None: + break + + # hyphen + hyphen = stream.get_token(HYPHEN) + if hyphen is None: + return False + + while True: + # alpha, num + keyword = stream.get_token(ALPHANUM) + + if keyword is None: + break + + # alpha or numeric must be end of stream + if not stream.end_of_stream(): + return False + + return True \ No newline at end of file diff --git a/tests/addresslib/plugins/yahoo_test.py b/tests/addresslib/plugins/yahoo_test.py index 3582c5f..2fee321 100644 --- a/tests/addresslib/plugins/yahoo_test.py +++ b/tests/addresslib/plugins/yahoo_test.py @@ -71,6 +71,58 @@ def test_yahoo_pass(): assert_not_equal(addr, None) +def test_yahoo_disposable_pass(): + with patch.object(validate, 'mail_exchanger_lookup') as mock_method: + mock_method.side_effect = mock_exchanger_lookup + + # valid length range + for i in range(1, 32): + base = ''.join(random.choice(string.ascii_letters) for x in range(i)) + keyword = ''.join(random.choice(string.ascii_letters) for x in range(i)) + localpart = base + '-' + keyword + addr = address.validate_address(localpart + DOMAIN) + assert_not_equal(addr, None) + + # base must be letter, number, underscore + for i in string.ascii_letters + string.digits + '_': + localpart = 'aa' + str(i) + '-00' + addr = address.validate_address(localpart + DOMAIN) + assert_not_equal(addr, None) + + # keyword must be letter, number + for i in string.ascii_letters + string.digits: + localpart = 'aa-' + str(i) + '00' + addr = address.validate_address(localpart + DOMAIN) + assert_not_equal(addr, None) + + +def test_yahoo_disposable_fail(): + with patch.object(validate, 'mail_exchanger_lookup') as mock_method: + mock_method.side_effect = mock_exchanger_lookup + + # invalid length range + for i in range(0) + range(33, 40): + base = ''.join(random.choice(string.ascii_letters) for x in range(i)) + keyword = ''.join(random.choice(string.ascii_letters) for x in range(i)) + localpart = base + '-' + keyword + addr = address.validate_address(localpart + DOMAIN) + assert_equal(addr, None) + + # invalid base (must be letter, num, underscore) + invalid_chars = string.punctuation + invalid_chars = invalid_chars.replace('_', '') + for i in invalid_chars: + localpart = 'aa' + str(i) + '-00' + addr = address.validate_address(localpart + DOMAIN) + assert_equal(addr, None) + + # invalid keyword (must be letter, num) + invalid_chars = string.punctuation + for i in invalid_chars: + localpart = 'aa-' + str(i) + '00' + addr = address.validate_address(localpart + DOMAIN) + assert_equal(addr, None) + def test_yahoo_fail(): with patch.object(validate, 'mail_exchanger_lookup') as mock_method: mock_method.side_effect = mock_exchanger_lookup @@ -94,7 +146,9 @@ def test_yahoo_fail(): assert_equal(addr, None) # invalid chars (must be letter, num, underscore, or dot) + # addresses containing a dash may be a valid disposable address invalid_chars = string.punctuation + invalid_chars = invalid_chars.replace('-', '') invalid_chars = invalid_chars.replace('.', '') invalid_chars = invalid_chars.replace('_', '') for i in invalid_chars: