Skip to content

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

fix(secret-sync): raise secret cap to 500, validate keySchema on the preview route

Calvin01c793ab00f1
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 2556a8eb0d..64f84f8ebd 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
@@ -10,6 +10,7 @@ import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { AuthMode } from "@app/services/auth/auth-type";
import { SecretSync, SecretSyncImportBehavior } from "@app/services/secret-sync/secret-sync-enums";
import { SECRET_SYNC_NAME_MAP } from "@app/services/secret-sync/secret-sync-maps";
+import { KeySchemaSchema } from "@app/services/secret-sync/secret-sync-schemas";
import { TSecretSync, TSecretSyncInput } from "@app/services/secret-sync/secret-sync-types";
import { PostHogEventTypes } from "@app/services/telemetry/telemetry-types";
@@ -557,8 +558,13 @@ export const registerSyncSecretsEndpoints = <T extends TSecretSync, I extends TS
body: z.object({
projectId: z.string().uuid(),
environment: slugSchema({ field: "environment", max: 64 }),
- secretPath: z.string().trim().min(1, "Secret path required").transform(removeTrailingSlash),
- keySchema: z.string().trim().max(255).optional()
+ secretPath: z
+ .string()
+ .trim()
+ .min(1, "Secret path required")
+ .transform(removeTrailingSlash)
+ .describe(SecretSyncs.CREATE(destination).secretPath),
+ keySchema: KeySchemaSchema
}),
response: {
200: z.object({
diff --git a/backend/src/services/secret-sync/secret-sync-recursive-fns.ts b/backend/src/services/secret-sync/secret-sync-recursive-fns.ts
index 7d36898a37..eb74957d7c 100644
--- a/backend/src/services/secret-sync/secret-sync-recursive-fns.ts
+++ b/backend/src/services/secret-sync/secret-sync-recursive-fns.ts
@@ -34,9 +34,11 @@ export const getAncestorPaths = (path: string): string[] => {
};
// buildSyncPayload expands secret references for every secret concurrently
-// (Promise.allSettled below), and expansion can hit the DB per secret. Kept low against a
-// 10-connection pool; raise it if customers need more room and the concurrency is bounded first.
-export const SECRET_SYNC_MAX_SECRETS = 100;
+// (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;
diff --git a/backend/src/services/secret-sync/secret-sync-schemas.test.ts b/backend/src/services/secret-sync/secret-sync-schemas.test.ts
index d7d42d180d..d8bb0a67f1 100644
--- a/backend/src/services/secret-sync/secret-sync-schemas.test.ts
+++ b/backend/src/services/secret-sync/secret-sync-schemas.test.ts
@@ -1,5 +1,5 @@
import { SecretSync, SecretSyncInitialSyncBehavior } from "./secret-sync-enums";
-import { BaseSecretSyncSchema } from "./secret-sync-schemas";
+import { BaseSecretSyncSchema, KeySchemaSchema } from "./secret-sync-schemas";
const schema = BaseSecretSyncSchema(SecretSync.Render, { canImportSecrets: true }).shape.syncOptions;
@@ -30,3 +30,32 @@ describe("BaseSyncOptionsSchema recursive", () => {
expect(result.success).toBe(false);
});
});
+
+describe("KeySchemaSchema", () => {
+ test("accepts a schema with the required secretKey placeholder", () => {
+ expect(KeySchemaSchema.safeParse("prefix/{{secretKey}}").success).toBe(true);
+ });
+
+ test("accepts the optional environment placeholder alongside secretKey", () => {
+ expect(KeySchemaSchema.safeParse("{{environment}}/{{secretKey}}").success).toBe(true);
+ });
+
+ test("rejects a value missing the secretKey placeholder", () => {
+ expect(KeySchemaSchema.safeParse("prefix/no-placeholder").success).toBe(false);
+ });
+
+ // The regression this guards: this string used to reach handlebars.compile() unvalidated on
+ // the recursive-conflicts preview route, which throws a raw, uncaught Error the moment the
+ // compiled template is invoked (getKeyWithSchema in secret-sync-payload.ts).
+ test("rejects a syntactically invalid Handlebars template", () => {
+ expect(KeySchemaSchema.safeParse("{{").success).toBe(false);
+ });
+
+ test("rejects disallowed characters outside the placeholders", () => {
+ expect(KeySchemaSchema.safeParse("{{secretKey}}!").success).toBe(false);
+ });
+
+ test("is optional", () => {
+ expect(KeySchemaSchema.safeParse(undefined).success).toBe(true);
+ });
+});
diff --git a/backend/src/services/secret-sync/secret-sync-schemas.ts b/backend/src/services/secret-sync/secret-sync-schemas.ts
index 233cce8007..782f304501 100644
--- a/backend/src/services/secret-sync/secret-sync-schemas.ts
+++ b/backend/src/services/secret-sync/secret-sync-schemas.ts
@@ -20,6 +20,39 @@ const RECURSIVE_SYNC_REFINEMENT = {
const isRecursiveCombinationAllowed = (options: { recursive?: unknown; initialSyncBehavior?: unknown }) =>
!options.recursive || options.initialSyncBehavior === SecretSyncInitialSyncBehavior.OverwriteDestination;
+// Shared by BaseSyncOptionsSchema and the recursive-conflicts preview route: both compile this
+// string as a Handlebars template (getKeyWithSchema in secret-sync-payload.ts), so both need the
+// same guard against a syntactically invalid one reaching handlebars.compile() uncaught.
+export const KeySchemaSchema = z
+ .string()
+ .trim()
+ .max(255)
+ .optional()
+ .refine(
+ (val) => {
+ if (!val) return true;
+
+ const allowedOptionalPlaceholders = ["{{environment}}"];
+
+ const allowedPlaceholdersRegexPart = ["{{secretKey}}", ...allowedOptionalPlaceholders]
+ .map((p) => p.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&")) // Escape regex special characters
+ .join("|");
+
+ const allowedContentRegex = new RE2(`^([a-zA-Z0-9_\\-/]|${allowedPlaceholdersRegexPart})*$`);
+ const contentIsValid = allowedContentRegex.test(val);
+
+ // Check if {{secretKey}} is present
+ const secretKeyRegex = new RE2(/\{\{secretKey\}\}/);
+ const secretKeyIsPresent = secretKeyRegex.test(val);
+
+ return contentIsValid && secretKeyIsPresent;
+ },
+ {
+ message:
+ "Key schema must include exactly one {{secretKey}} placeholder. It can also include {{environment}} placeholders. Only alphanumeric characters (a-z, A-Z, 0-9), dashes (-), underscores (_), and slashes (/) are allowed besides the placeholders."
+ }
+ );
+
const BaseSyncOptionsSchema = <T extends AnyZodObject | undefined = undefined>({
destination,
syncOptionsConfig: { canImportSecrets, supportsKeySchema = true, supportsDisableSecretDeletion = true },
@@ -39,34 +72,7 @@ const BaseSyncOptionsSchema = <T extends AnyZodObject | undefined = undefined>({
: z.literal(SecretSyncInitialSyncBehavior.OverwriteDestination)
).describe(SecretSyncs.SYNC_OPTIONS(destination).initialSyncBehavior),
keySchema: supportsKeySchema
- ? z
- .string()
- .optional()
- .refine(
- (val) => {
- if (!val) return true;
-
- const allowedOptionalPlaceholders = ["{{environment}}"];
-
- const allowedPlaceholdersRegexPart = ["{{secretKey}}", ...allowedOptionalPlaceholders]
- .map((p) => p.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&")) // Escape regex special characters
- .join("|");
-
- const allowedContentRegex = new RE2(`^([a-zA-Z0-9_\\-/]|${allowedPlaceholdersRegexPart})*$`);
- const contentIsValid = allowedContentRegex.test(val);
-
- // Check if {{secretKey}} is present
- const secretKeyRegex = new RE2(/\{\{secretKey\}\}/);
- const secretKeyIsPresent = secretKeyRegex.test(val);
-
- return contentIsValid && secretKeyIsPresent;
- },
- {
- message:
- "Key schema must include exactly one {{secretKey}} placeholder. It can also include {{environment}} placeholders. Only alphanumeric characters (a-z, A-Z, 0-9), dashes (-), underscores (_), and slashes (/) are allowed besides the placeholders."
- }
- )
- .describe(SecretSyncs.SYNC_OPTIONS(destination).keySchema)
+ ? KeySchemaSchema.describe(SecretSyncs.SYNC_OPTIONS(destination).keySchema)
: z
.string()
.optional()