Skip to content

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

fix(secret-rotation): wrap raw crypto errors in Stripe JWE decrypt

Calvin0a5f5bc14cca
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.ts
index 55c387d23c..7b8d7bfba3 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
@@ -68,12 +68,20 @@ describe("decryptStripeJwe", () => {
expect(() => decryptStripeJwe(jwe, privateKey)).toThrow(/A128GCM/);
});
- it("fails a tampered ciphertext", async () => {
+ it("fails a tampered ciphertext with a message an administrator can act on", async () => {
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(() => decryptStripeJwe(segments.join("."), privateKey)).toThrow(/could not decrypt the secret/);
+ });
+
+ 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/);
});
});
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.ts
index caa7d64448..dda8d48060 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
@@ -64,16 +64,24 @@ export const decryptStripeJwe = (jwe: string, privateKeyPem: string): string =>
});
}
- const contentKey = crypto.privateDecrypt(
- { key: privateKeyPem, padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, oaepHash },
- Buffer.from(wrappedKey, "base64url")
- );
+ 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"));
- decipher.setAAD(Buffer.from(protectedHeader, "ascii"));
- decipher.setAuthTag(Buffer.from(authTag, "base64url"));
+ const decipher = crypto.createDecipheriv("aes-256-gcm", contentKey, Buffer.from(iv, "base64url"));
+ decipher.setAAD(Buffer.from(protectedHeader, "ascii"));
+ decipher.setAuthTag(Buffer.from(authTag, "base64url"));
- return Buffer.concat([decipher.update(Buffer.from(ciphertext, "base64url")), decipher.final()]).toString();
+ return Buffer.concat([decipher.update(Buffer.from(ciphertext, "base64url")), decipher.final()]).toString();
+ } catch {
+ // Deliberately swallows the underlying OpenSSL error (unpad failure, auth tag mismatch): it is
+ // an implementation detail that must never reach a user, and its wording is not a stable contract.
+ throw new BadRequestError({
+ message: "Infisical could not decrypt the secret Stripe returned. Try creating the API key again."
+ });
+ }
};
/**