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-sync): address review feedback on recursive syncs
Calvinfbf383e62993
diff --git a/backend/e2e-test/routes/v1/secret-sync-recursive-duplicates.spec.ts b/backend/e2e-test/routes/v1/secret-sync-recursive-duplicates.spec.tsindex 221c8714ec..3c7d9d1022 100644--- a/backend/e2e-test/routes/v1/secret-sync-recursive-duplicates.spec.ts+++ b/backend/e2e-test/routes/v1/secret-sync-recursive-duplicates.spec.ts@@ -51,6 +51,55 @@ describe("A recursive secret sync is refused when two folders use the same secrereturn result;};+ // Creating a recursive sync over an existing collision is refused, so the only way a sync can be+ // holding one is for the second name to have appeared after it was saved. Every update test below+ // needs a sync in that state, so they all build it the same way.+ const recursiveSyncWithLateCollision = async (dto: { name: string; folder: string }) => {+ await createFolder({+ authToken: jwtAuthToken,+ workspaceId: projectId,+ environmentSlug: ENV,+ secretPath: "/",+ name: dto.folder+ });+ await createFolder({+ authToken: jwtAuthToken,+ workspaceId: projectId,+ environmentSlug: ENV,+ secretPath: `/${dto.folder}`,+ name: "child"+ });++ const { secretSync } = await createSecretSync({+ name: dto.name,+ projectId,+ connectionId,+ environmentSlug: ENV,+ secretPath: `/${dto.folder}`,+ region: REGION,+ destinationPath: `/${dto.name}/`,+ initialSyncBehavior: SecretSyncInitialSyncBehavior.OverwriteDestination,+ recursive: true,+ isAutoSyncEnabled: false,+ authToken: jwtAuthToken+ });+ createdSyncIds.push(secretSync!.id);++ for (const secretPath of [`/${dto.folder}`, `/${dto.folder}/child`]) {+ // eslint-disable-next-line no-await-in-loop+ await createSecretV2({+ authToken: jwtAuthToken,+ workspaceId: projectId,+ environmentSlug: ENV,+ secretPath,+ key: "LATE_KEY",+ value: `value-for-${secretPath}`+ });+ }++ return secretSync!;+ };+beforeAll(async () => {initLogger();await initEnvConfig(testHsmService, testKmsRootConfigDAL, testSuperAdminDAL, logger);@@ -138,6 +187,53 @@ describe("A recursive secret sync is refused when two folders use the same secreexpect(secretSync).toEqual(expect.objectContaining({ name: "non-recursive-duplicate" }));});+ // Both tests below pin the same rule from opposite sides: the checks read the options the update+ // is about to store. Reading the request alone misses that an absent syncOptions leaves the stored+ // ones in place; reading the stored row alone misses that a supplied syncOptions replaces them.++ test("turning subfolders off is allowed even though the subtree collides", async () => {+ const secretSync = await recursiveSyncWithLateCollision({+ name: "recursive-turned-off-later",+ folder: "reports"+ });++ const res = await testServer.inject({+ method: "PATCH",+ url: `/api/v1/secret-syncs/aws-parameter-store/${secretSync.id}`,+ headers: adminHeaders,+ body: {+ syncOptions: {+ initialSyncBehavior: SecretSyncInitialSyncBehavior.OverwriteDestination+ }+ }+ });++ expect(res.statusCode).toBe(200);++ const [row] = await testDb(TableName.SecretSync).where({ id: secretSync.id });++ expect((row.syncOptions as { recursive?: boolean }).recursive).toBeFalsy();+ });++ test("an update that leaves the sync options alone is still checked as recursive", async () => {+ const secretSync = await recursiveSyncWithLateCollision({+ name: "recursive-left-alone",+ folder: "metrics"+ });++ const res = await testServer.inject({+ method: "PATCH",+ url: `/api/v1/secret-syncs/aws-parameter-store/${secretSync.id}`,+ headers: adminHeaders,+ body: { name: "recursive-left-alone-renamed" }+ });++ expect(res.statusCode).toBe(400);+ expect(res.json().message).toContain("LATE_KEY");+ expect(res.json().message).toContain("/metrics");+ expect(res.json().message).toContain("/metrics/child");+ });+test("turning subfolders on for an existing sync is refused", async () => {const { secretSync } = await newSync({ name: "recursive-turned-on-later", recursive: false });diff --git a/backend/src/services/secret-sync/external-infisical/external-infisical-sync-fns.ts b/backend/src/services/secret-sync/external-infisical/external-infisical-sync-fns.tsindex 7be3679b77..d72217d260 100644--- a/backend/src/services/secret-sync/external-infisical/external-infisical-sync-fns.ts+++ b/backend/src/services/secret-sync/external-infisical/external-infisical-sync-fns.ts@@ -146,9 +146,7 @@ const batchDeleteSecrets = async (export const ExternalInfisicalSyncFns = {syncSecrets: async (secretSync: TExternalInfisicalSyncWithCredentials, payload: TSecretSyncPayload) =>withExternalInfisicalErrorHandling(async () => {- // Key schema must never be applied for Infisical-to-Infisical syncs, or the prefixed key- // triggers another sync cycle on the remote project.- const secretMap = payload.flatten({ applySchema: false });+ const secretMap = payload.flatten();const ctx = await getRemoteContext(secretSync);const remoteSecrets = await fetchRemoteSecrets(secretSync, ctx);const environmentSlug = secretSync.environment?.slug || "";@@ -195,7 +193,7 @@ export const ExternalInfisicalSyncFns = {removeSecrets: async (secretSync: TExternalInfisicalSyncWithCredentials, payload: TSecretSyncPayload) =>withExternalInfisicalErrorHandling(async () => {- const secretMap = payload.flatten({ applySchema: false });+ const secretMap = payload.flatten();const ctx = await getRemoteContext(secretSync);const secretsToDelete = Object.keys(secretMap);await batchDeleteSecrets(secretSync, secretsToDelete, ctx);diff --git a/backend/src/services/secret-sync/secret-sync-payload.ts b/backend/src/services/secret-sync/secret-sync-payload.tsindex e5932eed44..98d83cd718 100644--- a/backend/src/services/secret-sync/secret-sync-payload.ts+++ b/backend/src/services/secret-sync/secret-sync-payload.ts@@ -123,12 +123,11 @@ export const createSecretSyncPayload = (): TSecretSyncPayload => ({secrets,environment,- // A destination whose sync targets can't carry the configured key schema (eg a many-to-one- // JSON body whose fields are app-facing variable names, or an Infisical-to-Infisical sync,- // where a schema-renamed key would look like a new secret and retrigger a sync cycle) calls- // flatten({ applySchema: false }) to get the raw-key view instead. Either way flatten() still- // groups by the resulting destination key and rejects collisions, so duplicate-name detection- // never depends on whether the schema was applied.+ // A destination whose sync targets can't carry the configured key schema (eg a many-to-one JSON+ // body whose fields are app-facing variable names) calls flatten({ applySchema: false }) to get+ // the raw-key view instead. Either way flatten() still groups by the resulting destination key+ // and rejects collisions, so duplicate-name detection never depends on whether the schema was+ // applied.flatten: ({ applySchema = true }: { applySchema?: boolean } = {}) => {const conflicts = findFlattenConflicts({ secrets, environment, keySchema }, { applySchema });diff --git a/backend/src/services/secret-sync/secret-sync-queue.ts b/backend/src/services/secret-sync/secret-sync-queue.tsindex 42df37ebe4..bb0b9b50aa 100644--- a/backend/src/services/secret-sync/secret-sync-queue.ts+++ b/backend/src/services/secret-sync/secret-sync-queue.ts@@ -54,6 +54,7 @@ import {TQueueSecretSyncSyncSecretsByIdDTO,TQueueSendSecretSyncActionFailedNotificationsDTO,TSecretMap,+ TSecretSync,TSecretSyncImportSecretsDTO,TSecretSyncRaw,TSecretSyncRemoveSecretsDTO,@@ -283,7 +284,6 @@ export const secretSyncQueueFactory = ({projectId});const actorOrgId = secretSync.connection.orgId;- const syncOptions = secretSync.syncOptions as { recursive?: boolean; keySchema?: string } | undefined;const decryptSecretValue = (value?: Buffer | undefined | null) =>value ? secretManagerDecryptor({ cipherTextBlob: value }).toString() : "";@@ -302,6 +302,14 @@ export const secretSyncQueueFactory = ({});return buildSyncPayload(+ {+ projectId,+ environment: environment.slug,+ sourcePath: folder.path,+ sourceFolderId: folderId,+ syncOptions: secretSync.syncOptions as TSecretSync["syncOptions"],+ includeImports+ },{folderDAL,projectEnvDAL,@@ -315,15 +323,6 @@ export const secretSyncQueueFactory = ({orgDAL,kmsService}- },- {- projectId,- environment: environment.slug,- sourcePath: folder.path,- sourceFolderId: folderId,- recursive: Boolean(syncOptions?.recursive),- keySchema: syncOptions?.keySchema,- includeImports});};@@ -1070,7 +1069,7 @@ export const secretSyncQueueFactory = ({// ancestor folder only matches when it is recursive, so a non-recursive sync rooted above// this path is never triggered by a write it was never configured to cover.const secretSyncs = candidateSyncs.filter(- (sync) => sync.folderId === folder.id || Boolean((sync.syncOptions as { recursive?: boolean } | null)?.recursive)+ (sync) => sync.folderId === folder.id || Boolean((sync.syncOptions as TSecretSync["syncOptions"])?.recursive));await secretSyncDAL.update(diff --git a/backend/src/services/secret-sync/secret-sync-recursive-fns.test.ts b/backend/src/services/secret-sync/secret-sync-recursive-fns.test.tsindex cadd58c755..13472e73b9 100644--- a/backend/src/services/secret-sync/secret-sync-recursive-fns.test.ts+++ b/backend/src/services/secret-sync/secret-sync-recursive-fns.test.ts@@ -1,10 +1,8 @@import {- assertWithinSecretLimit,buildSyncPayload,getAncestorPaths,getSyncedFolders,- mergeImportedSecrets,- SECRET_SYNC_MAX_SECRETS+ mergeImportedSecrets} from "./secret-sync-recursive-fns";const deps = {@@ -100,28 +98,6 @@ describe("getAncestorPaths", () => {});});-describe("assertWithinSecretLimit", () => {- test("accepts a count at the limit", () => {- expect(() => assertWithinSecretLimit(SECRET_SYNC_MAX_SECRETS)).not.toThrow();- });-- test("rejects a count above the limit with a message naming both numbers", () => {- let error: unknown;- try {- assertWithinSecretLimit(SECRET_SYNC_MAX_SECRETS + 1);- } catch (err) {- error = err;- }-- expect(error).toBeInstanceOf(Error);- const { message } = error as Error;- const overIndex = message.indexOf(String(SECRET_SYNC_MAX_SECRETS + 1));- const limitIndex = message.indexOf(String(SECRET_SYNC_MAX_SECRETS), overIndex + 1);- expect(overIndex).toBeGreaterThanOrEqual(0);- expect(limitIndex).toBeGreaterThan(overIndex);- });-});-describe("mergeImportedSecrets", () => {const imported = (key: string, secretValue: string) => ({ key, secretValue }) as never;@@ -242,16 +218,19 @@ describe("buildSyncPayload", () => {})}}- } as unknown as Parameters<typeof buildSyncPayload>[0];-- const payload = await buildSyncPayload(buildDeps, {- projectId: "proj-1",- environment: "dev",- sourcePath: "/",- sourceFolderId: "root",- recursive: true,- includeImports: true- });+ } as unknown as Parameters<typeof buildSyncPayload>[1];++ const payload = await buildSyncPayload(+ {+ projectId: "proj-1",+ environment: "dev",+ sourcePath: "/",+ sourceFolderId: "root",+ syncOptions: { recursive: true },+ includeImports: true+ },+ buildDeps+ );const paths = payload.secrets.map((entry) => entry.path).sort();@@ -299,21 +278,21 @@ describe("buildSyncPayload cross-folder duplicates", () => {})}}- } as unknown as Parameters<typeof buildSyncPayload>[0];+ } as unknown as Parameters<typeof buildSyncPayload>[1];const args = {projectId: "proj-1",environment: "dev",sourcePath: "/",sourceFolderId: "root",- recursive: true,+ syncOptions: { recursive: true },includeImports: true- };+ } as Parameters<typeof buildSyncPayload>[0];// Pins the sync-path requirement from the same bug: a name used in two folders must still fail// loudly on this path, since only the remove path may look past it.test("a cross-folder duplicate name still throws by default", async () => {- const payload = await buildSyncPayload(dedupeDeps, args);+ const payload = await buildSyncPayload(args, dedupeDeps);expect(() => payload.flatten()).toThrow(/DB_URL/);});@@ -321,7 +300,7 @@ describe("buildSyncPayload cross-folder duplicates", () => {// A sync whose secrets collide across folders would otherwise be stuck: it fails to sync, and// until dedupeConflicts() existed, it also failed to remove, which is the only way to delete it.test("dedupeConflicts() lets the remove path complete despite the same duplicate", async () => {- const payload = await buildSyncPayload(dedupeDeps, args);+ const payload = await buildSyncPayload(args, dedupeDeps);const deduped = payload.dedupeConflicts();expect(() => deduped.flatten()).not.toThrow();diff --git a/backend/src/services/secret-sync/secret-sync-recursive-fns.ts b/backend/src/services/secret-sync/secret-sync-recursive-fns.tsindex eb74957d7c..c7c112e499 100644--- a/backend/src/services/secret-sync/secret-sync-recursive-fns.ts+++ b/backend/src/services/secret-sync/secret-sync-recursive-fns.ts@@ -12,6 +12,7 @@ import {TSecretPayload,TSecretSyncPayload} from "@app/services/secret-sync/secret-sync-payload";+import { TSecretSync } from "@app/services/secret-sync/secret-sync-types";import { expandSecretReferencesFactory } from "@app/services/secret-v2-bridge/secret-reference-fns";import { TSecretV2BridgeDALFactory } from "@app/services/secret-v2-bridge/secret-v2-bridge-dal";import { recursivelyGetSecretPaths } from "@app/services/secret-v2-bridge/secret-v2-bridge-fns";@@ -33,22 +34,6 @@ export const getAncestorPaths = (path: string): string[] => {return segments.map((_, index) => (index === 0 ? "/" : `/${segments.slice(0, index).join("/")}`));};-// buildSyncPayload expands secret references for every secret concurrently-// (Promise.allSettled below), and expansion can hit the DB per secret, so this stays well under-// what a 10-connection pool can take at once even with every slot serving one sync. Applies to-// every sync, not just recursive ones: an oversized single folder is the same risk as an-// oversized subtree. Raise further only once that concurrency is actually bounded.-export const SECRET_SYNC_MAX_SECRETS = 500;--export const assertWithinSecretLimit = (count: number) => {- if (count <= SECRET_SYNC_MAX_SECRETS) return;-- throw new SecretSyncError({- message: `This sync covers ${count} secrets, which is above the limit of ${SECRET_SYNC_MAX_SECRETS} for a single sync. Point the sync at a narrower secret path, or split it into several syncs.`,- shouldRetry: false- });-};-export const getSyncedFolders = async ({folderDAL,projectEnvDAL,@@ -120,28 +105,28 @@ export const mergeImportedSecrets = (// throws on it by default, and payload.dedupeConflicts() is how the one caller that needs to// tolerate it (removal) gets a payload that won't.export const buildSyncPayload = async (+ args: {+ projectId: string;+ environment: string;+ sourcePath: string;+ sourceFolderId: string;+ syncOptions: Pick<TSecretSync["syncOptions"], "recursive" | "keySchema"> | undefined;+ includeImports: boolean;+ },deps: {folderDAL: Pick<TSecretFolderDALFactory, "find" | "findByManySecretPath">;projectEnvDAL: Pick<TProjectEnvDALFactory, "findOne">;- secretV2BridgeDAL: Pick<TSecretV2BridgeDALFactory, "findByFolderId" | "findByFolderIds" | "find">;+ secretV2BridgeDAL: Pick<TSecretV2BridgeDALFactory, "findByFolderIds" | "find">;secretImportDAL: Pick<TSecretImportDALFactory, "findByFolderIds" | "findByIds">;expandSecretReferences: TExpandSecretReferences;decryptSecretValue: (value?: Buffer | null) => string;fnSecretsV2FromImportsDeps: TFnSecretsV2FromImportsDeps;- },- args: {- projectId: string;- environment: string;- sourcePath: string;- sourceFolderId: string;- recursive: boolean;- keySchema?: string;- includeImports: boolean;}): Promise<TSecretSyncPayload> => {const { folderDAL, projectEnvDAL, secretV2BridgeDAL, secretImportDAL, expandSecretReferences, decryptSecretValue } =deps;- const { projectId, environment, sourcePath, sourceFolderId, recursive, keySchema, includeImports } = args;+ const { projectId, environment, sourcePath, sourceFolderId, syncOptions, includeImports } = args;+ const { recursive, keySchema } = syncOptions ?? {};const folders = await getSyncedFolders({folderDAL,@@ -150,21 +135,12 @@ export const buildSyncPayload = async (environment,sourcePath,sourceFolderId,- recursive+ recursive: Boolean(recursive)});const pathByFolderId = new Map(folders.map(({ folderId, path }) => [folderId, path]));- // findByFolderIds joins tags, metadata, rotation, honey token, reminder, recipients and users- // plus a DENSE_RANK window, so it costs far more than the three narrow queries findByFolderId- // runs. Every non-recursive sync (the vast majority today) hits this path, so it keeps using- // the cheaper single-folder read; only a genuinely multi-folder subtree pays for the join.- const secrets =- folders.length === 1- ? await secretV2BridgeDAL.findByFolderId({ folderId: folders[0].folderId })- : await secretV2BridgeDAL.findByFolderIds({ folderIds: folders.map(({ folderId }) => folderId) });-- assertWithinSecretLimit(secrets.length);+ const secrets = await secretV2BridgeDAL.findByFolderIds({ folderIds: folders.map(({ folderId }) => folderId) });const entries: TSecretPayload[] = [];@@ -237,8 +213,6 @@ export const buildSyncPayload = async (secrets: group.secrets})));-- assertWithinSecretLimit(allEntries.length);}return createSecretSyncPayload(allEntries, { environment, keySchema });diff --git a/backend/src/services/secret-sync/secret-sync-service.ts b/backend/src/services/secret-sync/secret-sync-service.tsindex a130fb91c5..60700444ab 100644--- a/backend/src/services/secret-sync/secret-sync-service.ts+++ b/backend/src/services/secret-sync/secret-sync-service.ts@@ -67,10 +67,6 @@ import {} from "./secret-sync-maps";import { TSecretSyncQueueFactory } from "./secret-sync-queue";-// secretSyncDAL rows carry syncOptions as an untyped jsonb column, not the destination-specific-// shape TSecretSync narrows it to elsewhere, so reading a field back out needs a cast.-type TRawSyncOptions = { recursive?: boolean; keySchema?: string };-type TSecretSyncServiceFactoryDep = {secretSyncDAL: TSecretSyncDALFactory;secretImportDAL: TSecretImportDALFactory;@@ -186,31 +182,16 @@ export const secretSyncServiceFactory = ({if (!(error instanceof ForbiddenError)) throw error;throw new ForbiddenRequestError({- message:- path === sourcePath- ? `You do not have permission to read secrets at path "${path}" in environment "${environment}".`- : `You do not have permission to read secrets at path "${path}" in environment "${environment}". This sync includes subfolders, so it reads every folder beneath "${sourcePath}".`+ message: `You do not have permission to read secrets at path "${path}" in environment "${environment}".`});}}};- // A SecretSyncError is written for the end user already (see secret-sync-payload.ts); every- // caller that can surface one directly just needs it translated to the house error envelope.- const $rethrowSecretSyncErrorsAsBadRequest = async <T>(fn: () => T | Promise<T>): Promise<T> => {- try {- return await fn();- } catch (error) {- if (error instanceof SecretSyncError) throw new BadRequestError({ message: error.message });-- throw error;- }- };-// Shared by $assertSyncedSecretsAreFlattenable and findRecursiveConflicts: both need the same// decrypt-and-expand payload for the full recursive subtree, and differ only in what they do// with it once built (throw on the first conflict vs. report every conflict back).- const $buildRecursiveSyncPayload = ({+ const $buildRecursiveSyncPayload = async ({projectId,actorOrgId,environment,@@ -224,30 +205,38 @@ export const secretSyncServiceFactory = ({sourcePath: string;sourceFolderId: string;keySchema?: string;- }) =>- $rethrowSecretSyncErrorsAsBadRequest(async () => {- const { decryptor: secretManagerDecryptor } = await kmsService.createCipherPairWithDataKey({- type: KmsDataKey.SecretManager,- projectId- });+ }) => {+ const { decryptor: secretManagerDecryptor } = await kmsService.createCipherPairWithDataKey({+ type: KmsDataKey.SecretManager,+ projectId+ });- const decryptSecretValue = (value?: Buffer | null) =>- value ? secretManagerDecryptor({ cipherTextBlob: value }).toString() : "";+ const decryptSecretValue = (value?: Buffer | null) =>+ value ? secretManagerDecryptor({ cipherTextBlob: value }).toString() : "";- const { expandSecretReferences } = expandSecretReferencesFactory({- decryptSecretValue,- secretDAL: secretV2BridgeDAL,- folderDAL,- projectId,- canExpandValue: () => true,- actorOrgId,- orgDAL,- projectFolderGrantDAL,- projectDAL,- kmsService- });+ const { expandSecretReferences } = expandSecretReferencesFactory({+ decryptSecretValue,+ secretDAL: secretV2BridgeDAL,+ folderDAL,+ projectId,+ canExpandValue: () => true,+ actorOrgId,+ orgDAL,+ projectFolderGrantDAL,+ projectDAL,+ kmsService+ });- return buildSyncPayload(+ try {+ return await buildSyncPayload(+ {+ projectId,+ environment,+ sourcePath,+ sourceFolderId,+ syncOptions: { recursive: true, keySchema },+ includeImports: true+ },{folderDAL,projectEnvDAL,@@ -261,18 +250,16 @@ export const secretSyncServiceFactory = ({orgDAL,kmsService}- },- {- projectId,- environment,- sourcePath,- sourceFolderId,- recursive: true,- keySchema,- includeImports: true});- });+ } catch (error) {+ // A SecretSyncError is already written for the end user (see secret-sync-payload.ts), so it+ // only needs translating to the house error envelope.+ if (error instanceof SecretSyncError) throw new BadRequestError({ message: error.message });++ throw error;+ }+ };// Flattening a subtree onto a destination that holds one flat list can produce two secrets with// the same destination key. flatten() is what detects that, and the job runs it on every sync, so@@ -303,7 +290,13 @@ export const secretSyncServiceFactory = ({keySchema});- await $rethrowSecretSyncErrorsAsBadRequest(() => payload.flatten());+ try {+ payload.flatten();+ } catch (error) {+ if (error instanceof SecretSyncError) throw new BadRequestError({ message: error.message });++ throw error;+ }};// Lets the frontend check for duplicate-name conflicts while the user is still on the Source@@ -324,9 +317,13 @@ export const secretSyncServiceFactory = ({projectId});+ // Create rather than Read: the only reason to call this is to configure a sync at this source,+ // and the check is subject-scoped so it still holds for an actor whose sync permission is+ // narrowed to particular paths. Update is enforced separately in updateSecretSync, which has a+ // sync to name in the subject; this route does not.ForbiddenError.from(projectPermission).throwUnlessCan(- ProjectPermissionSecretSyncActions.Read,- ProjectPermissionSub.SecretSyncs+ ProjectPermissionSecretSyncActions.Create,+ subject(ProjectPermissionSub.SecretSyncs, { environment, secretPath }));const folder = await folderDAL.findBySecretPath(projectId, environment, secretPath);@@ -656,7 +653,7 @@ export const secretSyncServiceFactory = ({message: `Could not find folder with path "${secretPath}" in environment "${environment}" for project with ID "${projectId}"`});- const requestedSyncOptions = params.syncOptions as TRawSyncOptions | undefined;+ const requestedSyncOptions = params.syncOptions;const isRecursive = Boolean(requestedSyncOptions?.recursive);await $assertCanReadSyncedFolders(projectPermission, {@@ -867,34 +864,54 @@ export const secretSyncServiceFactory = ({(Boolean(secretPath) && secretPath !== secretSync.folder?.path) ||(Boolean(environment) && environment !== secretSync.environment?.slug);- const existingSyncOptions = secretSync.syncOptions as TRawSyncOptions | undefined;- const requestedSyncOptions = params.syncOptions as TRawSyncOptions | undefined;+ const updatedEnvironment = environment ?? secretSync.environment?.slug;+ const updatedSecretPath = secretPath ?? secretSync.folder?.path;++ if (isSourceChanged) {+ if (!updatedEnvironment || !updatedSecretPath)+ throw new BadRequestError({ message: "Must specify both source environment and secret path" });++ const newFolder = await folderDAL.findBySecretPath(secretSync.projectId, updatedEnvironment, updatedSecretPath);++ if (!newFolder)+ throw new BadRequestError({+ message: `Could not find folder with path "${updatedSecretPath}" in environment "${updatedEnvironment}" for project with ID "${secretSync.projectId}"`+ });- const wasRecursive = Boolean(existingSyncOptions?.recursive);- const isRecursive = Boolean(requestedSyncOptions?.recursive ?? wasRecursive);+ folderId = newFolder.id;+ }++ // preSaveTransformSyncOptions is the only place that knows how a destination folds the+ // requested options into the stored ones: most replace them outright, Azure Entra ID SCIM+ // merges. It returns nothing when the request carries no syncOptions at all, in which case the+ // row keeps what it has.+ const resolvedSyncOptions = folderId+ ? await preSaveTransformSyncOptions(+ destination,+ {+ syncOptions: params.syncOptions as Record<string, unknown> | undefined,+ existingSyncOptions: secretSync.syncOptions as Record<string, unknown> | undefined,+ folderId+ },+ preSaveTransformDeps+ )+ : (params.syncOptions as Record<string, unknown> | undefined);++ // What the row will hold once this update lands. The checks below and the write both read it,+ // so the gate cannot authorize one sync while the row ends up describing another.+ const updatedSyncOptions = resolvedSyncOptions ?? (secretSync.syncOptions as Record<string, unknown> | undefined);++ const { recursive, keySchema } = (updatedSyncOptions ?? {}) as NonNullable<TSecretSync["syncOptions"]>;+ const isRecursive = Boolean(recursive);// Every update to a recursive sync re-authorizes the whole subtree, because an actor holding// Edit on the source folder can otherwise repoint an existing recursive sync at a destination// they control and read descendants they were never granted. A non-recursive sync covers only// its source folder, so it re-authorizes when that source actually changes.if (isSourceChanged || isRecursive) {- const updatedEnvironment = environment ?? secretSync.environment?.slug;- const updatedSecretPath = secretPath ?? secretSync.folder?.path;-if (!updatedEnvironment || !updatedSecretPath)throw new BadRequestError({ message: "Must specify both source environment and secret path" });- if (isSourceChanged) {- const newFolder = await folderDAL.findBySecretPath(secretSync.projectId, updatedEnvironment, updatedSecretPath);-- if (!newFolder)- throw new BadRequestError({- message: `Could not find folder with path "${updatedSecretPath}" in environment "${updatedEnvironment}" for project with ID "${secretSync.projectId}"`- });-- folderId = newFolder.id;- }-if (!folderId)throw new BadRequestError({message: `Could not find folder with path "${updatedSecretPath}" in environment "${updatedEnvironment}" for project with ID "${secretSync.projectId}"`@@ -915,25 +932,13 @@ export const secretSyncServiceFactory = ({environment: updatedEnvironment,sourcePath: updatedSecretPath,sourceFolderId: folderId,- keySchema: requestedSyncOptions?.keySchema ?? existingSyncOptions?.keySchema+ keySchema});}}const isAutoSyncEnabled = params.isAutoSyncEnabled ?? secretSync.isAutoSyncEnabled;- const resolvedSyncOptions = folderId- ? await preSaveTransformSyncOptions(- destination,- {- syncOptions: params.syncOptions as Record<string, unknown> | undefined,- existingSyncOptions: secretSync.syncOptions as Record<string, unknown> | undefined,- folderId- },- preSaveTransformDeps- )- : (params.syncOptions as Record<string, unknown> | undefined);-const connectionIdForEnrich = params.connectionId ?? secretSync.connectionId;const enrichedDestinationConfig = connectionIdForEnrich@@ -950,7 +955,7 @@ export const secretSyncServiceFactory = ({try {const updatedSecretSync = await secretSyncDAL.updateById(syncId, {...params,- ...(resolvedSyncOptions && { syncOptions: resolvedSyncOptions }),+ ...(updatedSyncOptions && { syncOptions: updatedSyncOptions }),...(enrichedDestinationConfig && { destinationConfig: enrichedDestinationConfig }),...(isAutoSyncEnabled && folderId && { syncStatus: SecretSyncStatus.Pending }),folderIddiff --git a/frontend/src/components/secret-syncs/forms/SecretSyncInitialSyncBehaviorFields.tsx b/frontend/src/components/secret-syncs/forms/SecretSyncInitialSyncBehaviorFields.tsxindex 321e282c83..e7297c40c7 100644--- a/frontend/src/components/secret-syncs/forms/SecretSyncInitialSyncBehaviorFields.tsx+++ b/frontend/src/components/secret-syncs/forms/SecretSyncInitialSyncBehaviorFields.tsx@@ -1,6 +1,6 @@import { useEffect } from "react";import { Controller, useFormContext } from "react-hook-form";-import { ArrowDown, TriangleAlert } from "lucide-react";+import { ArrowDown, Info, TriangleAlert } from "lucide-react";import {Alert,@@ -344,6 +344,7 @@ export const SecretSyncInitialSyncBehaviorFields = () => {const currentInitialBehavior = watch("syncOptions.initialSyncBehavior");const disableSecretDeletion = watch("syncOptions.disableSecretDeletion");const keySchema = watch("syncOptions.keySchema");+ const recursive = Boolean(watch("syncOptions.recursive"));// Vercel "sensitive" secrets cannot be read back, so importing destination secrets is impossible.// Force the initial sync behavior to OverwriteDestination whenever sensitive is enabled.@@ -359,14 +360,27 @@ export const SecretSyncInitialSyncBehaviorFields = () => {}}, [vercelSensitive, currentInitialBehavior, setValue]);- const importAvailable = Boolean(syncOption?.canImportSecrets) && !vercelSensitive;+ // A sync that includes subfolders has no single folder to import destination secrets back into,+ // so overwrite is its only option. The source step sets that when the toggle goes on; this keeps+ // the choice from being offered again here.+ const importAvailable = Boolean(syncOption?.canImportSecrets) && !vercelSensitive && !recursive;const behaviorKeys = importAvailable? BEHAVIOR_ORDER: [SecretSyncInitialSyncBehavior.OverwriteDestination];return (<>- {!vercelSensitive && !syncOption?.canImportSecrets && (+ {recursive && syncOption?.canImportSecrets && (+ <Alert className="mb-3" variant="info">+ <Info />+ <AlertTitle>Importing secrets is not supported with subfolders</AlertTitle>+ <AlertDescription>+ This sync includes secrets from subfolders, so there is no single folder to import+ destination secrets into. Turn off subfolders on the source step to import instead.+ </AlertDescription>+ </Alert>+ )}+ {!vercelSensitive && !recursive && !syncOption?.canImportSecrets && (<Alert className="mb-3" variant="warning"><TriangleAlert /><AlertTitle>Importing secrets is not supported</AlertTitle>diff --git a/frontend/src/components/secret-syncs/forms/SecretSyncSourceFields.tsx b/frontend/src/components/secret-syncs/forms/SecretSyncSourceFields.tsxindex 3369667858..75a74e9edb 100644--- a/frontend/src/components/secret-syncs/forms/SecretSyncSourceFields.tsx+++ b/frontend/src/components/secret-syncs/forms/SecretSyncSourceFields.tsx@@ -1,7 +1,7 @@import { useEffect } from "react";import { Controller, useFormContext, useWatch } from "react-hook-form";import { subject } from "@casl/ability";-import { CheckCircle2, TriangleAlert } from "lucide-react";+import { CheckCircle2, Info, TriangleAlert } from "lucide-react";import {Alert,@@ -26,14 +26,13 @@ import {import {SecretSync,SecretSyncInitialSyncBehavior,- useRecursiveConflictsCheck+ useRecursiveConflictsCheck,+ useSecretSyncOption} from "@app/hooks/api/secretSyncs";import { AzureEntraIdScimSyncSourceFields } from "./AzureEntraIdScimSyncSourceFields";import { TSecretSyncForm } from "./schemas";-// Mirrors SECRET_SYNC_MAX_SECRETS in backend/src/services/secret-sync/secret-sync-recursive-fns.ts.-const SECRET_SYNC_MAX_SECRETS = 100;const MAX_DISPLAYED_CONFLICTS = 5;const DefaultSecretSyncSourceFields = () => {@@ -45,13 +44,10 @@ const DefaultSecretSyncSourceFields = () => {const destination = watch("destination");const selectedEnvironment = watch("environment");const selectedSecretPath = watch("secretPath");- const initialSyncBehavior = watch("syncOptions.initialSyncBehavior");const recursive = watch("syncOptions.recursive");const keySchema = watch("syncOptions.keySchema");- const importsFromDestination =- initialSyncBehavior === SecretSyncInitialSyncBehavior.ImportPrioritizeSource ||- initialSyncBehavior === SecretSyncInitialSyncBehavior.ImportPrioritizeDestination;+ const { syncOption } = useSecretSyncOption(destination);const { conflicts, isChecking, isClear } = useRecursiveConflictsCheck({destination,@@ -59,7 +55,7 @@ const DefaultSecretSyncSourceFields = () => {environment: selectedEnvironment?.slug,secretPath: selectedSecretPath,keySchema,- recursive: Boolean(recursive) && !importsFromDestination+ recursive: Boolean(recursive)});useEffect(() => {@@ -85,12 +81,6 @@ const DefaultSecretSyncSourceFields = () => {}}, [selectedEnvironment, selectedSecretPath]);- useEffect(() => {- if (importsFromDestination && recursive) {- setValue("syncOptions.recursive", false);- }- }, [importsFromDestination, recursive, setValue]);-return (<FieldGroup><Controller@@ -142,22 +132,41 @@ const DefaultSecretSyncSourceFields = () => {<Field><Field orientation="horizontal"><FieldContent>- <Label htmlFor="recursive">Recursively sync secrets</Label>+ <Label htmlFor="recursive">Include secrets from subfolders</Label><FieldDescription>- {importsFromDestination- ? "Not available when the initial sync imports secrets from the destination. There is no single folder to import them back into."- : `Also sync secrets from every folder beneath this path, however deep. Secret names must be unique across all of them, and the combined total can't exceed ${SECRET_SYNC_MAX_SECRETS} secrets.`}+ Also sync secrets from every folder beneath this path, however deep. Secret names+ must be unique across all of them.</FieldDescription></FieldContent><Toggleid="recursive"variant="project"- checked={Boolean(value) && !importsFromDestination}- disabled={importsFromDestination}- onCheckedChange={onChange}+ checked={Boolean(value)}+ onCheckedChange={(checked) => {+ onChange(checked);++ // There is no single folder to import a subtree back into, so the backend rejects+ // the combination. Move the user off it here rather than failing them on save.+ if (checked) {+ setValue(+ "syncOptions.initialSyncBehavior",+ SecretSyncInitialSyncBehavior.OverwriteDestination+ );+ }+ }}/></Field><FieldError errors={[error]} />+ {Boolean(value) && syncOption?.canImportSecrets && (+ <Alert variant="info">+ <Info />+ <AlertTitle>Initial sync is set to overwrite the destination</AlertTitle>+ <AlertDescription>+ A sync that includes subfolders cannot import secrets from the destination into+ Infisical, because there is no single folder to import them into.+ </AlertDescription>+ </Alert>+ )}{isChecking && (<p className="text-sm text-muted">Checking for naming conflicts across folders...</p>)}