Skip to content

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

fix: avoid retrying on 400 inside netlify

adilson3435b44b9987
diff --git a/backend/src/services/secret-sync/netlify/netlify-sync-fns.test.ts b/backend/src/services/secret-sync/netlify/netlify-sync-fns.test.ts
new file mode 100644
index 0000000000..164b799b07
--- /dev/null
+++ b/backend/src/services/secret-sync/netlify/netlify-sync-fns.test.ts
@@ -0,0 +1,75 @@
+import { AxiosError, AxiosResponse } from "axios";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+import { SecretSyncError } from "../secret-sync-errors";
+import { TSecretSyncPayload } from "../secret-sync-payload";
+import { NetlifySyncFns } from "./netlify-sync-fns";
+import { TNetlifySyncWithCredentials } from "./netlify-sync-types";
+
+vi.mock("@app/lib/config/env", () => ({ getConfig: () => ({}) }));
+
+const { getVariables, createVariable, updateVariableValue } = vi.hoisted(() => ({
+ getVariables: vi.fn(),
+ createVariable: vi.fn(),
+ updateVariableValue: vi.fn()
+}));
+vi.mock("@app/services/app-connection/netlify/netlify-connection-public-client", () => ({
+ NetlifyPublicAPI: { getVariables, createVariable, updateVariableValue }
+}));
+
+const sync = {
+ destination: "netlify",
+ destinationConfig: { accountId: "account-id", siteId: "site-id", context: "all" },
+ environment: { slug: "prod" },
+ syncOptions: { disableSecretDeletion: true },
+ connection: { method: "access-token", credentials: { accessToken: "token" } }
+} as unknown as TNetlifySyncWithCredentials;
+
+const payloadOf = (keys: string[]) =>
+ ({
+ flatten: () => Object.fromEntries(keys.map((key) => [key, { value: `${key}-value` }]))
+ }) as unknown as TSecretSyncPayload;
+
+const axiosErrorWithStatus = (status: number) =>
+ new AxiosError("Request failed", undefined, undefined, undefined, {
+ status,
+ data: { message: "rejected" }
+ } as AxiosResponse);
+
+describe("NetlifySyncFns.syncSecrets", () => {
+ beforeEach(() => {
+ getVariables.mockReset();
+ createVariable.mockReset();
+ updateVariableValue.mockReset();
+ getVariables.mockResolvedValue([{ key: "EXISTING", values: [] }]);
+ });
+
+ it("fails without retry when Netlify rejects a new variable with 400", async () => {
+ createVariable.mockRejectedValue(axiosErrorWithStatus(400));
+
+ const error = await NetlifySyncFns.syncSecrets(sync, payloadOf(["NEW"])).catch((err: unknown) => err);
+
+ expect(error).toBeInstanceOf(SecretSyncError);
+ expect((error as SecretSyncError).shouldRetry).toBe(false);
+ expect((error as SecretSyncError).secretKey).toBe("NEW");
+ });
+
+ it("fails without retry when Netlify rejects an updated variable with 400", async () => {
+ updateVariableValue.mockRejectedValue(axiosErrorWithStatus(400));
+
+ const error = await NetlifySyncFns.syncSecrets(sync, payloadOf(["EXISTING"])).catch((err: unknown) => err);
+
+ expect(error).toBeInstanceOf(SecretSyncError);
+ expect((error as SecretSyncError).shouldRetry).toBe(false);
+ expect((error as SecretSyncError).secretKey).toBe("EXISTING");
+ });
+
+ it("keeps retrying when Netlify fails with a server error", async () => {
+ createVariable.mockRejectedValue(axiosErrorWithStatus(500));
+
+ const error = await NetlifySyncFns.syncSecrets(sync, payloadOf(["NEW"])).catch((err: unknown) => err);
+
+ expect(error).toBeInstanceOf(SecretSyncError);
+ expect((error as SecretSyncError).shouldRetry).toBe(true);
+ });
+});
diff --git a/backend/src/services/secret-sync/netlify/netlify-sync-fns.ts b/backend/src/services/secret-sync/netlify/netlify-sync-fns.ts
index 4f4cbca7f5..8899ebcdc0 100644
--- a/backend/src/services/secret-sync/netlify/netlify-sync-fns.ts
+++ b/backend/src/services/secret-sync/netlify/netlify-sync-fns.ts
@@ -1,4 +1,6 @@
/* eslint-disable no-continue */
+import { HttpStatusCode, isAxiosError } from "axios";
+
import { NetlifyPublicAPI } from "@app/services/app-connection/netlify/netlify-connection-public-client";
import { matchesSchema } from "@app/services/secret-sync/secret-sync-fns";
import { TSecretSyncPayload } from "@app/services/secret-sync/secret-sync-payload";
@@ -7,6 +9,15 @@ import { TSecretMap } from "@app/services/secret-sync/secret-sync-types";
import { SecretSyncError } from "../secret-sync-errors";
import type { TNetlifySyncWithCredentials } from "./netlify-sync-types";
+const throwNetlifySyncError = (error: unknown, secretKey: string): never => {
+ throw new SecretSyncError({
+ error,
+ secretKey,
+ // Netlify answers 400 for variables it refuses to store, so retrying the same request can't succeed.
+ shouldRetry: !(isAxiosError(error) && error.response?.status === HttpStatusCode.BadRequest)
+ });
+};
+
export const NetlifySyncFns = {
async getSecrets(secretSync: TNetlifySyncWithCredentials): Promise<TSecretMap> {
const config = secretSync.destinationConfig;
@@ -76,7 +87,7 @@ export const NetlifySyncFns = {
values: [{ context: config.context, value: secretMap[key].value }]
});
} catch (error) {
- throw new SecretSyncError({ error, secretKey: key });
+ throwNetlifySyncError(error, key);
}
}
@@ -88,7 +99,7 @@ export const NetlifySyncFns = {
value: secretMap[key].value
});
} catch (error) {
- throw new SecretSyncError({ error, secretKey: key });
+ throwNetlifySyncError(error, key);
}
}
@@ -110,7 +121,7 @@ export const NetlifySyncFns = {
await NetlifyPublicAPI.deleteVariable(secretSync.connection, params, { key });
}
} catch (error) {
- throw new SecretSyncError({ error, secretKey: key });
+ throwNetlifySyncError(error, key);
}
}
},