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
use endpoint from wrangler
Matheus Nogueirad8cc685be97f
diff --git a/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-version-fns.test.ts b/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-version-fns.test.tsindex 8c74571786..bf96db15fd 100644--- a/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-version-fns.test.ts+++ b/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-version-fns.test.ts@@ -12,15 +12,15 @@ import { TCloudflareWorkersSyncWithCredentials } from "./cloudflare-workers-typevi.mock("@app/lib/config/env", () => ({ getConfig: () => ({}) }));vi.mock("@app/lib/logger", () => ({ logger: { info: vi.fn(), error: vi.fn(), warn: vi.fn(), debug: vi.fn() } }));-const { get, patch, post, del } = vi.hoisted(() => ({ get: vi.fn(), patch: vi.fn(), post: vi.fn(), del: vi.fn() }));+const { get, patch } = vi.hoisted(() => ({ get: vi.fn(), patch: vi.fn() }));vi.mock("@app/lib/config/request", async (importOriginal) => ({...(await importOriginal<object>()),- request: { get, patch, post, delete: del }+ request: { get, patch }}));-type TBinding = { type: string; name: string; text?: string; version_id?: string } & Record<string, unknown>;-type TVersion = { id: string; bindings: TBinding[] } & Record<string, unknown>;-type TCreateBody = { bindings: TBinding[] } & Record<string, unknown>;+type TBinding = { type: string; name: string; text?: string; json?: unknown } & Record<string, unknown>;+type TVersion = { id: string; bindings: TBinding[] };+type TPatchBody = { env: Record<string, Omit<TBinding, "name"> | null>; annotations: Record<string, string> };const cloudflareError = (code: number) =>new AxiosError("Request failed with status code 400", "ERR_BAD_REQUEST", undefined, undefined, {@@ -28,97 +28,65 @@ const cloudflareError = (code: number) =>data: { success: false, errors: [{ code, message: `error ${code}` }] }} as AxiosResponse);-// Shaped like a real read of a pending version: besides its configuration, Cloudflare returns fields that describe-// the version itself, and the bindings a second time as `env`.-const pendingVersion = (extra: Record<string, unknown> = {}): TVersion => ({- id: "v21",- number: 21,- created_on: "2026-01-01T00:00:00Z",- source: "wrangler",- urls: ["https://my-worker.example.workers.dev"],- env: { API_KEY: { type: "secret_text" } },- main_module: "index.js",- modules: [{ name: "index.js", content_type: "application/javascript+module", content_base64: "djIx" }],- bindings: [- { type: "secret_text", name: "API_KEY" },- { type: "secret_text", name: "STALE" },- { type: "kv_namespace", name: "CACHE", namespace_id: "ns" }- ],- compatibility_date: "2026-01-01",- annotations: { "workers/triggered_by": "upload" },- ...extra-});--// The latest version. Tests set it before installing the fake, which then keeps it pointing at the newest upload.-let base: TVersion;-let versions: Map<string, TVersion>;-let uploads: number;-let uploadGate: Promise<void> | undefined;-// Lets a test make Cloudflare store something other than what was uploaded.-let alterStoredVersion: ((version: TVersion) => TVersion) | undefined;+// Fake Cloudflare: a Worker's versions and the one deployed. The secrets and settings endpoints refuse to write while+// the latest version isn't the deployed one, and PATCH versions/latest merges `env` into a copy of the latest version,+// the way the live API does.+const worker = {+ versions: [] as TVersion[],+ deployedId: "",+ storeVersion: undefined as ((version: TVersion) => TVersion) | undefined+};-// The secrets endpoint refuses because v21 isn't deployed. Version reads and creates behave like Cloudflare's,-// including the two rejections seen against the live API.-const installFakeCloudflare = (secretsEndpointRefuses = true) => {- versions = new Map([[base.id, base]]);- uploads = 0;+const latest = () => worker.versions[worker.versions.length - 1];+const uploadVersion = (bindings: TBinding[]) => {+ const version = { id: `v${worker.versions.length + 1}`, bindings };+ worker.versions.push(version);+ return version;+};+const isPending = () => latest().id !== worker.deployedId;+const installFakeCloudflare = () => {get.mockImplementation(async (url: string) => {if (url.endsWith("/secrets")) {- return { data: { result: base.bindings.filter(({ type }) => type === "secret_text") } };+ return { data: { result: latest().bindings.filter(({ type }) => type === "secret_text") } };}- if (url.endsWith("/settings")) return { data: { result: { bindings: base.bindings } } };+ if (url.endsWith("/settings")) return { data: { result: { bindings: latest().bindings } } };const ref = url.split("/versions/")[1];- const version = ref === "latest" ? base : versions.get(ref);- if (!version) throw cloudflareError(10007);+ const version = ref === "latest" ? latest() : worker.versions.find(({ id }) => id === ref);return { data: { success: true, result: structuredClone(version) } };});- patch.mockImplementation(async () => {- if (secretsEndpointRefuses) throw cloudflareError(10215);- return { data: { success: true } };- });-- post.mockImplementation(async (url: string, body: TCreateBody) => {- if (body.env && body.bindings) throw cloudflareError(10021);- if (body.bindings.some(({ type }) => type === "assets") && !body.assets) throw cloudflareError(10305);-- const bindings = body.bindings.map((binding) =>- binding.type === "inherit"- ? versions.get(binding.version_id!)!.bindings.find(({ name }) => name === binding.name)!- : binding- );- // Holds only the upload that is in flight when the gate is set.- const gate = uploadGate;- uploadGate = undefined;- await gate;- uploads += 1;- const uploaded = { ...body, id: `new-${uploads}`, bindings };- const version = alterStoredVersion ? alterStoredVersion(uploaded) : uploaded;- versions.set(version.id, version);- base = version;- return { data: { success: true, result: { id: version.id } } };- });+ patch.mockImplementation(async (url: string, body: unknown) => {+ if (url.endsWith("/secrets-bulk") || url.endsWith("/settings")) {+ if (isPending()) throw cloudflareError(url.endsWith("/settings") ? 10214 : 10215);+ return { data: { success: true } };+ }- // Checked against the live API: neither the latest nor a deployed version can be deleted.- del.mockImplementation(async (url: string) => {- const id = url.split("/versions/")[1];- if (id === base.id) throw cloudflareError(10339);- versions.delete(id);- return { data: { success: true } };+ const bindings = new Map(latest().bindings.map((binding) => [binding.name, binding]));+ for (const [name, binding] of Object.entries((body as TPatchBody).env)) {+ if (binding === null) bindings.delete(name);+ else bindings.set(name, { ...binding, name } as TBinding);+ }+ const uploaded = uploadVersion([...bindings.values()]);+ if (worker.storeVersion) worker.versions[worker.versions.length - 1] = worker.storeVersion(uploaded);+ return { data: { success: true, result: { id: uploaded.id } } };});};-const syncOf = (syncOptions: Record<string, unknown> = {}, id = "sync-id") =>+const syncOf = (syncOptions: Record<string, unknown> = {}, variableType?: CloudflareWorkersVariableType) =>({- id,+ id: "sync-id",destination: "cloudflare-workers",- destinationConfig: { scriptId: "my-worker", variableType: CloudflareWorkersVariableType.Secret },+ destinationConfig: { scriptId: "my-worker", variableType: variableType ?? CloudflareWorkersVariableType.Secret },environment: { slug: "prod" },syncOptions: { disableSecretDeletion: false, ...syncOptions },connection: { credentials: { apiToken: "token", accountId: "account-id" } }}) as unknown as TCloudflareWorkersSyncWithCredentials;-const secretSync = syncOf();++const payloadOf = (secrets: Record<string, string>) =>+ ({+ flatten: () => Object.fromEntries(Object.entries(secrets).map(([key, value]) => [key, { value }]))+ }) as unknown as TSecretSyncPayload;// Like Redlock: acquiring a held lock fails rather than waits.const heldLocks = new Set<string>();@@ -134,221 +102,136 @@ const keyStore = {}} as unknown as Pick<TKeyStoreFactory, "acquireLock">;-const payloadOf = (secrets: Record<string, string>) =>- ({- flatten: () => Object.fromEntries(Object.entries(secrets).map(([key, value]) => [key, { value }]))- }) as unknown as TSecretSyncPayload;--const sync = (secrets: Record<string, string>, target = secretSync) =>+const sync = (secrets: Record<string, string>, target = syncOf()) =>CloudflareWorkersSyncFns.syncSecrets(target, payloadOf(secrets), { keyStore });-const versionCreates = () =>- post.mock.calls.filter(([url]) => (url as string).endsWith("/versions")) as Array<- [string, TCreateBody, { params?: unknown }]- >;+const versionPatches = () => patch.mock.calls.filter(([url]) => (url as string).endsWith("/versions/latest"));+const sentEnv = () => {+ expect(versionPatches()).toHaveLength(1);+ return (versionPatches()[0][1] as TPatchBody).env;+};-describe("Cloudflare Workers version fallback", () => {+const INITIAL_BINDINGS: TBinding[] = [+ { type: "secret_text", name: "API_KEY" },+ { type: "secret_text", name: "STALE" },+ { type: "plain_text", name: "MODE", text: "prod" },+ { type: "kv_namespace", name: "CACHE", namespace_id: "ns" }+];++describe("Cloudflare Workers sync version fallback", () => {beforeEach(() => {vi.clearAllMocks();- base = pendingVersion();- uploadGate = undefined;- alterStoredVersion = undefined;+ worker.versions = [];+ worker.storeVersion = undefined;heldLocks.clear();+ installFakeCloudflare();+ worker.deployedId = uploadVersion(INITIAL_BINDINGS).id;+ // A version uploaded without deploying it, as `wrangler versions upload` leaves one.+ uploadVersion(INITIAL_BINDINGS);});- it("doesn't create a version when the secrets endpoint succeeds", async () => {- installFakeCloudflare(false);+ it("isn't used when the latest version is the deployed one", async () => {+ worker.deployedId = latest().id;- await sync({ API_KEY: "new" });-- expect(versionCreates()).toEqual([]);+ expect(await sync({ API_KEY: "new" })).toBeUndefined();+ expect(versionPatches()).toHaveLength(0);});- it("adds the secrets to a new version built on the latest one, and never deploys it", async () => {- installFakeCloudflare();-+ it("patches the latest version when the Worker has a pending version, and names the new one", async () => {const result = await sync({ API_KEY: "new", ADDED: "a" });- const [[, body, config]] = versionCreates();- expect(config.params).toEqual({ deploy: false });- expect(Object.fromEntries(body.bindings.map((binding) => [binding.name, binding]))).toEqual({- API_KEY: { type: "secret_text", name: "API_KEY", text: "new" },- CACHE: { type: "inherit", name: "CACHE", version_id: "v21" },- ADDED: { type: "secret_text", name: "ADDED", text: "a" }+ expect(sentEnv()).toEqual({+ API_KEY: { type: "secret_text", text: "new" },+ ADDED: { type: "secret_text", text: "a" },+ STALE: null+ });+ expect((versionPatches()[0][1] as TPatchBody).annotations).toMatchObject({ "workers/tag": "infisical-sync" });+ expect(worker.deployedId).toBe("v1");+ expect(result).toEqual({+ createdSecretKeys: ["ADDED"],+ updatedSecretKeys: ["API_KEY"],+ deletedSecretKeys: ["STALE"],+ message:+ "Worker has undeployed changes; created version v3 with updated secrets. Deploy it for the secrets to take effect."});- expect(post.mock.calls.some(([url]) => (url as string).endsWith("/deployments"))).toBe(false);- expect(result?.message).toContain("new-1");- });-- it("sends back every field of the base version except its id and env", async () => {- const extra = { tail_consumers: [{ service: "logger" }], exports: { Room: { type: "durable-object" } } };- base = pendingVersion(extra);- installFakeCloudflare();-- await sync({ API_KEY: "new" });-- const [[, body]] = versionCreates();- expect(body).not.toHaveProperty("id");- expect(body).not.toHaveProperty("env");- expect(body).toMatchObject({ number: 21, main_module: "index.js", modules: base.modules, ...extra });});- it.each([- ["assets", { assets: { config: { html_handling: "auto-trailing-slash" } } }],- ["an assets binding", { bindings: [...pendingVersion().bindings, { type: "assets", name: "ASSETS" }] }]- ])("refuses a Worker with %s, which a copy would lose", async (label, extra) => {- base = pendingVersion(extra);- installFakeCloudflare();+ it("is used when the settings endpoint refuses a text sync", async () => {+ await sync({ NEW_VAR: "v" }, syncOf({}, CloudflareWorkersVariableType.Text));- await expect(sync({ API_KEY: "new" })).rejects.toThrow("static assets");- expect(versionCreates()).toEqual([]);+ expect(sentEnv()).toEqual({ NEW_VAR: { type: "plain_text", text: "v" }, MODE: null });});- it("fails a secret removal instead of leaving the secrets on the deployed Worker", async () => {- installFakeCloudflare();-- await expect(CloudflareWorkersSyncFns.removeSecrets(secretSync, payloadOf({ API_KEY: "x" }))).rejects.toThrow(- "deploy the latest Worker version"+ it("isn't used to remove secrets, which would stay live while the removal reported success", async () => {+ await expect(CloudflareWorkersSyncFns.removeSecrets(syncOf(), payloadOf({ API_KEY: "x" }))).rejects.toThrow(+ "latest Worker version is not deployed");- expect(versionCreates()).toEqual([]);- });-- it("explains a timed-out upload, and doesn't retry it", async () => {- installFakeCloudflare();- post.mockRejectedValueOnce(new AxiosError("timeout of 300000ms exceeded", "ECONNABORTED"));-- const error = (await sync({ API_KEY: "new" }).catch((err: unknown) => err)) as SecretSyncError;-- expect(error).toBeInstanceOf(SecretSyncError);- expect(error.message).toContain("took longer than 60 seconds");- expect(error.shouldRetry).toBe(false);+ expect(versionPatches()).toHaveLength(0);});- it("makes a second sync to the same Worker wait, so neither loses the other's changes", async () => {- installFakeCloudflare();- // Two different syncs, so only a lock on the Worker itself serializes them.- const firstSync = syncOf({ disableSecretDeletion: true }, "first-sync");- const secondSync = syncOf({ disableSecretDeletion: true }, "second-sync");- let openUploadGate = () => {};- uploadGate = new Promise((resolve) => {- openUploadGate = resolve;+ describe("planning the patch", () => {+ it("fails before patching when a secret has the name of a non-variable binding", async () => {+ await expect(sync({ CACHE: "value" })).rejects.toThrow("CACHE");+ expect(versionPatches()).toHaveLength(0);});- const first = sync({ API_KEY: "a" }, firstSync);- try {- await vi.waitFor(() => expect(versionCreates()).toHaveLength(1));-- // The first sync is mid-upload and holds the Worker's lock.- const second = (await sync({ STALE: "b" }, secondSync).catch((err: unknown) => err)) as SecretSyncError;- expect(second).toBeInstanceOf(SecretSyncError);- expect(second.shouldRetry).toBe(true);- } finally {- openUploadGate();- }- await first;+ it("fails before patching rather than change a variable's type", async () => {+ await expect(sync({ API_KEY: "new", MODE: "value" })).rejects.toThrow("MODE (text)");+ expect(versionPatches()).toHaveLength(0);+ });- // The queue's retry builds on the first sync's version.- await sync({ STALE: "b" }, secondSync);- const latest = Object.fromEntries(base.bindings.map((binding) => [binding.name, binding]));- expect(latest.API_KEY).toMatchObject({ text: "a" });- expect(latest.STALE).toMatchObject({ text: "b" });- });+ it.each([+ [false, { STALE: null }],+ [true, {}]+ ])(+ "deletes only secrets that exist on the latest version (disableSecretDeletion: %s)",+ async (disableSecretDeletion, deletions) => {+ await sync({ API_KEY: "new", NEVER_SYNCED: "x" }, syncOf({ disableSecretDeletion }));++ expect(sentEnv()).toEqual({+ API_KEY: { type: "secret_text", text: "new" },+ NEVER_SYNCED: { type: "secret_text", text: "x" },+ ...deletions+ });+ }+ );- it("releases the Worker's lock when the fallback fails", async () => {- base = pendingVersion({ assets: { config: {} } });- installFakeCloudflare();+ it("fails before patching a JSON sync on a value that isn't JSON", async () => {+ await expect(sync({ CONFIG: "not json" }, syncOf({}, CloudflareWorkersVariableType.Json))).rejects.toThrow(+ "not valid JSON"+ );+ expect(versionPatches()).toHaveLength(0);+ });- await expect(sync({ API_KEY: "new" })).rejects.toThrow("static assets");- expect(heldLocks.size).toBe(0);- });+ it("updates a text variable as it is when the toggle is on", async () => {+ await sync({ API_KEY: "new", MODE: "staging" }, syncOf({ syncNonSecretBindings: true }));- describe("when Cloudflare stores a version different from the upload", () => {- const tamper = (version: TVersion): TVersion => ({- ...version,- bindings: version.bindings.filter(({ name }) => name !== "CACHE"),- modules: [{ name: "index.js", content_type: "application/javascript+module", content_base64: "Y2hhbmdlZA==" }]+ expect(sentEnv()).toMatchObject({ MODE: { type: "plain_text", text: "staging" } });});- const tamperFirstUpload = () => {- alterStoredVersion = (version) => {- alterStoredVersion = undefined;- return tamper(version);- };- };+ });- it("replaces it with an unchanged copy of the base and deletes it", async () => {- installFakeCloudflare();- tamperFirstUpload();+ describe("failures", () => {+ it("rejects a version whose bindings don't match the patch, and names the difference", async () => {+ worker.storeVersion = (version) => ({+ ...version,+ bindings: version.bindings.filter(({ name }) => name !== "CACHE")+ });const error = (await sync({ API_KEY: "s3cr3t-value" }).catch((err: unknown) => err)) as Error;expect(error).toBeInstanceOf(SecretSyncError);- expect(error.message).toContain("CACHE");- expect(error.message).toContain("index.js");- expect(error.message).toContain("deleted that version");+ expect(error.message).toContain("missing CACHE (kv_namespace)");expect(error.message).not.toContain("s3cr3t-value");-- const [, [, restore]] = versionCreates();- expect(restore.modules).toEqual(pendingVersion().modules);- expect(- restore.bindings.every(({ type, version_id: versionId }) => type === "inherit" && versionId === "v21")- ).toBe(true);- expect(versions.has("new-1")).toBe(false);- expect(base.id).toBe("new-2");});- it("tells the user not to deploy either version when the restored copy is also different", async () => {- installFakeCloudflare();- alterStoredVersion = tamper;-- await expect(sync({ API_KEY: "new" })).rejects.toThrow("don't deploy version new-1 or new-2");- expect(del).not.toHaveBeenCalled();- });-- it.each([- ["reading the base", "/versions/v21", "new-1."],- ["reading back the restored copy", "/versions/new-2", "new-1 or new-2."]- ])(- "doesn't let the job retry build on the rejected version when %s fails",- async (label, failingRead, unsafeVersions) => {- installFakeCloudflare();- tamperFirstUpload();- const readVersion = get.getMockImplementation() as (url: string) => Promise<unknown>;- get.mockImplementation(async (url: string) => {- if (url.endsWith(failingRead)) throw new AxiosError("timeout of 60000ms exceeded", "ECONNABORTED");- return readVersion(url);- });-- const error = (await sync({ API_KEY: "new" }).catch((err: unknown) => err)) as SecretSyncError;-- expect(error).toBeInstanceOf(SecretSyncError);- expect(error.shouldRetry).toBe(false);- expect(error.message).toContain(`don't deploy version ${unsafeVersions}`);- expect(error.message).toContain("upload a new version of the Worker before syncing again");- expect(del).not.toHaveBeenCalled();- }- );-- it("tells the user not to deploy the rejected version when it can't be deleted", async () => {- installFakeCloudflare();- tamperFirstUpload();- del.mockRejectedValueOnce(new AxiosError("socket hang up", "ECONNRESET"));+ it("lets the job retry when another sync holds the Worker's lock", async () => {+ heldLocks.add("cloudflare-worker-version-mutex-account-id-my-worker");- await expect(sync({ API_KEY: "new" })).rejects.toThrow("couldn't delete the rejected version new-1");- expect(base.id).toBe("new-2");- });- });+ const error = (await sync({ API_KEY: "new" }).catch((err: unknown) => err)) as SecretSyncError;- it("doesn't retry the sync when reading back the uploaded version fails", async () => {- installFakeCloudflare();- const readVersion = get.getMockImplementation() as (url: string) => Promise<unknown>;- get.mockImplementation(async (url: string) => {- if (url.endsWith("/versions/new-1")) throw new AxiosError("timeout of 60000ms exceeded", "ECONNABORTED");- return readVersion(url);+ expect(error).toBeInstanceOf(SecretSyncError);+ expect(error.shouldRetry).toBe(true);+ expect(versionPatches()).toHaveLength(0);});-- const error = (await sync({ API_KEY: "new" }).catch((err: unknown) => err)) as SecretSyncError;-- expect(error).toBeInstanceOf(SecretSyncError);- expect(error.shouldRetry).toBe(false);});});diff --git a/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-version-fns.ts b/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-version-fns.tsindex a568f9397c..6a72adfb3d 100644--- a/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-version-fns.ts+++ b/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-version-fns.ts@@ -1,385 +1,142 @@-import { isAxiosError } from "axios";-import { KeyStorePrefixes, TKeyStoreFactory } from "@app/keystore/keystore";import { request } from "@app/lib/config/request";-import { crypto } from "@app/lib/crypto/cryptography";import { logger } from "@app/lib/logger";import { IntegrationUrls } from "@app/services/integration-auth/integration-list";import { SecretSyncError } from "@app/services/secret-sync/secret-sync-errors";import { TSecretMap, TSyncSecretsResult } from "@app/services/secret-sync/secret-sync-types";-import { planSyncBindings, TWrittenBinding } from "./cloudflare-workers-binding-fns";-import { getCloudflareErrors, validateCloudflareResponse } from "./cloudflare-workers-errors";+import { planSyncBindings, TBindingPlan } from "./cloudflare-workers-binding-fns";+import { throwOnCloudflareRequestError, validateCloudflareResponse } from "./cloudflare-workers-errors";import { TCloudflareWorkersSyncWithCredentials } from "./cloudflare-workers-types";-// A fallback moves the Worker's whole bundle each way (up to ~85 MB of base64), so only a few run at once. A job-// that finds them all busy is retried instead of waiting, since waiting counts against its lock.-const MAX_CONCURRENT_FALLBACKS = 2;-let activeFallbacks = 0;-// The three requests together stay well inside the sync job's 5-minute lock, so a second job for the same sync can't-// start while one is still uploading.-const VERSION_REQUEST_TIMEOUT_MS = 60 * 1000;-const MAX_VERSION_PAYLOAD_BYTES = 128 * 1024 * 1024;-// Outlives the version requests made under the lock at their longest: read, upload and read back, then the same three-// again to restore the base and a delete if Cloudflare stored something other than what was uploaded.-const WORKER_LOCK_DURATION_MS = 7 * VERSION_REQUEST_TIMEOUT_MS + 60 * 1000;--// Read as the reason in $fail's message, for the errors a copied version can run into.-const CLOUDFLARE_ERROR_REASONS: Record<number, string> = {- 10021: "Cloudflare rejected the Worker code copied from the latest version",- 10068: "Cloudflare rejected the Worker code copied from the latest version",- 10057: "a binding kept from the latest version no longer exists there",- 10027: "the Worker is over Cloudflare's 64 MiB size limit"-};+const REQUEST_TIMEOUT_MS = 30 * 1000;+// Outlives the three requests made under the lock at their longest.+const WORKER_LOCK_DURATION_MS = 3 * REQUEST_TIMEOUT_MS + 60 * 1000;-type TVersionModule = {- name: string;- content_type?: string;- content_base64: string;-};type TVersionBinding = { type: string; name: string };-type TWorkerVersion = {- id: string;- main_module?: string;- modules?: TVersionModule[];- bindings?: TVersionBinding[];-} & Record<string, unknown>;--// The sync whose secrets a new version applies. Without one, the new version is an unchanged copy of its base.-type TSecretChanges = { secretSync: TCloudflareWorkersSyncWithCredentials; secretMap: TSecretMap };+type TWorkerVersion = { id: string; bindings?: TVersionBinding[] };type TFallbackContext = {- syncId: string;scriptId: string;accountUrl: string;headers: Record<string, string>;};-type TExpectedVersion = {- baseVersionId: string;- bindings: string[];- moduleHashes: Map<string, string>;-};--const $fail = (- scriptId: string,- reason: string,- fix = "Deploy the latest version of the Worker in Cloudflare, then sync again.",- { error, shouldRetry = false }: { error?: unknown; shouldRetry?: boolean } = {}-) =>- new SecretSyncError({- message: `The latest version of Cloudflare Worker '${scriptId}' is not deployed, so Infisical tried to add the secrets to a new version of the Worker instead, but ${reason}. ${fix}`,- error,- shouldRetry- });--// Every failure of a fallback request reaches the user as a $fail message, including timeouts and the payload-// limits, which carry no Cloudflare error body.-const $throwRequestError = (scriptId: string, err: unknown, { canRetry }: { canRetry: boolean }): never => {- if (!isAxiosError(err)) throw err;-- const cloudflareErrors = getCloudflareErrors(err.response?.data);- if (cloudflareErrors.length) {- const known = cloudflareErrors.find(({ code }) => CLOUDFLARE_ERROR_REASONS[code]);- throw $fail(- scriptId,- known- ? CLOUDFLARE_ERROR_REASONS[known.code]- : `Cloudflare rejected the request: ${cloudflareErrors.map(({ message }) => message).join(". ")}`,- undefined,- { error: err }- );- }-- if (err.message.includes("maxContentLength") || err.message.includes("maxBodyLength")) {- throw $fail(- scriptId,- `the Worker is larger than the ${MAX_VERSION_PAYLOAD_BYTES / 1024 / 1024} MiB Infisical can copy`,- undefined,- { error: err }- );- }-- const isTimeout = err.code === "ECONNABORTED" || err.code === "ETIMEDOUT";- throw $fail(- scriptId,- isTimeout- ? `the request to Cloudflare took longer than ${VERSION_REQUEST_TIMEOUT_MS / 1000} seconds`- : "Infisical couldn't reach Cloudflare",- "Sync again.",- // A retried upload can't be told apart from a new one, so only reads are retried.- { error: err, shouldRetry: canRetry }- );-};--const $bindingLabel = ({ name, type }: { name: string; type: string }) => `${name} (${type})`;--const $hashModule = ({ content_type: contentType, content_base64: contentBase64 }: TVersionModule) =>- crypto.nativeCrypto- .createHash("sha256")- .update(contentType ?? "")- .update("\0")- .update(contentBase64)- .digest("hex");--const $hashModules = (modules: TVersionModule[]) =>- new Map(modules.map((module) => [module.name, $hashModule(module)]));--// A read that follows an upload isn't retried: the job's retry would upload another version on top of the one-// that was never checked.-const $getVersion = async (- { scriptId, accountUrl, headers }: TFallbackContext,- versionRef: string,- { canRetry = true }: { canRetry?: boolean } = {}-) => {+const $getVersion = async (ctx: TFallbackContext, versionRef: string) => {try {const { data } = await request.get<{ result: TWorkerVersion }>(- `${accountUrl}/workers/workers/${scriptId}/versions/${versionRef}`,- {- headers,- params: { include: "modules" },- timeout: VERSION_REQUEST_TIMEOUT_MS,- maxContentLength: MAX_VERSION_PAYLOAD_BYTES- }+ `${ctx.accountUrl}/workers/workers/${ctx.scriptId}/versions/${versionRef}`,+ { headers: ctx.headers, timeout: REQUEST_TIMEOUT_MS });return data.result;} catch (err) {- return $throwRequestError(scriptId, err, { canRetry });- }-};--const $validateBaseVersion = (scriptId: string, base: TWorkerVersion) => {- if (- !base.main_module ||- !base.modules?.length ||- base.modules.some((module) => typeof module.content_base64 !== "string" || !module.content_base64)- ) {- throw $fail(scriptId, `Cloudflare didn't return the code of version ${base.id}`);- }-- if (!Array.isArray(base.bindings)) {- throw $fail(scriptId, `Cloudflare didn't return the bindings of version ${base.id}`);- }-- // A read returns only the assets config, not the upload token, so a copy is created without the static files and- // serves 404 for them once deployed. Cloudflare accepts it unless the Worker also has an assets binding.- if ((base.assets !== null && base.assets !== undefined) || base.bindings.some(({ type }) => type === "assets")) {- throw $fail(scriptId, `version ${base.id} serves static assets, which Infisical can't copy into a new version`);+ return throwOnCloudflareRequestError(err);}};-// Kept apart from verification so the base version's modules can be freed before the new version is read back.-const $createVersion = async (- ctx: TFallbackContext,- baseVersionRef: string,- changes: TSecretChanges | undefined,- { message }: { message: string }-) => {- const base = await $getVersion(ctx, baseVersionRef);- $validateBaseVersion(ctx.scriptId, base);-- // Everything else is sent back, so configuration Cloudflare adds later carries over to the new version. A read- // returns the bindings as both `env` and `bindings`, and Cloudflare rejects an upload with both (10021).- const { id: baseVersionId, env: omittedEnv, ...config } = base;- const baseBindings = base.bindings as TVersionBinding[];- const plan = changes- ? planSyncBindings(changes.secretSync, changes.secretMap, baseBindings)- : { written: [], dropped: [] };-- if (changes && !plan.written.length && !plan.dropped.length) return null;-- const writtenByName = new Map(plan.written.map((binding) => [binding.name, binding]));- const dropped = new Set(plan.dropped);- const baseNames = new Set(baseBindings.map(({ name }) => name));-- const bindings: Array<TWrittenBinding | { type: "inherit"; name: string; version_id: string }> = [];- const expectedBindings: string[] = [];-- for (const binding of baseBindings) {- const written = writtenByName.get(binding.name);- if (written) {- bindings.push(written);- expectedBindings.push($bindingLabel(written));- } else if (!dropped.has(binding.name)) {- // Pinned to the base's id, since another upload can replace the latest version while this one is built.- bindings.push({- type: "inherit",- name: binding.name,- version_id: baseVersionId- });- expectedBindings.push($bindingLabel(binding));- }- }-- const createdSecretKeys: string[] = [];- for (const written of plan.written) {- if (!baseNames.has(written.name)) {- bindings.push(written);- expectedBindings.push($bindingLabel(written));- createdSecretKeys.push(written.name);- }- }-- const expected: TExpectedVersion = {- baseVersionId,- bindings: expectedBindings.sort(),- moduleHashes: $hashModules(base.modules as TVersionModule[])- };-- let newVersionId: string;+// Cloudflare copies the latest version, code and all, and merges `env` into its bindings by name: an entry creates or+// replaces that binding, and null deletes it. The new version isn't deployed.+const $patchLatestVersion = async (ctx: TFallbackContext, env: Record<string, unknown>) => {try {- const { data } = await request.post<{- result?: { id?: string };- success?: boolean;- }>(- `${ctx.accountUrl}/workers/workers/${ctx.scriptId}/versions`,+ const { data } = await request.patch<{ result?: TWorkerVersion; success?: boolean }>(+ `${ctx.accountUrl}/workers/workers/${ctx.scriptId}/versions/latest`,{- ...config,- bindings,- annotations: {- "workers/tag": "infisical-sync",- "workers/message": message- }+ env,+ annotations: { "workers/message": "Infisical secret sync", "workers/tag": "infisical-sync" }},{- headers: { ...ctx.headers, "Content-Type": "application/json" },- params: { deploy: false },- timeout: VERSION_REQUEST_TIMEOUT_MS,- maxBodyLength: MAX_VERSION_PAYLOAD_BYTES,- // A retried upload can't be told apart from a new one, and each one becomes the latest version.+ headers: { ...ctx.headers, "Content-Type": "application/merge-patch+json" },+ timeout: REQUEST_TIMEOUT_MS,+ // A retry would create a second version on top of the first."axios-retry": { retries: 0 }});validateCloudflareResponse(data);if (!data.result?.id) {- throw $fail(ctx.scriptId, "Cloudflare didn't return the id of the version it created");+ throw new SecretSyncError({+ message: `Cloudflare didn't return the version it created for Worker '${ctx.scriptId}'. Sync again.`,+ shouldRetry: false+ });}- newVersionId = data.result.id;+ return data.result;} catch (err) {if (err instanceof SecretSyncError) throw err;- return $throwRequestError(ctx.scriptId, err, { canRetry: false });+ return throwOnCloudflareRequestError(err);}-- logger.info(- `SecretSync Cloudflare Workers fallback created version [syncId=${ctx.syncId}] [scriptId=${ctx.scriptId}] [baseVersionId=${baseVersionId}] [newVersionId=${newVersionId}] [bindings=${expectedBindings.join(", ")}]`- );-- return {- newVersionId,- expected,- result: {- createdSecretKeys,- updatedSecretKeys: [...writtenByName.keys()].filter((name) => baseNames.has(name)),- deletedSecretKeys: baseBindings.filter(({ name }) => dropped.has(name)).map(({ name }) => name)- }- };};-// Compares the version Cloudflare stored with what was uploaded: binding names and types, and every module's code.-const $findDifferences = async (ctx: TFallbackContext, newVersionId: string, expected: TExpectedVersion) => {- const created = await $getVersion(ctx, newVersionId, { canRetry: false });- const differences: string[] = [];-- const actualBindings = (created.bindings ?? []).map($bindingLabel);- const missingBindings = expected.bindings.filter((binding) => !actualBindings.includes(binding));- const unexpectedBindings = actualBindings.filter((binding) => !expected.bindings.includes(binding));- if (missingBindings.length) differences.push(`missing bindings ${missingBindings.join(", ")}`);- if (unexpectedBindings.length) differences.push(`unexpected bindings ${unexpectedBindings.join(", ")}`);-- const actualModuleHashes = $hashModules(created.modules ?? []);- const moduleNames = new Set([...expected.moduleHashes.keys(), ...actualModuleHashes.keys()]);- const changedModules = [...moduleNames].filter(- (name) => expected.moduleHashes.get(name) !== actualModuleHashes.get(name)- );- if (changedModules.length) differences.push(`different modules ${changedModules.join(", ")}`);-- return differences;-};+const $bindingLabel = ({ name, type }: TVersionBinding) => `${name} (${type})`;-const $uploadCheckedVersion = async (+// Compares the bindings Cloudflare stored, by name and type, with the latest version's after the patch.+const $findDifferences = async (ctx: TFallbackContext,- baseVersionRef: string,- changes: TSecretChanges | undefined,- options: Parameters<typeof $createVersion>[3]+ versionId: string,+ baseBindings: TVersionBinding[],+ plan: TBindingPlan) => {- const created = await $createVersion(ctx, baseVersionRef, changes, options);- if (!created) return null;+ const writtenNames = new Set(plan.written.map(({ name }) => name));+ const dropped = new Set(plan.dropped);+ const expected = [+ ...baseBindings.filter(({ name }) => !writtenNames.has(name) && !dropped.has(name)),+ ...plan.written+ ].map($bindingLabel);++ const actual = ((await $getVersion(ctx, versionId)).bindings ?? []).map($bindingLabel);++ const missing = expected.filter((label) => !actual.includes(label));+ const unexpected = actual.filter((label) => !expected.includes(label));+ return [+ ...(missing.length ? [`missing ${missing.join(", ")}`] : []),+ ...(unexpected.length ? [`unexpected ${unexpected.join(", ")}`] : [])+ ];+};- const differences = await $findDifferences(ctx, created.newVersionId, created.expected);- if (differences.length) {- logger.error(- `SecretSync Cloudflare Workers fallback version mismatch [syncId=${ctx.syncId}] [scriptId=${ctx.scriptId}] [baseVersionId=${created.expected.baseVersionId}] [newVersionId=${created.newVersionId}] [differences=${differences.join("; ")}]`- );- }+const $applySecrets = async (+ secretSync: TCloudflareWorkersSyncWithCredentials,+ secretMap: TSecretMap,+ ctx: TFallbackContext+): Promise<TSyncSecretsResult | undefined> => {+ const latest = await $getVersion(ctx, "latest");+ const baseBindings = latest.bindings ?? [];- return { ...created, differences };-};+ const plan = planSyncBindings(secretSync, secretMap, baseBindings);+ if (!plan.written.length && !plan.dropped.length) return undefined;-const $deleteVersion = async ({ scriptId, accountUrl, headers }: TFallbackContext, versionId: string) => {- try {- const { data } = await request.delete<{ success?: boolean }>(- `${accountUrl}/workers/workers/${scriptId}/versions/${versionId}`,- { headers }- );- validateCloudflareResponse(data);- } catch (err) {- if (err instanceof SecretSyncError) throw err;- $throwRequestError(scriptId, err, { canRetry: false });- }-};+ // Null-prototype object so secret names like "__proto__" are sent as regular keys.+ const env = Object.create(null) as Record<string, unknown>;+ for (const { name, ...binding } of plan.written) env[name] = binding;+ for (const name of plan.dropped) env[name] = null;-// A rejected version is the latest, so the next sync would build on it, and Cloudflare refuses to delete the latest-// version. An unchanged copy of the base takes its place first, after which the rejected version can be deleted.-// Nothing here is thrown: a failure must not reach the job's retry, which would build on the rejected version.-const $replaceRejectedVersion = async (- ctx: TFallbackContext,- rejectedVersionId: string,- baseVersionId: string-): Promise<{ replaced: true; deleted: boolean } | { replaced: false; unsafeVersionIds: string[] }> => {- const unsafeVersionIds = [rejectedVersionId];+ const created = await $patchLatestVersion(ctx, env);+ logger.info(+ `SecretSync Cloudflare Workers created version [scriptId=${ctx.scriptId}] [baseVersionId=${latest.id}] [newVersionId=${created.id}]`+ );- try {- const restored = await $createVersion(ctx, baseVersionId, undefined, {- message: `Infisical secret sync (restores ${baseVersionId} in place of rejected ${rejectedVersionId})`+ const differences = await $findDifferences(ctx, created.id, baseBindings, plan);+ if (differences.length) {+ throw new SecretSyncError({+ message: `Cloudflare stored version ${created.id} of Worker '${ctx.scriptId}' with different bindings than Infisical sent (${differences.join("; ")}). Don't deploy it. Sync again, and contact support if this keeps happening.`,+ shouldRetry: false});- if (!restored) return { replaced: false, unsafeVersionIds };-- unsafeVersionIds.push(restored.newVersionId);- const differences = await $findDifferences(ctx, restored.newVersionId, restored.expected);- if (differences.length) {- logger.error(- `SecretSync Cloudflare Workers restored version mismatch [syncId=${ctx.syncId}] [scriptId=${ctx.scriptId}] [baseVersionId=${baseVersionId}] [newVersionId=${restored.newVersionId}] [differences=${differences.join("; ")}]`- );- return { replaced: false, unsafeVersionIds };- }- } catch (err) {- logger.error(- err,- `SecretSync Cloudflare Workers failed to restore version [syncId=${ctx.syncId}] [scriptId=${ctx.scriptId}] [baseVersionId=${baseVersionId}] [rejectedVersionId=${rejectedVersionId}]`- );- return { replaced: false, unsafeVersionIds };}- try {- await $deleteVersion(ctx, rejectedVersionId);- return { replaced: true, deleted: true };- } catch (err) {- logger.warn(- err,- `SecretSync Cloudflare Workers failed to delete rejected version [syncId=${ctx.syncId}] [scriptId=${ctx.scriptId}] [versionId=${rejectedVersionId}]`- );- return { replaced: true, deleted: false };- }+ const baseNames = new Set(baseBindings.map(({ name }) => name));+ return {+ createdSecretKeys: plan.written.filter(({ name }) => !baseNames.has(name)).map(({ name }) => name),+ updatedSecretKeys: plan.written.filter(({ name }) => baseNames.has(name)).map(({ name }) => name),+ deletedSecretKeys: plan.dropped,+ message: `Worker has undeployed changes; created version ${created.id} with updated secrets. Deploy it for the secrets to take effect.`+ };};-// The secrets and settings endpoints refuse to write while the latest version is not the deployed one. This builds-// a new version from the latest one's code and bindings with the secrets changed, uploads it without deploying it,-// and checks it. Deploying it is left to the user, because it also ships the pending changes it was built on.-const $applySecretsWithVersionFallback = async (+// The secrets and settings endpoints refuse to write while the latest version is not the deployed one. This has+// Cloudflare build a new version from the latest one with the secrets changed, and leaves deploying it to the user,+// because it also ships the pending changes it was built on.+export const applySecretsWithVersionFallback = async (secretSync: TCloudflareWorkersSyncWithCredentials,secretMap: TSecretMap,keyStore: Pick<TKeyStoreFactory, "acquireLock">-): Promise<TSyncSecretsResult | undefined> => {+) => {const {id: syncId,connection: {@@ -389,21 +146,17 @@ const $applySecretsWithVersionFallback = async (} = secretSync;const ctx: TFallbackContext = {- syncId,scriptId,accountUrl: `${IntegrationUrls.CLOUDFLARE_WORKERS_API_URL}/client/v4/accounts/${accountId}`,- headers: {- Authorization: `Bearer ${apiToken}`,- Accept: "application/json"- }+ headers: { Authorization: `Bearer ${apiToken}`, Accept: "application/json" }};logger.info(`SecretSync Cloudflare Workers latest version not deployed, using fallback [syncId=${syncId}] [scriptId=${scriptId}]`);- // Each sync builds on the latest version, so two syncs to one Worker reading it at once would each upload a- // copy missing the other's changes. The lock is per Worker because syncs only lock themselves.+ // Each fallback plans its deletions from the latest version, so two syncs to one Worker patching at once would+ // each see the other's change as unexpected. The lock is per Worker because syncs only lock themselves.let lock: Awaited<ReturnType<typeof keyStore.acquireLock>>;try {lock = await keyStore.acquireLock(@@ -411,54 +164,14 @@ const $applySecretsWithVersionFallback = async (WORKER_LOCK_DURATION_MS);} catch {- throw $fail(- scriptId,- "another sync was adding secrets to a new version of the same Worker",- "Infisical retries the sync automatically. If it keeps failing, sync again later.",- { shouldRetry: true }- );+ throw new SecretSyncError({+ message: `Another sync was adding secrets to a new version of Cloudflare Worker '${scriptId}' at the same time. Infisical retries the sync automatically. If it keeps failing, sync again later.`,+ shouldRetry: true+ });}try {- const created = await $uploadCheckedVersion(- ctx,- "latest",- { secretSync, secretMap },- {- message: "Infisical secret sync"- }- );- if (!created) return undefined;-- if (created.differences.length) {- const {- newVersionId,- expected: { baseVersionId }- } = created;- const outcome = await $replaceRejectedVersion(ctx, newVersionId, baseVersionId);- const reason = `Cloudflare stored the new version differently from what Infisical uploaded (${created.differences.join("; ")})`;-- if (!outcome.replaced) {- throw $fail(- scriptId,- reason,- `Infisical couldn't put an unchanged copy of version ${baseVersionId} in its place, so don't deploy version ${outcome.unsafeVersionIds.join(" or ")}. The next sync builds on the latest version, so upload a new version of the Worker before syncing again, and contact support.`- );- }-- throw $fail(- scriptId,- reason,- outcome.deleted- ? `Infisical deleted that version and made an unchanged copy of version ${baseVersionId} the latest. Sync again, and contact support if this keeps happening.`- : `Infisical made an unchanged copy of version ${baseVersionId} the latest, but couldn't delete the rejected version ${newVersionId}. Don't deploy it. Sync again, and contact support if this keeps happening.`- );- }-- return {- ...created.result,- message: `Worker has undeployed changes; created version ${created.newVersionId} with updated secrets. Deploy it for the secrets to take effect.`- };+ return await $applySecrets(secretSync, secretMap, ctx);} finally {// A release that fails (the lock already expired) mustn't turn a finished sync into a failed one.await lock.release().catch((err: unknown) => {@@ -469,25 +182,3 @@ const $applySecretsWithVersionFallback = async (});}};--export const applySecretsWithVersionFallback = async (- secretSync: TCloudflareWorkersSyncWithCredentials,- secretMap: TSecretMap,- keyStore: Pick<TKeyStoreFactory, "acquireLock">-) => {- if (activeFallbacks >= MAX_CONCURRENT_FALLBACKS) {- throw $fail(- secretSync.destinationConfig.scriptId,- "Infisical was already building as many Worker versions as it builds at once",- "Infisical retries the sync automatically. If it keeps failing, sync again later.",- { shouldRetry: true }- );- }-- activeFallbacks += 1;- try {- return await $applySecretsWithVersionFallback(secretSync, secretMap, keyStore);- } finally {- activeFallbacks -= 1;- }-};diff --git a/docs/integrations/secret-syncs/cloudflare-workers.mdx b/docs/integrations/secret-syncs/cloudflare-workers.mdxindex dec9acd28d..dbfa54c15e 100644--- a/docs/integrations/secret-syncs/cloudflare-workers.mdx+++ b/docs/integrations/secret-syncs/cloudflare-workers.mdx@@ -136,14 +136,10 @@ description: "Learn how to configure a Cloudflare Workers Sync for Infisical."## Syncing while a version is pending-If someone uploads a version of the Worker without deploying it, for example with `wrangler versions upload`, that version is pending, and Cloudflare doesn't let Infisical change the Worker's secrets directly. Instead, Infisical uploads a new version with the pending version's code and settings and the updated secrets, and labels the new version with the `infisical-sync` tag in Cloudflare.+If someone uploads a version of the Worker without deploying it, for example with `wrangler versions upload`, that version is pending, and Cloudflare doesn't let Infisical change the Worker's secrets directly. Instead, Cloudflare creates a new version from the pending one with the updated secrets, and Infisical labels it with the `infisical-sync` tag.Infisical doesn't deploy the new version, because deploying it also deploys the pending changes. The sync's status shows the ID of the new version, and the secrets take effect when you [deploy that version](https://developers.cloudflare.com/workers/versions-and-deployments/) in Cloudflare.-### When Infisical can't upload a new version--If the pending version serves static assets, the sync fails without changing the Worker. To sync the secrets, deploy the pending version in Cloudflare, then sync again.-### Removing secrets while a version is pendingInfisical can only remove secrets while the Worker's latest version is the deployed one, so if a version is pending, removing secrets fails and nothing on the Worker changes. If you delete the sync with **Remove Synced Secrets** turned on, Infisical keeps the sync. To remove the secrets, deploy the pending version in Cloudflare, then remove the secrets again.