/
forms.py
546 lines (463 loc) · 19 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from email.errors import HeaderParseError
from email.headerregistry import Address
import disposable_email_domains
import humanize
import markupsafe
import wtforms
import wtforms.fields
import warehouse.utils.otp as otp
import warehouse.utils.webauthn as webauthn
from warehouse import forms, recaptcha
from warehouse.accounts.interfaces import (
BurnedRecoveryCode,
InvalidRecoveryCode,
NoRecoveryCodes,
TooManyFailedLogins,
)
from warehouse.accounts.models import DisableReason
from warehouse.accounts.services import RECOVERY_CODE_BYTES
from warehouse.email import (
send_password_compromised_email_hibp,
send_recovery_code_used_email,
)
from warehouse.events.tags import EventTag
from warehouse.i18n import localize as _
# Taken from passlib
MAX_PASSWORD_SIZE = 4096
# Common messages, set as constants to keep them from drifting.
INVALID_PASSWORD_MESSAGE = _("The password is invalid. Try again.")
INVALID_USERNAME_MESSAGE = _(
"The username is invalid. Usernames "
"must be composed of letters, numbers, "
"dots, hyphens and underscores. And must "
"also start and finish with a letter or number. "
"Choose a different username."
)
class PreventNullBytesValidator:
"""
Validation if field contains a null byte.
Use after `InputRequired()` but before other validators to prevent
sending null bytes to the database, which would result in `psycopg.DataError`
"""
def __init__(self, message=None):
if message is None:
message = _("Null bytes are not allowed.")
self.message = message
def __call__(self, form, field):
if "\x00" in field.data:
raise wtforms.validators.StopValidation(self.message)
class UsernameMixin:
username = wtforms.StringField(
validators=[
wtforms.validators.InputRequired(),
PreventNullBytesValidator(message=INVALID_USERNAME_MESSAGE),
]
)
def validate_username(self, field):
userid = self.user_service.find_userid(field.data)
if userid is None:
raise wtforms.validators.ValidationError(
_("No user found with that username")
)
class TOTPValueMixin:
totp_value = wtforms.StringField(
validators=[
wtforms.validators.InputRequired(),
PreventNullBytesValidator(),
wtforms.validators.Regexp(
rf"^ *([0-9] *){{{otp.TOTP_LENGTH}}}$",
message=_(
"TOTP code must be ${totp_length} digits.",
mapping={"totp_length": otp.TOTP_LENGTH},
),
),
]
)
class WebAuthnCredentialMixin:
credential = wtforms.StringField(validators=[wtforms.validators.InputRequired()])
class RecoveryCodeValueMixin:
recovery_code_value = wtforms.StringField(
validators=[
wtforms.validators.InputRequired(),
PreventNullBytesValidator(),
wtforms.validators.Regexp(
rf"^ *([0-9a-f] *){{{2*RECOVERY_CODE_BYTES}}}$",
message=_(
"Recovery Codes must be ${recovery_code_length} characters.",
mapping={"recovery_code_length": 2 * RECOVERY_CODE_BYTES},
),
),
]
)
class NewUsernameMixin:
username = wtforms.StringField(
validators=[
wtforms.validators.InputRequired(),
PreventNullBytesValidator(message=INVALID_USERNAME_MESSAGE),
wtforms.validators.Length(
max=50, message=_("Choose a username with 50 characters or less.")
),
# the regexp below must match the CheckConstraint
# for the username field in accounts.models.User
wtforms.validators.Regexp(
r"^[a-zA-Z0-9][a-zA-Z0-9._-]*[a-zA-Z0-9]$",
message=INVALID_USERNAME_MESSAGE,
),
]
)
def validate_username(self, field):
if (
self.user_service.username_is_prohibited(field.data)
or self.user_service.find_userid(field.data) is not None
):
raise wtforms.validators.ValidationError(
_(
"This username is already being used by another "
"account. Choose a different username."
)
)
class PasswordMixin:
password = wtforms.PasswordField(
validators=[
wtforms.validators.InputRequired(),
PreventNullBytesValidator(message=INVALID_PASSWORD_MESSAGE),
wtforms.validators.Length(
max=MAX_PASSWORD_SIZE,
message=_("Password too long."),
),
]
)
def __init__(
self, *args, request, action="login", check_password_metrics_tags=None, **kwargs
):
self.request = request
self.action = action
self._check_password_metrics_tags = check_password_metrics_tags
super().__init__(*args, **kwargs)
def validate_password(self, field):
userid = self.user_service.find_userid(self.username.data)
if userid is not None:
try:
if not self.user_service.check_password(
userid,
field.data,
tags=self._check_password_metrics_tags,
):
user = self.user_service.get_user(userid)
user.record_event(
tag=f"account:{self.action}:failure",
request=self.request,
additional={"reason": "invalid_password"},
)
raise wtforms.validators.ValidationError(INVALID_PASSWORD_MESSAGE)
except TooManyFailedLogins as err:
raise wtforms.validators.ValidationError(
_(
"There have been too many unsuccessful login attempts. "
"You have been locked out for ${time}. "
"Please try again later.",
mapping={
"time": humanize.naturaldelta(err.resets_in.total_seconds())
},
)
) from None
class NewPasswordMixin:
new_password = wtforms.PasswordField(
validators=[
wtforms.validators.InputRequired(),
PreventNullBytesValidator(message=INVALID_PASSWORD_MESSAGE),
wtforms.validators.Length(
max=MAX_PASSWORD_SIZE,
message=_("Password too long."),
),
forms.PasswordStrengthValidator(
user_input_fields=["full_name", "username", "email"]
),
],
)
password_confirm = wtforms.PasswordField(
validators=[
wtforms.validators.InputRequired(),
wtforms.validators.Length(
max=MAX_PASSWORD_SIZE,
message=_("Password too long."),
),
wtforms.validators.EqualTo(
"new_password", message=_("Your passwords don't match. Try again.")
),
],
)
# These fields are here to provide the various user-defined fields to the
# PasswordStrengthValidator of the new_password field, to ensure that the
# newly set password doesn't contain any of them
# NOTE: These intentionally use `DataRequired` instead of `InputRequired`,
# since they may not be form inputs (i.e., they may be precomputed).
full_name = wtforms.StringField() # May be empty
username = wtforms.StringField(validators=[wtforms.validators.DataRequired()])
email = wtforms.StringField(validators=[wtforms.validators.DataRequired()])
def __init__(self, *args, breach_service, **kwargs):
super().__init__(*args, **kwargs)
self._breach_service = breach_service
def validate_new_password(self, field):
if self._breach_service.check_password(
field.data, tags=["method:new_password"]
):
raise wtforms.validators.ValidationError(
markupsafe.Markup(self._breach_service.failure_message)
)
class NewEmailMixin:
email = wtforms.fields.EmailField(
validators=[
wtforms.validators.InputRequired(),
PreventNullBytesValidator(),
wtforms.validators.Regexp(
r".+@.+\..+", message=_("The email address isn't valid. Try again.")
),
wtforms.validators.Length(
max=254, message=_("The email address is too long. Try again.")
),
]
)
def validate_email(self, field):
# Additional checks for the validity of the address
try:
Address(addr_spec=field.data)
except (ValueError, HeaderParseError):
raise wtforms.validators.ValidationError(
_("The email address isn't valid. Try again.")
)
# Check if the domain is valid
domain = field.data.split("@")[-1]
if domain in disposable_email_domains.blocklist:
raise wtforms.validators.ValidationError(
_(
"You can't use an email address from this domain. Use a "
"different email."
)
)
# Check if this email address is already in use
userid = self.user_service.find_userid_by_email(field.data)
if userid and userid == self.user_id:
raise wtforms.validators.ValidationError(
_(
"This email address is already being used by this account. "
"Use a different email."
)
)
if userid:
raise wtforms.validators.ValidationError(
_(
"This email address is already being used "
"by another account. Use a different email."
)
)
class HoneypotMixin:
"""A mixin to catch spammers. This field should always be blank"""
confirm_form = wtforms.StringField()
class UsernameSearchForm(UsernameMixin, forms.Form):
def validate_username(self, field):
# Override this function from UsernameMixin. We don't care if the
# username is valid or not
return True
class RegistrationForm( # type: ignore[misc]
# Both `NewEmailMixin` and `NewPasswordMixin` declare an `email` field,
# we ignore the difference in implementation.
NewUsernameMixin,
NewEmailMixin,
NewPasswordMixin,
HoneypotMixin,
forms.Form,
):
full_name = wtforms.StringField(
validators=[
wtforms.validators.Length(
max=100,
message=_(
"The name is too long. "
"Choose a name with 100 characters or less."
),
)
]
)
g_recaptcha_response = wtforms.StringField()
def __init__(self, *args, recaptcha_service, user_service, **kwargs):
super().__init__(*args, **kwargs)
self.user_service = user_service
self.user_id = None
self.recaptcha_service = recaptcha_service
def validate_g_recaptcha_response(self, field):
# do required data validation here due to enabled flag being required
if self.recaptcha_service.enabled and not field.data:
raise wtforms.validators.ValidationError("Recaptcha error.")
try:
self.recaptcha_service.verify_response(field.data)
except recaptcha.RecaptchaError:
# TODO: log error
# don't want to provide the user with any detail
raise wtforms.validators.ValidationError("Recaptcha error.")
class LoginForm(PasswordMixin, UsernameMixin, forms.Form):
def __init__(self, *args, user_service, breach_service, **kwargs):
super().__init__(*args, **kwargs)
self.user_service = user_service
self.breach_service = breach_service
def validate_password(self, field):
# Before we try to validate anything, first check to see if the IP is banned
if self.request.banned.by_ip(self.request.remote_addr):
raise wtforms.validators.ValidationError(INVALID_PASSWORD_MESSAGE)
# Do our typical validation of the password.
super().validate_password(field)
userid = self.user_service.find_userid(self.username.data)
# If we have a user ID, then we'll go and check it against our breached password
# service. If the password has appeared in a breach or is otherwise compromised
# we will disable the user and reject the login.
if userid is not None:
# Now we'll check to see if the user is disabled.
is_disabled, disabled_for = self.user_service.is_disabled(userid)
if is_disabled and disabled_for == DisableReason.CompromisedPassword:
raise wtforms.validators.ValidationError(
markupsafe.Markup(self.breach_service.failure_message)
)
if self.breach_service.check_password(
field.data, tags=["method:auth", "auth_method:login_form"]
):
user = self.user_service.get_user(userid)
send_password_compromised_email_hibp(self.request, user)
self.user_service.disable_password(
user.id,
self.request,
reason=DisableReason.CompromisedPassword,
)
raise wtforms.validators.ValidationError(
markupsafe.Markup(self.breach_service.failure_message)
)
class _TwoFactorAuthenticationForm(forms.Form):
def __init__(self, *args, request, user_id, user_service, **kwargs):
super().__init__(*args, **kwargs)
self.request = request
self.user_id = user_id
self.user_service = user_service
remember_device = wtforms.BooleanField(default=False)
class TOTPAuthenticationForm(TOTPValueMixin, _TwoFactorAuthenticationForm):
def validate_totp_value(self, field):
totp_value = field.data.replace(" ", "").encode("utf8")
try:
self.user_service.check_totp_value(self.user_id, totp_value)
except (otp.InvalidTOTPError, otp.OutOfSyncTOTPError):
user = self.user_service.get_user(self.user_id)
user.record_event(
tag=EventTag.Account.LoginFailure,
request=self.request,
additional={"reason": "invalid_totp"},
)
raise wtforms.validators.ValidationError(_("Invalid TOTP code."))
class WebAuthnAuthenticationForm(WebAuthnCredentialMixin, _TwoFactorAuthenticationForm):
__params__ = ["credential"]
def __init__(self, *args, challenge, origin, rp_id, **kwargs):
super().__init__(*args, **kwargs)
self.challenge = challenge
self.origin = origin
self.rp_id = rp_id
def validate_credential(self, field):
try:
json.loads(field.data.encode("utf8"))
except json.JSONDecodeError:
raise wtforms.validators.ValidationError(
_("Invalid WebAuthn assertion: Bad payload")
)
try:
validated_credential = self.user_service.verify_webauthn_assertion(
self.user_id,
field.data.encode("utf8"),
challenge=self.challenge,
origin=self.origin,
rp_id=self.rp_id,
)
except webauthn.AuthenticationRejectedError as e:
user = self.user_service.get_user(self.user_id)
user.record_event(
tag=EventTag.Account.LoginFailure,
request=self.request,
additional={"reason": "invalid_webauthn"},
)
raise wtforms.validators.ValidationError(str(e))
self.validated_credential = validated_credential
class ReAuthenticateForm(PasswordMixin, forms.Form):
__params__ = [
"username",
"password",
"next_route",
"next_route_matchdict",
"next_route_query",
]
username = wtforms.fields.HiddenField(
validators=[wtforms.validators.InputRequired()]
)
next_route = wtforms.fields.HiddenField(
validators=[wtforms.validators.InputRequired()]
)
next_route_matchdict = wtforms.fields.HiddenField(
validators=[wtforms.validators.InputRequired()]
)
next_route_query = wtforms.fields.HiddenField(
validators=[wtforms.validators.InputRequired()]
)
def __init__(self, *args, user_service, **kwargs):
super().__init__(*args, **kwargs)
self.user_service = user_service
class RecoveryCodeAuthenticationForm(
RecoveryCodeValueMixin, _TwoFactorAuthenticationForm
):
def validate_recovery_code_value(self, field):
recovery_code_value = field.data.encode("utf-8").strip()
try:
self.user_service.check_recovery_code(self.user_id, recovery_code_value)
send_recovery_code_used_email(
self.request, self.user_service.get_user(self.user_id)
)
except (InvalidRecoveryCode, NoRecoveryCodes):
user = self.user_service.get_user(self.user_id)
user.record_event(
tag=EventTag.Account.LoginFailure,
request=self.request,
additional={"reason": "invalid_recovery_code"},
)
raise wtforms.validators.ValidationError(_("Invalid recovery code."))
except BurnedRecoveryCode:
user = self.user_service.get_user(self.user_id)
user.record_event(
tag=EventTag.Account.LoginFailure,
request=self.request,
additional={"reason": "burned_recovery_code"},
)
raise wtforms.validators.ValidationError(
_("Recovery code has been previously used.")
)
class RequestPasswordResetForm(forms.Form):
username_or_email = wtforms.StringField(
validators=[wtforms.validators.InputRequired()]
)
def __init__(self, *args, user_service, **kwargs):
super().__init__(*args, **kwargs)
self.user_service = user_service
def validate_username_or_email(self, field):
username_or_email = self.user_service.get_user_by_username(field.data)
if username_or_email is None:
username_or_email = self.user_service.get_user_by_email(field.data)
if username_or_email is None:
raise wtforms.validators.ValidationError(
_("No user found with that username or email")
)
class ResetPasswordForm(NewPasswordMixin, forms.Form):
pass