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
import { ReactNode, useEffect, useMemo, useState } from "react";
import { Controller, useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { useSearch } from "@tanstack/react-router";
import { format } from "date-fns";
import {
CalendarClock,
CheckCircle2,
Eye,
EyeOff,
ForwardIcon,
Info,
Lock,
MailCheck
} from "lucide-react";
import { z } from "zod";
import { createNotification } from "@app/components/notifications";
import {
Accordion,
AccordionContent,
AccordionItem,
AccordionTrigger,
Badge,
Button,
CopyButton,
EmailInput,
Field,
FieldDescription,
FieldError,
FieldGroup,
FieldLabel,
FieldLegend,
FieldSet,
Input,
InputGroup,
InputGroupAddon,
InputGroupButton,
InputGroupInput,
SecretInput,
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
Separator,
SheetFooter,
Toggle,
Tooltip,
TooltipContent,
TooltipTrigger
} from "@app/components/v3";
import { emailListSchema } from "@app/helpers/email";
import { useToggle } from "@app/hooks";
import { useCreatePublicSharedSecret, useCreateSharedSecret } from "@app/hooks/api";
import { SecretSharingAccessType } from "@app/hooks/api/secretSharing";
import { ms } from "@app/lib/fn/time";
const expiresInOptions = [
{ label: "5 minutes", value: "5m" },
{ label: "30 minutes", value: "30m" },
{ label: "1 hour", value: "1h" },
{ label: "1 day", value: "1d" },
{ label: "7 days", value: "7d" },
{ label: "14 days", value: "14d" },
{ label: "30 days", value: "30d" }
];
const MAX_RECIPIENTS = 100;
const ABSOLUTE_VIEW_CEILING = 1000;
const baseSchema = z.object({
name: z.string().max(50, "Name must be 50 characters or fewer.").optional(),
password: z.string().optional(),
secret: z.string().min(1, "Enter the value you want to share."),
expiresIn: z.string(),
viewLimit: z.string(),
shouldLimitView: z.boolean(),
accessType: z.nativeEnum(SecretSharingAccessType).optional(),
emails: emailListSchema({ max: MAX_RECIPIENTS }).optional(),
allowExternalEmails: z.boolean().optional()
});
export type FormData = z.infer<typeof baseSchema>;
const buildSchema = (maxSharedSecretViewLimit?: number | null) =>
baseSchema.superRefine((data, ctx) => {
if (data.shouldLimitView) {
const views = Number(data.viewLimit);
const ceiling = maxSharedSecretViewLimit ?? ABSOLUTE_VIEW_CEILING;
if (!Number.isInteger(views) || views < 1) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ["viewLimit"],
message: "Enter a whole number of views, 1 or greater."
});
} else if (views > ceiling) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ["viewLimit"],
message: `Enter at most ${ceiling} view${ceiling === 1 ? "" : "s"}.`
});
}
}
if (data.allowExternalEmails && data.emails?.length && !data.password) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ["password"],
message: "External recipients need a password to open the secret."
});
}
});
type ShareResult =
| { kind: "link"; url: string; expiresAt: string; views: string }
| { kind: "email"; recipientCount: number; requiresAccount: boolean };
const describeResult = (result: ShareResult) => {
if (result.kind === "link") {
return "Save the link below before leaving this page.";
}
const plural = result.recipientCount === 1 ? "" : "s";
if (result.requiresAccount) {
return `Sent to ${result.recipientCount} recipient${plural}. Only those with an Infisical account will receive a link.`;
}
return `Sent to ${result.recipientCount} recipient${plural}. Each one receives a unique link.`;
};
const formatExpiry = (expiresIn: string) => {
try {
return format(new Date(Date.now() + ms(expiresIn)), "MMM d, yyyy 'at' h:mm a");
} catch {
return null;
}
};
type Props = {
isPublic: boolean; // whether or not this is a public (non-authenticated) secret sharing form
value?: string;
secretKey?: string;
allowSecretSharingOutsideOrganization?: boolean;
maxSharedSecretLifetime?: number;
maxSharedSecretViewLimit?: number | null;
onCancel?: () => void;
onDirtyChange?: (isDirty: boolean) => void;
};
export const ShareSecretForm = ({
isPublic,
value,
secretKey,
allowSecretSharingOutsideOrganization = true,
maxSharedSecretLifetime,
maxSharedSecretViewLimit,
onCancel,
onDirtyChange
}: Props) => {
const [result, setResult] = useState<ShareResult | null>(null);
const [isSharingKeyValue, setIsSharingKeyValue] = useState(true);
const [isPasswordVisible, setIsPasswordVisible] = useToggle(false);
const subOrganization = useSearch({
strict: false,
select: (el) => el?.subOrganization
});
const publicSharedSecretCreator = useCreatePublicSharedSecret();
const privateSharedSecretCreator = useCreateSharedSecret();
const createSharedSecret = isPublic ? publicSharedSecretCreator : privateSharedSecretCreator;
// Note: maxSharedSecretLifetime is in seconds
const filteredExpiresInOptions = maxSharedSecretLifetime
? expiresInOptions.filter((v) => ms(v.value) / 1000 <= maxSharedSecretLifetime)
: expiresInOptions;
const isLifetimeCapped = expiresInOptions.length !== filteredExpiresInOptions.length;
const isViewLimitEnforced = Boolean(maxSharedSecretViewLimit);
const formatSharedValue = (includeKey: boolean) =>
secretKey && includeKey ? `${secretKey}=${value ?? ""}` : (value ?? "");
const schema = useMemo(() => buildSchema(maxSharedSecretViewLimit), [maxSharedSecretViewLimit]);
const {
control,
reset,
setValue,
handleSubmit,
formState: { isSubmitting, isDirty },
watch
} = useForm<FormData>({
resolver: zodResolver(schema),
defaultValues: {
secret: formatSharedValue(true),
viewLimit: maxSharedSecretViewLimit?.toString() ?? "1",
shouldLimitView: isViewLimitEnforced,
accessType: isPublic ? undefined : SecretSharingAccessType.Organization,
expiresIn:
filteredExpiresInOptions[Math.min(filteredExpiresInOptions.length - 1, 2)].value.toString()
}
});
useEffect(() => {
onDirtyChange?.(isDirty);
return () => onDirtyChange?.(false);
}, [isDirty, onDirtyChange]);
const isLimitingView = watch("shouldLimitView");
const isAllowingExternalEmails = watch("allowExternalEmails");
const accessType = watch("accessType");
const selectedExpiresIn = watch("expiresIn");
const isOrgAccess =
accessType === SecretSharingAccessType.Organization || !allowSecretSharingOutsideOrganization;
const resolvedExpiry = formatExpiry(selectedExpiresIn);
const isValueLocked = value !== undefined;
const constraintCount = [
isLimitingView,
Boolean(watch("password")),
!isPublic && isOrgAccess,
!isPublic && !isAllowingExternalEmails && Boolean(watch("emails")?.length)
].filter(Boolean).length;
const onFormSubmit = async ({
name,
password,
secret,
expiresIn,
viewLimit,
accessType: formAccessType,
emails,
shouldLimitView,
allowExternalEmails
}: FormData) => {
const recipients = emails ?? [];
const { id } = await createSharedSecret.mutateAsync({
name,
password,
secretValue: secret,
expiresIn,
maxViews: shouldLimitView ? Number(viewLimit) : undefined,
accessType: formAccessType,
authorizedEmails: recipients.length ? recipients : undefined,
allowExternalEmails
});
if (recipients.length > 0) {
const requiresAccount = !allowExternalEmails && !isOrgAccess;
setResult({ kind: "email", recipientCount: recipients.length, requiresAccount });
createNotification({
text: requiresAccount
? `If the provided ${recipients.length > 1 ? "emails are" : "email is"} associated with an Infisical account they will receive a link`
: `Secret link has been sent to the provided ${recipients.length > 1 ? "emails" : "email"}`,
type: "success"
});
} else {
const link = new URL(`${window.location.origin}/shared/secret/${id}`);
if (subOrganization) {
link.searchParams.set("subOrganization", subOrganization);
}
setResult({
kind: "link",
url: link.toString(),
expiresAt: formatExpiry(expiresIn) ?? "",
views: shouldLimitView ? viewLimit : "Unlimited"
});
try {
await navigator.clipboard.writeText(link.toString());
createNotification({
text: "Shared secret link copied to clipboard.",
type: "success"
});
} catch {
createNotification({
text: "Secret link created. Copy it below, your browser blocked clipboard access.",
type: "info"
});
}
}
reset();
setIsSharingKeyValue(true);
};
const renderLayout = (body: ReactNode, actions: ReactNode) =>
isPublic ? (
<>
{body}
<div className="mt-6 flex w-full items-center justify-end gap-2">{actions}</div>
</>
) : (
<>
<div className="flex min-h-0 flex-1 flex-col overflow-y-auto p-4">{body}</div>
<SheetFooter className="shrink-0 justify-end border-t">{actions}</SheetFooter>
</>
);
const formClassName = isPublic ? undefined : "flex min-h-0 flex-1 flex-col";
if (result !== null) {
return (
<div className={formClassName}>
{renderLayout(
<div className="flex flex-col gap-5 py-2">
<div className="flex flex-col items-center px-4 text-center">
<div className="mb-3 flex size-11 items-center justify-center rounded-full border border-success/20 bg-success/10">
{result.kind === "link" ? (
<CheckCircle2 className="size-5 text-success" />
) : (
<MailCheck className="size-5 text-success" />
)}
</div>
<div className="max-w-sm">
<p className="text-lg font-semibold text-foreground">
{result.kind === "link" ? "Secret Link Created" : "Secret Link Sent"}
</p>
<p className="mt-1.5 text-sm leading-relaxed text-accent">
{describeResult(result)}
</p>
</div>
</div>
{result.kind === "link" && (
<div className="rounded-md border border-border bg-container">
<div className="flex items-center justify-between gap-3 p-3">
<p className="min-w-0 font-mono text-xs break-all text-foreground">
{result.url}
</p>
<CopyButton value={result.url} ariaLabel="Copy secret link" size="sm" />
</div>
{(result.expiresAt || result.views) && (
<div className="flex flex-wrap gap-x-4 gap-y-1 border-t border-border px-3 py-2 text-2xs text-muted">
{result.expiresAt && (
<span className="inline-flex items-center gap-1.5">
<CalendarClock className="size-3" /> Expires {result.expiresAt}
</span>
)}
<span className="inline-flex items-center gap-1.5">
{result.views === "Unlimited"
? "Unlimited views"
: `${result.views} view${result.views === "1" ? "" : "s"}`}
</span>
</div>
)}
</div>
)}
</div>,
<Button variant="project" isFullWidth={isPublic} onClick={() => setResult(null)}>
Share Another Secret
<ForwardIcon />
</Button>
)}
</div>
);
}
return (
<form className={formClassName} onSubmit={handleSubmit(onFormSubmit)}>
{renderLayout(
<FieldGroup>
<FieldSet>
{!isPublic && (
<Controller
control={control}
name="name"
render={({ field, fieldState: { error } }) => (
<Field>
<FieldLabel htmlFor="share-secret-name">
Name <span className="text-xs text-muted italic">- Optional</span>
</FieldLabel>
<Input
{...field}
id="share-secret-name"
placeholder="API Key"
type="text"
autoComplete="off"
autoCorrect="off"
spellCheck={false}
isError={Boolean(error)}
/>
<FieldDescription>
Only visible to your organization, never to the recipient.
</FieldDescription>
{error && <FieldError>{error.message}</FieldError>}
</Field>
)}
/>
)}
<Controller
control={control}
name="secret"
render={({ field, fieldState: { error } }) => (
<Field>
<FieldLabel htmlFor="share-secret-value">Secret Value</FieldLabel>
<SecretInput
placeholder="Enter sensitive data to share via an encrypted link"
{...field}
id="share-secret-value"
isMultiline
isReadOnly={isValueLocked}
isError={Boolean(error)}
/>
{isValueLocked && (
<div className="flex items-center justify-between gap-4">
<FieldDescription>
{secretKey
? "Locked to the stored value. Turn off Include Key to share the value alone."
: "Locked so the link shares the saved contents without edits."}
</FieldDescription>
{secretKey && (
<div className="flex shrink-0 items-center gap-2">
<Toggle
size="sm"
variant="project"
id="share-secret-include-key"
checked={isSharingKeyValue}
onCheckedChange={(isChecked) => {
setIsSharingKeyValue(isChecked);
setValue("secret", formatSharedValue(isChecked));
}}
/>
<FieldLabel htmlFor="share-secret-include-key">Include Key</FieldLabel>
</div>
)}
</div>
)}
{error && <FieldError>{error.message}</FieldError>}
</Field>
)}
/>
</FieldSet>
<FieldSet>
<FieldLegend variant="label">Expiration</FieldLegend>
<Controller
control={control}
name="expiresIn"
render={({ field: { onChange, ...field }, fieldState: { error } }) => (
<Field>
<FieldLabel htmlFor="share-secret-expires-in">Expires In</FieldLabel>
<Select value={field.value} onValueChange={(e) => onChange(e)}>
<SelectTrigger className="w-full" id="share-secret-expires-in">
<SelectValue />
</SelectTrigger>
<SelectContent>
{expiresInOptions.map(({ label, value: expiresInValue }) => {
const isAvailable = filteredExpiresInOptions.some((v) => v.label === label);
return (
<SelectItem
value={String(expiresInValue || "")}
key={label}
disabled={!isAvailable}
>
{label}
</SelectItem>
);
})}
</SelectContent>
</Select>
<FieldDescription>
{resolvedExpiry ? `The link stops working on ${resolvedExpiry}.` : null}
{isLifetimeCapped && (
<span className="mt-0.5 block text-info">
Your organization caps links at{" "}
{filteredExpiresInOptions[filteredExpiresInOptions.length - 1].label}.
</span>
)}
</FieldDescription>
{error && <FieldError>{error.message}</FieldError>}
</Field>
)}
/>
</FieldSet>
<Accordion type="single" collapsible variant="ghost">
<AccordionItem value="options" className="border-b-0">
<AccordionTrigger>
<span className="flex w-0 flex-1 items-center gap-3">
<span className="shrink-0">More Options</span>
<Separator className="flex-1" />
<span className="shrink-0 text-xs font-normal text-muted">
{constraintCount === 0
? "No constraints"
: `${constraintCount} constraint${constraintCount === 1 ? "" : "s"}`}
</span>
</span>
</AccordionTrigger>
<AccordionContent className="flex flex-col gap-6">
<FieldSet className="gap-4">
<FieldLegend className="sr-only">Access</FieldLegend>
{!isViewLimitEnforced && (
<Controller
control={control}
name="shouldLimitView"
render={({ field: { onChange, value: isChecked, ...field } }) => (
<Field orientation="horizontal">
<Toggle
{...field}
variant="project"
id="share-secret-limit-views"
checked={isChecked}
onCheckedChange={onChange}
/>
<FieldLabel htmlFor="share-secret-limit-views" className="flex-auto">
Limit how many times it can be viewed
</FieldLabel>
</Field>
)}
/>
)}
{isLimitingView && (
<Controller
control={control}
name="viewLimit"
render={({ field: { onChange, ...field }, fieldState: { error } }) => (
<Field>
<FieldLabel htmlFor="share-secret-view-limit">
Maximum Views
{isViewLimitEnforced && (
<Tooltip>
<TooltipTrigger asChild>
<Lock className="size-3 text-muted" />
</TooltipTrigger>
<TooltipContent>Enforced by your organization</TooltipContent>
</Tooltip>
)}
</FieldLabel>
<Input
onChange={onChange}
{...field}
id="share-secret-view-limit"
min={1}
max={maxSharedSecretViewLimit ?? ABSOLUTE_VIEW_CEILING}
type="number"
isError={Boolean(error)}
/>
{isViewLimitEnforced && (
<FieldDescription className="text-info">
Your organization caps links at {maxSharedSecretViewLimit} view
{maxSharedSecretViewLimit === 1 ? "" : "s"}.
</FieldDescription>
)}
{error && <FieldError>{error.message}</FieldError>}
</Field>
)}
/>
)}
<Controller
control={control}
name="password"
render={({ field, fieldState: { error } }) => (
<Field>
<FieldLabel htmlFor="share-secret-password">
Password <span className="text-xs text-muted italic">- Optional</span>
</FieldLabel>
<InputGroup>
<InputGroupInput
{...field}
id="share-secret-password"
name="shared-secret-passphrase"
placeholder="Password"
type={isPasswordVisible ? "text" : "password"}
autoComplete="off"
autoCorrect="off"
spellCheck={false}
aria-autocomplete="none"
data-form-type="other"
data-1p-ignore
data-lpignore="true"
aria-invalid={Boolean(error)}
/>
<InputGroupAddon align="inline-end">
<InputGroupButton
onClick={setIsPasswordVisible.toggle}
aria-label={isPasswordVisible ? "Hide password" : "Show password"}
>
{isPasswordVisible ? <EyeOff /> : <Eye />}
</InputGroupButton>
</InputGroupAddon>
</InputGroup>
{error && <FieldError>{error.message}</FieldError>}
</Field>
)}
/>
</FieldSet>
{!isPublic && (
<FieldSet className="gap-4" aria-labelledby="share-secret-delivery-heading">
<FieldLabel
id="share-secret-delivery-heading"
className="w-full gap-3 text-sm font-medium text-foreground"
>
Delivery
<span className="h-px flex-1 bg-border" aria-hidden="true" />
</FieldLabel>
<Controller
control={control}
name="accessType"
render={({ field: { onChange, ...field }, fieldState: { error } }) => (
<Field orientation="horizontal">
<Toggle
variant="project"
id="share-secret-org-only"
checked={
field.value === SecretSharingAccessType.Organization ||
!allowSecretSharingOutsideOrganization
}
disabled={!allowSecretSharingOutsideOrganization}
onCheckedChange={(v) => {
if (v) {
setValue("allowExternalEmails", false, { shouldValidate: true });
}
onChange(
v
? SecretSharingAccessType.Organization
: SecretSharingAccessType.Anyone
);
}}
/>
<FieldLabel htmlFor="share-secret-org-only" className="flex-auto">
Limit access to people within organization
{!allowSecretSharingOutsideOrganization && (
<Tooltip>
<TooltipTrigger asChild>
<Lock className="size-3 text-muted" />
</TooltipTrigger>
<TooltipContent>Enforced by your organization</TooltipContent>
</Tooltip>
)}
</FieldLabel>
{error && <FieldError>{error.message}</FieldError>}
</Field>
)}
/>
<Controller
control={control}
name="allowExternalEmails"
render={({
field: { onChange, value: isChecked, ...field },
fieldState: { error }
}) => (
<Field orientation="horizontal">
<Toggle
{...field}
variant="project"
id="share-secret-allow-external"
checked={isOrgAccess ? false : (isChecked ?? false)}
onCheckedChange={onChange}
disabled={isOrgAccess}
/>
<FieldLabel htmlFor="share-secret-allow-external" className="flex-auto">
Allow recipients without an Infisical account
{!allowSecretSharingOutsideOrganization && (
<Tooltip>
<TooltipTrigger asChild>
<Lock className="size-3 text-muted" />
</TooltipTrigger>
<TooltipContent>
External sharing is disabled by your organization
</TooltipContent>
</Tooltip>
)}
</FieldLabel>
{error && <FieldError>{error.message}</FieldError>}
</Field>
)}
/>
<Controller
control={control}
name="emails"
render={({ field, fieldState: { error } }) => (
<Field>
<FieldLabel htmlFor="share-secret-emails">
Email Recipients{" "}
<span className="text-xs text-muted italic">- Optional</span>
<Tooltip>
<TooltipTrigger asChild>
<Info className="size-3 cursor-help text-muted" />
</TooltipTrigger>
<TooltipContent className="max-w-sm">
Each recipient gets their own link. Leave this empty to get one link
you can share yourself.
</TooltipContent>
</Tooltip>
</FieldLabel>
<EmailInput
ref={field.ref}
id="share-secret-emails"
value={field.value}
onValueChange={field.onChange}
placeholder="[email protected], [email protected]"
isError={Boolean(error)}
/>
<FieldDescription>
{isAllowingExternalEmails
? "Recipients do not need an Infisical account, but they do need the password above."
: "Recipients must sign in to Infisical so their identity can be verified."}
</FieldDescription>
{error && <FieldError>{error.message}</FieldError>}
</Field>
)}
/>
</FieldSet>
)}
</AccordionContent>
</AccordionItem>
</Accordion>
</FieldGroup>,
<>
{isPublic ? (
<Badge variant="ghost" className="mr-auto">
<img
src="/images/logotransparent_trimmed.png"
alt="Infisical"
className="mr-0.5 h-[8px]"
/>
Powered by Infisical
</Badge>
) : (
<Button variant="ghost" isDisabled={isSubmitting} onClick={onCancel}>
Cancel
</Button>
)}
<Button
variant="project"
type="submit"
isPending={isSubmitting}
isDisabled={isSubmitting}
>
Create Secret Link
</Button>
</>
)}
</form>
);
};