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
fix(pam): read the winning mint from the primary
bernie-g9b0499dc9b08
diff --git a/backend/src/ee/services/pam-session-recording/pam-recording-secrets.ts b/backend/src/ee/services/pam-session-recording/pam-recording-secrets.tsindex 050c2f83c4..e393193ec1 100644--- a/backend/src/ee/services/pam-session-recording/pam-recording-secrets.ts+++ b/backend/src/ee/services/pam-session-recording/pam-recording-secrets.ts@@ -75,7 +75,7 @@ export const decryptSessionKey = async ({// Every rejection keeps the same client-facing message; only the error name differs, so the// branch is recoverable from logs without widening what the caller learns. A malformed token and-// a token that simply does not match point at completely different causes (PAM-463).+// a token that simply does not match point at completely different causes.export const PamUploadTokenRejection = {Missing: "PamUploadTokenMissing",MalformedLength: "PamUploadTokenMalformedLength",diff --git a/backend/src/ee/services/pam-session/pam-session-dal.ts b/backend/src/ee/services/pam-session/pam-session-dal.tsindex a8969a3dee..abdea44438 100644--- a/backend/src/ee/services/pam-session/pam-session-dal.ts+++ b/backend/src/ee/services/pam-session/pam-session-dal.ts@@ -24,6 +24,35 @@ export const pamSessionDALFactory = (db: TDbClient) => {return session;};+ // Sets the recording secrets only if the session has none yet, and returns whichever key is+ // stored afterwards. One statement on the primary, so a caller that loses a concurrent claim+ // still gets the winner's key back; a follow-up read would go to a replica that may not have+ // caught up. COALESCE and CASE both see the pre-update value, so the winner is whoever the row+ // was null for.+ const claimRecordingSecrets = async (+ sessionId: string,+ encryptedSessionKey: Buffer,+ gatewayUploadTokenHash: Buffer,+ tx?: Knex+ ) => {+ const [row] = await (tx || db)(TableName.PamSession)+ .where({ id: sessionId })+ .update({+ encryptedSessionKey: db.raw("COALESCE(??, ?)", [+ "encryptedSessionKey",+ encryptedSessionKey+ ]) as unknown as Buffer,+ gatewayUploadTokenHash: db.raw("CASE WHEN ?? IS NULL THEN ? ELSE ?? END", [+ "encryptedSessionKey",+ gatewayUploadTokenHash,+ "gatewayUploadTokenHash"+ ]) as unknown as Buffer+ })+ .returning(["encryptedSessionKey"]);++ return row as { encryptedSessionKey: Buffer | null } | undefined;+ };+const countActiveWebSessions = async (userId: string, projectId: string, tx?: Knex): Promise<number> => {const result = await (tx || db.replicaNode())(TableName.PamSession).where("userId", userId)@@ -169,6 +198,7 @@ export const pamSessionDALFactory = (db: TDbClient) => {return {...orm,findById,+ claimRecordingSecrets,countActiveWebSessions,endExpiredWebSessions,endSessionById,diff --git a/backend/src/ee/services/pam-session/pam-session-service.ts b/backend/src/ee/services/pam-session/pam-session-service.tsindex 75494160ab..125532e945 100644--- a/backend/src/ee/services/pam-session/pam-session-service.ts+++ b/backend/src/ee/services/pam-session/pam-session-service.ts@@ -3,7 +3,7 @@ import RE2 from "re2";import { TGatewayPoolServiceFactory } from "@app/ee/services/gateway-pool/gateway-pool-service";import { TGatewayV2ServiceFactory } from "@app/ee/services/gateway-v2/gateway-v2-service";import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";-import { BadRequestError, ForbiddenRequestError, InternalServerError, NotFoundError } from "@app/lib/errors";+import { BadRequestError, ForbiddenRequestError, NotFoundError } from "@app/lib/errors";import { ms } from "@app/lib/ms";import { alphaNumericNanoId } from "@app/lib/nanoid";import { createSshCert, createSshKeyPair, SshCertKeyAlgorithm, SshCertType } from "@app/lib/ssh";@@ -89,7 +89,7 @@ type TPamSessionServiceFactoryDep = {| "endSessionById"| "terminateSessionById"| "updateById"- | "update"+ | "claimRecordingSecrets"| "activateSession">;pamAccountDAL: Pick<TPamAccountDALFactory, "findByIdWithDetails" | "findOne">;@@ -327,16 +327,17 @@ export const pamSessionServiceFactory = ({// between check and write to tens of milliseconds. Without the guard both callers mint, the// later write wins, and the earlier caller is left holding a session key and upload token the// row no longer matches: every chunk upload 400s forever, and whatever did upload is- // undecryptable at playback (PAM-463).- const [claimed] = await pamSessionDAL.update(- { id: sessionId, encryptedSessionKey: null },- {- encryptedSessionKey: secrets.encryptedSessionKey,- gatewayUploadTokenHash: secrets.uploadTokenHash- }+ // undecryptable at playback.+ const claimed = await pamSessionDAL.claimRecordingSecrets(+ sessionId,+ secrets.encryptedSessionKey,+ secrets.uploadTokenHash);+ if (!claimed?.encryptedSessionKey) {+ throw new NotFoundError({ message: `Session with ID '${sessionId}' was not found` });+ }- if (claimed) {+ if (claimed.encryptedSessionKey.equals(secrets.encryptedSessionKey)) {recording = {sessionKey: secrets.sessionKey.toString("base64"),uploadToken: secrets.uploadToken.toString("base64"),@@ -345,14 +346,9 @@ export const pamSessionServiceFactory = ({sessionId};} else {- // Another fetch claimed it first; adopt its secrets rather than our own discarded ones.- const claimedSession = await pamSessionDAL.findById(sessionId);- storedSessionKey = claimedSession?.encryptedSessionKey ?? null;- if (!storedSessionKey) {- throw new InternalServerError({- message: `Recording secrets for session ${sessionId} are missing after a lost mint`- });- }+ // Another fetch claimed it first; adopt its key rather than our own discarded one. The+ // upload token stays empty here, same as any other re-fetch.+ storedSessionKey = claimed.encryptedSessionKey;}}