Skip to content

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

chore(secret-rotation): log stripe JWE decrypt failures, add auth test

Calvinfd59e4efcd41
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 7b8d7bfba3..5cafb85053 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
@@ -1,7 +1,12 @@
import crypto from "node:crypto";
-import { describe, expect, it } from "vitest";
+import { describe, expect, it, vi } from "vitest";
+vi.mock("@app/lib/logger", () => ({
+ logger: { error: vi.fn(), info: vi.fn(), warn: vi.fn(), debug: vi.fn() }
+}));
+
+// eslint-disable-next-line import/first
import { decryptStripeJwe, generateStripeEncryptionKeyPair, readStripeSecret } from "./stripe-api-key-jwe";
/** Builds the compact JWE Stripe would return, so the test proves the real unwrap path. */
@@ -76,6 +81,16 @@ describe("decryptStripeJwe", () => {
expect(() => decryptStripeJwe(segments.join("."), privateKey)).toThrow(/could not decrypt the secret/);
});
+ it("logs the underlying OpenSSL error instead of discarding it", 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(".");
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 dda8d48060..3168707ad5 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
@@ -1,6 +1,7 @@
import crypto from "node:crypto";
import { BadRequestError } from "@app/lib/errors";
+import { logger } from "@app/lib/logger";
export type TStripeSecretKeyField = {
token?: string | null;
@@ -75,9 +76,11 @@ export const decryptStripeJwe = (jwe: string, privateKeyPem: string): string =>
decipher.setAuthTag(Buffer.from(authTag, "base64url"));
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.
+ } catch (error) {
+ // The underlying OpenSSL error (unpad failure, auth tag mismatch) carries no plaintext or key
+ // material, but it is an implementation detail that must never reach a user, and its wording is
+ // not a stable contract, so it is logged here rather than thrown.
+ logger.error(error, "decryptStripeJwe: failed to decrypt a Stripe-returned secret");
throw new BadRequestError({
message: "Infisical could not decrypt the secret Stripe returned. Try creating the API key again."
});
diff --git a/backend/src/services/app-connection/stripe/stripe-connection-service.test.ts b/backend/src/services/app-connection/stripe/stripe-connection-service.test.ts
index 517c1ad8ad..9bcffa3548 100644
--- a/backend/src/services/app-connection/stripe/stripe-connection-service.test.ts
+++ b/backend/src/services/app-connection/stripe/stripe-connection-service.test.ts
@@ -13,6 +13,9 @@ vi.mock("@app/lib/logger", () => ({
logger: { error: vi.fn(), info: vi.fn(), warn: vi.fn(), debug: vi.fn() }
}));
+// eslint-disable-next-line import/first
+import { AppConnection } from "@app/services/app-connection/app-connection-enums";
+
// eslint-disable-next-line import/first
import { stripeConnectionService } from "./stripe-connection-service";
@@ -84,4 +87,15 @@ describe("stripeConnectionService.listApiKeys", () => {
expect(keys.map((key) => key.id)).toEqual(["mk_1"]);
expect(getMock).toHaveBeenCalledTimes(1);
});
+
+ it("authorizes the caller for this connection before reaching Stripe", async () => {
+ getMock.mockResolvedValueOnce(keyPage("mk_1"));
+ const getAppConnection = vi.fn(async () => ({ credentials: { accountId: "acct_123" } }));
+ const actor = { type: "user", id: "user-1", authMethod: "email", orgId: "org-1" };
+
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument
+ await stripeConnectionService(getAppConnection as any).listApiKeys("connection-id", actor as any);
+
+ expect(getAppConnection).toHaveBeenCalledWith(AppConnection.Stripe, "connection-id", actor);
+ });
});