Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

fix: slug validation, name uniqueness check, popover close on select

bernie-g48d39d81b881
diff --git a/backend/src/db/migrations/20260414000001_add-gateway-pools.ts b/backend/src/db/migrations/20260414000001_add-gateway-pools.ts
index fa4205dc99..ad9ced4acd 100644
--- a/backend/src/db/migrations/20260414000001_add-gateway-pools.ts
+++ b/backend/src/db/migrations/20260414000001_add-gateway-pools.ts
@@ -10,7 +10,7 @@ export async function up(knex: Knex): Promise<void> {
t.uuid("id", { primaryKey: true }).defaultTo(knex.fn.uuid());
t.uuid("orgId").notNullable();
t.foreign("orgId").references("id").inTable(TableName.Organization).onDelete("CASCADE");
- t.string("name", 255).notNullable();
+ t.string("name", 32).notNullable();
t.timestamps(true, true, true);
t.unique(["orgId", "name"]);
});
diff --git a/backend/src/ee/routes/v2/gateway-pool-router.ts b/backend/src/ee/routes/v2/gateway-pool-router.ts
index ebe688b020..0382acf73f 100644
--- a/backend/src/ee/routes/v2/gateway-pool-router.ts
+++ b/backend/src/ee/routes/v2/gateway-pool-router.ts
@@ -3,6 +3,7 @@ import z from "zod";
import { GatewayPoolMembershipsSchema, GatewayPoolsSchema, GatewaysV2Schema } from "@app/db/schemas";
import { EventType } from "@app/ee/services/audit-log/audit-log-types";
import { readLimit, writeLimit } from "@app/server/config/rateLimiter";
+import { slugSchema } from "@app/server/lib/schemas";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { AuthMode } from "@app/services/auth/auth-type";
@@ -29,7 +30,7 @@ export const registerGatewayPoolRouter = async (server: FastifyZodProvider) => {
schema: {
operationId: "createGatewayPool",
body: z.object({
- name: z.string().min(1).max(255).describe("Name for the gateway pool")
+ name: slugSchema({ min: 1, max: 32, field: "name" }).describe("Name for the gateway pool")
}),
response: {
200: SanitizedGatewayPoolSchema
@@ -118,7 +119,7 @@ export const registerGatewayPoolRouter = async (server: FastifyZodProvider) => {
poolId: z.string().uuid()
}),
body: z.object({
- name: z.string().min(1).max(255).optional().describe("New name for the pool")
+ name: slugSchema({ min: 1, max: 32, field: "name" }).optional().describe("New name for the pool")
}),
response: {
200: SanitizedGatewayPoolSchema
diff --git a/frontend/src/pages/organization/NetworkingPage/components/GatewayTab/components/CreateGatewayPoolModal.tsx b/frontend/src/pages/organization/NetworkingPage/components/GatewayTab/components/CreateGatewayPoolModal.tsx
index 8fdb880148..19a1352aaa 100644
--- a/frontend/src/pages/organization/NetworkingPage/components/GatewayTab/components/CreateGatewayPoolModal.tsx
+++ b/frontend/src/pages/organization/NetworkingPage/components/GatewayTab/components/CreateGatewayPoolModal.tsx
@@ -1,17 +1,15 @@
-import { useForm } from "react-hook-form";
-import { zodResolver } from "@hookform/resolvers/zod";
+import { useMemo, useState } from "react";
import { z } from "zod";
import { createNotification } from "@app/components/notifications";
import { Button, FormControl, Input, Modal, ModalContent } from "@app/components/v2";
-import { useCreateGatewayPool } from "@app/hooks/api/gateway-pools";
+import { useCreateGatewayPool, useListGatewayPools } from "@app/hooks/api/gateway-pools";
+import { slugSchema } from "@app/lib/schemas";
-const schema = z.object({
- name: z.string().min(1, "Name is required").max(255)
+const formSchema = z.object({
+ name: slugSchema({ field: "name" })
});
-type FormData = z.infer<typeof schema>;
-
type Props = {
isOpen: boolean;
onToggle: (isOpen: boolean) => void;
@@ -19,24 +17,40 @@ type Props = {
export const CreateGatewayPoolModal = ({ isOpen, onToggle }: Props) => {
const createPool = useCreateGatewayPool();
+ const { data: pools } = useListGatewayPools();
+ const [name, setName] = useState("");
+ const [formErrors, setFormErrors] = useState<z.ZodIssue[]>([]);
- const {
- register,
- handleSubmit,
- reset,
- formState: { errors, isSubmitting }
- } = useForm<FormData>({
- resolver: zodResolver(schema),
- defaultValues: { name: "" }
- });
+ const errors = useMemo(() => {
+ const errorMap: Record<string, string | undefined> = {};
+ formErrors.forEach((issue) => {
+ if (issue.path.length > 0) errorMap[String(issue.path[0])] = issue.message;
+ });
+ return errorMap;
+ }, [formErrors]);
- const onSubmit = async (data: FormData) => {
- try {
- await createPool.mutateAsync({
- name: data.name
+ const handleSubmit = async () => {
+ setFormErrors([]);
+ const validation = formSchema.safeParse({ name });
+ if (!validation.success) {
+ setFormErrors(validation.error.issues);
+ return;
+ }
+
+ const existingNames = pools?.map((p) => p.name) || [];
+ if (existingNames.includes(name.trim())) {
+ createNotification({
+ type: "error",
+ text: "A gateway pool with this name already exists."
});
- createNotification({ type: "success", text: `Pool "${data.name}" created` });
- reset();
+ return;
+ }
+
+ try {
+ await createPool.mutateAsync({ name });
+ createNotification({ type: "success", text: `Pool "${name}" created` });
+ setName("");
+ setFormErrors([]);
onToggle(false);
} catch (err: unknown) {
const message = err instanceof Error ? err.message : "Failed to create pool";
@@ -45,26 +59,38 @@ export const CreateGatewayPoolModal = ({ isOpen, onToggle }: Props) => {
};
return (
- <Modal isOpen={isOpen} onOpenChange={onToggle}>
+ <Modal
+ isOpen={isOpen}
+ onOpenChange={(open) => {
+ if (!open) {
+ setName("");
+ setFormErrors([]);
+ }
+ onToggle(open);
+ }}
+ >
<ModalContent title="Create Gateway Pool">
- <form onSubmit={handleSubmit(onSubmit)}>
- <FormControl
- label="Name"
- isRequired
- isError={Boolean(errors.name)}
- errorText={errors.name?.message}
- >
- <Input {...register("name")} placeholder="e.g. us-east-1 prod VPC" autoFocus />
- </FormControl>
- <div className="mt-4 flex justify-end gap-2">
- <Button variant="outline_bg" onClick={() => onToggle(false)} type="button">
- Cancel
- </Button>
- <Button type="submit" isLoading={isSubmitting}>
- Create Pool
- </Button>
- </div>
- </form>
+ <FormControl
+ label="Name"
+ isRequired
+ isError={Boolean(errors.name)}
+ errorText={errors.name}
+ >
+ <Input
+ value={name}
+ onChange={(e) => setName(e.target.value)}
+ placeholder="Enter gateway pool name"
+ autoFocus
+ />
+ </FormControl>
+ <div className="mt-4 flex justify-end gap-2">
+ <Button variant="outline_bg" onClick={() => onToggle(false)} type="button">
+ Cancel
+ </Button>
+ <Button onClick={handleSubmit} isLoading={createPool.isPending}>
+ Create Pool
+ </Button>
+ </div>
</ModalContent>
</Modal>
);
diff --git a/frontend/src/pages/organization/NetworkingPage/components/GatewayTab/components/EditGatewayPoolModal.tsx b/frontend/src/pages/organization/NetworkingPage/components/GatewayTab/components/EditGatewayPoolModal.tsx
index 2a16a6dd5c..4addba0d69 100644
--- a/frontend/src/pages/organization/NetworkingPage/components/GatewayTab/components/EditGatewayPoolModal.tsx
+++ b/frontend/src/pages/organization/NetworkingPage/components/GatewayTab/components/EditGatewayPoolModal.tsx
@@ -1,19 +1,16 @@
-import { useEffect } from "react";
-import { useForm } from "react-hook-form";
-import { zodResolver } from "@hookform/resolvers/zod";
+import { useEffect, useMemo, useState } from "react";
import { z } from "zod";
import { createNotification } from "@app/components/notifications";
import { Button, FormControl, Input, Modal, ModalContent } from "@app/components/v2";
-import { useUpdateGatewayPool } from "@app/hooks/api/gateway-pools";
+import { useListGatewayPools, useUpdateGatewayPool } from "@app/hooks/api/gateway-pools";
import { TGatewayPool } from "@app/hooks/api/gateway-pools/types";
+import { slugSchema } from "@app/lib/schemas";
-const schema = z.object({
- name: z.string().min(1, "Name is required").max(255)
+const formSchema = z.object({
+ name: slugSchema({ field: "name" })
});
-type FormData = z.infer<typeof schema>;
-
type Props = {
isOpen: boolean;
onToggle: (isOpen: boolean) => void;
@@ -22,29 +19,45 @@ type Props = {
export const EditGatewayPoolModal = ({ isOpen, onToggle, pool }: Props) => {
const updatePool = useUpdateGatewayPool();
+ const { data: pools } = useListGatewayPools();
+ const [name, setName] = useState("");
+ const [formErrors, setFormErrors] = useState<z.ZodIssue[]>([]);
- const {
- register,
- handleSubmit,
- reset,
- formState: { errors, isSubmitting }
- } = useForm<FormData>({
- resolver: zodResolver(schema)
- });
+ const errors = useMemo(() => {
+ const errorMap: Record<string, string | undefined> = {};
+ formErrors.forEach((issue) => {
+ if (issue.path.length > 0) errorMap[String(issue.path[0])] = issue.message;
+ });
+ return errorMap;
+ }, [formErrors]);
useEffect(() => {
if (pool) {
- reset({ name: pool.name });
+ setName(pool.name);
+ setFormErrors([]);
}
- }, [pool, reset]);
+ }, [pool]);
- const onSubmit = async (data: FormData) => {
+ const handleSubmit = async () => {
if (!pool) return;
- try {
- await updatePool.mutateAsync({
- poolId: pool.id,
- name: data.name
+ setFormErrors([]);
+ const validation = formSchema.safeParse({ name });
+ if (!validation.success) {
+ setFormErrors(validation.error.issues);
+ return;
+ }
+
+ const existingNames = pools?.filter((p) => p.id !== pool.id).map((p) => p.name) || [];
+ if (existingNames.includes(name.trim())) {
+ createNotification({
+ type: "error",
+ text: "A gateway pool with this name already exists."
});
+ return;
+ }
+
+ try {
+ await updatePool.mutateAsync({ poolId: pool.id, name });
createNotification({ type: "success", text: "Pool updated" });
onToggle(false);
} catch (err: unknown) {
@@ -56,24 +69,25 @@ export const EditGatewayPoolModal = ({ isOpen, onToggle, pool }: Props) => {
return (
<Modal isOpen={isOpen} onOpenChange={onToggle}>
<ModalContent title="Edit Gateway Pool">
- <form onSubmit={handleSubmit(onSubmit)}>
- <FormControl
- label="Name"
- isRequired
- isError={Boolean(errors.name)}
- errorText={errors.name?.message}
- >
- <Input {...register("name")} />
- </FormControl>
- <div className="mt-4 flex justify-end gap-2">
- <Button variant="outline_bg" onClick={() => onToggle(false)} type="button">
- Cancel
- </Button>
- <Button type="submit" isLoading={isSubmitting}>
- Save
- </Button>
- </div>
- </form>
+ <FormControl
+ label="Name"
+ isRequired
+ isError={Boolean(errors.name)}
+ errorText={errors.name}
+ >
+ <Input
+ value={name}
+ onChange={(e) => setName(e.target.value)}
+ />
+ </FormControl>
+ <div className="mt-4 flex justify-end gap-2">
+ <Button variant="outline_bg" onClick={() => onToggle(false)} type="button">
+ Cancel
+ </Button>
+ <Button onClick={handleSubmit} isLoading={updatePool.isPending}>
+ Save
+ </Button>
+ </div>
</ModalContent>
</Modal>
);
diff --git a/frontend/src/pages/organization/NetworkingPage/components/GatewayTab/components/PoolDetailSheet.tsx b/frontend/src/pages/organization/NetworkingPage/components/GatewayTab/components/PoolDetailSheet.tsx
index 1a2d04b1ae..d425a75a72 100644
--- a/frontend/src/pages/organization/NetworkingPage/components/GatewayTab/components/PoolDetailSheet.tsx
+++ b/frontend/src/pages/organization/NetworkingPage/components/GatewayTab/components/PoolDetailSheet.tsx
@@ -1,4 +1,4 @@
-import { useMemo } from "react";
+import { useMemo, useState } from "react";
import { faEllipsisV, faHeartPulse, faPlus, faTrash } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { useQuery } from "@tanstack/react-query";
@@ -59,6 +59,7 @@ export const PoolDetailSheet = ({ isOpen, onOpenChange, pool }: Props) => {
const addGateway = useAddGatewayToPool();
const removeGateway = useRemoveGatewayFromPool();
const triggerHealthCheck = useTriggerGatewayV2Heartbeat();
+ const [isAddGatewayOpen, setIsAddGatewayOpen] = useState(false);
const { popUp, handlePopUpOpen, handlePopUpToggle } = usePopUp(["removeGateway"] as const);
@@ -150,7 +151,7 @@ export const PoolDetailSheet = ({ isOpen, onOpenChange, pool }: Props) => {
{(isAllowed: boolean) => {
const isDisabled = !isAllowed || availableGateways.length === 0;
return (
- <Popover>
+ <Popover open={isAddGatewayOpen} onOpenChange={setIsAddGatewayOpen} modal>
<PopoverTrigger asChild>
<Button
variant="outline_bg"
@@ -172,7 +173,10 @@ export const PoolDetailSheet = ({ isOpen, onOpenChange, pool }: Props) => {
key={gw.id}
value={gw.id}
keywords={[gw.name]}
- onSelect={() => handleAdd(gw.id)}
+ onSelect={() => {
+ setIsAddGatewayOpen(false);
+ handleAdd(gw.id);
+ }}
>
{gw.name}
</CommandItem>