Skip to content

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

fix(secret-sync): keep existing cloudflare syncs skipping other-type keys

DimitrisStaratzisbb81df3c10ed
diff --git a/backend/src/lib/api-docs/constants.ts b/backend/src/lib/api-docs/constants.ts
index 0490462591..4500090e22 100644
--- a/backend/src/lib/api-docs/constants.ts
+++ b/backend/src/lib/api-docs/constants.ts
@@ -3520,12 +3520,12 @@ export const SecretSyncs = {
projectName: "The name of the Cloudflare Pages project to sync secrets to.",
environment: "The environment of the Cloudflare Pages project to sync secrets to.",
variableType:
- "The type of environment variable to sync secrets as: secret_text (default) or plain_text. If a secret already exists in the environment as the other type, the sync fails and nothing is changed. Infisical only deletes variables of this type."
+ "The type of environment variable to sync secrets as: secret_text (default) or plain_text. If a secret already exists in the environment as the other type, the sync fails and nothing is changed. Syncs created before this option existed have no type and skip those secrets instead. Infisical only deletes variables of this type."
},
CLOUDFLARE_WORKERS: {
scriptId: "The ID of the Cloudflare Workers script to sync secrets to.",
variableType:
- "The type of variable to sync secrets as: secret_text (default), plain_text or json. If a secret already exists on the Worker as a different type, the sync fails and nothing is changed, except that with syncNonSecretBindings on, a secret_text sync updates existing plaintext and JSON variables as they are. Infisical only deletes variables of this type."
+ "The type of variable to sync secrets as: secret_text (default), plain_text or json. If a secret already exists on the Worker as a different type, the sync fails and nothing is changed, except that with syncNonSecretBindings on, a secret_text sync updates existing plaintext and JSON variables as they are. Syncs created before this option existed have no type and skip those secrets instead. Infisical only deletes variables of this type."
},
CLOUDFLARE_SECRETS_STORE: {
storeId: "The ID of the Cloudflare Secrets Store to sync secrets to.",
diff --git a/backend/src/services/secret-sync/cloudflare-pages/cloudflare-pages-fns.test.ts b/backend/src/services/secret-sync/cloudflare-pages/cloudflare-pages-fns.test.ts
index a8b2977990..16e753b763 100644
--- a/backend/src/services/secret-sync/cloudflare-pages/cloudflare-pages-fns.test.ts
+++ b/backend/src/services/secret-sync/cloudflare-pages/cloudflare-pages-fns.test.ts
@@ -85,4 +85,39 @@ describe("CloudflarePagesSyncFns", () => {
expect(patch).not.toHaveBeenCalled();
});
+
+ it("skips keys of the other type on a sync with no saved type", async () => {
+ get.mockResolvedValue({
+ data: {
+ result: {
+ deployment_configs: {
+ production: {
+ env_vars: {
+ API_URL: { type: "plain_text", value: "old" },
+ OTHER_TEXT: { type: "plain_text", value: "old" },
+ STALE_SECRET: { type: "secret_text", value: "" }
+ }
+ }
+ }
+ }
+ }
+ });
+
+ await CloudflarePagesSyncFns.syncSecrets(syncWith(), payloadOf({ API_URL: "new", API_KEY: "key" }));
+
+ expect(patch).toHaveBeenCalledWith(
+ expect.stringMatching(/\/pages\/projects\/my-site$/),
+ {
+ deployment_configs: {
+ production: {
+ env_vars: {
+ API_KEY: { type: "secret_text", value: "key" },
+ STALE_SECRET: null
+ }
+ }
+ }
+ },
+ expect.anything()
+ );
+ });
});
diff --git a/backend/src/services/secret-sync/cloudflare-pages/cloudflare-pages-fns.ts b/backend/src/services/secret-sync/cloudflare-pages/cloudflare-pages-fns.ts
index 1c4e42efd5..d7e90bb330 100644
--- a/backend/src/services/secret-sync/cloudflare-pages/cloudflare-pages-fns.ts
+++ b/backend/src/services/secret-sync/cloudflare-pages/cloudflare-pages-fns.ts
@@ -55,15 +55,16 @@ export const CloudflarePagesSyncFns = {
credentials: { apiToken, accountId }
}
} = secretSync;
- const { variableType = CloudflarePagesVariableType.Secret } = destinationConfig;
+ const variableType = destinationConfig.variableType ?? CloudflarePagesVariableType.Secret;
const existingSecrets = await getProjectEnvironmentSecrets(secretSync);
// Changing a variable's type would expose a secret's value or hide a readable one, so a sync never changes
- // one; it fails before writing anything.
+ // one; it fails before writing anything. A sync created before the type could be chosen (no variableType)
+ // skips those keys instead, so it keeps running as it did.
const secretMapKeys = new Set(Object.keys(secretMap));
const conflicts = existingSecrets.filter((secret) => secretMapKeys.has(secret.key) && secret.type !== variableType);
- if (conflicts.length > 0) {
+ if (conflicts.length > 0 && destinationConfig.variableType) {
throw new SecretSyncError({
message: `The following secrets already exist in the Cloudflare Pages environment as a different type: ${conflicts
.map(({ key, type }) => `${key} (${VARIABLE_TYPE_LABELS[type]})`)
@@ -74,11 +75,12 @@ export const CloudflarePagesSyncFns = {
});
}
+ const skippedKeys = new Set(conflicts.map((secret) => secret.key));
+
// Create/update secret entries
- let secretEntries: [string, object | null][] = Object.entries(secretMap).map(([key, val]) => [
- key,
- { type: variableType, value: val.value }
- ]);
+ let secretEntries: [string, object | null][] = Object.entries(secretMap)
+ .filter(([key]) => !skippedKeys.has(key))
+ .map(([key, val]) => [key, { type: variableType, value: val.value }]);
// Handle deletions if not disabled. A sync only owns variables of its type, so syncs of different types can
// share an environment.
diff --git a/backend/src/services/secret-sync/cloudflare-pages/cloudflare-pages-schema.ts b/backend/src/services/secret-sync/cloudflare-pages/cloudflare-pages-schema.ts
index 35a55b1b78..fd53ed895a 100644
--- a/backend/src/services/secret-sync/cloudflare-pages/cloudflare-pages-schema.ts
+++ b/backend/src/services/secret-sync/cloudflare-pages/cloudflare-pages-schema.ts
@@ -44,7 +44,14 @@ export const CreateCloudflarePagesSyncSchema = GenericCreateSecretSyncFieldsSche
SecretSync.CloudflarePages,
CloudflarePagesSyncOptionsConfig
).extend({
- destinationConfig: CloudflarePagesSyncDestinationConfigSchema
+ // New syncs always get a type. It stays optional elsewhere because syncs created before it existed have
+ // none, and those skip same-name text variables.
+ destinationConfig: CloudflarePagesSyncDestinationConfigSchema.extend({
+ variableType: z
+ .nativeEnum(CloudflarePagesVariableType)
+ .default(CloudflarePagesVariableType.Secret)
+ .describe(SecretSyncs.DESTINATION_CONFIG.CLOUDFLARE_PAGES.variableType)
+ })
});
export const UpdateCloudflarePagesSyncSchema = GenericUpdateSecretSyncFieldsSchema(
diff --git a/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-fns.test.ts b/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-fns.test.ts
index 866f016881..0d18124994 100644
--- a/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-fns.test.ts
+++ b/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-fns.test.ts
@@ -97,7 +97,7 @@ describe("CloudflareWorkersSyncFns", () => {
it.each([
[CloudflareWorkersVariableType.Secret, { type: "json", name: "CONFIG", json: { a: 1 } }, "CONFIG (JSON)"],
- [undefined, { type: "plain_text", name: "CONFIG", text: "old" }, "CONFIG (text)"],
+ [CloudflareWorkersVariableType.Secret, { type: "plain_text", name: "CONFIG", text: "old" }, "CONFIG (text)"],
[CloudflareWorkersVariableType.Text, { type: "secret_text", name: "CONFIG" }, "CONFIG (secret)"],
[CloudflareWorkersVariableType.Json, { type: "plain_text", name: "CONFIG", text: "{}" }, "CONFIG (text)"]
])("fails a %s sync before writing when a key exists as another type", async (variableType, binding, listed) => {
@@ -110,6 +110,21 @@ describe("CloudflareWorkersSyncFns", () => {
expect(patch).not.toHaveBeenCalled();
});
+ it("skips same-name text and JSON variables on a sync with no saved type", async () => {
+ stubWorker([
+ { type: "plain_text", name: "API_URL", text: "old" },
+ { type: "json", name: "CONFIG", json: { a: 1 } }
+ ]);
+
+ await CloudflareWorkersSyncFns.syncSecrets(syncWith(), payloadOf({ API_URL: "new", CONFIG: "{}", NEW_KEY: "n" }));
+
+ expect(patch).toHaveBeenCalledTimes(1);
+ expect(patch.mock.calls[0][0]).toMatch(/\/secrets-bulk$/);
+ expect(patch.mock.calls[0][1]).toEqual({
+ secrets: { NEW_KEY: { name: "NEW_KEY", text: "n", type: "secret_text" } }
+ });
+ });
+
it("updates same-name text variables as they are when the toggle is on", async () => {
stubWorker([{ type: "plain_text", name: "API_URL", text: "old" }]);
diff --git a/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-fns.ts b/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-fns.ts
index 1187e3904d..132ed9c115 100644
--- a/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-fns.ts
+++ b/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-fns.ts
@@ -290,15 +290,19 @@ export const CloudflareWorkersSyncFns = {
return;
}
+ const shouldSyncNonSecretBindings = Boolean(secretSync.syncOptions.syncNonSecretBindings);
const cloudflareBindings = await getCloudflareBindings(secretSync);
const existingSecrets = $getManagedBindings(secretSync, cloudflareBindings);
+ const nonSecretBindingKeys = new Set(cloudflareBindings.nonSecretBindings.map((b) => b.key));
const existingSecretsMap = Object.fromEntries(existingSecrets.map(({ key, type }) => [key, type]));
const secretMapKeys = new Set(Object.keys(secretMap));
// With shouldSyncNonSecretBindings on, same-name plaintext and JSON variables are updated as they are.
- if (!secretSync.syncOptions.syncNonSecretBindings) {
+ // Otherwise the sync fails rather than change their type, except a sync created before the type could be
+ // chosen (no variableType), which keeps skipping them as it always has.
+ if (!shouldSyncNonSecretBindings && variableType) {
$throwOnTypeConflicts(
cloudflareBindings.nonSecretBindings
.filter((binding) => secretMapKeys.has(binding.key))
@@ -307,10 +311,14 @@ export const CloudflareWorkersSyncFns = {
);
}
+ const syncableEntries = Object.entries(secretMap).filter(
+ ([key]) => shouldSyncNonSecretBindings || !nonSecretBindingKeys.has(key)
+ );
+
const bindingEntries: Array<[string, { value: string }]> = [];
const secretEntries: Array<[string, { value: string }]> = [];
- for (const [key, val] of Object.entries(secretMap)) {
+ for (const [key, val] of syncableEntries) {
const existingType = existingSecretsMap[key];
if (existingType && existingType !== CLOUDFLARE_SECRET_TYPE) {
bindingEntries.push([key, val]);
diff --git a/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-schemas.ts b/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-schemas.ts
index da9e2a94a9..e462d1aa82 100644
--- a/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-schemas.ts
+++ b/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-schemas.ts
@@ -55,7 +55,14 @@ export const CreateCloudflareWorkersSyncSchema = GenericCreateSecretSyncFieldsSc
CloudflareWorkersSyncOptionsConfig,
CloudflareWorkersSyncAdditionalOptionsSchema
).extend({
- destinationConfig: CloudflareWorkersSyncDestinationConfigSchema
+ // New syncs always get a type. It stays optional elsewhere because syncs created before it existed have
+ // none, and those keep skipping same-name plaintext and JSON variables.
+ destinationConfig: CloudflareWorkersSyncDestinationConfigSchema.extend({
+ variableType: z
+ .nativeEnum(CloudflareWorkersVariableType)
+ .default(CloudflareWorkersVariableType.Secret)
+ .describe(SecretSyncs.DESTINATION_CONFIG.CLOUDFLARE_WORKERS.variableType)
+ })
});
export const UpdateCloudflareWorkersSyncSchema = GenericUpdateSecretSyncFieldsSchema(
diff --git a/docs/integrations/secret-syncs/cloudflare-pages.mdx b/docs/integrations/secret-syncs/cloudflare-pages.mdx
index 4bc993b7be..51b2e2cfa2 100644
--- a/docs/integrations/secret-syncs/cloudflare-pages.mdx
+++ b/docs/integrations/secret-syncs/cloudflare-pages.mdx
@@ -46,7 +46,7 @@ description: "Learn how to configure a Cloudflare Pages Sync for Infisical."
- **Secret**: Encrypted, so you can't read the value in the Cloudflare dashboard
- **Text**: Plain text that you can read in the Cloudflare dashboard
- If a secret already exists in the environment as the other type, the sync fails with an error that names it, and nothing in Cloudflare changes. To sync it, delete the variable in Cloudflare or change the sync's variable type.
+ If a secret already exists in the environment as the other type, the sync fails with an error that names it, and nothing in Cloudflare changes. To sync it, delete the variable in Cloudflare or change the sync's variable type. Syncs created before the variable type option existed skip those secrets rather than failing.
<Tip>
To sync both secrets and text variables to one environment, create one sync of each type from different folders. Each sync only deletes variables of its own type. When you create the second sync, Infisical warns that the destination is already in use. If your organization blocks duplicate destinations, you can't create the second sync.
diff --git a/docs/integrations/secret-syncs/cloudflare-workers.mdx b/docs/integrations/secret-syncs/cloudflare-workers.mdx
index 6ec135b720..1886c35066 100644
--- a/docs/integrations/secret-syncs/cloudflare-workers.mdx
+++ b/docs/integrations/secret-syncs/cloudflare-workers.mdx
@@ -46,7 +46,7 @@ description: "Learn how to configure a Cloudflare Workers Sync for Infisical."
- **Text**: Plain text that you can read in the Cloudflare dashboard
- **JSON**: A JSON value that your Worker receives already parsed (each secret value must be valid JSON)
- If a secret already exists on the Worker as a different type, the sync fails with an error that names it, and nothing on the Worker changes. To sync it, delete the variable in Cloudflare or change the sync's variable type. With **Sync plaintext and JSON variables** turned on, a Secret sync updates existing text and JSON variables as they are instead.
+ If a secret already exists on the Worker as a different type, the sync fails with an error that names it, and nothing on the Worker changes. To sync it, delete the variable in Cloudflare or change the sync's variable type. With **Sync plaintext and JSON variables** turned on, a Secret sync updates existing text and JSON variables as they are instead. Syncs created before the variable type option existed skip those secrets rather than failing.
6. Configure the **Details** of your Cloudflare Workers Sync, then click **Next**.
![Configure Details](/images/secret-syncs/cloudflare-workers/cloudflare-workers-sync-details.png)
diff --git a/frontend/src/components/secret-syncs/forms/SecretSyncOptionsFields/CloudflarePagesSyncOptionsFields.tsx b/frontend/src/components/secret-syncs/forms/SecretSyncOptionsFields/CloudflarePagesSyncOptionsFields.tsx
index 293e2334e2..c9f329ceaf 100644
--- a/frontend/src/components/secret-syncs/forms/SecretSyncOptionsFields/CloudflarePagesSyncOptionsFields.tsx
+++ b/frontend/src/components/secret-syncs/forms/SecretSyncOptionsFields/CloudflarePagesSyncOptionsFields.tsx
@@ -27,12 +27,13 @@ export const CloudflarePagesSyncOptionsFields = () => {
<Controller
name="destinationConfig.variableType"
control={control}
- defaultValue={CloudflarePagesVariableType.Secret}
render={({ field: { value, onChange }, fieldState: { error } }) => (
<Field className="mb-4">
<FieldLabel htmlFor="cloudflare-pages-variable-type">Variable type</FieldLabel>
<FieldContent>
- <Select value={value} onValueChange={onChange}>
+ {/* A sync without a type predates the option and skips same-name text variables, so a type is
+ only saved once someone picks one. The backend gives new syncs Secret. */}
+ <Select value={value ?? CloudflarePagesVariableType.Secret} onValueChange={onChange}>
<SelectTrigger
id="cloudflare-pages-variable-type"
className="w-full"
diff --git a/frontend/src/components/secret-syncs/forms/SecretSyncOptionsFields/CloudflareWorkersSyncOptionsFields.tsx b/frontend/src/components/secret-syncs/forms/SecretSyncOptionsFields/CloudflareWorkersSyncOptionsFields.tsx
index f89e408a42..aeb78b66f8 100644
--- a/frontend/src/components/secret-syncs/forms/SecretSyncOptionsFields/CloudflareWorkersSyncOptionsFields.tsx
+++ b/frontend/src/components/secret-syncs/forms/SecretSyncOptionsFields/CloudflareWorkersSyncOptionsFields.tsx
@@ -45,8 +45,9 @@ export const CloudflareWorkersSyncOptionsFields = () => {
<Field className="mb-4">
<FieldLabel htmlFor="cloudflare-workers-variable-type">Variable type</FieldLabel>
<FieldContent>
- {/* Unset means Secret. It isn't filled in, so editing an older sync doesn't look like a
- destination change. */}
+ {/* A sync without a type predates the option and skips same-name plaintext and JSON
+ variables, so a type is only saved once someone picks one. The backend gives new syncs
+ Secret. */}
<Select
value={value ?? CloudflareWorkersVariableType.Secret}
onValueChange={onChange}
diff --git a/frontend/src/components/secret-syncs/forms/SecretSyncReviewFields/CloudflarePagesReviewFields.tsx b/frontend/src/components/secret-syncs/forms/SecretSyncReviewFields/CloudflarePagesReviewFields.tsx
index ecdb488e1a..58cf326b00 100644
--- a/frontend/src/components/secret-syncs/forms/SecretSyncReviewFields/CloudflarePagesReviewFields.tsx
+++ b/frontend/src/components/secret-syncs/forms/SecretSyncReviewFields/CloudflarePagesReviewFields.tsx
@@ -4,10 +4,12 @@ import { TSecretSyncForm } from "@app/components/secret-syncs/forms/schemas";
import { Detail, DetailLabel, DetailValue } from "@app/components/v3";
import { CLOUDFLARE_PAGES_VARIABLE_TYPE_LABELS } from "@app/helpers/secretSyncs";
import { SecretSync } from "@app/hooks/api/secretSyncs";
+import { CloudflarePagesVariableType } from "@app/hooks/api/secretSyncs/types/cloudflare-pages-sync";
export const CloudflarePagesSyncOptionsReviewFields = () => {
const { watch } = useFormContext<TSecretSyncForm & { destination: SecretSync.CloudflarePages }>();
- const variableType = watch("destinationConfig.variableType");
+ const variableType =
+ watch("destinationConfig.variableType") ?? CloudflarePagesVariableType.Secret;
return (
<Detail>
diff --git a/frontend/src/components/secret-syncs/forms/schemas/cloudflare-pages-sync-destination-schema.ts b/frontend/src/components/secret-syncs/forms/schemas/cloudflare-pages-sync-destination-schema.ts
index 2d4020124f..72184ad2eb 100644
--- a/frontend/src/components/secret-syncs/forms/schemas/cloudflare-pages-sync-destination-schema.ts
+++ b/frontend/src/components/secret-syncs/forms/schemas/cloudflare-pages-sync-destination-schema.ts
@@ -10,9 +10,7 @@ export const CloudflarePagesSyncDestinationSchema = BaseSecretSyncSchema().merge
destinationConfig: z.object({
projectName: z.string().trim().min(1, "Project name is required"),
environment: z.string().trim().min(1, "Environment is required"),
- variableType: z
- .nativeEnum(CloudflarePagesVariableType)
- .default(CloudflarePagesVariableType.Secret)
+ variableType: z.nativeEnum(CloudflarePagesVariableType).optional()
})
})
);