Skip to content

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

refactor(secret-sync): consolidate secret name validation to a single check in SecretNameSchema

Victor Santos41e3026658a5
diff --git a/backend/src/server/lib/schemas.ts b/backend/src/server/lib/schemas.ts
index d0750926d3..035e645f48 100644
--- a/backend/src/server/lib/schemas.ts
+++ b/backend/src/server/lib/schemas.ts
@@ -43,6 +43,6 @@ export const GenericResourceNameSchema = z
export const BaseSecretNameSchema = z.string().trim().min(1);
export const SecretNameSchema = BaseSecretNameSchema.refine(
- (el) => !el.includes(":"),
- "Secret name cannot contain colon."
-).refine((el) => !el.includes("/"), "Secret name cannot contain forward slash.");
+ (el) => !el.includes(":") && !el.includes("/"),
+ "Secret name cannot contain colon or forward slash."
+);
diff --git a/backend/src/services/secret-sync/secret-sync-queue.ts b/backend/src/services/secret-sync/secret-sync-queue.ts
index 4b28564c01..d1cdc57744 100644
--- a/backend/src/services/secret-sync/secret-sync-queue.ts
+++ b/backend/src/services/secret-sync/secret-sync-queue.ts
@@ -11,6 +11,7 @@ import { KeyStorePrefixes, TKeyStoreFactory } from "@app/keystore/keystore";
import { getConfig } from "@app/lib/config/env";
import { logger } from "@app/lib/logger";
import { QueueJobs, QueueName, TQueueServiceFactory } from "@app/queue";
+import { SecretNameSchema } from "@app/server/lib/schemas";
import { decryptAppConnectionCredentials } from "@app/services/app-connection/app-connection-fns";
import { ActorType } from "@app/services/auth/auth-type";
import { TKmsServiceFactory } from "@app/services/kms/kms-service";
@@ -63,7 +64,6 @@ import { TAppConnectionDALFactory } from "../app-connection/app-connection-dal";
import { TFolderCommitServiceFactory } from "../folder-commit/folder-commit-service";
import { TNotificationServiceFactory } from "../notification/notification-service";
import { NotificationType } from "../notification/notification-types";
-import { SecretNameSchema } from "@app/server/lib/schemas";
export type TSecretSyncQueueFactory = ReturnType<typeof secretSyncQueueFactory>;
@@ -409,14 +409,23 @@ export const secretSyncQueueFactory = ({
if (!Object.keys(importedSecrets).length) return {};
+ let invalidNameCount = 0;
+ let errorMessage = "";
+
for (const [key] of Object.entries(importedSecrets)) {
const result = SecretNameSchema.safeParse(key);
if (!result.success) {
- const errorMessage = result.error.issues[0]?.message || "Invalid secret name";
- throw new Error(`Invalid secret name "${key}": ${errorMessage}`);
+ invalidNameCount += 1;
+ if (errorMessage === "") errorMessage = result.error.issues[0]?.message;
}
}
+ if (invalidNameCount > 0) {
+ throw new Error(
+ `Found ${invalidNameCount} invalid secret name${invalidNameCount === 1 ? "" : "s"}. ${errorMessage}`
+ );
+ }
+
const importedSecretMap: TSecretMap = {};
const secretMap = await $getInfisicalSecrets(secretSync, false);