Restrict first/last character in instance name (#5680)

Co-authored-by: Flame442 <34169552+Flame442@users.noreply.github.com>
This commit is contained in:
Jakub Kuczys 2022-11-13 23:50:02 +01:00 committed by GitHub
parent d5cdebcd76
commit 6023f9015c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -125,12 +125,23 @@ def get_storage_type(backend: Optional[str], *, interactive: bool):
def get_name(name: str) -> str: def get_name(name: str) -> str:
INSTANCE_NAME_RE = re.compile(r"[A-Za-z0-9_\.\-]*") INSTANCE_NAME_RE = re.compile(
r"""
[a-z0-9] # starts with letter or digit
(?:
(?!.*[_\.\-]{2}) # ensure no consecutive dots, hyphens, or underscores
[a-z0-9_\.\-]* # match allowed characters
[a-z0-9] # ensure string ends with letter or digit
)? # optional to allow strings of length 1
""",
re.VERBOSE | re.IGNORECASE,
)
if name: if name:
if INSTANCE_NAME_RE.fullmatch(name) is None: if INSTANCE_NAME_RE.fullmatch(name) is None:
print( print(
"ERROR: Instance names can only include characters A-z, numbers, " "ERROR: Instance names need to start and end with a letter or a number"
"underscores (_) and periods (.)." " and can only include characters A-z, numbers,"
" and non-consecutive underscores (_) and periods (.)."
) )
sys.exit(1) sys.exit(1)
return name return name
@ -139,14 +150,18 @@ def get_name(name: str) -> str:
print( print(
"Please enter a name for your instance," "Please enter a name for your instance,"
" it will be used to run your bot from here on out.\n" " it will be used to run your bot from here on out.\n"
"This name is case-sensitive and should only include characters" "This name is case-sensitive, needs to start and end with a letter or a number"
" A-z, numbers, underscores (_) and periods (.)." " and should only include characters A-z, numbers,"
" and non-consecutive underscores (_) and periods (.)."
) )
name = input("> ") name = input("> ")
if INSTANCE_NAME_RE.fullmatch(name) is None: if not name:
pass
elif INSTANCE_NAME_RE.fullmatch(name) is None:
print( print(
"ERROR: Instance names can only include characters A-z, numbers, " "ERROR: Instance names need to start and end with a letter or a number"
"underscores (_) and periods (.)." " and can only include characters A-z, numbers,"
" and non-consecutive underscores (_) and periods (.)."
) )
name = "" name = ""
elif "-" in name and not confirm( elif "-" in name and not confirm(