Better handling of namespace validation to fix a number of issues

- Fixes a bug which allows for underscores at the beginning of namespaces: Fixes #1849
- Allows dots and dashes for newer Docker clients: Fixes #1188
- Has the UI display better messaging associated with namespace entry
This commit is contained in:
Joseph Schorr 2016-09-20 15:21:26 -04:00
parent efbbeeb07f
commit 3a68740ff7
11 changed files with 126 additions and 21 deletions

View file

@ -7,13 +7,15 @@ import anunidecode # Don't listen to pylint's lies. This import is required.
INVALID_PASSWORD_MESSAGE = 'Invalid password, password must be at least ' + \
'8 characters and contain no whitespace.'
INVALID_USERNAME_CHARACTERS = r'[^a-z0-9_]'
VALID_CHARACTERS = string.digits + string.lowercase
MIN_USERNAME_LENGTH = 4
MAX_USERNAME_LENGTH = 30
VALID_LABEL_KEY_REGEX = r'^[a-z0-9](([a-z0-9]|[-.](?![.-]))*[a-z0-9])?$'
VALID_USERNAME_REGEX = r'^([a-z0-9]+(?:[._-][a-z0-9]+)*)$'
INVALID_USERNAME_CHARACTERS = r'[^a-z0-9_]'
def validate_label_key(label_key):
@ -29,9 +31,8 @@ def validate_email(email_address):
def validate_username(username):
# Based off the restrictions defined in the Docker Registry API spec
regex_match = (re.search(INVALID_USERNAME_CHARACTERS, username) is None)
if not regex_match:
return (False, 'Username must match expression [a-z0-9_]+')
if not re.match(VALID_USERNAME_REGEX, username):
return (False, 'Username must match expression ' + VALID_USERNAME_REGEX)
length_match = (len(username) >= MIN_USERNAME_LENGTH and len(username) <= MAX_USERNAME_LENGTH)
if not length_match:
@ -58,7 +59,6 @@ def _gen_filler_chars(num_filler_chars):
def generate_valid_usernames(input_username):
# Docker's regex: [a-z0-9]+(?:[._-][a-z0-9]+)*
normalized = input_username.encode('unidecode', 'ignore').strip().lower()
prefix = re.sub(INVALID_USERNAME_CHARACTERS, '_', normalized)[:30]
prefix = re.sub(r'_{2,}', '_', prefix)
@ -66,6 +66,9 @@ def generate_valid_usernames(input_username):
if prefix.endswith('_'):
prefix = prefix[0:len(prefix) - 1]
while prefix.startswith('_'):
prefix = prefix[1:]
num_filler_chars = max(0, MIN_USERNAME_LENGTH - len(prefix))
while num_filler_chars + len(prefix) <= MAX_USERNAME_LENGTH: