@@ -220,12 +220,27 @@ def __init__(self, hs):
220220 self .auth = hs .get_auth ()
221221 self .auth_handler = hs .get_auth_handler ()
222222 self .datastore = self .hs .get_datastore ()
223+ self .password_policy_handler = hs .get_password_policy_handler ()
223224 self ._set_password_handler = hs .get_set_password_handler ()
224225
225226 @interactive_auth_handler
226227 async def on_POST (self , request ):
227228 body = parse_json_object_from_request (request )
228229
230+ # we do basic sanity checks here because the auth layer will store these
231+ # in sessions. Pull out the new password provided to us.
232+ if "new_password" in body :
233+ new_password = body .pop ("new_password" )
234+ if not isinstance (new_password , str ) or len (new_password ) > 512 :
235+ raise SynapseError (400 , "Invalid password" )
236+ self .password_policy_handler .validate_password (new_password )
237+
238+ # If the password is valid, hash it and store it back on the body.
239+ # This ensures that only the hashed password is handled everywhere.
240+ if "new_password_hash" in body :
241+ raise SynapseError (400 , "Unexpected property: new_password_hash" )
242+ body ["new_password_hash" ] = await self .auth_handler .hash (new_password )
243+
229244 # there are two possibilities here. Either the user does not have an
230245 # access token, and needs to do a password reset; or they have one and
231246 # need to validate their identity.
@@ -276,12 +291,12 @@ async def on_POST(self, request):
276291 logger .error ("Auth succeeded but no known type! %r" , result .keys ())
277292 raise SynapseError (500 , "" , Codes .UNKNOWN )
278293
279- assert_params_in_dict (params , ["new_password " ])
280- new_password = params ["new_password " ]
294+ assert_params_in_dict (params , ["new_password_hash " ])
295+ new_password_hash = params ["new_password_hash " ]
281296 logout_devices = params .get ("logout_devices" , True )
282297
283298 await self ._set_password_handler .set_password (
284- user_id , new_password , logout_devices , requester
299+ user_id , new_password_hash , logout_devices , requester
285300 )
286301
287302 return 200 , {}
0 commit comments