Skip to content

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

gateway attachment hardening

x0322053d9ed501d81d
diff --git a/backend/src/ee/services/pam-account/pam-account-service.ts b/backend/src/ee/services/pam-account/pam-account-service.ts
index 6c0f210b0b..dd207d2d56 100644
--- a/backend/src/ee/services/pam-account/pam-account-service.ts
+++ b/backend/src/ee/services/pam-account/pam-account-service.ts
@@ -1,6 +1,9 @@
import { ForbiddenError } from "@casl/ability";
-import { ActionProjectType, RESOURCE_SCOPE, ResourceType } from "@app/db/schemas";
+import { ActionProjectType, OrganizationActionScope, RESOURCE_SCOPE, ResourceType } from "@app/db/schemas";
+import { TGatewayPoolServiceFactory } from "@app/ee/services/gateway-pool/gateway-pool-service";
+import { TGatewayV2DALFactory } from "@app/ee/services/gateway-v2/gateway-v2-dal";
+import { OrgPermissionGatewayActions, OrgPermissionSubjects } from "@app/ee/services/permission/org-permission";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
import {
ResourcePermissionPamResourceActions,
@@ -40,8 +43,13 @@ type TPamAccountServiceFactoryDep = {
pamAccountTemplateDAL: Pick<TPamAccountTemplateDALFactory, "findById">;
membershipDAL: Pick<TMembershipDALFactory, "find" | "delete" | "findResourceMembershipsForActor">;
membershipRoleDAL: Pick<TMembershipRoleDALFactory, "delete">;
- permissionService: Pick<TPermissionServiceFactory, "getProjectPermission" | "getResourcePermission">;
+ permissionService: Pick<
+ TPermissionServiceFactory,
+ "getProjectPermission" | "getResourcePermission" | "getOrgPermission"
+ >;
kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">;
+ gatewayV2DAL: Pick<TGatewayV2DALFactory, "findOne">;
+ gatewayPoolService: Pick<TGatewayPoolServiceFactory, "resolveAttachableGatewayFromPool">;
};
export type TPamAccountServiceFactory = ReturnType<typeof pamAccountServiceFactory>;
@@ -53,7 +61,9 @@ export const pamAccountServiceFactory = ({
membershipDAL,
membershipRoleDAL,
permissionService,
- kmsService
+ kmsService,
+ gatewayV2DAL,
+ gatewayPoolService
}: TPamAccountServiceFactoryDep) => {
const getProjectCipher = async (projectId: string) =>
kmsService.createCipherPairWithDataKey({ type: KmsDataKey.SecretManager, projectId });
@@ -92,6 +102,39 @@ export const pamAccountServiceFactory = ({
});
};
+ const validateGatewayAttachment = async (
+ gwId: string | null | undefined,
+ poolId: string | null | undefined,
+ ctx: TActorContext
+ ) => {
+ if (gwId) {
+ const gw = await gatewayV2DAL.findOne({ id: gwId, orgId: ctx.actorOrgId });
+ if (!gw) {
+ throw new NotFoundError({ message: "Gateway not found in your organization" });
+ }
+
+ const { permission: orgPermission } = await permissionService.getOrgPermission({
+ scope: OrganizationActionScope.Any,
+ actor: ctx.actor,
+ actorId: ctx.actorId,
+ orgId: ctx.actorOrgId,
+ actorAuthMethod: ctx.actorAuthMethod,
+ actorOrgId: ctx.actorOrgId
+ });
+ ForbiddenError.from(orgPermission).throwUnlessCan(
+ OrgPermissionGatewayActions.AttachGateways,
+ OrgPermissionSubjects.Gateway
+ );
+ }
+ if (poolId) {
+ await gatewayPoolService.resolveAttachableGatewayFromPool({
+ poolId,
+ orgId: ctx.actorOrgId,
+ actor: { type: ctx.actor, id: ctx.actorId, authMethod: ctx.actorAuthMethod, orgId: ctx.actorOrgId }
+ });
+ }
+ };
+
const list = async ({ projectId, folderId, templateId, search, ...ctx }: TListPamAccountsDTO & TActorContext) => {
await verifyProductMembership(projectId, ctx);
@@ -240,6 +283,8 @@ export const pamAccountServiceFactory = ({
});
}
+ await validateGatewayAttachment(gatewayId, gatewayPoolId, ctx);
+
const validatedConnectionDetails = validateConnectionDetails(accountType, connectionDetails);
const validatedCredentials = validateCredentials(accountType, credentials);
@@ -330,6 +375,8 @@ export const pamAccountServiceFactory = ({
}
}
+ await validateGatewayAttachment(gatewayId, gatewayPoolId, ctx);
+
const updateData: Record<string, unknown> = {};
if (name !== undefined) updateData.name = name;
if (description !== undefined) updateData.description = description;
diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.ts
index 8b99abc512..2ab186bd90 100644
--- a/backend/src/server/routes/index.ts
+++ b/backend/src/server/routes/index.ts
@@ -1539,16 +1539,6 @@ export const registerRoutes = async (
permissionService
});
- const pamAccountService = pamAccountServiceFactory({
- pamAccountDAL,
- pamFolderDAL,
- pamAccountTemplateDAL,
- membershipDAL,
- membershipRoleDAL,
- permissionService,
- kmsService
- });
-
const pamMembershipService = pamMembershipServiceFactory({
membershipDAL,
membershipRoleDAL,
@@ -1677,6 +1667,18 @@ export const registerRoutes = async (
dynamicSecretDAL
});
+ const pamAccountService = pamAccountServiceFactory({
+ pamAccountDAL,
+ pamFolderDAL,
+ pamAccountTemplateDAL,
+ membershipDAL,
+ membershipRoleDAL,
+ permissionService,
+ kmsService,
+ gatewayV2DAL,
+ gatewayPoolService
+ });
+
const pamSessionDAL = pamSessionDALFactory(db);
const pamWebAccessService = pamWebAccessServiceFactory({