Skip to content

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

improvement(pam): tie access request reason requirement to template policy

bernie-gb9f680212ab6
diff --git a/backend/src/ee/routes/v1/pam-routers/pam-account-router.ts b/backend/src/ee/routes/v1/pam-routers/pam-account-router.ts
index 000fd82e0c..2f8c543de4 100644
--- a/backend/src/ee/routes/v1/pam-routers/pam-account-router.ts
+++ b/backend/src/ee/routes/v1/pam-routers/pam-account-router.ts
@@ -399,6 +399,7 @@ export const registerPamAccountRouter = async (server: FastifyZodProvider) => {
requiresApproval: z
.boolean()
.describe("Whether this account requires approval before launching a session"),
+ requireReason: z.boolean().describe("Whether the account's template requires a reason for access"),
accessStatus: z.nativeEnum(PamAccessStatus).describe("Current approval status for the caller"),
grantExpiresAt: z.date().nullable().describe("When the current grant expires, if granted"),
disabledReason: z.string().nullable().describe("Why this account is disabled, or null if usable")
diff --git a/backend/src/ee/services/pam-access-request/pam-access-request-service.ts b/backend/src/ee/services/pam-access-request/pam-access-request-service.ts
index 59f1f4f3e4..6fb6e17d12 100644
--- a/backend/src/ee/services/pam-access-request/pam-access-request-service.ts
+++ b/backend/src/ee/services/pam-access-request/pam-access-request-service.ts
@@ -430,6 +430,7 @@ export const pamAccessRequestServiceFactory = ({
};
const createRequest = async ({ accountId, path, projectId, reason, duration, ...ctx }: TCreateAccessRequestDTO) => {
+ const trimmedReason = reason?.trim() || undefined;
await verifyProductMembership(permissionService, projectId, ctx);
if (!accountId && !path) {
@@ -455,10 +456,18 @@ export const pamAccessRequestServiceFactory = ({
ctx
);
- if (!resolveAccessControls(account.templatePolicies).requiresApproval) {
+ const accessControls = resolveAccessControls(account.templatePolicies);
+ if (!accessControls.requiresApproval) {
throw new BadRequestError({ message: "This account does not require approval" });
}
+ if (accessControls.requireReason && !trimmedReason) {
+ throw new BadRequestError({
+ name: "PAM_REASON_REQUIRED",
+ message: "A reason is required to request access to this account"
+ });
+ }
+
if (!account.folderId) {
throw new BadRequestError({ message: "Account must be in a folder to require approval" });
}
@@ -521,7 +530,7 @@ export const pamAccessRequestServiceFactory = ({
const requestData = {
accountId: account.id,
folderId: account.folderId,
- reason,
+ reason: trimmedReason,
duration
} as unknown as TApprovalRequestData;
@@ -533,7 +542,7 @@ export const pamAccessRequestServiceFactory = ({
policyType: ApprovalPolicyType.PamAccess,
policySteps: stepsForRequest,
requestData,
- justification: reason,
+ justification: trimmedReason,
requesterUserId: ctx.actorId,
requesterName: requesterName || "Unknown",
requesterEmail: user.email || "",
@@ -586,7 +595,7 @@ export const pamAccessRequestServiceFactory = ({
accountName: account.name,
folderName: account.folderName ?? undefined,
accessDuration: formatDuration(duration),
- reason,
+ reason: trimmedReason,
approvalUrl
}
});
diff --git a/backend/src/ee/services/pam-account/pam-account-service.ts b/backend/src/ee/services/pam-account/pam-account-service.ts
index 692bd09039..a16180963a 100644
--- a/backend/src/ee/services/pam-account/pam-account-service.ts
+++ b/backend/src/ee/services/pam-account/pam-account-service.ts
@@ -567,7 +567,7 @@ export const pamAccountServiceFactory = (deps: TPamAccountServiceFactoryDep) =>
return {
accounts: accounts.map((a) => {
- const { requiresApproval } = resolveAccessControls(a.templatePolicies);
+ const { requiresApproval, requireReason } = resolveAccessControls(a.templatePolicies);
const statusEntry = accessStatusMap.get(a.id);
const hasPolicyConfigured = a.folderId ? foldersWithApprovalPolicy.has(a.folderId) : false;
let disabledReason: string | null = null;
@@ -587,6 +587,7 @@ export const pamAccountServiceFactory = (deps: TPamAccountServiceFactoryDep) =>
projectId: a.projectId,
canLaunch: launchAccountIds.has(a.id) || (!!a.folderId && launchFolderIds.has(a.folderId)),
requiresApproval,
+ requireReason,
accessStatus: requiresApproval ? (statusEntry?.accessStatus ?? PamAccessStatus.None) : PamAccessStatus.None,
grantExpiresAt: statusEntry?.grantExpiresAt ?? null,
disabledReason,
diff --git a/backend/src/ee/services/pam-session/pam-session-service.ts b/backend/src/ee/services/pam-session/pam-session-service.ts
index 5743e3e365..295b5f53e2 100644
--- a/backend/src/ee/services/pam-session/pam-session-service.ts
+++ b/backend/src/ee/services/pam-session/pam-session-service.ts
@@ -423,7 +423,8 @@ export const pamSessionServiceFactory = ({
if (!grant) {
throw new ForbiddenRequestError({
name: "PAM_APPROVAL_REQUIRED",
- message: "Access request required"
+ message: "Access request required",
+ details: { requireReason: policy.requireReason }
});
}
// A null expiresAt means a never-expiring grant per the checkGrant contract
@@ -432,7 +433,8 @@ export const pamSessionServiceFactory = ({
if (grantRemainingMs <= 0) {
throw new ForbiddenRequestError({
name: "PAM_GRANT_EXPIRED",
- message: "Your approved access has expired"
+ message: "Your approved access has expired",
+ details: { requireReason: policy.requireReason }
});
}
sessionDurationMs = Math.min(sessionDurationMs, grantRemainingMs);
diff --git a/frontend/src/hooks/api/pam/types/index.ts b/frontend/src/hooks/api/pam/types/index.ts
index 496cbb1d46..5f921b290a 100644
--- a/frontend/src/hooks/api/pam/types/index.ts
+++ b/frontend/src/hooks/api/pam/types/index.ts
@@ -209,6 +209,7 @@ export type TAccessiblePamAccount = {
accountType: PamAccountType;
canLaunch: boolean;
requiresApproval?: boolean;
+ requireReason?: boolean;
accessStatus?: PamAccessStatus;
grantExpiresAt?: string | null;
disabledReason?: string | null;
diff --git a/frontend/src/pages/pam/PamAccessPage/components/RequestAccessSheet.tsx b/frontend/src/pages/pam/PamAccessPage/components/RequestAccessSheet.tsx
index b086c4153a..d321a85d74 100644
--- a/frontend/src/pages/pam/PamAccessPage/components/RequestAccessSheet.tsx
+++ b/frontend/src/pages/pam/PamAccessPage/components/RequestAccessSheet.tsx
@@ -1,3 +1,4 @@
+import { useMemo } from "react";
import { Controller, useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import {
@@ -54,12 +55,16 @@ const DURATION_OPTIONS = [
{ value: "7d", label: "1 week" }
];
-const schema = z.object({
- duration: z.string().min(1, "Required"),
- reason: z.string().min(1, "Required").max(500)
-});
+// Reason requiredness follows the template's Require Reason policy, same as the API and CLI
+const makeSchema = (requireReason: boolean) =>
+ z.object({
+ duration: z.string().min(1, "Required"),
+ reason: requireReason
+ ? z.string().trim().min(1, "Required").max(500)
+ : z.string().trim().max(500)
+ });
-type FormData = z.infer<typeof schema>;
+type FormData = z.infer<ReturnType<typeof makeSchema>>;
type Props = {
account: TAccessiblePamAccount | null;
@@ -140,6 +145,8 @@ export const RequestAccessSheet = ({ account, isOpen, onOpenChange }: Props) =>
const { typeName, subtitle, metadata } = useAccountSheetDetails(account, isOpen);
const createRequest = useCreatePamAccessRequest();
const isPending = account?.accessStatus === PamAccessStatus.Pending;
+ const requireReason = Boolean(account?.requireReason);
+ const schema = useMemo(() => makeSchema(requireReason), [requireReason]);
const {
control,
@@ -162,7 +169,7 @@ export const RequestAccessSheet = ({ account, isOpen, onOpenChange }: Props) =>
{
accountId: account.id,
duration: data.duration,
- reason: data.reason
+ reason: data.reason || undefined
},
{
onSuccess: () => {
@@ -211,7 +218,7 @@ export const RequestAccessSheet = ({ account, isOpen, onOpenChange }: Props) =>
render={({ field, fieldState }) => (
<Field>
<FieldLabel>
- Reason <span className="text-danger">*</span>
+ Reason {requireReason && <span className="text-danger">*</span>}
</FieldLabel>
<FieldContent>
<TextArea