fix: implement missing password validation (#27634)

* fix: validate password presence and requirements

* chore: add error code for invalid passwords

* chore: avoid trimming password

* chore: add test for password validation

* fix: update password to delete user

* fix: create user password following protocol

* test: simplify deleteMeHandler password validation tests

* fix: address coderabbit

* fix: modify login params for failing tests

* fix: failing e2e tests

---------

Co-authored-by: Bandhan Majumder <[email protected]>
This commit is contained in:
Anirban SinghaandBandhan Majumder authored and GitHub committed 2026-06-22 10:37:09 +05:30
1 parent a46d5f8dd4
commit 0d164da8dd
10 files changed
+116 -23

No files matched your search

+1
View File
@@ -17,4 +17,5 @@ export enum ErrorCode {
RateLimitExceeded = "rate-limit-exceeded",
SocialIdentityProviderRequired = "social-identity-provider-required",
UserAccountLocked = "user-account-locked",
PasswordPolicyViolation = "password-policy-violation",
}
@@ -0,0 +1,63 @@
import { describe, it, expect } from "vitest";
import { ErrorCode } from "@calcom/features/auth/lib/ErrorCode";
import { HttpError } from "@calcom/lib/http-error";
import { deleteMeHandler } from "./deleteMe.handler";
import type { DeleteMeOptions } from "./deleteMe.handler";
const makeMockCtx = (role: "USER" | "ADMIN" = "USER") =>
({
user: {
id: 123,
name: "Test User",
email: "[email protected]",
role,
},
} as DeleteMeOptions["ctx"]);
describe("deleteMeHandler", () => {
describe("Password presence validation", () => {
it("should throw error when password is empty", async () => {
await expect(
deleteMeHandler({
ctx: makeMockCtx(),
input: { password: "" },
})
).rejects.toThrow(new HttpError({ statusCode: 400, message: ErrorCode.UserMissingPassword }));
});
it("should throw error when password is only whitespace", async () => {
await expect(
deleteMeHandler({
ctx: makeMockCtx(),
input: { password: " " },
})
).rejects.toThrow(new HttpError({ statusCode: 400, message: ErrorCode.UserMissingPassword }));
});
});
describe("Password policy validation", () => {
it("should throw error for a weak password (USER role)", async () => {
await expect(
deleteMeHandler({
ctx: makeMockCtx("USER"),
input: { password: "weak" },
})
).rejects.toThrow(
new HttpError({ statusCode: 400, message: ErrorCode.PasswordPolicyViolation })
);
});
it("should throw error for a weak password (ADMIN role)", async () => {
await expect(
deleteMeHandler({
ctx: makeMockCtx("ADMIN"),
input: { password: "weak" },
})
).rejects.toThrow(
new HttpError({ statusCode: 400, message: ErrorCode.PasswordPolicyViolation })
);
});
});
});
@@ -2,6 +2,7 @@ import { ErrorCode } from "@calcom/features/auth/lib/ErrorCode";
import { verifyPassword } from "@calcom/features/auth/lib/verifyPassword";
import { deleteUser } from "@calcom/features/users/lib/deleteUser";
import { symmetricDecrypt } from "@calcom/lib/crypto";
import { isPasswordValid } from "@calcom/lib/auth/isPasswordValid";
import { HttpError } from "@calcom/lib/http-error";
import { totpAuthenticatorCheck } from "@calcom/lib/totp";
import { prisma } from "@calcom/prisma";
@@ -10,7 +11,7 @@ import type { TrpcSessionUser } from "@calcom/trpc/server/types";
import type { TDeleteMeInputSchema } from "./deleteMe.schema";
type DeleteMeOptions = {
export type DeleteMeOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
};
@@ -18,7 +19,19 @@ type DeleteMeOptions = {
};
export const deleteMeHandler = async ({ ctx, input }: DeleteMeOptions) => {
// TODO: First check password is part of input and meets requirements.
if (!input.password || input.password.trim().length === 0) {
throw new HttpError({ statusCode: 400, message: ErrorCode.UserMissingPassword });
}
const password = input.password;
const strict = ctx.user.role !== "USER";
if (!isPasswordValid(password, false, strict)) {
throw new HttpError({
statusCode: 400,
message: ErrorCode.PasswordPolicyViolation,
});
}
// Check if input.password is correct
const user = await prisma.user.findUnique({
@@ -47,7 +60,7 @@ export const deleteMeHandler = async ({ ctx, input }: DeleteMeOptions) => {
throw new HttpError({ statusCode: 400, message: ErrorCode.UserMissingPassword });
}
const isCorrectPassword = await verifyPassword(input.password, user.password.hash);
const isCorrectPassword = await verifyPassword(password, user.password.hash);
if (!isCorrectPassword) {
throw new HttpError({ statusCode: 403, message: ErrorCode.IncorrectPassword });
}