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): claim recording secret mint atomically
bernie-g9fb8830eff18
diff --git a/backend/src/ee/services/pam-session/pam-session-service.ts b/backend/src/ee/services/pam-session/pam-session-service.tsindex 1923c55af8..75494160ab 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, NotFoundError } from "@app/lib/errors";+import { BadRequestError, ForbiddenRequestError, InternalServerError, 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,6 +89,7 @@ type TPamSessionServiceFactoryDep = {| "endSessionById"| "terminateSessionById"| "updateById"+ | "update"| "activateSession">;pamAccountDAL: Pick<TPamAccountDALFactory, "findByIdWithDetails" | "findOne">;@@ -312,32 +313,56 @@ export const pamSessionServiceFactory = ({sessionId: string;} | null = null;- if (!session.encryptedSessionKey) {+ let storedSessionKey = session.encryptedSessionKey ?? null;++ if (!storedSessionKey) {const secrets = await generateSessionRecordingSecrets({projectId: session.projectId,sessionId,kmsService});- await pamSessionDAL.updateById(sessionId, {- encryptedSessionKey: secrets.encryptedSessionKey,- gatewayUploadTokenHash: secrets.uploadTokenHash- });+ // Claim the mint in a single statement. Concurrent credential fetches for one session are+ // routine -- a gateway fetches per connection -- and the KMS call above widens the gap+ // 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+ }+ );- recording = {- sessionKey: secrets.sessionKey.toString("base64"),- uploadToken: secrets.uploadToken.toString("base64"),- storageBackend: resolvedBackend,- projectId: session.projectId,- sessionId- };- } else {+ if (claimed) {+ recording = {+ sessionKey: secrets.sessionKey.toString("base64"),+ uploadToken: secrets.uploadToken.toString("base64"),+ storageBackend: resolvedBackend,+ projectId: session.projectId,+ 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`+ });+ }+ }+ }++ if (!recording && storedSessionKey) {// On re-fetch (e.g. gateway restart) return the existing key; empty token since the gateway// restores its own from disk and the server only keeps the token hash.const sessionKey = await decryptSessionKey({projectId: session.projectId,sessionId,- encryptedSessionKey: session.encryptedSessionKey,+ encryptedSessionKey: storedSessionKey,kmsService});