Skip to content

Commit

Permalink
Forbid NULL byte in secret
Browse files Browse the repository at this point in the history
  • Loading branch information
LilDojd committed Nov 19, 2024
1 parent 718a44d commit 1c106ac
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions alchemiscale/security/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
# this is deliberately higher than any reasonable key length
# this is the same max size that `passlib` defaults to
MAX_SECRET_SIZE = 4096
# Bcrypt truncates the secret at first NULL it encounters. For this reason,
# passlib forbids NULL bytes in the secret. This is not necessary for backwards
# compatibility, but we follow passlib's lead.
_BNULL = b"\x00"


class BcryptPasswordHandler(object):
Expand Down Expand Up @@ -52,14 +56,16 @@ def verify(self, key: str, hashed_salted: str) -> bool:
pwd_context = BcryptPasswordHandler()


def validate_secret(secret):
def validate_secret(secret: str):
"""ensure secret has correct type & size"""
if not isinstance(secret, (str, bytes)):
raise TypeError("secret must be a string or bytes")
if not isinstance(secret, str):
raise TypeError("secret must be a string")

Check warning on line 62 in alchemiscale/security/auth.py

View check run for this annotation

Codecov / codecov/patch

alchemiscale/security/auth.py#L62

Added line #L62 was not covered by tests
if len(secret) > MAX_SECRET_SIZE:
raise ValueError(

Check warning on line 64 in alchemiscale/security/auth.py

View check run for this annotation

Codecov / codecov/patch

alchemiscale/security/auth.py#L64

Added line #L64 was not covered by tests
f"secret is too long, maximum length is {MAX_SECRET_SIZE} characters"
)
if _BNULL in secret.encode("utf-8"):
raise ValueError("secret contains NULL byte")

Check warning on line 68 in alchemiscale/security/auth.py

View check run for this annotation

Codecov / codecov/patch

alchemiscale/security/auth.py#L68

Added line #L68 was not covered by tests


def generate_secret_key():
Expand Down

0 comments on commit 1c106ac

Please sign in to comment.