From: Jesse Spears Date: Tue, 19 May 2015 01:02:29 +0000 (-0700) Subject: validate(): reject consecutive dots in local name X-Git-Tag: v0.4.29~3^2 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=dc5ce91d5f2c68590bd62a20e2b2c6ccd6383834;p=flanker.git validate(): reject consecutive dots in local name --- diff --git a/flanker/addresslib/plugins/gmail.py b/flanker/addresslib/plugins/gmail.py index bdf36db..232692f 100644 --- a/flanker/addresslib/plugins/gmail.py +++ b/flanker/addresslib/plugins/gmail.py @@ -9,7 +9,8 @@ must start with letter or number must end with letter or number must use letters, numbers, or dots (.) - all dots (.) are ignored + consecutive dots (..) are not permitted + dots (.) at the beginning or end are not permitted case is ignored plus (+) is allowed, everything after + is ignored 1. All characters prefixing the plus symbol (+) and stripping all dot @@ -18,12 +19,13 @@ Grammar: - local-part -> main-part [ tags ] - main-part -> gmail-prefix gmail-root gmail-suffix - tags -> { + [ gmail-root ] } - gmail-prefix -> alpha | num - gmail-root -> alpha | num | dot - gmail-suffix -> alpha | num + local-part -> main-part [ tags ] + main-part -> alphanum { [dot] alphanum } + tags -> { + [ dot-atom ] } + dot-atom -> atom { [ dot atom ] } + atom -> { A-Za-z0-9!#$%&'*+\-/=?^_`{|}~ } + alphanum -> alpha | num + dot -> . ''' import re from flanker.addresslib.tokenizer import TokenStream @@ -39,7 +41,10 @@ ALPHANUM = re.compile(r''' ''', re.MULTILINE | re.VERBOSE) PLUS = re.compile(r''' - [\+]+ + [\+] + ''', re.MULTILINE | re.VERBOSE) +DOT = re.compile(r''' + [\.] ''', re.MULTILINE | re.VERBOSE) @@ -48,23 +53,21 @@ def validate(localpart): if not localpart: return False - slpart = localpart.replace('.', '') - lparts = slpart.split('+') + lparts = localpart.split('+') real_localpart = lparts[0] + stripped_localpart = real_localpart.replace('.', '') # length check - l = len(real_localpart) + l = len(stripped_localpart) if l < 6 or l > 30: return False # must start with letter or num if ALPHANUM.match(real_localpart[0]) is None: return False - # must end with letter or num if ALPHANUM.match(real_localpart[-1]) is None: return False - # grammar check return _validate(real_localpart) @@ -72,10 +75,15 @@ def validate(localpart): def _validate(localpart): stream = TokenStream(localpart) - # get the gmail base (alpha, num, or dot) - mpart = stream.get_token(GMAIL_BASE) - if mpart is None: - return False + while True: + # get alphanumeric portion + mpart = stream.get_token(ALPHANUM) + if mpart is None: + return False + # get optional dot, must be followed by more alphanumerics + mpart = stream.get_token(DOT) + if mpart is None: + break # optional tags tgs = _tags(stream) diff --git a/flanker/addresslib/validate.py b/flanker/addresslib/validate.py index 116ad1f..7b3d034 100644 --- a/flanker/addresslib/validate.py +++ b/flanker/addresslib/validate.py @@ -86,11 +86,6 @@ def preparse_address(addr_spec): if len(parts) < 2: return None - # if we add more esp specific checks, they should be done - # with a dns lookup not string matching domain - if parts[1] == 'gmail.com' or parts[1] == 'googlemail.com': - parts[0] = parts[0].replace('.', '') - return parts diff --git a/tests/addresslib/plugins/gmail_test.py b/tests/addresslib/plugins/gmail_test.py index 2aa0d92..f15955a 100644 --- a/tests/addresslib/plugins/gmail_test.py +++ b/tests/addresslib/plugins/gmail_test.py @@ -65,8 +65,8 @@ def test_gmail_pass(): addr = address.validate_address(localpart + DOMAIN) assert_not_equal(addr, None) - # all dots (.) are ignored - for localpart in ['aaaaaa......', '......aaaaaa', 'aaa......aaa','aa...aa...aa']: + # non-consecutive dots (.) within an address are legal + for localpart in ['a.aaaaa', 'aa.aaaa', 'aaa.aaa','aa.aa.aa']: addr = address.validate_address(localpart + DOMAIN) assert_not_equal(addr, None) @@ -106,8 +106,8 @@ def test_gmail_fail(): addr = address.validate_address(localpart + DOMAIN) assert_equal(addr, None) - # all dots (.) are ignored - for localpart in ['aaaaa......', '......aaaaa', 'aaa......aa','aa...a...aa']: + # invalid consecutive dots (.) + for localpart in ['aaaaaa......', '......aaaaaa', 'aaa......aaa','aa...aa...aa']: addr = address.validate_address(localpart + DOMAIN) assert_equal(addr, None)