Skip to content

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

fix(secret-rotation): decrypt the Stripe JWE rather than reading the plaintext beside it

Calvin43be0a64c466
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 5cafb85053..9851be8298 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
@@ -101,13 +101,24 @@ describe("decryptStripeJwe", () => {
});
describe("readStripeSecret", () => {
- it("prefers the plaintext token test mode returns", async () => {
+ // Observed against a real sandbox: supplying a public key makes Stripe return the JWE and the
+ // plaintext token together, so this is the ordinary case rather than an edge one.
+ it("decrypts the JWE even when Stripe also returns the plaintext token", async () => {
+ const { publicKey, privateKey } = await generateStripeEncryptionKeyPair();
+ const jwe = encryptJwe("rk_test_from_jwe", publicKey);
+
+ expect(readStripeSecret({ token: "rk_test_plain", encrypted_secret: { ciphertext: jwe } }, privateKey)).toBe(
+ "rk_test_from_jwe"
+ );
+ });
+
+ it("falls back to the plaintext token when Stripe returns no ciphertext", async () => {
const { privateKey } = await generateStripeEncryptionKeyPair();
expect(readStripeSecret({ token: "rk_test_plain", encrypted_secret: null }, privateKey)).toBe("rk_test_plain");
});
- it("decrypts the JWE live mode returns", async () => {
+ it("decrypts the JWE when it is the only shape returned", async () => {
const { publicKey, privateKey } = await generateStripeEncryptionKeyPair();
const jwe = encryptJwe("rk_live_secret", publicKey);
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 3168707ad5..3e4aeef267 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
@@ -88,19 +88,19 @@ export const decryptStripeJwe = (jwe: string, privateKeyPem: string): string =>
};
/**
- * Test and sandbox accounts return the secret in plaintext, live accounts return a JWE. Both shapes
- * are real, and the one we develop against is not the one that matters.
+ * Stripe returns both shapes when a public key is supplied: a JWE, and the plaintext token beside
+ * it. The JWE is preferred rather than the token, so the path live mode depends on is the one every
+ * sandbox rotation exercises. Reading the token first would leave decryption unexercised outside
+ * this module's own tests, which decrypt only what they themselves encrypted.
*/
export const readStripeSecret = (secretKey: TStripeSecretKeyField | undefined, privateKeyPem: string): string => {
- if (secretKey?.token) return secretKey.token;
-
const ciphertext = secretKey?.encrypted_secret?.ciphertext;
- if (!ciphertext) {
- throw new BadRequestError({
- message: "Stripe returned an API key without a secret. The key may need to be created again."
- });
- }
+ if (ciphertext) return decryptStripeJwe(ciphertext, privateKeyPem);
+
+ if (secretKey?.token) return secretKey.token;
- return decryptStripeJwe(ciphertext, privateKeyPem);
+ throw new BadRequestError({
+ message: "Stripe returned an API key without a secret. The key may need to be created again."
+ });
};