Skip to content

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

fix(secret-sync): address cloudflare review comments

DimitrisStaratzis837b5f36d7e8
diff --git a/backend/src/server/routes/v1/secret-sync-routers/secret-sync-endpoints.ts b/backend/src/server/routes/v1/secret-sync-routers/secret-sync-endpoints.ts
index 0da1899d4b..835bda2696 100644
--- a/backend/src/server/routes/v1/secret-sync-routers/secret-sync-endpoints.ts
+++ b/backend/src/server/routes/v1/secret-sync-routers/secret-sync-endpoints.ts
@@ -527,11 +527,12 @@ export const registerSyncSecretsEndpoints = <T extends TSecretSync, I extends TS
},
onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN, AuthMode.OAUTH]),
handler: async (req) => {
- const { destinationConfig, connectionId, excludeSyncId, projectId } = req.body;
+ const { destinationConfig, syncOptions, connectionId, excludeSyncId, projectId } = req.body;
const result = await server.services.secretSync.checkDuplicateDestination(
{
destinationConfig: destinationConfig as Record<string, unknown>,
+ syncOptions,
destination,
connectionId,
excludeSyncId,
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 b5e8c3fbc6..e0f6ff4782 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
@@ -106,12 +106,11 @@ describe("CloudflareWorkersSyncFns", () => {
payloadOf({ API_URL: "new" })
);
- expect(patch).toHaveBeenCalledTimes(2);
- expect(await patchedBindings(0)).toEqual([{ type: "kv_namespace", name: "CACHE" }]);
- expect(patch.mock.calls[1][0]).toMatch(/\/secrets-bulk$/);
- expect(patch.mock.calls[1][1]).toEqual({
- secrets: { API_URL: { name: "API_URL", text: "new", type: "secret_text" } }
- });
+ expect(patch).toHaveBeenCalledTimes(1);
+ expect(await patchedBindings()).toEqual([
+ { type: "secret_text", name: "API_URL", text: "new" },
+ { type: "kv_namespace", name: "CACHE" }
+ ]);
});
it("keeps skipping same-name text variables for syncs saved without a type", async () => {
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 9b9b6968bf..7d71bd6d5e 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
@@ -276,9 +276,10 @@ export const CloudflareWorkersSyncFns = {
const existingSecretsMap = Object.fromEntries(existingSecrets.map(({ key, type }) => [key, type]));
const secretMapKeys = new Set(Object.keys(secretMap));
- // Cloudflare rejects a secret with the same name as a plaintext or JSON variable. A sync with Secret
- // chosen explicitly removes those variables so it can write the secrets. Syncs saved before the type
- // could be chosen keep skipping those keys, unless shouldSyncNonSecretBindings updates them in place.
+ // Cloudflare rejects a secret with the same name as a plaintext or JSON variable, so those keys can't go
+ // through secrets-bulk. A sync with Secret chosen explicitly turns them into secrets in the settings
+ // update below, in one step so a failure can't leave them missing. Syncs saved before the type could be
+ // chosen skip them, unless shouldSyncNonSecretBindings updates them in place.
const bindingKeysToConvert = new Set(
variableType === CloudflareWorkersVariableType.Secret && !shouldSyncNonSecretBindings
? Object.keys(secretMap).filter((key) => nonSecretBindingKeys.has(key))
@@ -286,7 +287,7 @@ export const CloudflareWorkersSyncFns = {
);
const syncableEntries = Object.entries(secretMap).filter(
- ([key]) => shouldSyncNonSecretBindings || bindingKeysToConvert.has(key) || !nonSecretBindingKeys.has(key)
+ ([key]) => shouldSyncNonSecretBindings || !nonSecretBindingKeys.has(key)
);
const bindingEntries: Array<[string, { value: string }]> = [];
@@ -319,9 +320,10 @@ export const CloudflareWorkersSyncFns = {
$validateJsonBindings(settingsData.result.bindings, updatedBindingMap);
- const keptBindings = settingsData.result.bindings.filter((binding) => !bindingKeysToConvert.has(binding.name));
-
- const updatedBindings = keptBindings.map((binding) => {
+ const updatedBindings = settingsData.result.bindings.map((binding) => {
+ if (bindingKeysToConvert.has(binding.name)) {
+ return { type: CLOUDFLARE_SECRET_TYPE, name: binding.name, text: secretMap[binding.name].value };
+ }
if (binding.type !== CLOUDFLARE_SECRET_TYPE && updatedBindingMap[binding.name] !== undefined) {
const newValue = updatedBindingMap[binding.name].value;
if (binding.type === "json") {
diff --git a/backend/src/services/secret-sync/secret-sync-duplicate-check.test.ts b/backend/src/services/secret-sync/secret-sync-duplicate-check.test.ts
index a87650b117..3a0820c727 100644
--- a/backend/src/services/secret-sync/secret-sync-duplicate-check.test.ts
+++ b/backend/src/services/secret-sync/secret-sync-duplicate-check.test.ts
@@ -1,5 +1,7 @@
+import { CloudflareWorkersVariableType } from "./cloudflare-workers/cloudflare-workers-constants";
import { areSecretSyncDestinationConfigsEqual } from "./secret-sync-duplicate-check";
import { SecretSync } from "./secret-sync-enums";
+import { DESTINATION_DUPLICATE_CHECK_MAP } from "./secret-sync-maps";
describe("secret sync destination comparison", () => {
test.each([
@@ -63,3 +65,52 @@ describe("secret sync destination comparison", () => {
).toBe(false);
});
});
+
+describe("Cloudflare Workers duplicate check", () => {
+ const workersSync = (variableType?: CloudflareWorkersVariableType, syncNonSecretBindings?: boolean) => ({
+ connectionId: null,
+ destinationConfig: { scriptId: "worker", variableType },
+ syncOptions: { syncNonSecretBindings }
+ });
+
+ test.each([
+ [
+ "a Secret and a Text sync",
+ workersSync(CloudflareWorkersVariableType.Secret),
+ workersSync(CloudflareWorkersVariableType.Text),
+ false
+ ],
+ [
+ "two Text syncs",
+ workersSync(CloudflareWorkersVariableType.Text),
+ workersSync(CloudflareWorkersVariableType.Text),
+ true
+ ],
+ [
+ "a sync saved without a type and a Secret sync",
+ workersSync(),
+ workersSync(CloudflareWorkersVariableType.Secret),
+ true
+ ],
+ [
+ "a Secret sync that also syncs plaintext and JSON variables and a Text sync",
+ workersSync(CloudflareWorkersVariableType.Secret, true),
+ workersSync(CloudflareWorkersVariableType.Text),
+ true
+ ],
+ [
+ "a JSON sync and a sync saved without a type that also syncs plaintext and JSON variables",
+ workersSync(CloudflareWorkersVariableType.Json),
+ workersSync(undefined, true),
+ true
+ ]
+ ])("%s: duplicate is %s", async (_, existingSync, newSync, isDuplicate) => {
+ await expect(
+ DESTINATION_DUPLICATE_CHECK_MAP[SecretSync.CloudflareWorkers]({
+ existingSync,
+ newSync,
+ decryptConnection: vi.fn()
+ })
+ ).resolves.toBe(isDuplicate);
+ });
+});
diff --git a/backend/src/services/secret-sync/secret-sync-maps.ts b/backend/src/services/secret-sync/secret-sync-maps.ts
index c76c0b87dc..a50b51e83d 100644
--- a/backend/src/services/secret-sync/secret-sync-maps.ts
+++ b/backend/src/services/secret-sync/secret-sync-maps.ts
@@ -8,7 +8,7 @@ import { CloudflarePagesVariableType } from "@app/services/secret-sync/cloudflar
import { CloudflareWorkersVariableType } from "@app/services/secret-sync/cloudflare-workers/cloudflare-workers-constants";
import { GcpSyncScope } from "@app/services/secret-sync/gcp/gcp-sync-enums";
import { SecretSync, SecretSyncPlanType } from "@app/services/secret-sync/secret-sync-enums";
-import { DestinationDuplicateCheckFn } from "@app/services/secret-sync/secret-sync-types";
+import { DestinationDuplicateCheckFn, DuplicateCheckSyncFields } from "@app/services/secret-sync/secret-sync-types";
export const SECRET_SYNC_NAME_MAP: Record<SecretSync, string> = {
[SecretSync.AWSParameterStore]: "AWS Parameter Store",
@@ -326,6 +326,17 @@ const cloudflareVariableTypeDuplicateCheck =
(existingSync.destinationConfig.variableType ?? defaultVariableType) ===
(newSync.destinationConfig.variableType ?? defaultVariableType);
+// A Secret sync with syncNonSecretBindings also deletes plaintext and JSON variables that it doesn't sync,
+// so it can't share a Worker with any other sync.
+const ownsEveryWorkerVariable = ({ destinationConfig, syncOptions }: DuplicateCheckSyncFields) =>
+ (destinationConfig.variableType ?? CloudflareWorkersVariableType.Secret) === CloudflareWorkersVariableType.Secret &&
+ Boolean(syncOptions?.syncNonSecretBindings);
+
+const cloudflareWorkersDuplicateCheck: DestinationDuplicateCheckFn = async (params) =>
+ ownsEveryWorkerVariable(params.existingSync) ||
+ ownsEveryWorkerVariable(params.newSync) ||
+ cloudflareVariableTypeDuplicateCheck(CloudflareWorkersVariableType.Secret)(params);
+
const gcpDuplicateCheck: DestinationDuplicateCheckFn = async ({ existingSync, newSync }) => {
const existingConfig = existingSync.destinationConfig;
const newConfig = newSync.destinationConfig;
@@ -390,7 +401,7 @@ export const DESTINATION_DUPLICATE_CHECK_MAP: Record<SecretSync, DestinationDupl
return existingTargetEnv === newTargetEnv;
},
[SecretSync.CloudflarePages]: cloudflareVariableTypeDuplicateCheck(CloudflarePagesVariableType.Secret),
- [SecretSync.CloudflareWorkers]: cloudflareVariableTypeDuplicateCheck(CloudflareWorkersVariableType.Secret),
+ [SecretSync.CloudflareWorkers]: cloudflareWorkersDuplicateCheck,
[SecretSync.CloudflareSecretsStore]: defaultDuplicateCheck,
[SecretSync.Supabase]: defaultDuplicateCheck,
[SecretSync.Rundeck]: defaultDuplicateCheck,
diff --git a/backend/src/services/secret-sync/secret-sync-service.ts b/backend/src/services/secret-sync/secret-sync-service.ts
index aa825f2f21..9764e0ea3e 100644
--- a/backend/src/services/secret-sync/secret-sync-service.ts
+++ b/backend/src/services/secret-sync/secret-sync-service.ts
@@ -499,7 +499,14 @@ export const secretSyncServiceFactory = ({
};
const checkDuplicateDestination = async (
- { destination, destinationConfig, connectionId, excludeSyncId, projectId }: TCheckDuplicateDestinationDTO,
+ {
+ destination,
+ destinationConfig,
+ syncOptions,
+ connectionId,
+ excludeSyncId,
+ projectId
+ }: TCheckDuplicateDestinationDTO,
actor: OrgServiceActor
) => {
const skipFields = SECRET_SYNC_SKIP_FIELDS_MAP[destination];
@@ -561,11 +568,13 @@ export const secretSyncServiceFactory = ({
const isDuplicate = await DESTINATION_DUPLICATE_CHECK_MAP[destination]({
existingSync: {
connectionId: sync.connectionId,
- destinationConfig: sync.destinationConfig as Record<string, unknown>
+ destinationConfig: sync.destinationConfig as Record<string, unknown>,
+ syncOptions: sync.syncOptions as Record<string, unknown>
},
newSync: {
connectionId: connectionId ?? null,
- destinationConfig
+ destinationConfig,
+ syncOptions
},
decryptConnection
});
@@ -698,6 +707,7 @@ export const secretSyncServiceFactory = ({
{
destination: params.destination,
destinationConfig: params.destinationConfig,
+ syncOptions: params.syncOptions,
connectionId: params.connectionId,
projectId
},
@@ -835,6 +845,7 @@ export const secretSyncServiceFactory = ({
{
destination,
destinationConfig: params.destinationConfig ?? (secretSync.destinationConfig as Record<string, unknown>),
+ syncOptions: params.syncOptions ?? (secretSync.syncOptions as Record<string, unknown>),
connectionId: params.connectionId ?? connectionId,
projectId: secretSync.projectId,
excludeSyncId: secretSync.id
diff --git a/backend/src/services/secret-sync/secret-sync-types.ts b/backend/src/services/secret-sync/secret-sync-types.ts
index e119a11029..4197b84914 100644
--- a/backend/src/services/secret-sync/secret-sync-types.ts
+++ b/backend/src/services/secret-sync/secret-sync-types.ts
@@ -524,6 +524,7 @@ export type TDeleteSecretSyncDTO = {
export type TCheckDuplicateDestinationDTO = {
destination: SecretSync;
destinationConfig: Record<string, unknown>;
+ syncOptions?: Record<string, unknown>;
connectionId?: string | null;
excludeSyncId?: string;
projectId: string;
@@ -631,6 +632,7 @@ export type TSecretMap = Record<
export type DuplicateCheckSyncFields = {
connectionId: string | null;
destinationConfig: Record<string, unknown>;
+ syncOptions?: Record<string, unknown>;
};
export type DestinationDuplicateCheckFn = (opts: {
diff --git a/frontend/src/components/secret-syncs/forms/CreateSecretSyncForm.tsx b/frontend/src/components/secret-syncs/forms/CreateSecretSyncForm.tsx
index 21ea014735..27cd67a0a9 100644
--- a/frontend/src/components/secret-syncs/forms/CreateSecretSyncForm.tsx
+++ b/frontend/src/components/secret-syncs/forms/CreateSecretSyncForm.tsx
@@ -235,7 +235,8 @@ export const CreateSecretSyncForm = ({
projectId: currentProject?.id || "",
connectionId: watch("connection")?.id,
enabled: true,
- destinationConfig: getSecretSyncDestinationConfig(destination, watch("destinationConfig"))
+ destinationConfig: getSecretSyncDestinationConfig(destination, watch("destinationConfig")),
+ syncOptions: watch("syncOptions")
});
const disableSecretDeletion = watch("syncOptions.disableSecretDeletion");
diff --git a/frontend/src/components/secret-syncs/forms/EditSecretSyncForm.tsx b/frontend/src/components/secret-syncs/forms/EditSecretSyncForm.tsx
index 10990abde7..33caad0b0a 100644
--- a/frontend/src/components/secret-syncs/forms/EditSecretSyncForm.tsx
+++ b/frontend/src/components/secret-syncs/forms/EditSecretSyncForm.tsx
@@ -157,6 +157,7 @@ export const EditSecretSyncForm = ({ secretSync, onComplete, onDirtyChange, onCa
secretSync.projectId,
secretSync.id,
formMethods.watch("connection")?.id,
+ pendingFormData?.syncOptions,
{ enabled: checkDuplicateEnabled && Boolean(destinationConfigToCheck) }
);
diff --git a/frontend/src/components/secret-syncs/forms/SecretSyncOptionsFields/CloudflareWorkersSyncOptionsFields.tsx b/frontend/src/components/secret-syncs/forms/SecretSyncOptionsFields/CloudflareWorkersSyncOptionsFields.tsx
index 7acb501bd1..a9fd2a62ad 100644
--- a/frontend/src/components/secret-syncs/forms/SecretSyncOptionsFields/CloudflareWorkersSyncOptionsFields.tsx
+++ b/frontend/src/components/secret-syncs/forms/SecretSyncOptionsFields/CloudflareWorkersSyncOptionsFields.tsx
@@ -41,12 +41,15 @@ export const CloudflareWorkersSyncOptionsFields = () => {
<Controller
name="destinationConfig.variableType"
control={control}
- defaultValue={CloudflareWorkersVariableType.Secret}
render={({ field: { value, onChange }, fieldState: { error } }) => (
<Field className="mb-4">
<FieldLabel htmlFor="cloudflare-workers-variable-type">Variable type</FieldLabel>
<FieldContent>
- <Select value={value} onValueChange={onChange}>
+ {/* Only a type the user picks is saved, so editing an older sync keeps its behavior */}
+ <Select
+ value={value ?? CloudflareWorkersVariableType.Secret}
+ onValueChange={onChange}
+ >
<SelectTrigger
id="cloudflare-workers-variable-type"
className="w-full"
diff --git a/frontend/src/components/secret-syncs/forms/SecretSyncReviewFields/CloudflareWorkersReviewFields.tsx b/frontend/src/components/secret-syncs/forms/SecretSyncReviewFields/CloudflareWorkersReviewFields.tsx
index 59fcaa10f4..55ba457200 100644
--- a/frontend/src/components/secret-syncs/forms/SecretSyncReviewFields/CloudflareWorkersReviewFields.tsx
+++ b/frontend/src/components/secret-syncs/forms/SecretSyncReviewFields/CloudflareWorkersReviewFields.tsx
@@ -11,7 +11,7 @@ export const CloudflareWorkersSyncOptionsReviewFields = () => {
TSecretSyncForm & { destination: SecretSync.CloudflareWorkers }
>();
- const [{ syncNonSecretBindings }, variableType] = watch([
+ const [{ syncNonSecretBindings }, variableType = CloudflareWorkersVariableType.Secret] = watch([
"syncOptions",
"destinationConfig.variableType"
]);
diff --git a/frontend/src/components/secret-syncs/forms/SecretSyncReviewFields/SecretSyncReviewFields.tsx b/frontend/src/components/secret-syncs/forms/SecretSyncReviewFields/SecretSyncReviewFields.tsx
index 4028a15142..72dee21b49 100644
--- a/frontend/src/components/secret-syncs/forms/SecretSyncReviewFields/SecretSyncReviewFields.tsx
+++ b/frontend/src/components/secret-syncs/forms/SecretSyncReviewFields/SecretSyncReviewFields.tsx
@@ -117,7 +117,8 @@ export const SecretSyncReviewFields = () => {
projectId: currentProject?.id || "",
connectionId: connection?.id,
enabled: true,
- destinationConfig: getSecretSyncDestinationConfig(destination, watch("destinationConfig"))
+ destinationConfig: getSecretSyncDestinationConfig(destination, watch("destinationConfig")),
+ syncOptions: watch("syncOptions")
});
switch (destination) {
diff --git a/frontend/src/components/secret-syncs/forms/schemas/cloudflare-workers-sync-destination-schema.ts b/frontend/src/components/secret-syncs/forms/schemas/cloudflare-workers-sync-destination-schema.ts
index 49fb942de0..ad03fb7a5b 100644
--- a/frontend/src/components/secret-syncs/forms/schemas/cloudflare-workers-sync-destination-schema.ts
+++ b/frontend/src/components/secret-syncs/forms/schemas/cloudflare-workers-sync-destination-schema.ts
@@ -18,9 +18,7 @@ export const CloudflareWorkersSyncDestinationSchema = BaseSecretSyncSchema(
.min(1, "Script ID is required")
.max(64)
.regex(/^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/, "Invalid script ID format"),
- variableType: z
- .nativeEnum(CloudflareWorkersVariableType)
- .default(CloudflareWorkersVariableType.Secret)
+ variableType: z.nativeEnum(CloudflareWorkersVariableType).optional()
})
})
);
diff --git a/frontend/src/hooks/api/secretSyncs/queries.tsx b/frontend/src/hooks/api/secretSyncs/queries.tsx
index 3baa580010..ce40ede944 100644
--- a/frontend/src/hooks/api/secretSyncs/queries.tsx
+++ b/frontend/src/hooks/api/secretSyncs/queries.tsx
@@ -20,7 +20,8 @@ export const secretSyncKeys = {
destination: SecretSync,
destinationConfig: unknown,
connectionId?: string,
- excludeSyncId?: string
+ excludeSyncId?: string,
+ syncOptions?: unknown
) =>
[
...secretSyncKeys.all,
@@ -28,7 +29,8 @@ export const secretSyncKeys = {
"duplicate-check",
destinationConfig,
connectionId,
- excludeSyncId
+ excludeSyncId,
+ syncOptions
] as const,
recursiveConflicts: (
destination: SecretSync,
@@ -149,6 +151,7 @@ export const useCheckDuplicateDestination = (
projectId: string,
excludeSyncId?: string,
connectionId?: string,
+ syncOptions?: unknown,
options?: Omit<
UseQueryOptions<
{ hasDuplicate: boolean; duplicateProjectId?: string },
@@ -164,7 +167,8 @@ export const useCheckDuplicateDestination = (
destination,
destinationConfig,
connectionId,
- excludeSyncId
+ excludeSyncId,
+ syncOptions
),
queryFn: async () => {
const { data } = await apiRequest.post<{
@@ -172,6 +176,7 @@ export const useCheckDuplicateDestination = (
duplicateProjectId?: string;
}>(`/api/v1/secret-syncs/${destination}/check-destination`, {
destinationConfig,
+ syncOptions,
connectionId,
excludeSyncId,
projectId
diff --git a/frontend/src/hooks/api/secretSyncs/useDuplicateDestinationCheck.ts b/frontend/src/hooks/api/secretSyncs/useDuplicateDestinationCheck.ts
index b0c3a4646e..0821a61aa1 100644
--- a/frontend/src/hooks/api/secretSyncs/useDuplicateDestinationCheck.ts
+++ b/frontend/src/hooks/api/secretSyncs/useDuplicateDestinationCheck.ts
@@ -10,6 +10,7 @@ type UseDuplicateDestinationCheckProps = {
connectionId?: string;
enabled?: boolean;
destinationConfig?: unknown;
+ syncOptions?: unknown;
};
export const useDuplicateDestinationCheck = ({
@@ -18,7 +19,8 @@ export const useDuplicateDestinationCheck = ({
excludeSyncId,
connectionId,
enabled = true,
- destinationConfig
+ destinationConfig,
+ syncOptions
}: UseDuplicateDestinationCheckProps) => {
const normalizedConfig = useMemo(
() => getSecretSyncDestinationConfig(destination, destinationConfig),
@@ -40,6 +42,7 @@ export const useDuplicateDestinationCheck = ({
projectId,
excludeSyncId,
connectionId,
+ syncOptions,
{
enabled: shouldCheck,
staleTime: 0,