Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
Prevent concurrent environment mutations and validate names
Andrew Huang9a876bcaebbc
diff --git a/frontend/src/components/permissions/ProjectPermissionCan.tsx b/frontend/src/components/permissions/ProjectPermissionCan.tsxindex 71d9e24ff2..3a8755783c 100644--- a/frontend/src/components/permissions/ProjectPermissionCan.tsx+++ b/frontend/src/components/permissions/ProjectPermissionCan.tsx@@ -6,12 +6,13 @@ import {AccessRestrictedDialog,AccessRestrictedNotice,TAccessRestrictedRequirement,+ Tooltip,+ TooltipContent,+ TooltipTrigger,toPermissionRequirement} from "@app/components/v3";import { ProjectPermissionSet, useProjectPermission } from "@app/context/ProjectPermissionContext";-import { Tooltip } from "../v2/Tooltip";-export const ProjectPermissionGuardBanner = ({requirement,accessRestrictedMode = "notice"@@ -69,11 +70,21 @@ export const ProjectPermissionCan: FunctionComponent<Props<ProjectPermissionSet>}if (!isAllowed && passThrough) {- return <Tooltip content={label}>{finalChild}</Tooltip>;+ return (+ <Tooltip>+ <TooltipTrigger asChild>{finalChild}</TooltipTrigger>+ <TooltipContent>{label}</TooltipContent>+ </Tooltip>+ );}if (isAllowed && renderTooltip && allowedLabel) {- return <Tooltip content={allowedLabel}>{finalChild}</Tooltip>;+ return (+ <Tooltip>+ <TooltipTrigger asChild>{finalChild}</TooltipTrigger>+ <TooltipContent>{allowedLabel}</TooltipContent>+ </Tooltip>+ );}if (!isAllowed) return null;diff --git a/frontend/src/pages/secret-manager/SettingsPage/components/EnvironmentSection/EnvironmentSection.tsx b/frontend/src/pages/secret-manager/SettingsPage/components/EnvironmentSection/EnvironmentSection.tsxindex 893d69f427..4d423b1538 100644--- a/frontend/src/pages/secret-manager/SettingsPage/components/EnvironmentSection/EnvironmentSection.tsx+++ b/frontend/src/pages/secret-manager/SettingsPage/components/EnvironmentSection/EnvironmentSection.tsx@@ -69,7 +69,7 @@ export const EnvironmentSection = () => {| undefined;const onEnvDeleteSubmit = async () => {- if (!currentProject?.id || !deleteEnvData?.id) return;+ if (!currentProject?.id || !deleteEnvData?.id || deleteWsEnvironment.isPending) return;await deleteWsEnvironment.mutateAsync({projectId: currentProject.id,@@ -85,13 +85,13 @@ export const EnvironmentSection = () => {};const onSwitchToHardDelete = () => {- if (!deleteEnvData) return;+ if (!deleteEnvData || deleteWsEnvironment.isPending) return;handlePopUpClose("deleteEnv");handlePopUpOpen("hardDeleteEnv", deleteEnvData);};const onEnvHardDeleteSubmit = async () => {- if (!currentProject?.id || !hardDeleteEnvData?.id) return;+ if (!currentProject?.id || !hardDeleteEnvData?.id || deleteWsEnvironment.isPending) return;await deleteWsEnvironment.mutateAsync({projectId: currentProject.id,@@ -126,13 +126,14 @@ export const EnvironmentSection = () => {variant="project"size="sm"onClick={() => {+ if (deleteWsEnvironment.isPending) return;if (isMoreEnvironmentsAllowed) {handlePopUpOpen("createEnv");} else {handlePopUpOpen("upgradePlan");}}}- isDisabled={!isAllowed}+ isDisabled={!isAllowed || deleteWsEnvironment.isPending}><PlusIcon className="size-4" />Create Environment@@ -143,7 +144,10 @@ export const EnvironmentSection = () => {</CardHeader><CardContent>{permission.can(ProjectPermissionActions.Read, ProjectPermissionSub.Environments) ? (- <EnvironmentTable handlePopUpOpen={handlePopUpOpen} />+ <EnvironmentTable+ handlePopUpOpen={handlePopUpOpen}+ isDeletePending={deleteWsEnvironment.isPending}+ />) : (<PermissionDeniedBanner />)}@@ -159,7 +163,10 @@ export const EnvironmentSection = () => {/><AlertDialogopen={popUp.deleteEnv.isOpen}- onOpenChange={(isOpen) => handlePopUpToggle("deleteEnv", isOpen)}+ onOpenChange={(isOpen) => {+ if (!isOpen && deleteWsEnvironment.isPending) return;+ handlePopUpToggle("deleteEnv", isOpen);+ }}><AlertDialogContent className="sm:max-w-xl!"><AlertDialogHeader>@@ -196,12 +203,23 @@ export const EnvironmentSection = () => {</div></div><AlertDialogFooter className="sm:justify-between">- <Button variant="danger" size="sm" onClick={onSwitchToHardDelete}>+ <Button+ variant="danger"+ size="sm"+ onClick={onSwitchToHardDelete}+ isDisabled={deleteWsEnvironment.isPending}+ >Delete Permanently</Button><div className="flex gap-2">- <AlertDialogCancel>Cancel</AlertDialogCancel>- <AlertDialogAction variant="project" onClick={onEnvDeleteSubmit}>+ <AlertDialogCancel isDisabled={deleteWsEnvironment.isPending}>+ Cancel+ </AlertDialogCancel>+ <AlertDialogAction+ variant="project"+ onClick={onEnvDeleteSubmit}+ isPending={deleteWsEnvironment.isPending}+ >Confirm</AlertDialogAction></div>@@ -211,6 +229,7 @@ export const EnvironmentSection = () => {<AlertDialogopen={popUp.hardDeleteEnv.isOpen}onOpenChange={(isOpen) => {+ if (!isOpen && deleteWsEnvironment.isPending) return;handlePopUpToggle("hardDeleteEnv", isOpen);if (!isOpen) setHardDeleteConfirmation("");}}@@ -246,14 +265,16 @@ export const EnvironmentSection = () => {value={hardDeleteConfirmation}onChange={(e) => setHardDeleteConfirmation(e.target.value)}placeholder={hardDeleteEnvData?.slug ?? ""}+ disabled={deleteWsEnvironment.isPending}/></div><AlertDialogFooter>- <AlertDialogCancel>Cancel</AlertDialogCancel>+ <AlertDialogCancel isDisabled={deleteWsEnvironment.isPending}>Cancel</AlertDialogCancel><Buttonvariant="danger"size="sm"onClick={onEnvHardDeleteSubmit}+ isPending={deleteWsEnvironment.isPending}isDisabled={!hardDeleteEnvData?.slug || hardDeleteConfirmation !== hardDeleteEnvData.slug}diff --git a/frontend/src/pages/secret-manager/SettingsPage/components/EnvironmentSection/EnvironmentTable.tsx b/frontend/src/pages/secret-manager/SettingsPage/components/EnvironmentSection/EnvironmentTable.tsxindex dc11f22439..2b7f8c7a41 100644--- a/frontend/src/pages/secret-manager/SettingsPage/components/EnvironmentSection/EnvironmentTable.tsx+++ b/frontend/src/pages/secret-manager/SettingsPage/components/EnvironmentSection/EnvironmentTable.tsx@@ -1,3 +1,4 @@+import { useState } from "react";import { format, intervalToDuration } from "date-fns";import {ArrowDownIcon,@@ -48,6 +49,7 @@ type EnvPayload = { name: string; slug: string; id: string; deleteAfter?: stringtype Props = {handlePopUpOpen: (popUpName: keyof UsePopUpState<[PopUpKeys]>, env: EnvPayload) => void;+ isDeletePending: boolean;};const getActorLabel = (actor: ProjectDeletedEnvActor | null): string => {@@ -76,33 +78,60 @@ const formatRemainingDuration = (target: Date): string | null => {return nonZero.map(([value, suffix]) => `${value}${suffix}`).join(" ");};-export const EnvironmentTable = ({ handlePopUpOpen }: Props) => {+export const EnvironmentTable = ({ handlePopUpOpen, isDeletePending }: Props) => {const { currentProject } = useProject();const { subscription } = useSubscription();const updateEnvironment = useUpdateWsEnvironment();const restoreEnvironment = useRestoreEnvironment();+ const [pendingAction, setPendingAction] = useState<+ | { type: "reorder"; id: string; direction: "up" | "down" }+ | { type: "restore"; id: string }+ | undefined+ >();const activeEnvironments = currentProject.environments ?? [];const deletedEnvironments = currentProject.deletedEnvironments ?? [];- const handleReorderEnv = async (id: string, position: number) => {- if (!currentProject?.id) return;+ const handleReorderEnv = async (id: string, position: number, direction: "up" | "down") => {+ if (+ !currentProject?.id ||+ isDeletePending ||+ updateEnvironment.isPending ||+ restoreEnvironment.isPending+ ) {+ return;+ }++ setPendingAction({ type: "reorder", id, direction });- await updateEnvironment.mutateAsync({- projectId: currentProject.id,- id,- position- });+ try {+ await updateEnvironment.mutateAsync({+ projectId: currentProject.id,+ id,+ position+ });- createNotification({- text: "Successfully re-ordered environments",- type: "success"- });+ createNotification({+ text: "Successfully re-ordered environments",+ type: "success"+ });+ } finally {+ setPendingAction(undefined);+ }};const handleRestoreEnv = async (id: string) => {- if (!currentProject?.id) return;+ if (+ !currentProject?.id ||+ isDeletePending ||+ updateEnvironment.isPending ||+ restoreEnvironment.isPending+ ) {+ return;+ }++ setPendingAction({ type: "restore", id });try {await restoreEnvironment.mutateAsync({@@ -120,9 +149,14 @@ export const EnvironmentTable = ({ handlePopUpOpen }: Props) => {text: message,type: "error"});+ } finally {+ setPendingAction(undefined);}};+ const isMutationPending =+ isDeletePending || updateEnvironment.isPending || restoreEnvironment.isPending;+const isMoreEnvironmentsAllowed =subscription?.environmentLimit && activeEnvironments? activeEnvironments.length <= subscription.environmentLimit@@ -170,9 +204,16 @@ export const EnvironmentTable = ({ handlePopUpOpen }: Props) => {aria-label="Move down"variant="ghost-muted"onClick={() =>- handleReorderEnv(id, Math.min(activeEnvironments.length, pos + 2))+ handleReorderEnv(id, Math.min(activeEnvironments.length, pos + 2), "down")+ }+ isPending={+ pendingAction?.type === "reorder" &&+ pendingAction.id === id &&+ pendingAction.direction === "down"+ }+ isDisabled={+ pos === activeEnvironments.length - 1 || !isAllowed || isMutationPending}- isDisabled={pos === activeEnvironments.length - 1 || !isAllowed}><ArrowDownIcon /></IconButton>@@ -186,8 +227,13 @@ export const EnvironmentTable = ({ handlePopUpOpen }: Props) => {<IconButtonaria-label="Move up"variant="ghost-muted"- onClick={() => handleReorderEnv(id, Math.max(1, pos))}- isDisabled={pos === 0 || !isAllowed}+ onClick={() => handleReorderEnv(id, Math.max(1, pos), "up")}+ isPending={+ pendingAction?.type === "reorder" &&+ pendingAction.id === id &&+ pendingAction.direction === "up"+ }+ isDisabled={pos === 0 || !isAllowed || isMutationPending}><ArrowUpIcon /></IconButton>@@ -195,7 +241,11 @@ export const EnvironmentTable = ({ handlePopUpOpen }: Props) => {</ProjectPermissionCan><DropdownMenu><DropdownMenuTrigger asChild>- <IconButton aria-label="Environment options" variant="ghost-muted">+ <IconButton+ aria-label="Environment options"+ variant="ghost-muted"+ isDisabled={isMutationPending}+ ><Ellipsis /></IconButton></DropdownMenuTrigger>@@ -208,7 +258,9 @@ export const EnvironmentTable = ({ handlePopUpOpen }: Props) => {<Tooltip open={!isMoreEnvironmentsAllowed ? undefined : false}><TooltipTrigger asChild><DropdownMenuItem- isDisabled={!isAllowed || !isMoreEnvironmentsAllowed}+ isDisabled={+ !isAllowed || !isMoreEnvironmentsAllowed || isMutationPending+ }onClick={() => handlePopUpOpen("updateEnv", { name, slug, id })}><PencilIcon />@@ -231,7 +283,7 @@ export const EnvironmentTable = ({ handlePopUpOpen }: Props) => {{(isAllowed) => (<DropdownMenuItemvariant="danger"- isDisabled={!isAllowed}+ isDisabled={!isAllowed || isMutationPending}onClick={() => handlePopUpOpen("deleteEnv", { name, slug, id })}><Trash2Icon />@@ -251,8 +303,8 @@ export const EnvironmentTable = ({ handlePopUpOpen }: Props) => {return (<TableRow key={id} className="bg-warning/[0.025]">- <TableCell className="text-mineshaft-400 line-through">{name}</TableCell>- <TableCell className="text-mineshaft-400">{slug}</TableCell>+ <TableCell className="text-muted line-through">{name}</TableCell>+ <TableCell className="text-muted">{slug}</TableCell><TableCell><div className="flex items-center justify-end gap-2"><Tooltip>@@ -269,12 +321,12 @@ export const EnvironmentTable = ({ handlePopUpOpen }: Props) => {? `${remaining} until permanent deletion`: "Will be permanently deleted soon"}</span>- <span className="text-xs text-mineshaft-300">+ <span className="text-xs text-accent">{remaining? `Scheduled for ${format(deleteAfterDate, "MMM d, yyyy, h:mm a")}`: "Awaiting the next daily cleanup sweep"}</span>- <span className="text-xs text-mineshaft-300">+ <span className="text-xs text-accent">Soft-deleted by {getActorLabel(deletedBy)} ·{" "}{format(new Date(softDeletedAt), "MMM d, yyyy")}</span>@@ -287,22 +339,26 @@ export const EnvironmentTable = ({ handlePopUpOpen }: Props) => {>{(isAllowed) => (<DropdownMenu>- <DropdownMenuTrigger asChild disabled={!isAllowed}>+ <DropdownMenuTrigger asChild disabled={!isAllowed || isMutationPending}><IconButtonaria-label="Environment options"variant="ghost-muted"- isDisabled={!isAllowed}+ isDisabled={!isAllowed || isMutationPending}><Ellipsis /></IconButton></DropdownMenuTrigger><DropdownMenuContent align="end">- <DropdownMenuItem onClick={() => handleRestoreEnv(id)}>+ <DropdownMenuItem+ isDisabled={isMutationPending}+ onClick={() => handleRestoreEnv(id)}+ ><RotateCcwIcon />Restore environment</DropdownMenuItem><DropdownMenuItemvariant="danger"+ isDisabled={isMutationPending}onClick={() =>handlePopUpOpen("hardDeleteEnv", { name, slug, id, deleteAfter })}diff --git a/frontend/src/pages/secret-manager/SettingsPage/components/EnvironmentSection/UpdateEnvironmentModal.tsx b/frontend/src/pages/secret-manager/SettingsPage/components/EnvironmentSection/UpdateEnvironmentModal.tsxindex c888ee5d49..b65ca4b0ff 100644--- a/frontend/src/pages/secret-manager/SettingsPage/components/EnvironmentSection/UpdateEnvironmentModal.tsx+++ b/frontend/src/pages/secret-manager/SettingsPage/components/EnvironmentSection/UpdateEnvironmentModal.tsx@@ -29,7 +29,7 @@ type Props = {};const schema = z.object({- name: z.string(),+ name: z.string().min(1, { message: "Environment Name field must be at least 1 character" }),slug: slugSchema({ min: 1, max: 64 })});