Skip to content

Commit

Permalink
fix: disallow null bytes in name during register (#15951)
Browse files Browse the repository at this point in the history
  • Loading branch information
miketheman committed May 13, 2024
1 parent c81fac9 commit 15a9a6c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
16 changes: 16 additions & 0 deletions tests/unit/accounts/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,22 @@ def test_name_too_long(self, pyramid_config):
== "The name is too long. Choose a name with 100 characters or less."
)

def test_name_contains_null_bytes(self, pyramid_config):
form = forms.RegistrationForm(
request=pretend.stub(),
formdata=MultiDict({"full_name": "hello\0world"}),
user_service=pretend.stub(
find_userid=pretend.call_recorder(lambda _: None)
),
captcha_service=pretend.stub(
enabled=False,
verify_response=pretend.call_recorder(lambda _: None),
),
breach_service=pretend.stub(check_password=lambda pw, tags=None: True),
)
assert not form.validate()
assert form.full_name.errors.pop() == "Null bytes are not allowed."


class TestRequestPasswordResetForm:
@pytest.mark.parametrize(
Expand Down
5 changes: 3 additions & 2 deletions warehouse/accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(self, message=None):
self.message = message

def __call__(self, form, field):
if "\x00" in field.data:
if field.data and "\x00" in field.data:
raise wtforms.validators.StopValidation(self.message)


Expand Down Expand Up @@ -349,7 +349,8 @@ class RegistrationForm( # type: ignore[misc]
"The name is too long. "
"Choose a name with 100 characters or less."
),
)
),
PreventNullBytesValidator(),
]
)
g_recaptcha_response = wtforms.StringField()
Expand Down
10 changes: 5 additions & 5 deletions warehouse/locale/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,23 @@ msgstr ""
msgid "The name is too long. Choose a name with 100 characters or less."
msgstr ""

#: warehouse/accounts/forms.py:439
#: warehouse/accounts/forms.py:440
msgid "Invalid TOTP code."
msgstr ""

#: warehouse/accounts/forms.py:456
#: warehouse/accounts/forms.py:457
msgid "Invalid WebAuthn assertion: Bad payload"
msgstr ""

#: warehouse/accounts/forms.py:525
#: warehouse/accounts/forms.py:526
msgid "Invalid recovery code."
msgstr ""

#: warehouse/accounts/forms.py:534
#: warehouse/accounts/forms.py:535
msgid "Recovery code has been previously used."
msgstr ""

#: warehouse/accounts/forms.py:564
#: warehouse/accounts/forms.py:565
msgid "The username isn't valid. Try again."
msgstr ""

Expand Down

0 comments on commit 15a9a6c

Please sign in to comment.