fix security vulnerability with update password API route

This commit is contained in:
Amruth Pillai
2025-01-24 21:13:24 +01:00
parent 460a40711e
commit 4c90cc1838
7 changed files with 1155 additions and 1165 deletions

View File

@ -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." };
}

View File

@ -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 } },
});
}