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): retire path attempts expire before existence check
Calvinc92f47b75ed8
diff --git a/backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-rotation-fns.test.ts b/backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-rotation-fns.test.tsindex 4423dfabd0..d9bb429f0a 100644--- a/backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-rotation-fns.test.ts+++ b/backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-rotation-fns.test.ts@@ -183,18 +183,17 @@ describe("stripeApiKeyRotationFactory", () => {).resolves.toBeDefined();});- it("completes a rotation when the old key is already gone from Stripe, without ever calling expire on it", async () => {- // A double expire's response has never been observed, so this drives the fix through the- // existence check (a 404 on retrieve) rather than assuming a 404 on a second expire call.+ it("completes a rotation when the old key is already gone from Stripe, via the existence check after a non-404 expire failure", async () => {+ // A double expire's response has never been observed, so a repeat expire is mocked to fail with+ // something other than 404 (a generic 400), and the existence check (a 404 on retrieve) is what+ // recognizes the key as already gone.getMock.mockImplementation(async (url: string) => {if (url.endsWith("/mk_old")) throw httpError(404, "No such key");throw new Error(`unexpected GET ${url}`);});mockStripe({expire: async (keyId) => {- if (keyId === "mk_old") {- throw new Error("expire must not be called for a key the existence check already found gone");- }+ if (keyId === "mk_old") throw httpError(400, "Key is not active");return { data: {} };}});@@ -204,10 +203,37 @@ describe("stripeApiKeyRotationFactory", () => {makeFactory().rotateCredentials({ keyId: "mk_old" } as any, callback as any, {} as any)).resolves.toBeDefined();- expect(expireCalls("mk_old")).toHaveLength(0);+ expect(expireCalls("mk_old")).toHaveLength(1);expect(callback).toHaveBeenCalled();});+ it("issues no GET at all on the happy path", async () => {+ mockStripe();+ const callback = vi.fn(async (credentials: unknown) => credentials);++ await makeFactory().rotateCredentials({ keyId: "mk_old", apiKey: "rk_old" } as any, callback as any, {} as any);++ expect(getMock).not.toHaveBeenCalled();+ });++ it("surfaces the original expire error, not the existence check's error, when the check itself fails", async () => {+ getMock.mockImplementation(async (url: string) => {+ if (url.endsWith("/mk_old")) throw httpError(500, "Stripe existence check is down");+ throw new Error(`unexpected GET ${url}`);+ });+ mockStripe({+ expire: async (keyId) => {+ if (keyId === "mk_old") throw httpError(400, "Key is not active");+ return { data: {} };+ }+ });+ const callback = vi.fn(async (credentials: unknown) => credentials);++ await expect(+ makeFactory().rotateCredentials({ keyId: "mk_old" } as any, callback as any, {} as any)+ ).rejects.toThrow("Key is not active");+ });+it("revokeCredentials returns early without contacting Stripe when there are no credentials", async () => {const callback = vi.fn(async () => "done");diff --git a/backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-rotation-fns.ts b/backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-rotation-fns.tsindex 1e492b8fb0..6fcf8c7c03 100644--- a/backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-rotation-fns.ts+++ b/backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-rotation-fns.ts@@ -44,9 +44,11 @@ export const stripeApiKeyRotationFactory: TRotationFactory<// It is what makes a key stranded by a timed-out create identifiable in the Stripe dashboard.const $keyName = () => `infisical-${secretsMapping.apiKey}-${Date.now()}`.slice(0, STRIPE_KEY_NAME_MAX_LENGTH);- /** No 404 on a double expire has ever been observed, so retiring a key twice can't be trusted to look- * like the first expire. Checking existence first sidesteps the question: a key that is already- * gone is retired either way, and a key that still exists gets the expire call as before.+ /** No 404 on a double expire has ever been observed, so a non-404 failure on retiring a key that+ * was already expired can't be told apart from a real failure by status code alone. This is only+ * ever consulted after the expire call itself has failed, to check whether the key was already+ * gone; it must never run first, since a transient failure here (a 429, a 5xx, a socket error)+ * would otherwise block an expire attempt that would very likely have succeeded.*/const $keyExists = async (keyId: string): Promise<boolean> => {try {@@ -60,12 +62,6 @@ export const stripeApiKeyRotationFactory: TRotationFactory<};const $tryRetireKey = async (keyId: string): Promise<{ retired: true } | { retired: false; error: unknown }> => {- try {- if (!(await $keyExists(keyId))) return { retired: true };- } catch (error) {- return { retired: false, error };- }-try {await request.post(`${STRIPE_API_KEYS_URL}/${keyId}/expire`,@@ -73,10 +69,21 @@ export const stripeApiKeyRotationFactory: TRotationFactory<withIdempotencyKey(getStripePlatformRequestConfig(accountId)));return { retired: true };- } catch (error) {- if (getStripeErrorStatus(error) === 404) return { retired: true };+ } catch (expireError) {+ if (getStripeErrorStatus(expireError) === 404) return { retired: true };++ // The expire failed for some other reason. It might still be that the key was already+ // retired and a repeat expire doesn't return 404 the way a first one would, so check+ // existence to excuse the failure. But the check is only ever an excuse, never a+ // replacement: if it also fails, or it finds the key still there, the original expire+ // error is what gets surfaced.+ try {+ if (!(await $keyExists(keyId))) return { retired: true };+ } catch {+ // Fall through and surface expireError below.+ }- return { retired: false, error };+ return { retired: false, error: expireError };}};