Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Password validtion added to prevent security issue with bcrypt hashing #866

Open
wants to merge 7 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/api/src/app/auth/dtos/register-user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsDefined, IsString, IsEmail, IsOptional } from 'class-validator';
import { IsDefined, IsString, IsEmail, IsOptional, MaxLength } from 'class-validator';

export class RegisterUserDto {
@ApiProperty({
Expand Down Expand Up @@ -28,6 +28,7 @@ export class RegisterUserDto {
})
@IsString()
@IsDefined()
@MaxLength(24)
password: string;

@ApiProperty({
Expand Down
3 changes: 2 additions & 1 deletion apps/api/src/app/auth/dtos/reset-password.dto.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsDefined, IsString, IsUUID } from 'class-validator';
import { IsDefined, IsString, IsUUID, MaxLength } from 'class-validator';

export class ResetPasswordDto {
@IsString()
@IsDefined()
@MaxLength(24)
@ApiProperty({
description: 'New password of the user',
})
Expand Down
3 changes: 2 additions & 1 deletion apps/web/config/constants.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const CONSTANTS = {
'An error occurred with the payment. No amount has been deducted. Please try again later or contact the support team.',
SUBSCRIPTION_ACTIVATED_TITLE: 'Subscription activated',
SUBSCRIPTION_FAILED_TITLE: 'Payment failed',
MAX_PASSWORD_LENGTH: 24
};

export const VARIABLES = {
Expand Down Expand Up @@ -463,7 +464,7 @@ export enum PLANCODEENUM {
GROWTH_YEARLY = 'GROWTH-YEARLY',
STARTER = 'STARTER',
}
export const plans: { monthly: Plan[]; yearly: Plan[] } = {
export const plans: { monthly: Plan[]; yearly: Plan[]; } = {
monthly: [
{
name: 'Starter (Default)',
Expand Down
15 changes: 13 additions & 2 deletions apps/web/hooks/auth/useResetPassword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useRouter } from 'next/router';
import { useForm } from 'react-hook-form';
import { useMutation } from '@tanstack/react-query';

import { API_KEYS, ROUTES } from '@config';
import { API_KEYS, CONSTANTS, ROUTES } from '@config';
import { commonApi } from '@libs/api';
import { IErrorObject, ILoginResponse, SCREENS } from '@impler/shared';
import { track } from '@libs/amplitude';
Expand All @@ -19,7 +19,11 @@ interface IResetPasswordData extends IResetPasswordFormData {

export function useResetPassword() {
const { push, query } = useRouter();
const { register, handleSubmit } = useForm<IResetPasswordFormData>();
const {
register,
handleSubmit,
setError,
formState: { errors }, } = useForm<IResetPasswordFormData>();
const {
mutate: resetPassword,
isLoading: isResetPasswordLoading,
Expand Down Expand Up @@ -49,6 +53,12 @@ export function useResetPassword() {
};

const onResetPassword = (data: IResetPasswordFormData) => {
if (data.password && data.password.length > CONSTANTS.MAX_PASSWORD_LENGTH) {
setError("password", {
type: "manual",
message: `Password length must be less than ${CONSTANTS.MAX_PASSWORD_LENGTH}!`
});
}
resetPassword({
...data,
token: query.token as string,
Expand All @@ -57,6 +67,7 @@ export function useResetPassword() {

return {
error,
errors,
isError,
register,
goToLogin,
Expand Down
11 changes: 9 additions & 2 deletions apps/web/hooks/auth/useSignup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useForm } from 'react-hook-form';
import { useMutation, useQuery } from '@tanstack/react-query';

import { notify } from '@libs/notify';
import { API_KEYS, NOTIFICATION_KEYS, ROUTES } from '@config';
import { API_KEYS, CONSTANTS, NOTIFICATION_KEYS, ROUTES } from '@config';
import { commonApi } from '@libs/api';
import { track } from '@libs/amplitude';
import { useAppState } from 'store/app.context';
Expand Down Expand Up @@ -40,7 +40,7 @@ export function useSignup() {
const [isInvitationLink, setIsInvitationLink] = useState<boolean | undefined>();
const invitationId = query.invitationId as string | undefined;

const { isLoading: isAcceptingInvitation, isError } = useQuery<any, IErrorObject, { email: string }>(
const { isLoading: isAcceptingInvitation, isError } = useQuery<any, IErrorObject, { email: string; }>(
[API_KEYS.GET_TEAM_INVITATIONS, invitationId],
() =>
commonApi(API_KEYS.GET_TEAM_INVITATIONS as any, {
Expand Down Expand Up @@ -99,6 +99,13 @@ export function useSignup() {
});

const onSignup = (data: ISignupFormData) => {
if (data.password && data.password.length > CONSTANTS.MAX_PASSWORD_LENGTH) {
setError("password", {
type: "manual",
message: `Password length must be less than ${CONSTANTS.MAX_PASSWORD_LENGTH}!`
});
return;
}
const signupData: ISignupData = {
firstName: data.fullName.split(' ')[0],
lastName: data.fullName.split(' ')[1],
Expand Down
8 changes: 4 additions & 4 deletions apps/web/pages/auth/reset/[token].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { OnboardLayout } from '@layouts/OnboardLayout';
import { useResetPassword } from '@hooks/auth/useResetPassword';
import { PLACEHOLDERS, ROUTES, colors } from '@config';

export default function ResetPasswordPage({}) {
const { register, resetPassword, error, isError } = useResetPassword();
export default function ResetPasswordPage({ }) {
const { register, resetPassword, error, isError, errors } = useResetPassword();

return (
<>
Expand Down Expand Up @@ -44,9 +44,9 @@ export default function ResetPasswordPage({}) {
Back to <Link href={ROUTES.SIGNIN}>Signin</Link>
</Text>
</Stack>
{isError && (
{isError || errors.password.message && (
<Text mt={20} size="md" align="center" color={colors.danger}>
{error?.message}
{error?.message || errors.password.message}
</Text>
)}
</form>
Expand Down
Loading