From 6e45634e8d530f45a9f96786990c44bb0b15caf5 Mon Sep 17 00:00:00 2001 From: vinod-techstax Date: Fri, 11 Oct 2024 10:36:18 +0530 Subject: [PATCH 1/5] Fix #2297:field should not allow only whitespace values --- .../app/(app)/apis/[apiId]/settings/default-prefix.tsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/apps/dashboard/app/(app)/apis/[apiId]/settings/default-prefix.tsx b/apps/dashboard/app/(app)/apis/[apiId]/settings/default-prefix.tsx index 56ee285285..a3f2d9f2c9 100644 --- a/apps/dashboard/app/(app)/apis/[apiId]/settings/default-prefix.tsx +++ b/apps/dashboard/app/(app)/apis/[apiId]/settings/default-prefix.tsx @@ -53,6 +53,9 @@ export const DefaultPrefix: React.FC = ({ keyAuth }) => { }, }); async function onSubmit(values: z.infer) { + if (values.defaultPrefix.trim() === '') { + return toast.error("Default prefix cannot be empty or contain only spaces."); + } if (values.defaultPrefix.length > 8) { return toast.error("Default prefix is too long, maximum length is 8 characters."); } From 07a548196c9cceb0b1527fe6c853ff2363a78d7d Mon Sep 17 00:00:00 2001 From: Vinod_Mane Date: Sun, 13 Oct 2024 23:56:20 +0530 Subject: [PATCH 2/5] added whitespace validation in trpc and client side --- .../app/(app)/apis/[apiId]/settings/default-prefix.tsx | 2 +- apps/dashboard/lib/trpc/routers/api/setDefaultPrefix.ts | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/apps/dashboard/app/(app)/apis/[apiId]/settings/default-prefix.tsx b/apps/dashboard/app/(app)/apis/[apiId]/settings/default-prefix.tsx index a3f2d9f2c9..d6a50b12ba 100644 --- a/apps/dashboard/app/(app)/apis/[apiId]/settings/default-prefix.tsx +++ b/apps/dashboard/app/(app)/apis/[apiId]/settings/default-prefix.tsx @@ -53,7 +53,7 @@ export const DefaultPrefix: React.FC = ({ keyAuth }) => { }, }); async function onSubmit(values: z.infer) { - if (values.defaultPrefix.trim() === '') { + if (values.defaultPrefix.includes(' ')) { return toast.error("Default prefix cannot be empty or contain only spaces."); } if (values.defaultPrefix.length > 8) { diff --git a/apps/dashboard/lib/trpc/routers/api/setDefaultPrefix.ts b/apps/dashboard/lib/trpc/routers/api/setDefaultPrefix.ts index 1d3ec5681c..d362b44a7d 100644 --- a/apps/dashboard/lib/trpc/routers/api/setDefaultPrefix.ts +++ b/apps/dashboard/lib/trpc/routers/api/setDefaultPrefix.ts @@ -8,7 +8,12 @@ import { rateLimitedProcedure, ratelimit } from "@/lib/trpc/ratelimitProcedure"; export const setDefaultApiPrefix = rateLimitedProcedure(ratelimit.update) .input( z.object({ - defaultPrefix: z.string().max(8, "Prefix can be a maximum of 8 characters"), + defaultPrefix: z + .string() + .max(8, "Prefix can be a maximum of 8 characters") + .refine(prefix => !prefix.includes(' '), { + message: "Default prefix cannot contain spaces.", + }), keyAuthId: z.string(), workspaceId: z.string(), }), From 4ed37add1991b6bba2d79682752b3ec06831e025 Mon Sep 17 00:00:00 2001 From: Vinod_Mane Date: Mon, 14 Oct 2024 00:12:14 +0530 Subject: [PATCH 3/5] added prefix validation for Keys --- .../(app)/apis/[apiId]/keys/[keyAuthId]/new/client.tsx | 10 ++++++++++ apps/dashboard/lib/trpc/routers/key/create.ts | 8 +++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/new/client.tsx b/apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/new/client.tsx index b63ffd734e..eec637ffe2 100644 --- a/apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/new/client.tsx +++ b/apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/new/client.tsx @@ -57,6 +57,9 @@ const formSchema = z.object({ prefix: z .string() .max(8, { message: "Please limit the prefix to under 8 characters." }) + .refine(prefix => !prefix.includes(' '), { + message: "Prefix cannot contain spaces.", + }) .optional(), ownerId: z.string().optional(), name: z.string().optional(), @@ -184,6 +187,13 @@ export const CreateKey: React.FC = ({ apiId, keyAuthId, defaultBytes, def async function onSubmit(values: z.infer) { // make sure they aren't sent to the server if they are disabled. + + console.log({val: values.prefix}) + + if (values.prefix && values.prefix.includes(' ')) { + return toast.error("Default prefix cannot contain spaces."); + } + if (!values.expireEnabled) { delete values.expires; } diff --git a/apps/dashboard/lib/trpc/routers/key/create.ts b/apps/dashboard/lib/trpc/routers/key/create.ts index 46f2b0e6fc..3ff3d0d6e2 100644 --- a/apps/dashboard/lib/trpc/routers/key/create.ts +++ b/apps/dashboard/lib/trpc/routers/key/create.ts @@ -9,7 +9,13 @@ import { z } from "zod"; export const createKey = rateLimitedProcedure(ratelimit.create) .input( z.object({ - prefix: z.string().optional(), + prefix: z + .string() + .max(8, { message: "Please limit the prefix to under 8 characters." }) + .refine(prefix => !prefix.includes(' '), { + message: "Prefix cannot contain spaces.", + }) + .optional(), bytes: z.number().int().gte(1).default(16), keyAuthId: z.string(), ownerId: z.string().nullish(), From 814f1875bb7ec83694d5d5afb3896e4ea9889beb Mon Sep 17 00:00:00 2001 From: Vinod_Mane Date: Mon, 14 Oct 2024 00:18:39 +0530 Subject: [PATCH 4/5] removed console log and state statement which is not required --- .../app/(app)/apis/[apiId]/keys/[keyAuthId]/new/client.tsx | 7 ------- 1 file changed, 7 deletions(-) diff --git a/apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/new/client.tsx b/apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/new/client.tsx index eec637ffe2..52edbd989b 100644 --- a/apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/new/client.tsx +++ b/apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/new/client.tsx @@ -187,13 +187,6 @@ export const CreateKey: React.FC = ({ apiId, keyAuthId, defaultBytes, def async function onSubmit(values: z.infer) { // make sure they aren't sent to the server if they are disabled. - - console.log({val: values.prefix}) - - if (values.prefix && values.prefix.includes(' ')) { - return toast.error("Default prefix cannot contain spaces."); - } - if (!values.expireEnabled) { delete values.expires; } From 7ad275d084f424c551ecc8b062d9ce35dcd9b611 Mon Sep 17 00:00:00 2001 From: Vinod_Mane Date: Mon, 14 Oct 2024 00:22:19 +0530 Subject: [PATCH 5/5] changes message --- .../app/(app)/apis/[apiId]/settings/default-prefix.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/dashboard/app/(app)/apis/[apiId]/settings/default-prefix.tsx b/apps/dashboard/app/(app)/apis/[apiId]/settings/default-prefix.tsx index d6a50b12ba..40fb47fb28 100644 --- a/apps/dashboard/app/(app)/apis/[apiId]/settings/default-prefix.tsx +++ b/apps/dashboard/app/(app)/apis/[apiId]/settings/default-prefix.tsx @@ -54,7 +54,7 @@ export const DefaultPrefix: React.FC = ({ keyAuth }) => { }); async function onSubmit(values: z.infer) { if (values.defaultPrefix.includes(' ')) { - return toast.error("Default prefix cannot be empty or contain only spaces."); + return toast.error("Default prefix cannot contain spaces."); } if (values.defaultPrefix.length > 8) { return toast.error("Default prefix is too long, maximum length is 8 characters.");