mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-15 09:11:57 +10:00
fix security vulnerability with update password API route
This commit is contained in:
@ -173,8 +173,11 @@ export class AuthController {
|
||||
|
||||
@Patch("password")
|
||||
@UseGuards(TwoFactorGuard)
|
||||
async updatePassword(@User("email") email: string, @Body() { password }: UpdatePasswordDto) {
|
||||
await this.authService.updatePassword(email, password);
|
||||
async updatePassword(
|
||||
@User("email") email: string,
|
||||
@Body() { currentPassword, newPassword }: UpdatePasswordDto,
|
||||
) {
|
||||
await this.authService.updatePassword(email, currentPassword, newPassword);
|
||||
|
||||
return { message: "Your password has been successfully updated." };
|
||||
}
|
||||
|
||||
@ -159,11 +159,19 @@ export class AuthService {
|
||||
await this.mailService.sendEmail({ to: email, subject, text });
|
||||
}
|
||||
|
||||
async updatePassword(email: string, password: string) {
|
||||
const hashedPassword = await this.hash(password);
|
||||
async updatePassword(email: string, currentPassword: string, newPassword: string) {
|
||||
const user = await this.userService.findOneByIdentifierOrThrow(email);
|
||||
|
||||
if (!user.secrets?.password) {
|
||||
throw new BadRequestException(ErrorMessage.OAuthUser);
|
||||
}
|
||||
|
||||
await this.validatePassword(currentPassword, user.secrets.password);
|
||||
|
||||
const newHashedPassword = await this.hash(newPassword);
|
||||
|
||||
await this.userService.updateByEmail(email, {
|
||||
secrets: { update: { password: hashedPassword } },
|
||||
secrets: { update: { password: newHashedPassword } },
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user