Skip to content

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

fix: review comments

Piyush Guptaf3a59bbb6cab
diff --git a/backend/src/ee/routes/v1/group-router.ts b/backend/src/ee/routes/v1/group-router.ts
index 9d5859dd5b..d5d2455a7b 100644
--- a/backend/src/ee/routes/v1/group-router.ts
+++ b/backend/src/ee/routes/v1/group-router.ts
@@ -11,6 +11,7 @@ import {
} from "@app/ee/services/group/group-types";
import { ApiDocsTags, GROUPS } from "@app/lib/api-docs";
import { OrderByDirection } from "@app/lib/types";
+import { CharacterType, characterValidator } from "@app/lib/validator/validate-string";
import { readLimit, writeLimit } from "@app/server/config/rateLimiter";
import { slugSchema } from "@app/server/lib/schemas";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
@@ -198,7 +199,14 @@ export const registerGroupRouter = async (server: FastifyZodProvider) => {
offset: z.coerce.number().min(0).default(0).describe(GROUPS.LIST_USERS.offset),
limit: z.coerce.number().min(1).max(100).default(10).describe(GROUPS.LIST_USERS.limit),
username: z.string().trim().optional().describe(GROUPS.LIST_USERS.username),
- search: z.string().trim().optional().describe(GROUPS.LIST_USERS.search),
+ search: z
+ .string()
+ .trim()
+ .refine((val) => characterValidator([CharacterType.AlphaNumeric, CharacterType.Hyphen])(val), {
+ message: "Invalid pattern: only alphanumeric characters, - are allowed."
+ })
+ .optional()
+ .describe(GROUPS.LIST_USERS.search),
filter: z.nativeEnum(FilterReturnedUsers).optional().describe(GROUPS.LIST_USERS.filterUsers)
}),
response: {
@@ -249,7 +257,14 @@ export const registerGroupRouter = async (server: FastifyZodProvider) => {
querystring: z.object({
offset: z.coerce.number().min(0).default(0).describe(GROUPS.LIST_MACHINE_IDENTITIES.offset),
limit: z.coerce.number().min(1).max(100).default(10).describe(GROUPS.LIST_MACHINE_IDENTITIES.limit),
- search: z.string().trim().optional().describe(GROUPS.LIST_MACHINE_IDENTITIES.search),
+ search: z
+ .string()
+ .trim()
+ .refine((val) => characterValidator([CharacterType.AlphaNumeric, CharacterType.Hyphen])(val), {
+ message: "Invalid pattern: only alphanumeric characters, - are allowed."
+ })
+ .optional()
+ .describe(GROUPS.LIST_MACHINE_IDENTITIES.search),
filter: z
.nativeEnum(FilterReturnedMachineIdentities)
.optional()
@@ -295,7 +310,14 @@ export const registerGroupRouter = async (server: FastifyZodProvider) => {
querystring: z.object({
offset: z.coerce.number().min(0).default(0).describe(GROUPS.LIST_MEMBERS.offset),
limit: z.coerce.number().min(1).max(100).default(10).describe(GROUPS.LIST_MEMBERS.limit),
- search: z.string().trim().optional().describe(GROUPS.LIST_MEMBERS.search),
+ search: z
+ .string()
+ .trim()
+ .refine((val) => characterValidator([CharacterType.AlphaNumeric, CharacterType.Hyphen])(val), {
+ message: "Invalid pattern: only alphanumeric characters, - are allowed."
+ })
+ .optional()
+ .describe(GROUPS.LIST_MEMBERS.search),
orderBy: z
.nativeEnum(GroupMembersOrderBy)
.default(GroupMembersOrderBy.Name)
@@ -363,7 +385,14 @@ export const registerGroupRouter = async (server: FastifyZodProvider) => {
querystring: z.object({
offset: z.coerce.number().min(0).default(0).describe(GROUPS.LIST_PROJECTS.offset),
limit: z.coerce.number().min(1).max(100).default(10).describe(GROUPS.LIST_PROJECTS.limit),
- search: z.string().trim().optional().describe(GROUPS.LIST_PROJECTS.search),
+ search: z
+ .string()
+ .trim()
+ .refine((val) => characterValidator([CharacterType.AlphaNumeric, CharacterType.Hyphen])(val), {
+ message: "Invalid pattern: only alphanumeric characters, - are allowed."
+ })
+ .optional()
+ .describe(GROUPS.LIST_PROJECTS.search),
filter: z.nativeEnum(FilterReturnedProjects).optional().describe(GROUPS.LIST_PROJECTS.filterProjects),
orderBy: z
.nativeEnum(GroupProjectsOrderBy)
diff --git a/backend/src/ee/services/group/group-fns.ts b/backend/src/ee/services/group/group-fns.ts
index 4f988ca70d..e42a86261e 100644
--- a/backend/src/ee/services/group/group-fns.ts
+++ b/backend/src/ee/services/group/group-fns.ts
@@ -300,51 +300,45 @@ export const addIdentitiesToGroup = async ({
identityGroupMembershipDAL,
membershipDAL
}: TAddIdentitiesToGroup) => {
- return identityDAL.transaction(async (tx) => {
- const identityIdsSet = new Set(identityIds);
- const identityIdsArray = Array.from(identityIdsSet);
-
- // ensure all identities exist and belong to the org via org scoped membership
- const foundIdentitiesMemberships = await membershipDAL.find(
- {
- scope: AccessScope.Organization,
- scopeOrgId: group.orgId,
- $in: {
- actorIdentityId: identityIdsArray
- }
- },
- { tx }
- );
-
- const existingIdentityOrgMembershipsIdentityIdsSet = new Set(
- foundIdentitiesMemberships.map((u) => u.actorIdentityId as string)
- );
-
- identityIdsArray.forEach((identityId) => {
- if (!existingIdentityOrgMembershipsIdentityIdsSet.has(identityId)) {
- throw new ForbiddenRequestError({
- message: `Identity with id ${identityId} is not part of the organization`
- });
- }
- });
+ const identityIdsSet = new Set(identityIds);
+ const identityIdsArray = Array.from(identityIdsSet);
+
+ // ensure all identities exist and belong to the org via org scoped membership
+ const foundIdentitiesMemberships = await membershipDAL.find({
+ scope: AccessScope.Organization,
+ scopeOrgId: group.orgId,
+ $in: {
+ actorIdentityId: identityIdsArray
+ }
+ });
- // check if identity group membership already exists
- const existingIdentityGroupMemberships = await identityGroupMembershipDAL.find(
- {
- groupId: group.id,
- $in: {
- identityId: identityIdsArray
- }
- },
- { tx }
- );
+ const existingIdentityOrgMembershipsIdentityIdsSet = new Set(
+ foundIdentitiesMemberships.map((u) => u.actorIdentityId as string)
+ );
- if (existingIdentityGroupMemberships.length) {
- throw new BadRequestError({
- message: `${identityIdsArray.length > 1 ? `Identities are` : `Identity is`} already part of the group ${group.slug}`
+ identityIdsArray.forEach((identityId) => {
+ if (!existingIdentityOrgMembershipsIdentityIdsSet.has(identityId)) {
+ throw new ForbiddenRequestError({
+ message: `Identity with id ${identityId} is not part of the organization`
});
}
+ });
+ // check if identity group membership already exists
+ const existingIdentityGroupMemberships = await identityGroupMembershipDAL.find({
+ groupId: group.id,
+ $in: {
+ identityId: identityIdsArray
+ }
+ });
+
+ if (existingIdentityGroupMemberships.length) {
+ throw new BadRequestError({
+ message: `${identityIdsArray.length > 1 ? `Identities are` : `Identity is`} already part of the group ${group.slug}`
+ });
+ }
+
+ return identityDAL.transaction(async (tx) => {
await identityGroupMembershipDAL.insertMany(
foundIdentitiesMemberships.map((membership) => ({
identityId: membership.actorIdentityId as string,
@@ -506,55 +500,48 @@ export const removeIdentitiesFromGroup = async ({
membershipDAL,
identityGroupMembershipDAL
}: TRemoveIdentitiesFromGroup) => {
- return identityDAL.transaction(async (tx) => {
- const identityIdsSet = new Set(identityIds);
- const identityIdsArray = Array.from(identityIdsSet);
+ const identityIdsSet = new Set(identityIds);
+ const identityIdsArray = Array.from(identityIdsSet);
+
+ // ensure all identities exist and belong to the org via org scoped membership
+ const foundIdentitiesMemberships = await membershipDAL.find({
+ scope: AccessScope.Organization,
+ scopeOrgId: group.orgId,
+ $in: {
+ actorIdentityId: identityIdsArray
+ }
+ });
- // ensure all identities exist and belong to the org via org scoped membership
- const foundIdentitiesMemberships = await membershipDAL.find(
- {
- scope: AccessScope.Organization,
- scopeOrgId: group.orgId,
- $in: {
- actorIdentityId: identityIdsArray
- }
- },
- { tx }
- );
+ const foundIdentitiesMembershipsIdentityIdsSet = new Set(
+ foundIdentitiesMemberships.map((u) => u.actorIdentityId as string)
+ );
- const foundIdentitiesMembershipsIdentityIdsSet = new Set(
- foundIdentitiesMemberships.map((u) => u.actorIdentityId as string)
- );
+ if (foundIdentitiesMembershipsIdentityIdsSet.size !== identityIdsArray.length) {
+ throw new NotFoundError({
+ message: `Machine identities not found`
+ });
+ }
- if (foundIdentitiesMembershipsIdentityIdsSet.size !== identityIdsArray.length) {
- throw new NotFoundError({
- message: `Machine identities not found`
- });
+ // check if identity group membership already exists
+ const existingIdentityGroupMemberships = await identityGroupMembershipDAL.find({
+ groupId: group.id,
+ $in: {
+ identityId: identityIdsArray
}
+ });
- // check if identity group membership already exists
- const existingIdentityGroupMemberships = await identityGroupMembershipDAL.find(
- {
- groupId: group.id,
- $in: {
- identityId: identityIdsArray
- }
- },
- { tx }
- );
-
- const existingIdentityGroupMembershipsIdentityIdsSet = new Set(
- existingIdentityGroupMemberships.map((u) => u.identityId)
- );
-
- identityIdsArray.forEach((identityId) => {
- if (!existingIdentityGroupMembershipsIdentityIdsSet.has(identityId)) {
- throw new ForbiddenRequestError({
- message: `Machine identities are not part of the group ${group.slug}`
- });
- }
- });
+ const existingIdentityGroupMembershipsIdentityIdsSet = new Set(
+ existingIdentityGroupMemberships.map((u) => u.identityId)
+ );
+ identityIdsArray.forEach((identityId) => {
+ if (!existingIdentityGroupMembershipsIdentityIdsSet.has(identityId)) {
+ throw new ForbiddenRequestError({
+ message: `Machine identities are not part of the group ${group.slug}`
+ });
+ }
+ });
+ return identityDAL.transaction(async (tx) => {
await identityGroupMembershipDAL.delete(
{
groupId: group.id,
diff --git a/backend/src/services/project/project-dal.ts b/backend/src/services/project/project-dal.ts
index 11d4239db0..90e4c6a854 100644
--- a/backend/src/services/project/project-dal.ts
+++ b/backend/src/services/project/project-dal.ts
@@ -25,12 +25,27 @@ export const projectDALFactory = (db: TDbClient) => {
const findIdentityProjects = async (identityId: string, orgId: string, projectType?: ProjectType) => {
try {
+ const identityGroupSubquery = db
+ .replicaNode()(TableName.Groups)
+ .leftJoin(
+ TableName.IdentityGroupMembership,
+ `${TableName.IdentityGroupMembership}.groupId`,
+ `${TableName.Groups}.id`
+ )
+ .where(`${TableName.Groups}.orgId`, orgId)
+ .where(`${TableName.IdentityGroupMembership}.identityId`, identityId)
+ .select(db.ref("id").withSchema(TableName.Groups));
+
const workspaces = await db
.replicaNode()(TableName.Membership)
.where(`${TableName.Membership}.scope`, AccessScope.Project)
- .where(`${TableName.Membership}.actorIdentityId`, identityId)
.join(TableName.Project, `${TableName.Membership}.scopeProjectId`, `${TableName.Project}.id`)
.where(`${TableName.Project}.orgId`, orgId)
+ .andWhere((qb) => {
+ void qb
+ .where(`${TableName.Membership}.actorIdentityId`, identityId)
+ .orWhereIn(`${TableName.Membership}.actorGroupId`, identityGroupSubquery);
+ })
.andWhere((qb) => {
if (projectType) {
void qb.where(`${TableName.Project}.type`, projectType);
diff --git a/frontend/src/hooks/api/groups/queries.tsx b/frontend/src/hooks/api/groups/queries.tsx
index fadb0e833c..cd2b9a00cb 100644
--- a/frontend/src/hooks/api/groups/queries.tsx
+++ b/frontend/src/hooks/api/groups/queries.tsx
@@ -140,7 +140,7 @@ export const useListGroupUsers = ({
const params = new URLSearchParams({
offset: String(offset),
limit: String(limit),
- search,
+ ...(search && { search }),
...(filter && { filter })
});
@@ -191,7 +191,7 @@ export const useListGroupMembers = ({
const params = new URLSearchParams({
offset: String(offset),
limit: String(limit),
- search,
+ ...(search && { search }),
...(orderBy && { orderBy: orderBy.toString() }),
...(orderDirection && { orderDirection })
});
@@ -243,7 +243,7 @@ export const useListGroupMachineIdentities = ({
const params = new URLSearchParams({
offset: String(offset),
limit: String(limit),
- search,
+ ...(search && { search }),
...(filter && { filter })
});
@@ -292,7 +292,7 @@ export const useListGroupProjects = ({
const params = new URLSearchParams({
offset: String(offset),
limit: String(limit),
- search,
+ ...(search && { search }),
...(filter && { filter }),
...(orderBy && { orderBy }),
...(orderDirection && { orderDirection })