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
feat: add validation when creating a new dynamic secrets
adilsond9fbdadd482f
diff --git a/backend/src/ee/services/dynamic-secret/providers/gcp-iam.ts b/backend/src/ee/services/dynamic-secret/providers/gcp-iam.tsindex d87e9f6c26..f262dd53b0 100644--- a/backend/src/ee/services/dynamic-secret/providers/gcp-iam.ts+++ b/backend/src/ee/services/dynamic-secret/providers/gcp-iam.ts@@ -2,17 +2,41 @@ import { gaxios, Impersonated } from "google-auth-library";import { GetAccessTokenResponse } from "google-auth-library/build/src/auth/oauth2client";import { getConfig } from "@app/lib/config/env";-import { BadRequestError, InternalServerError } from "@app/lib/errors";+import { BadRequestError, InternalServerError, NotFoundError } from "@app/lib/errors";import { sanitizeString } from "@app/lib/fn";import { logger } from "@app/lib/logger";import { alphaNumericNanoId } from "@app/lib/nanoid";+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";+import { requestMemoize } from "@app/lib/request-context/request-memoizer";import { buildGcpSourceCredential } from "@app/services/app-connection/gcp/gcp-connection-fns";+import { TProjectDALFactory } from "@app/services/project/project-dal";import { DynamicSecretGcpIamSchema, TDynamicProviderFns } from "./models";-export const GcpIamProvider = (): TDynamicProviderFns => {- const validateProviderInputs = async (inputs: unknown) => {+type TGcpIamProviderDTO = {+ projectDAL: Pick<TProjectDALFactory, "findById">;+};++export const GcpIamProvider = ({ projectDAL }: TGcpIamProviderDTO): TDynamicProviderFns => {+ const validateProviderInputs = async (inputs: unknown, metadata: { projectId: string }) => {const providerInputs = await DynamicSecretGcpIamSchema.parseAsync(inputs);++ // Check if provided service account email suffix matches organization ID.+ // We do this to mitigate confused deputy attacks in multi-tenant instances+ const project = await requestMemoize(requestMemoKeys.projectFindById(metadata.projectId), () =>+ projectDAL.findById(metadata.projectId)+ );+ if (!project) throw new NotFoundError({ message: `Project with ID '${metadata.projectId}' not found` });++ const expectedAccountIdSuffix = project.orgId.split("-").slice(0, 2).join("-");+ const serviceAccountId = providerInputs.serviceAccountEmail.split("@")[0];++ if (!serviceAccountId.endsWith(expectedAccountIdSuffix)) {+ throw new BadRequestError({+ message: `GCP service account ID must have a suffix of "${expectedAccountIdSuffix}"`+ });+ }+return providerInputs;};@@ -57,8 +81,8 @@ export const GcpIamProvider = (): TDynamicProviderFns => {return tokenResponse.token;};- const validateConnection = async (inputs: unknown) => {- const providerInputs = await validateProviderInputs(inputs);+ const validateConnection = async (inputs: unknown, metadata: { projectId: string }) => {+ const providerInputs = await validateProviderInputs(inputs, metadata);try {await $getToken(providerInputs.serviceAccountEmail, 10, providerInputs.tokenScopes);return true;@@ -73,10 +97,10 @@ export const GcpIamProvider = (): TDynamicProviderFns => {}};- const create = async (data: { inputs: unknown; expireAt: number }) => {- const { inputs, expireAt } = data;+ const create = async (data: { inputs: unknown; expireAt: number; metadata: { projectId: string } }) => {+ const { inputs, expireAt, metadata } = data;- const providerInputs = await validateProviderInputs(inputs);+ const providerInputs = await validateProviderInputs(inputs, metadata);try {const now = Math.floor(Date.now() / 1000);@@ -102,14 +126,14 @@ export const GcpIamProvider = (): TDynamicProviderFns => {return { entityId };};- const renew = async (inputs: unknown, entityId: string, expireAt: number) => {+ const renew = async (inputs: unknown, entityId: string, expireAt: number, metadata: { projectId: string }) => {try {// To renew a token it must be re-created- const data = await create({ inputs, expireAt });+ const data = await create({ inputs, expireAt, metadata });return { ...data, entityId };} catch (err) {- const providerInputs = await validateProviderInputs(inputs);+ const providerInputs = await validateProviderInputs(inputs, metadata);const sanitizedErrorMessage = sanitizeString({unsanitizedString: (err as Error)?.message,tokens: [providerInputs.serviceAccountEmail]diff --git a/backend/src/ee/services/dynamic-secret/providers/index.ts b/backend/src/ee/services/dynamic-secret/providers/index.tsindex ed7f99c22c..a07d46947d 100644--- a/backend/src/ee/services/dynamic-secret/providers/index.ts+++ b/backend/src/ee/services/dynamic-secret/providers/index.ts@@ -1,4 +1,5 @@import { SnowflakeProvider } from "@app/ee/services/dynamic-secret/providers/snowflake";+import { TProjectDALFactory } from "@app/services/project/project-dal";import { TGatewayServiceFactory } from "../../gateway/gateway-service";import { TGatewayPoolServiceFactory } from "../../gateway-pool/gateway-pool-service";@@ -34,12 +35,14 @@ type TBuildDynamicSecretProviderDTO = {gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">;gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">;gatewayPoolService: Pick<TGatewayPoolServiceFactory, "resolveEffectiveGatewayId">;+ projectDAL: Pick<TProjectDALFactory, "findById">;};export const buildDynamicSecretProviders = ({gatewayService,gatewayV2Service,- gatewayPoolService+ gatewayPoolService,+ projectDAL}: TBuildDynamicSecretProviderDTO): Record<DynamicSecretProviders, TDynamicProviderFns> => ({[DynamicSecretProviders.SqlDatabase]: SqlDatabaseProvider({ gatewayService, gatewayV2Service, gatewayPoolService }),[DynamicSecretProviders.Clickhouse]: ClickhouseProvider({ gatewayService, gatewayV2Service, gatewayPoolService }),@@ -65,7 +68,7 @@ export const buildDynamicSecretProviders = ({[DynamicSecretProviders.SapAse]: SapAseProvider(),[DynamicSecretProviders.Kubernetes]: KubernetesProvider({ gatewayService, gatewayV2Service, gatewayPoolService }),[DynamicSecretProviders.Vertica]: VerticaProvider({ gatewayService, gatewayV2Service, gatewayPoolService }),- [DynamicSecretProviders.GcpIam]: GcpIamProvider(),+ [DynamicSecretProviders.GcpIam]: GcpIamProvider({ projectDAL }),[DynamicSecretProviders.Github]: GithubProvider(),[DynamicSecretProviders.Couchbase]: CouchbaseProvider(),[DynamicSecretProviders.Milvus]: MilvusProvider({ gatewayService, gatewayV2Service, gatewayPoolService }),diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.tsindex d5623a26f5..b183c7254a 100644--- a/backend/src/server/routes/index.ts+++ b/backend/src/server/routes/index.ts@@ -2708,7 +2708,8 @@ export const registerRoutes = async (const dynamicSecretProviders = buildDynamicSecretProviders({gatewayService,gatewayV2Service,- gatewayPoolService+ gatewayPoolService,+ projectDAL});const dynamicSecretQueueService = dynamicSecretLeaseQueueServiceFactory({