Skip to content

Commit b5f689e

Browse files
authored
[ENG-11761] Add password length restriction (#11811)
## Ticket https://openscience.atlassian.net/browse/ENG-11761 ## Purpose Add a password length restriction to UserResetPasswordSerializer. It should match other uses in the codebase, which looks to be a limit of 255 characters. Unrestricted user input is bad and can cause system problems. ## Changes Add password length restriction to UserResetPasswordSerializer with 255 chars
1 parent d43a0f6 commit b5f689e

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

api/users/serializers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ class Meta:
455455
class UserResetPasswordSerializer(BaseAPISerializer):
456456
uid = ser.CharField(write_only=True, required=True)
457457
token = ser.CharField(write_only=True, required=True)
458-
password = ser.CharField(write_only=True, required=True)
458+
password = ser.CharField(write_only=True, required=True, max_length=255)
459459

460460
class Meta:
461461
type_ = 'user_reset_password'

api_tests/users/views/test_user_settings_reset_password.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,28 @@ def test_post_invalid_password(self, app, url, user_one, csrf_token):
129129
res = app.post_json_api(url, payload, expect_errors=True, headers={'X-THROTTLE-TOKEN': 'test-token', 'X-CSRFToken': csrf_token})
130130
assert res.status_code == 400
131131

132+
def test_post_password_too_long(self, app, url, user_one, csrf_token):
133+
app.set_cookie(CSRF_COOKIE_NAME, csrf_token)
134+
encoded_email = urllib.parse.quote(user_one.email)
135+
url = f'{url}?email={encoded_email}'
136+
from tests.utils import capture_notifications
137+
138+
with capture_notifications():
139+
res = app.get(url)
140+
user_one.reload()
141+
payload = {
142+
'data': {
143+
'attributes': {
144+
'uid': user_one._id,
145+
'token': user_one.verification_key_v2['token'],
146+
'password': 'a' * 256,
147+
}
148+
}
149+
}
150+
151+
res = app.post_json_api(url, payload, expect_errors=True, headers={'X-THROTTLE-TOKEN': 'test-token', 'X-CSRFToken': csrf_token})
152+
assert res.status_code == 400 and res.json['errors'][0]['detail'] == 'Ensure this field has no more than 255 characters.'
153+
132154
def test_throttle(self, app, url, throttle_user, csrf_token):
133155
app.set_cookie(CSRF_COOKIE_NAME, csrf_token)
134156
encoded_email = urllib.parse.quote(throttle_user.email)

0 commit comments

Comments
 (0)