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(secret-rotation): require a full-length GCM tag on the Stripe JWE
Calvinaca2dd2e8e53
diff --git a/backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-jwe.test.ts b/backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-jwe.test.tsindex 9851be8298..bbbc149698 100644--- a/backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-jwe.test.ts+++ b/backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-jwe.test.ts@@ -73,30 +73,22 @@ describe("decryptStripeJwe", () => {expect(() => decryptStripeJwe(jwe, privateKey)).toThrow(/A128GCM/);});- it("fails a tampered ciphertext with a message an administrator can act on", async () => {+ it("rejects an authentication tag that is not the 16 bytes A256GCM requires", async () => {const { publicKey, privateKey } = await generateStripeEncryptionKeyPair();const segments = encryptJwe("rk_live_secret", publicKey).split(".");- segments[4] = Buffer.alloc(16).toString("base64url");+ segments[4] = Buffer.from(segments[4], "base64url").subarray(0, 12).toString("base64url");- expect(() => decryptStripeJwe(segments.join("."), privateKey)).toThrow(/could not decrypt the secret/);+ expect(() => decryptStripeJwe(segments.join("."), privateKey)).toThrow(/authentication tag is 12 bytes/);});- it("logs the underlying OpenSSL error instead of discarding it", async () => {+ it("fails a tampered ciphertext with a message an administrator can act on, and logs the cause", async () => {const { logger } = await import("@app/lib/logger");const { publicKey, privateKey } = await generateStripeEncryptionKeyPair();const segments = encryptJwe("rk_live_secret", publicKey).split(".");segments[4] = Buffer.alloc(16).toString("base64url");- expect(() => decryptStripeJwe(segments.join("."), privateKey)).toThrow();- expect(logger.error).toHaveBeenCalledWith(expect.any(Error), expect.stringContaining("decryptStripeJwe"));- });-- it("fails a corrupted wrapped key with a message an administrator can act on", async () => {- const { publicKey, privateKey } = await generateStripeEncryptionKeyPair();- const segments = encryptJwe("rk_live_secret", publicKey).split(".");- segments[1] = Buffer.alloc(256).toString("base64url");-expect(() => decryptStripeJwe(segments.join("."), privateKey)).toThrow(/could not decrypt the secret/);+ expect(logger.error).toHaveBeenCalledWith(expect.any(Error), expect.stringContaining("decryptStripeJwe"));});});@@ -118,13 +110,6 @@ describe("readStripeSecret", () => {expect(readStripeSecret({ token: "rk_test_plain", encrypted_secret: null }, privateKey)).toBe("rk_test_plain");});- it("decrypts the JWE when it is the only shape returned", async () => {- const { publicKey, privateKey } = await generateStripeEncryptionKeyPair();- const jwe = encryptJwe("rk_live_secret", publicKey);-- expect(readStripeSecret({ encrypted_secret: { ciphertext: jwe } }, privateKey)).toBe("rk_live_secret");- });-it("throws when Stripe returns neither", async () => {const { privateKey } = await generateStripeEncryptionKeyPair();diff --git a/backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-jwe.ts b/backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-jwe.tsindex 3e4aeef267..b8c20b5276 100644--- a/backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-jwe.ts+++ b/backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-jwe.ts@@ -17,6 +17,11 @@ const SUPPORTED_KEY_ALGORITHMS: Record<string, "sha1" | "sha256"> = {const SUPPORTED_CONTENT_ALGORITHM = "A256GCM";+// A256GCM fixes the authentication tag at 16 bytes. Node accepts any tag length OpenSSL supports+// unless it is told otherwise, so a truncated tag would authenticate against fewer bits than the+// algorithm promises.+const GCM_AUTH_TAG_LENGTH = 16;+export const generateStripeEncryptionKeyPair = async () =>new Promise<{ publicKey: string; privateKey: string }>((resolve, reject) => {crypto.generateKeyPair(@@ -65,15 +70,25 @@ export const decryptStripeJwe = (jwe: string, privateKeyPem: string): string =>});}+ const decodedAuthTag = Buffer.from(authTag, "base64url");++ if (decodedAuthTag.length !== GCM_AUTH_TAG_LENGTH) {+ throw new BadRequestError({+ message: `Stripe returned an encrypted secret whose authentication tag is ${decodedAuthTag.length} bytes. ${SUPPORTED_CONTENT_ALGORITHM} requires ${GCM_AUTH_TAG_LENGTH}.`+ });+ }+try {const contentKey = crypto.privateDecrypt({ key: privateKeyPem, padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, oaepHash },Buffer.from(wrappedKey, "base64url"));- const decipher = crypto.createDecipheriv("aes-256-gcm", contentKey, Buffer.from(iv, "base64url"));+ const decipher = crypto.createDecipheriv("aes-256-gcm", contentKey, Buffer.from(iv, "base64url"), {+ authTagLength: GCM_AUTH_TAG_LENGTH+ });decipher.setAAD(Buffer.from(protectedHeader, "ascii"));- decipher.setAuthTag(Buffer.from(authTag, "base64url"));+ decipher.setAuthTag(decodedAuthTag);return Buffer.concat([decipher.update(Buffer.from(ciphertext, "base64url")), decipher.final()]).toString();} catch (error) {