Skip to content

Commit e2f84fa

Browse files
cannikinjtoar
authored andcommitted
Strip resetToken and resetTokenExpiresAt from dbAuth forgotPassword handler (#6778)
* Clear reset token with built-in function * Remove any resetToken or resetTokenExpiresAt from forgotPassword handler response * Updates test for forgotPassword return data
1 parent 2ef22ac commit e2f84fa

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

packages/api/src/functions/dbAuth/DbAuthHandler.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,18 @@ export class DbAuthHandler<TUser extends Record<string | number, any>> {
507507
this.options.forgotPassword as ForgotPasswordFlowOptions
508508
).handler(this._sanitizeUser(user))
509509

510+
// remove resetToken and resetTokenExpiresAt if in the body of the
511+
// forgotPassword handler response
512+
let responseObj = response
513+
if (typeof response === 'object') {
514+
responseObj = Object.assign(response, {
515+
[this.options.authFields.resetToken]: undefined,
516+
[this.options.authFields.resetTokenExpiresAt]: undefined,
517+
})
518+
}
519+
510520
return [
511-
response ? JSON.stringify(response) : '',
521+
response ? JSON.stringify(responseObj) : '',
512522
{
513523
...this._deleteSessionHeader,
514524
},
@@ -613,14 +623,14 @@ export class DbAuthHandler<TUser extends Record<string | number, any>> {
613623
},
614624
data: {
615625
[this.options.authFields.hashedPassword]: hashedPassword,
616-
[this.options.authFields.resetToken]: null,
617-
[this.options.authFields.resetTokenExpiresAt]: null,
618626
},
619627
})
620628
} catch (e) {
621629
throw new DbAuthError.GenericError()
622630
}
623631

632+
await this._clearResetToken(user)
633+
624634
// call the user-defined handler so they can decide what to do with this user
625635
const response = await (
626636
this.options.resetPassword as ResetPasswordFlowOptions

packages/api/src/functions/dbAuth/__tests__/DbAuthHandler.test.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -762,15 +762,16 @@ describe('dbAuth', () => {
762762
// base64 characters only, except =
763763
expect(resetUser.resetToken).toMatch(/^\w{16}$/)
764764
expect(resetUser.resetTokenExpiresAt instanceof Date).toEqual(true)
765-
// response contains the user data, minus `hashedPassword` and `salt`
765+
766+
// response contains data returned from the handler
766767
expect(responseBody.id).toEqual(resetUser.id)
767768
expect(responseBody.email).toEqual(resetUser.email)
768-
expect(responseBody.resetToken).toEqual(resetUser.resetToken)
769-
expect(responseBody.resetTokenExpiresAt).toEqual(
770-
resetUser.resetTokenExpiresAt.toISOString()
771-
)
772-
expect(responseBody.hashedPassword).toEqual(undefined)
773-
expect(responseBody.salt).toEqual(undefined)
769+
770+
// response data should not include sensitive info
771+
expect(responseBody.resetToken).toBeUndefined()
772+
expect(responseBody.resetTokenExpiresAt).toBeUndefined()
773+
expect(responseBody.hashedPassword).toBeUndefined()
774+
expect(responseBody.salt).toBeUndefined()
774775
})
775776

776777
it('returns a logout session cookie', async () => {
@@ -797,6 +798,22 @@ describe('dbAuth', () => {
797798
expect.assertions(1)
798799
})
799800

801+
it('removes the token from the forgotPassword response', async () => {
802+
const user = await createDbUser()
803+
event.body = JSON.stringify({
804+
username: user.email,
805+
})
806+
options.forgotPassword.handler = (handlerUser) => {
807+
return handlerUser
808+
}
809+
const dbAuth = new DbAuthHandler(event, context, options)
810+
const response = await dbAuth.forgotPassword()
811+
const jsonResponse = JSON.parse(response[0])
812+
813+
expect(jsonResponse.resetToken).toBeUndefined()
814+
expect(jsonResponse.resetTokenExpiresAt).toBeUndefined()
815+
})
816+
800817
it('throws a generic error for an invalid client', async () => {
801818
const user = await createDbUser()
802819
event.body = JSON.stringify({

0 commit comments

Comments
 (0)