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
chore(pam): name upload token rejection reasons
bernie-gc8bbc9ed4012
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 36ec0dd1dd..050c2f83c4 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@@ -73,30 +73,51 @@ export const decryptSessionKey = async ({return decrypted.subarray(32, 32 + SESSION_KEY_LENGTH);};+// 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).+export const PamUploadTokenRejection = {+ Missing: "PamUploadTokenMissing",+ MalformedLength: "PamUploadTokenMalformedLength",+ StoredHashMalformed: "PamUploadTokenStoredHashMalformed",+ HashMismatch: "PamUploadTokenHashMismatch"+} as const;++const UPLOAD_TOKEN_INVALID_MESSAGE = "Invalid upload token";+export const verifyGatewayUploadToken = (presentedTokenBase64: string | undefined | null,storedTokenHash: Buffer | null | undefined) => {if (!presentedTokenBase64 || !storedTokenHash) {throw new BadRequestError({+ name: PamUploadTokenRejection.Missing,message: "Gateway upload token missing or session not configured for chunked uploads"});}- let presentedBuf: Buffer;- try {- presentedBuf = Buffer.from(presentedTokenBase64, "base64");- } catch {- throw new BadRequestError({ message: "Invalid upload token" });- }++ // Buffer.from drops invalid base64 characters rather than throwing, so a bad encoding arrives+ // here as a short decode, never as an exception.+ const presentedBuf = Buffer.from(presentedTokenBase64, "base64");if (presentedBuf.length !== UPLOAD_TOKEN_LENGTH) {- throw new BadRequestError({ message: "Invalid upload token" });+ throw new BadRequestError({+ name: PamUploadTokenRejection.MalformedLength,+ message: UPLOAD_TOKEN_INVALID_MESSAGE+ });}+const presentedHash = crypto.nativeCrypto.createHash("sha256").update(presentedBuf).digest();if (presentedHash.length !== storedTokenHash.length) {- throw new BadRequestError({ message: "Invalid upload token" });+ throw new BadRequestError({+ name: PamUploadTokenRejection.StoredHashMalformed,+ message: UPLOAD_TOKEN_INVALID_MESSAGE+ });}if (!crypto.nativeCrypto.timingSafeEqual(presentedHash, storedTokenHash)) {- throw new BadRequestError({ message: "Invalid upload token" });+ throw new BadRequestError({+ name: PamUploadTokenRejection.HashMismatch,+ message: UPLOAD_TOKEN_INVALID_MESSAGE+ });}};