Skip to content

Commit e2a3fa6

Browse files
fix: make sure password requirements are in sync with Auth0 (AAI-615) (#165)
* fix: update password special characters to match OWASP list * test: update invalid special character in test * refactor: use constants to make password length limits more explicit
1 parent 1f035e3 commit e2a3fa6

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

schemas/biocommons.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,17 @@
3030
from db import models
3131

3232
# From Auth0 password settings
33-
ALLOWED_SPECIAL_CHARS = "!@#$%^&*"
33+
PASSWORD_MIN_LENGTH = 8
34+
# Auth0's max password length is 72, it "allows" more characters but silently ignores
35+
# them beyond 72. We try to explicitly set a max of 72
36+
PASSWORD_MAX_LENGTH = 72
37+
# List of special characters from:
38+
# https://owasp.org/www-community/password-special-characters
39+
ALLOWED_SPECIAL_CHARS = r""" !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
40+
# Define the regex for special characters explicitly (escaping some of them is tricky)
41+
SPECIAL_CHARS_PATTERN = r'[ !"#$%&\'()*+,\-./:;<=>?@[\\\]^_`{|}~]'
3442
VALID_PASSWORD_REGEX = re.compile(
35-
f"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[{ALLOWED_SPECIAL_CHARS}]).{{8,}}$"
43+
f"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?{SPECIAL_CHARS_PATTERN}).{{8,}}$"
3644
)
3745
PASSWORD_FORMAT_MESSAGE = (
3846
"Password must contain at least one uppercase letter, one lowercase letter, one number, "
@@ -79,9 +87,11 @@ def _check(v: str) -> str:
7987
"max_length": "Username must be 128 characters or less.",
8088
"pattern": "Username must only contain lowercase letters, numbers, hyphens and underscores."
8189
})
82-
BiocommonsPassword = ValidatedString(min_length=8, max_length=72, pattern=VALID_PASSWORD_REGEX, messages={
83-
"min_length": "Password must be at least 8 characters.",
84-
"max_length": "Password must be 72 characters or less.",
90+
BiocommonsPassword = ValidatedString(min_length=PASSWORD_MIN_LENGTH,
91+
max_length=PASSWORD_MAX_LENGTH,
92+
pattern=VALID_PASSWORD_REGEX, messages={
93+
"min_length": f"Password must be at least {PASSWORD_MIN_LENGTH} characters.",
94+
"max_length": f"Password must be {PASSWORD_MAX_LENGTH} characters or less.",
8595
"pattern": PASSWORD_FORMAT_MESSAGE
8696
})
8797
BiocommonsFullName = ValidatedString(min_length=1, max_length=300,

tests/schemas/test_biocommons_schemas.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ def test_valid_password(password: str):
4848
("AbcdEfgh!", PASSWORD_FORMAT_MESSAGE),
4949
# Missing special character
5050
("abCD1234", PASSWORD_FORMAT_MESSAGE),
51-
# Invalid special characters
52-
("Password123.", PASSWORD_FORMAT_MESSAGE),
51+
# Invalid special characters (not in OWASP list)
52+
("Password123🙂", PASSWORD_FORMAT_MESSAGE),
5353
])
5454
def test_invalid_password(password: str, expected_error: str):
5555
"""Test that invalid passwords raise appropriate validation errors."""

0 commit comments

Comments
 (0)