Skip to content

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

fix(pam): only treat a supplied secret as a credential change

bernie-gd1237635542f
diff --git a/backend/src/ee/services/pam-account-heartbeat/pam-heartbeat-fns.test.ts b/backend/src/ee/services/pam-account-heartbeat/pam-heartbeat-fns.test.ts
index f878176258..96c0c04370 100644
--- a/backend/src/ee/services/pam-account-heartbeat/pam-heartbeat-fns.test.ts
+++ b/backend/src/ee/services/pam-account-heartbeat/pam-heartbeat-fns.test.ts
@@ -9,6 +9,7 @@ import {
classifyCloudProbeError,
describeFailure,
isHeartbeatScheduled,
+ pausesHeartbeatForRoutingChange,
UNCLASSIFIED_FAILURE_NOTE
} from "./pam-heartbeat-fns";
@@ -74,3 +75,23 @@ describe("describeFailure", () => {
expect(describeFailure(null, undefined)).toBe(UNCLASSIFIED_FAILURE_NOTE);
});
});
+
+describe("pausesHeartbeatForRoutingChange", () => {
+ const base = { routingChanged: true, credentialsSupplied: false, actorCanViewCredentials: false };
+
+ test("pauses when an actor who cannot read the credential repoints the account", () => {
+ expect(pausesHeartbeatForRoutingChange(base)).toBe(true);
+ });
+
+ test("leaves the schedule alone when the actor can already read the credential", () => {
+ expect(pausesHeartbeatForRoutingChange({ ...base, actorCanViewCredentials: true })).toBe(false);
+ });
+
+ test("leaves the schedule alone when a credential came with the change", () => {
+ expect(pausesHeartbeatForRoutingChange({ ...base, credentialsSupplied: true })).toBe(false);
+ });
+
+ test("leaves the schedule alone when nothing about the routing changed", () => {
+ expect(pausesHeartbeatForRoutingChange({ ...base, routingChanged: false })).toBe(false);
+ });
+});
diff --git a/backend/src/ee/services/pam-account-heartbeat/pam-heartbeat-fns.ts b/backend/src/ee/services/pam-account-heartbeat/pam-heartbeat-fns.ts
index d10ea3e27c..8037f86c01 100644
--- a/backend/src/ee/services/pam-account-heartbeat/pam-heartbeat-fns.ts
+++ b/backend/src/ee/services/pam-account-heartbeat/pam-heartbeat-fns.ts
@@ -33,6 +33,19 @@ export const computeNextHeartbeatAt = ({
export const isHeartbeatScheduled = (heartbeat: TPamHeartbeatConfig | undefined): heartbeat is TPamHeartbeatConfig =>
heartbeat?.enabled === true && heartbeat.intervalSeconds != null;
+export const pausesHeartbeatForRoutingChange = ({
+ routingChanged,
+ credentialsSupplied,
+ actorCanViewCredentials
+}: {
+ routingChanged: boolean;
+ credentialsSupplied: boolean;
+ actorCanViewCredentials: boolean;
+}): boolean => routingChanged && !credentialsSupplied && !actorCanViewCredentials;
+
+export const HEARTBEAT_PAUSED_FOR_ROUTING_CHANGE =
+ "Scheduled checks are paused because the target changed. Enter the credential again, or run a check, to resume.";
+
// Retrying a rejected credential is how a monitoring feature locks out a privileged account.
export const stopsSchedule = (status: PamHeartbeatStatus) => status === PamHeartbeatStatus.InvalidCredentials;
diff --git a/backend/src/ee/services/pam-account/pam-account-schemas.test.ts b/backend/src/ee/services/pam-account/pam-account-schemas.test.ts
index bc6b1e808e..692b8d9cc1 100644
--- a/backend/src/ee/services/pam-account/pam-account-schemas.test.ts
+++ b/backend/src/ee/services/pam-account/pam-account-schemas.test.ts
@@ -11,6 +11,7 @@ import {
PamAccountTypeMetadataSchema,
PamFieldDescriptorSchema,
sanitizeCredentials,
+ suppliesCredentialSecret,
validateCredentials
} from "./pam-account-schemas";
@@ -393,3 +394,36 @@ describe("getAccountAccessibilityIssues", () => {
).toEqual([PamAccountAccessibilityIssue.NoRecordingConfig]);
});
});
+
+describe("suppliesCredentialSecret", () => {
+ test("a resent username and auth method is not a supplied secret", () => {
+ expect(suppliesCredentialSecret(PamAccountType.Postgres, { authMethod: "password", username: "pamadmin" })).toBe(
+ false
+ );
+ });
+
+ test("a real password is", () => {
+ expect(
+ suppliesCredentialSecret(PamAccountType.Postgres, {
+ authMethod: "password",
+ username: "pamadmin",
+ password: "hunter2"
+ })
+ ).toBe(true);
+ });
+
+ test("an empty or whitespace password is not", () => {
+ expect(suppliesCredentialSecret(PamAccountType.Postgres, { password: "" })).toBe(false);
+ expect(suppliesCredentialSecret(PamAccountType.Postgres, { password: " " })).toBe(false);
+ });
+
+ test("an absent credentials object is not", () => {
+ expect(suppliesCredentialSecret(PamAccountType.Postgres, undefined)).toBe(false);
+ });
+
+ test("a certificate account never supplies one", () => {
+ expect(suppliesCredentialSecret(PamAccountType.SSH, { authMethod: "certificate", username: "pamuser" })).toBe(
+ false
+ );
+ });
+});
diff --git a/backend/src/ee/services/pam-account/pam-account-schemas.ts b/backend/src/ee/services/pam-account/pam-account-schemas.ts
index e2e84ca2b9..b963a12099 100644
--- a/backend/src/ee/services/pam-account/pam-account-schemas.ts
+++ b/backend/src/ee/services/pam-account/pam-account-schemas.ts
@@ -1309,6 +1309,25 @@ export const applyForcedFields = (accountType: PamAccountType, values: TPamField
return result;
};
+// The edit form resends username and auth method with an untouched password stripped, so a credentials object
+// on its own does not mean the caller held the credential.
+export const suppliesCredentialSecret = (
+ accountType: PamAccountType,
+ rawCredentials: Record<string, unknown> | undefined
+): boolean => {
+ if (!rawCredentials) return false;
+ const config = ACCOUNT_TYPE_CONFIGS[accountType as TSupportedAccountType];
+ if (!config) return false;
+
+ const credentials = normalizeCredentialAuthMethod(accountType, rawCredentials);
+ return fieldsFromSchema(config.credentials, config.ui)
+ .filter((field) => field.secret)
+ .some((field) => {
+ const value = credentials[field.key];
+ return typeof value === "string" && value.trim().length > 0;
+ });
+};
+
export const isCredentialConfigured = (
accountType: PamAccountType,
rawCredentials: Record<string, unknown>
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 f428eb359d..e7457af30a 100644
--- a/backend/src/ee/services/pam-account/pam-account-service.ts
+++ b/backend/src/ee/services/pam-account/pam-account-service.ts
@@ -39,6 +39,7 @@ import {
} from "../pam/pam-enums";
import { enforceMfa } from "../pam/pam-mfa";
import {
+ accountAccessAllows,
checkAccountAccess,
checkFolderPermission,
getAccountPermissionRulesMap,
@@ -54,6 +55,10 @@ import {
validateRecordingConnection
} from "../pam/pam-validators";
import { TPamAccessRequestServiceFactory } from "../pam-access-request/pam-access-request-service";
+import {
+ HEARTBEAT_PAUSED_FOR_ROUTING_CHANGE,
+ pausesHeartbeatForRoutingChange
+} from "../pam-account-heartbeat/pam-heartbeat-fns";
import { TPamAccountTemplateDALFactory } from "../pam-account-template/pam-account-template-dal";
import { PamTemplateSettingsSchema } from "../pam-account-template/pam-account-template-schemas";
import { TPamDiscoverySourceDALFactory } from "../pam-discovery/pam-discovery-source-dal";
@@ -76,6 +81,7 @@ import {
PamAccountAccessibilityIssue,
parseInternalMetadata,
sanitizeCredentials,
+ suppliesCredentialSecret,
type TSshInternalMetadata,
validateConnectionDetails,
validateCredentials
@@ -909,8 +915,35 @@ export const pamAccountServiceFactory = (deps: TPamAccountServiceFactoryDep) =>
);
}
+ const credentialSecretSupplied = suppliesCredentialSecret(accountType, credentials);
+
+ // Only asked when it can change the outcome, so an ordinary edit costs no extra permission read.
+ const actorCanViewCredentials =
+ routingChanged && !credentialSecretSupplied
+ ? await accountAccessAllows(
+ permissionService,
+ accountId,
+ existing.folderId,
+ projectId,
+ ResourcePermissionPamResourceActions.ViewCredentials,
+ ctx
+ )
+ : true;
+ const pausedForRoutingChange = pausesHeartbeatForRoutingChange({
+ routingChanged,
+ credentialsSupplied: credentialSecretSupplied,
+ actorCanViewCredentials
+ });
+ if (pausedForRoutingChange) {
+ updateData.nextHeartbeatAt = null;
+ updateData.heartbeatStatus = PamHeartbeatStatus.Unknown;
+ updateData.encryptedLastHeartbeatMessage = await encrypt(projectId, {
+ message: HEARTBEAT_PAUSED_FOR_ROUTING_CHANGE
+ });
+ }
+
// Clearing the old verdict resumes checking; types unverifiable here would otherwise stay stopped.
- if (credentials) {
+ if (credentialSecretSupplied) {
updateData.heartbeatStatus = credentialVerified ? PamHeartbeatStatus.Healthy : null;
updateData.encryptedLastHeartbeatMessage = null;
if (credentialVerified) {
@@ -934,7 +967,9 @@ export const pamAccountServiceFactory = (deps: TPamAccountServiceFactoryDep) =>
// atomically with the write so a failure can't leave a stale nextRotationAt.
if (templateId !== undefined || credentials || routingChanged) {
await pamAccountDAL.reconcileRotationScheduleForAccount(accountId, tx);
- await pamAccountDAL.reconcileHeartbeatScheduleForAccount(accountId, tx);
+ if (!pausedForRoutingChange) {
+ await pamAccountDAL.reconcileHeartbeatScheduleForAccount(accountId, tx);
+ }
}
return updated;
});
diff --git a/backend/src/ee/services/pam/pam-permission.test.ts b/backend/src/ee/services/pam/pam-permission.test.ts
new file mode 100644
index 0000000000..14e5fa416e
--- /dev/null
+++ b/backend/src/ee/services/pam/pam-permission.test.ts
@@ -0,0 +1,53 @@
+import { createMongoAbility, ForbiddenError } from "@casl/ability";
+import { describe, expect, test, vi } from "vitest";
+
+import { ResourcePermissionSub } from "@app/ee/services/permission/resource-permission";
+import { ForbiddenRequestError } from "@app/lib/errors";
+
+import { accountAccessAllows } from "./pam-permission";
+
+const abilityFor = (allowed: boolean) =>
+ createMongoAbility(allowed ? [{ action: "view-credentials", subject: ResourcePermissionSub.PamResource }] : []);
+
+const ctx = { actorId: "u1", actor: "user", actorOrgId: "o1", actorAuthMethod: "email" } as never;
+
+describe("accountAccessAllows", () => {
+ test("is true when the actor holds the action", async () => {
+ const permissionService = { getResourcePermission: vi.fn(async () => ({ permission: abilityFor(true) })) };
+ await expect(
+ accountAccessAllows(permissionService as never, "acc-1", null, "proj-1", "view-credentials" as never, ctx)
+ ).resolves.toBe(true);
+ });
+
+ test("is false when the actor does not, rather than throwing", async () => {
+ const permissionService = { getResourcePermission: vi.fn(async () => ({ permission: abilityFor(false) })) };
+ await expect(
+ accountAccessAllows(permissionService as never, "acc-1", null, "proj-1", "view-credentials" as never, ctx)
+ ).resolves.toBe(false);
+ });
+
+ test("rethrows a failure that is not a permission refusal", async () => {
+ const permissionService = {
+ getResourcePermission: vi.fn(async () => {
+ throw new Error("database unreachable");
+ })
+ };
+ await expect(
+ accountAccessAllows(permissionService as never, "acc-1", null, "proj-1", "view-credentials" as never, ctx)
+ ).rejects.toThrow("database unreachable");
+ });
+
+ test("treats both refusal classes as a denial", async () => {
+ for (const err of [new ForbiddenRequestError({ message: "no" }), new ForbiddenError(abilityFor(false))]) {
+ const permissionService = {
+ getResourcePermission: vi.fn(async () => {
+ throw err;
+ })
+ };
+ // eslint-disable-next-line no-await-in-loop
+ await expect(
+ accountAccessAllows(permissionService as never, "acc-1", null, "proj-1", "view-credentials" as never, ctx)
+ ).resolves.toBe(false);
+ }
+ });
+});
diff --git a/backend/src/ee/services/pam/pam-permission.ts b/backend/src/ee/services/pam/pam-permission.ts
index 192f14794d..29b40dfffb 100644
--- a/backend/src/ee/services/pam/pam-permission.ts
+++ b/backend/src/ee/services/pam/pam-permission.ts
@@ -128,6 +128,24 @@ export const checkAccountAccess = async (
ForbiddenError.from(permission).throwUnlessCan(action, ResourcePermissionSub.PamResource);
};
+// Non-throwing counterpart to checkAccountAccess, for deciding what a caller may do.
+export const accountAccessAllows = async (
+ permissionService: TPermissionDep,
+ accountId: string,
+ folderId: string | null | undefined,
+ projectId: string,
+ action: ResourcePermissionPamResourceActions,
+ ctx: TActorContext
+): Promise<boolean> => {
+ try {
+ await checkAccountAccess(permissionService, accountId, folderId, projectId, action, ctx);
+ return true;
+ } catch (err) {
+ if (err instanceof ForbiddenError || err instanceof ForbiddenRequestError) return false;
+ throw err;
+ }
+};
+
// `tx` is for callers that must see their own uncommitted membership writes (revoking access and closing
// the sessions it was holding open, in one transaction). Without it these are replica reads.
export const getResourceIdsWithActions = async (