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
improvement(settings): gate cross-project sharing actions (#7746)
Andrew Huanga473a6d5e762
diff --git a/frontend/src/context/ProjectPermissionContext/index.tsx b/frontend/src/context/ProjectPermissionContext/index.tsxindex 8360e1f99a..6e2ce316d9 100644--- a/frontend/src/context/ProjectPermissionContext/index.tsx+++ b/frontend/src/context/ProjectPermissionContext/index.tsx@@ -21,6 +21,7 @@ export {ProjectPermissionPkiSubscriberActions,ProjectPermissionPkiSyncActions,ProjectPermissionPkiTemplateActions,+ ProjectPermissionProjectFolderGrantActions,ProjectPermissionProxiedServiceActions,ProjectPermissionSub} from "./types";diff --git a/frontend/src/context/index.tsx b/frontend/src/context/index.tsxindex a5c3efe691..a8718c9224 100644--- a/frontend/src/context/index.tsx+++ b/frontend/src/context/index.tsx@@ -35,6 +35,7 @@ export {ProjectPermissionPkiSubscriberActions,ProjectPermissionPkiSyncActions,ProjectPermissionPkiTemplateActions,+ ProjectPermissionProjectFolderGrantActions,ProjectPermissionSub,useProjectPermission} from "./ProjectPermissionContext";diff --git a/frontend/src/hooks/api/projectFolderGrants/queries.tsx b/frontend/src/hooks/api/projectFolderGrants/queries.tsxindex a2e7c86638..f948148dfe 100644--- a/frontend/src/hooks/api/projectFolderGrants/queries.tsx+++ b/frontend/src/hooks/api/projectFolderGrants/queries.tsx@@ -17,7 +17,7 @@ export const projectFolderGrantKeys = {[{ grantId, sourceProjectId }, "project-folder-grant-usage"] as const};-export const useListProjectFolderGrants = (sourceProjectId: string) =>+export const useListProjectFolderGrants = (sourceProjectId: string, enabled = true) =>useQuery({queryKey: projectFolderGrantKeys.listByProject(sourceProjectId),queryFn: async () => {@@ -29,7 +29,7 @@ export const useListProjectFolderGrants = (sourceProjectId: string) =>);return data.grants;},- enabled: Boolean(sourceProjectId)+ enabled: Boolean(sourceProjectId) && enabled});export const useListProjectFolderGrantsReceived = (targetProjectId: string) =>diff --git a/frontend/src/pages/project/SettingsPage/components/CrossProjectSharingSection/CrossProjectSharingSection.tsx b/frontend/src/pages/project/SettingsPage/components/CrossProjectSharingSection/CrossProjectSharingSection.tsxindex 9559aa3dcf..0390218868 100644--- a/frontend/src/pages/project/SettingsPage/components/CrossProjectSharingSection/CrossProjectSharingSection.tsx+++ b/frontend/src/pages/project/SettingsPage/components/CrossProjectSharingSection/CrossProjectSharingSection.tsx@@ -1,4 +1,5 @@import { useMemo, useState } from "react";+import { subject } from "@casl/ability";import { useQueryClient } from "@tanstack/react-query";import axios from "axios";import { format, formatDistanceToNowStrict } from "date-fns";@@ -14,6 +15,7 @@ import {} from "lucide-react";import { createNotification } from "@app/components/notifications";+import { PermissionDeniedBanner } from "@app/components/permissions";import {Accordion,AccordionContent,@@ -44,12 +46,18 @@ import {EmptyTitle,IconButton,Input,+ Skeleton,Tooltip,TooltipContent,TooltipTrigger} from "@app/components/v3";import { apiRequest } from "@app/config/request";-import { useProject } from "@app/context";+import {+ ProjectPermissionProjectFolderGrantActions,+ ProjectPermissionSub,+ useProject,+ useProjectPermission+} from "@app/context";import {TProjectFolderGrant,useListProjectFolderGrants@@ -81,6 +89,9 @@ const groupGrantsByProject = (grants: TProjectFolderGrant[]): ProjectGroup[] =>}));};+const projectFolderGrantSubject = (environment: string, secretPath: string) =>+ subject(ProjectPermissionSub.ProjectFolderGrant, { environment, secretPath });+type DeleteProjectGrantsDialogProps = {grants: TProjectFolderGrant[];projectName: string;@@ -188,10 +199,48 @@ export const CrossProjectSharingSection = () => {const [editData, setEditData] = useState<ShareSecretsEditData | null>(null);const [deleteTarget, setDeleteTarget] = useState<ProjectGroup | null>(null);const { currentProject } = useProject();+ const { permission } = useProjectPermission();+ const canEditGrants =+ permission.can(+ ProjectPermissionProjectFolderGrantActions.CreateGrant,+ ProjectPermissionSub.ProjectFolderGrant+ ) &&+ permission.can(+ ProjectPermissionProjectFolderGrantActions.RevokeGrant,+ ProjectPermissionSub.ProjectFolderGrant+ );+ const canRevokeGrants = permission.can(+ ProjectPermissionProjectFolderGrantActions.RevokeGrant,+ ProjectPermissionSub.ProjectFolderGrant+ );- const { data: grants = [] } = useListProjectFolderGrants(currentProject.id);+ const canCreateGrant = (environment: string, secretPath: string) =>+ permission.can(+ ProjectPermissionProjectFolderGrantActions.CreateGrant,+ projectFolderGrantSubject(environment, secretPath)+ );- const projectGroups = useMemo(() => groupGrantsByProject(grants), [grants]);+ const canRevokeGrant = (environment: string, secretPath: string) =>+ permission.can(+ ProjectPermissionProjectFolderGrantActions.RevokeGrant,+ projectFolderGrantSubject(environment, secretPath)+ );++ const canCreateAnyGrant = permission+ .rulesFor(+ ProjectPermissionProjectFolderGrantActions.CreateGrant,+ ProjectPermissionSub.ProjectFolderGrant+ )+ .some((rule) => !rule.inverted);++ const canReadGrants = permission.can(+ ProjectPermissionProjectFolderGrantActions.ReadGrant,+ ProjectPermissionSub.ProjectFolderGrant+ );+ const { data: grants, isPending: isGrantsLoading } = useListProjectFolderGrants(+ currentProject.id,+ canReadGrants+ );const handleEdit = (group: ProjectGroup) => {setEditData({@@ -202,6 +251,132 @@ export const CrossProjectSharingSection = () => {setIsShareSheetOpen(true);};+ const projectGroups = useMemo(() => groupGrantsByProject(grants ?? []), [grants]);++ let grantsContent: JSX.Element;+ if (!canReadGrants) {+ grantsContent = <PermissionDeniedBanner />;+ } else if (isGrantsLoading) {+ grantsContent = (+ <div className="space-y-3" aria-label="Loading shared projects">+ <Skeleton className="h-12 w-full" />+ <Skeleton className="h-12 w-full" />+ </div>+ );+ } else if (grants?.length === 0) {+ grantsContent = (+ <Empty>+ <EmptyHeader>+ <EmptyTitle>No projects have access yet</EmptyTitle>+ <EmptyDescription>Share secrets to grant another project read access.</EmptyDescription>+ </EmptyHeader>+ </Empty>+ );+ } else {+ grantsContent = (+ <Accordion type="multiple" defaultValue={projectGroups.map((g) => g.targetProjectId)}>+ {projectGroups.map((projectGroup) => (+ <AccordionItem key={projectGroup.targetProjectId} value={projectGroup.targetProjectId}>+ <AccordionTrigger>+ <div className="flex flex-1 items-center gap-3">+ <Badge variant="project" className="gap-1.5">+ <Box className="size-3" />+ {projectGroup.targetProjectName}+ </Badge>+ <span className="text-xs text-muted">+ {projectGroup.totalSecrets}{" "}+ {projectGroup.totalSecrets === 1 ? "secret" : "secrets"} shared+ </span>+ </div>+ {(canEditGrants || canRevokeGrants || canCreateAnyGrant) && (+ <div+ className="pr-2"+ onClick={(e) => e.stopPropagation()}+ onKeyDown={(e) => e.stopPropagation()}+ role="presentation"+ >+ <DropdownMenu>+ <DropdownMenuTrigger asChild>+ <IconButton aria-label="Actions" variant="ghost-muted" size="xs">+ <EllipsisVerticalIcon className="size-4" />+ </IconButton>+ </DropdownMenuTrigger>+ <DropdownMenuContent align="end">+ {(canEditGrants ||+ projectGroup.grants.some((grant) =>+ canRevokeGrant(grant.environmentSlug, grant.secretPath)+ )) && (+ <DropdownMenuItem onClick={() => handleEdit(projectGroup)}>+ <PencilIcon className="mr-2 size-4" />+ Edit+ </DropdownMenuItem>+ )}+ {projectGroup.grants.every((grant) =>+ canRevokeGrant(grant.environmentSlug, grant.secretPath)+ ) && (+ <DropdownMenuItem+ variant="danger"+ onClick={() => setDeleteTarget(projectGroup)}+ >+ <TrashIcon className="mr-2 size-4" />+ Delete+ </DropdownMenuItem>+ )}+ </DropdownMenuContent>+ </DropdownMenu>+ </div>+ )}+ </AccordionTrigger>+ <AccordionContent className="p-6">+ <div className="rounded-md border border-border">+ <div className="grid grid-cols-[1fr_auto] items-center gap-4 border-b border-border px-4 py-2 text-xs text-muted">+ <span>Shared location in this project</span>+ <span className="w-24 text-right">Secrets shared</span>+ </div>+ {projectGroup.grants+ .sort((a, b) => {+ const envOrder = a.environmentName.localeCompare(b.environmentName);+ if (envOrder !== 0) return envOrder;+ return a.secretPath.localeCompare(b.secretPath);+ })+ .map((grant) => (+ <div+ key={grant.id}+ className="grid grid-cols-[1fr_auto] items-center gap-4 border-b border-border px-4 py-2.5 last:border-b-0"+ >+ <Tooltip>+ <TooltipTrigger asChild>+ <div className="flex w-fit items-center gap-2 text-sm">+ <Badge variant="neutral" className="gap-1.5">+ <Layers className="size-3" />+ {grant.environmentName}+ </Badge>+ <ChevronRight className="size-3.5 text-muted" />+ <FolderIcon className="size-3.5 text-muted" />+ <span>{grant.secretPath}</span>+ </div>+ </TooltipTrigger>+ <TooltipContent>+ Shared{" "}+ {formatDistanceToNowStrict(new Date(grant.createdAt), {+ addSuffix: true+ })}{" "}+ ({format(new Date(grant.createdAt), "MMM d, yyyy 'at' h:mm a")})+ </TooltipContent>+ </Tooltip>+ <span className="w-24 text-right text-sm tabular-nums">+ {grant.secretCount}+ </span>+ </div>+ ))}+ </div>+ </AccordionContent>+ </AccordionItem>+ ))}+ </Accordion>+ );+ }+const handleSheetOpenChange = (open: boolean) => {setIsShareSheetOpen(open);if (!open) setEditData(null);@@ -215,28 +390,32 @@ export const CrossProjectSharingSection = () => {Cross-Project Secret Sharing<DocumentationLinkBadge href="https://infisical.com/docs/documentation/platform/secret-reference#cross-project-secret-sharing" /></CardTitle>- <Button- variant="project"- size="sm"- onClick={() => {- setEditData(null);- setIsShareSheetOpen(true);- }}- >- <Plus className="size-3.5" />- Share Secrets- </Button>+ {canCreateAnyGrant && (+ <Button+ variant="project"+ size="sm"+ onClick={() => {+ setEditData(null);+ setIsShareSheetOpen(true);+ }}+ >+ <Plus className="size-3.5" />+ Share Secrets+ </Button>+ )}<ShareSecretsSheetisOpen={isShareSheetOpen}onOpenChange={handleSheetOpenChange}editData={editData}- existingGrants={grants}+ existingGrants={grants ?? []}+ canCreateGrant={canCreateGrant}+ canRevokeGrant={canRevokeGrant}/></div><p className="max-w-2xl text-sm text-accent">Grant another project read access to a slice of this project's secrets. The targetproject can then import them, or reference them inline with{" "}- <code className="rounded bg-mineshaft-700 px-1 py-0.5 font-mono text-xs text-yellow-200">+ <code className="rounded bg-container px-1 py-0.5 font-mono text-xs text-foreground">${"{@project-a.SECRET}"}</code>.@@ -244,112 +423,10 @@ export const CrossProjectSharingSection = () => {</CardHeader><CardContent><div className="mb-2 flex items-center gap-2">- <span className="text-sm text-mineshaft-400">Linked Projects</span>+ <span className="text-sm text-muted">Linked Projects</span><Badge variant="neutral">{projectGroups.length}</Badge></div>- {grants.length === 0 ? (- <Empty>- <EmptyHeader>- <EmptyTitle>No projects have access yet</EmptyTitle>- <EmptyDescription>- Share secrets to grant another project read access.- </EmptyDescription>- </EmptyHeader>- </Empty>- ) : (- <Accordion type="multiple" defaultValue={projectGroups.map((g) => g.targetProjectId)}>- {projectGroups.map((projectGroup) => (- <AccordionItem- key={projectGroup.targetProjectId}- value={projectGroup.targetProjectId}- >- <AccordionTrigger>- <div className="flex flex-1 items-center gap-3">- <Badge variant="project" className="gap-1.5">- <Box className="size-3" />- {projectGroup.targetProjectName}- </Badge>- <span className="text-xs text-muted">- {projectGroup.totalSecrets}{" "}- {projectGroup.totalSecrets === 1 ? "secret" : "secrets"} shared- </span>- </div>- <div- className="pr-2"- onClick={(e) => e.stopPropagation()}- onKeyDown={(e) => e.stopPropagation()}- role="presentation"- >- <DropdownMenu>- <DropdownMenuTrigger asChild>- <IconButton aria-label="Actions" variant="ghost-muted" size="xs">- <EllipsisVerticalIcon className="size-4" />- </IconButton>- </DropdownMenuTrigger>- <DropdownMenuContent align="end">- <DropdownMenuItem onClick={() => handleEdit(projectGroup)}>- <PencilIcon className="mr-2 size-4" />- Edit- </DropdownMenuItem>- <DropdownMenuItem- variant="danger"- onClick={() => setDeleteTarget(projectGroup)}- >- <TrashIcon className="mr-2 size-4" />- Delete- </DropdownMenuItem>- </DropdownMenuContent>- </DropdownMenu>- </div>- </AccordionTrigger>- <AccordionContent className="p-6">- <div className="rounded-md border border-mineshaft-600">- <div className="grid grid-cols-[1fr_auto] items-center gap-4 border-b border-mineshaft-600 px-4 py-2 text-xs text-muted">- <span>Shared location in this project</span>- <span className="w-24 text-right">Secrets shared</span>- </div>- {projectGroup.grants- .sort((a, b) => {- const envOrder = a.environmentName.localeCompare(b.environmentName);- if (envOrder !== 0) return envOrder;- return a.secretPath.localeCompare(b.secretPath);- })- .map((grant) => (- <div- key={grant.id}- className="grid grid-cols-[1fr_auto] items-center gap-4 border-b border-mineshaft-600 px-4 py-2.5 last:border-b-0"- >- <Tooltip>- <TooltipTrigger asChild>- <div className="flex w-fit items-center gap-2 text-sm">- <Badge variant="neutral" className="gap-1.5">- <Layers className="size-3" />- {grant.environmentName}- </Badge>- <ChevronRight className="size-3.5 text-muted" />- <FolderIcon className="size-3.5 text-muted" />- <span>{grant.secretPath}</span>- </div>- </TooltipTrigger>- <TooltipContent>- Shared{" "}- {formatDistanceToNowStrict(new Date(grant.createdAt), {- addSuffix: true- })}{" "}- ({format(new Date(grant.createdAt), "MMM d, yyyy 'at' h:mm a")})- </TooltipContent>- </Tooltip>- <span className="w-24 text-right text-sm tabular-nums">- {grant.secretCount}- </span>- </div>- ))}- </div>- </AccordionContent>- </AccordionItem>- ))}- </Accordion>- )}+ {grantsContent}</CardContent><DeleteProjectGrantsDialoggrants={deleteTarget?.grants ?? []}diff --git a/frontend/src/pages/project/SettingsPage/components/CrossProjectSharingSection/ShareSecretsSheet.tsx b/frontend/src/pages/project/SettingsPage/components/CrossProjectSharingSection/ShareSecretsSheet.tsxindex d15bf1cb40..0e361ef0b6 100644--- a/frontend/src/pages/project/SettingsPage/components/CrossProjectSharingSection/ShareSecretsSheet.tsx+++ b/frontend/src/pages/project/SettingsPage/components/CrossProjectSharingSection/ShareSecretsSheet.tsx@@ -61,9 +61,10 @@ type PathAdderProps = {environment: string;existingPaths: string[];onAdd: (path: string) => void;+ canCreateGrant: (environment: string, secretPath: string) => boolean;};-const PathAdder = ({ environment, existingPaths, onAdd }: PathAdderProps) => {+const PathAdder = ({ environment, existingPaths, onAdd, canCreateGrant }: PathAdderProps) => {const { currentProject } = useProject();const [value, setValue] = useState("/");@@ -80,7 +81,11 @@ const PathAdder = ({ environment, existingPaths, onAdd }: PathAdderProps) => {});const pathExists = isRoot || folders.some((f) => f.name === folderName);- const canAdd = value.trim().length > 0 && !existingPaths.includes(normalized) && pathExists;+ const canAdd =+ value.trim().length > 0 &&+ !existingPaths.includes(normalized) &&+ pathExists &&+ canCreateGrant(environment, normalized);const handleAdd = () => {if (!canAdd) return;@@ -121,6 +126,8 @@ type EnvironmentGroupRowProps = {onAddPath: (index: number, path: string) => void;onRemovePath: (index: number, pathIndex: number) => void;onRemoveGroup: (index: number) => void;+ canCreateGrant: (environment: string, secretPath: string) => boolean;+ canRevokeGrant: (environment: string, secretPath: string) => boolean;};const EnvironmentGroupRow = ({@@ -131,7 +138,9 @@ const EnvironmentGroupRow = ({onChangeEnvironment,onAddPath,onRemovePath,- onRemoveGroup+ onRemoveGroup,+ canCreateGrant,+ canRevokeGrant}: EnvironmentGroupRowProps) => {const envName = environments.find((e) => e.slug === group.environment)?.name;@@ -169,7 +178,12 @@ const EnvironmentGroupRow = ({</Select></div>)}- <IconButton variant="ghost-muted" size="xs" onClick={() => onRemoveGroup(index)}>+ <IconButton+ variant="ghost-muted"+ size="xs"+ onClick={() => onRemoveGroup(index)}+ isDisabled={group.secretPaths.some((path) => !canRevokeGrant(group.environment, path))}+ ><Trash2 /></IconButton></div>@@ -182,6 +196,7 @@ const EnvironmentGroupRow = ({type="button"className="text-muted transition-colors hover:text-foreground"onClick={() => onRemovePath(index, pathIndex)}+ disabled={!canRevokeGrant(group.environment, sp)}><X className="size-3.5" /></button>@@ -194,6 +209,7 @@ const EnvironmentGroupRow = ({environment={group.environment}existingPaths={group.secretPaths}onAdd={(path) => onAddPath(index, path)}+ canCreateGrant={canCreateGrant}/></div>)}@@ -206,13 +222,17 @@ type Props = {onOpenChange: (isOpen: boolean) => void;editData?: ShareSecretsEditData | null;existingGrants?: TProjectFolderGrant[];+ canCreateGrant?: (environment: string, secretPath: string) => boolean;+ canRevokeGrant?: (environment: string, secretPath: string) => boolean;};export const ShareSecretsSheet = ({isOpen,onOpenChange,editData,- existingGrants = []+ existingGrants = [],+ canCreateGrant = () => true,+ canRevokeGrant = () => true}: Props) => {const { currentProject } = useProject();const [groups, setGroups] = useState<EnvironmentGroup[]>([{ environment: "", secretPaths: [] }]);@@ -318,7 +338,7 @@ export const ShareSecretsSheet = ({editData.grants.forEach((g) => {const key = entryKey(g.environmentSlug, g.secretPath);- if (!currentKeys.has(key)) {+ if (!currentKeys.has(key) && canRevokeGrant(g.environmentSlug, g.secretPath)) {operations.push(deleteGrant.mutateAsync({ grantId: g.id, sourceProjectId: currentProject.id }));@@ -326,7 +346,10 @@ export const ShareSecretsSheet = ({});validEntries.forEach((entry) => {- if (!originalKeys.has(entryKey(entry.environment, entry.secretPath))) {+ if (+ !originalKeys.has(entryKey(entry.environment, entry.secretPath)) &&+ canCreateGrant(entry.environment, entry.secretPath)+ ) {operations.push(createGrant.mutateAsync({sourceProjectId: currentProject.id,@@ -341,7 +364,12 @@ export const ShareSecretsSheet = ({validEntries.forEach((entry) => {targetProjects.forEach((project) => {const grantKey = `${project.id}:${entry.environment}:${entry.secretPath}`;- if (existingGrantKeys.has(grantKey)) return;+ if (+ existingGrantKeys.has(grantKey) ||+ !canCreateGrant(entry.environment, entry.secretPath)+ ) {+ return;+ }operations.push(createGrant.mutateAsync({@@ -405,6 +433,8 @@ export const ShareSecretsSheet = ({onAddPath={handleAddPath}onRemovePath={handleRemovePath}onRemoveGroup={handleRemoveGroup}+ canCreateGrant={canCreateGrant}+ canRevokeGrant={canRevokeGrant}/>))}</div>