Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(api): hide invited project ids from viewers

Andrej73bb07a1bfe7
diff --git a/apps/api/src/auth.ts b/apps/api/src/auth.ts
index 55e9b1c5e..6472e87c2 100644
--- a/apps/api/src/auth.ts
+++ b/apps/api/src/auth.ts
@@ -49,6 +49,7 @@ import { applyInvitationProjectAccess } from "./project-access/apply-invitation-
import { resolveInvitationProjectAccess } from "./project-access/resolve-invitation-project-access";
import { clearMemberProjectAccess } from "./project-access/clear-member-project-access";
import { isOwnerRole } from "./project-access/is-owner-role";
+import { hideInaccessibleInvitationProjects } from "./project-access/hide-inaccessible-invitation-projects";
import clearEmailVerificationOnAdminChange from "./user/controllers/clear-email-verification-on-admin-change";
import deleteAccountData from "./user/controllers/delete-account-data";
import prepareAdminUserRemoval from "./user/controllers/prepare-admin-user-removal";
@@ -889,6 +890,21 @@ export const auth = betterAuth({
}
}),
after: createAuthMiddleware(async (ctx) => {
+ const viewerId = ctx.context.session?.user.id;
+ if (viewerId && ctx.path === "/organization/list-invitations") {
+ await hideInaccessibleInvitationProjects(
+ viewerId,
+ ctx.context.returned,
+ );
+ }
+ if (viewerId && ctx.path === "/organization/get-full-organization") {
+ await hideInaccessibleInvitationProjects(
+ viewerId,
+ (ctx.context.returned as { invitations?: unknown } | null)
+ ?.invitations,
+ );
+ }
+
if (ctx.path === "/organization/leave") {
// The successful endpoint returns the removed member. No post-delete
// query may prevent revocation after membership has already committed.
diff --git a/apps/api/src/project-access/hide-inaccessible-invitation-projects.ts b/apps/api/src/project-access/hide-inaccessible-invitation-projects.ts
new file mode 100644
index 000000000..470c34ef9
--- /dev/null
+++ b/apps/api/src/project-access/hide-inaccessible-invitation-projects.ts
@@ -0,0 +1,31 @@
+import { findInaccessibleProjectIds } from "./find-inaccessible-project-ids";
+
+type InvitationWithProjects = { projectIds: string[] };
+
+function hasProjectIds(value: unknown): value is InvitationWithProjects {
+ return (
+ typeof value === "object" &&
+ value !== null &&
+ Array.isArray((value as { projectIds?: unknown }).projectIds)
+ );
+}
+
+export async function hideInaccessibleInvitationProjects(
+ viewerId: string,
+ invitations: unknown,
+): Promise<void> {
+ if (!Array.isArray(invitations)) return;
+ const restricted = invitations.filter(hasProjectIds);
+ const hidden = new Set(
+ await findInaccessibleProjectIds(
+ viewerId,
+ restricted.flatMap((invitation) => invitation.projectIds),
+ ),
+ );
+ if (hidden.size === 0) return;
+ for (const invitation of restricted) {
+ invitation.projectIds = invitation.projectIds.filter(
+ (projectId) => !hidden.has(projectId),
+ );
+ }
+}
diff --git a/tests/api-integration/project-access-management.test.ts b/tests/api-integration/project-access-management.test.ts
index aa5ab7894..0e708eb84 100644
--- a/tests/api-integration/project-access-management.test.ts
+++ b/tests/api-integration/project-access-management.test.ts
@@ -422,4 +422,91 @@ describe("invitations with project access", () => {
expect(invited.status).toBe(400);
expect(await db.select().from(schema.invitationTable)).toHaveLength(0);
});
+
+ it("hides invited project ids from a limited admin", async () => {
+ const ctx = await createInvitationWorkspace();
+ const invite = async (email: string, role: string, projectIds: string[]) =>
+ (await (
+ await ctx.ownerRequest("/auth/organization/invite-member", {
+ method: "POST",
+ body: {
+ organizationId: ctx.workspaceId,
+ email,
+ role,
+ projectAccess: "selected",
+ projectIds,
+ },
+ })
+ ).json()) as { id: string };
+
+ const adminInvitation = await invite("[email protected]", "admin", [
+ ctx.alpha.id,
+ ]);
+ const admin = await signUpWithSession(ctx.app, {
+ email: "[email protected]",
+ name: "Limited Admin",
+ });
+ const adminRequest = projectAccessApi({ cookie: admin.cookies });
+ await adminRequest("/auth/organization/accept-invitation", {
+ method: "POST",
+ body: { invitationId: adminInvitation.id },
+ });
+ await invite("[email protected]", "member", [ctx.alpha.id, ctx.beta.id]);
+
+ const listed = await adminRequest(
+ `/auth/organization/list-invitations?organizationId=${ctx.workspaceId}`,
+ );
+ expect(listed.status).toBe(200);
+ const invitations = (await listed.json()) as {
+ email: string;
+ projectIds: string[];
+ }[];
+ expect(
+ invitations.find(
+ (invitation) => invitation.email === "[email protected]",
+ )?.projectIds,
+ ).toEqual([ctx.alpha.id]);
+ expect(JSON.stringify(invitations)).not.toContain(ctx.beta.id);
+ });
+});
+
+describe("notification rules with project access", () => {
+ it("keeps projects a member lost when they save the rule again", async () => {
+ const ctx = await createRestrictedWorkspace();
+ const member = await addWorkspaceMember(ctx.workspace.id);
+ mockAuthenticatedSession(member);
+ const request = projectAccessApi();
+ const rule = (projectIds: string[]) =>
+ request(`/notification-preferences/workspaces/${ctx.workspace.id}`, {
+ method: "PUT",
+ body: {
+ isActive: true,
+ emailEnabled: false,
+ ntfyEnabled: false,
+ gotifyEnabled: false,
+ webhookEnabled: false,
+ projectMode: "selected",
+ selectedProjectIds: projectIds,
+ },
+ });
+
+ expect((await rule([ctx.alpha.id, ctx.beta.id])).status).toBe(200);
+ await restrictToProjects(ctx.workspace.id, member.id, [ctx.alpha.id]);
+
+ const read = await request("/notification-preferences");
+ const preferences = (await read.json()) as {
+ workspaces: { selectedProjectIds: string[] }[];
+ };
+ expect(preferences.workspaces[0]?.selectedProjectIds).toEqual([
+ ctx.alpha.id,
+ ]);
+
+ expect((await rule([])).status).toBe(200);
+ const stored = await db
+ .select({
+ projectId: schema.userNotificationWorkspaceProjectTable.projectId,
+ })
+ .from(schema.userNotificationWorkspaceProjectTable);
+ expect(stored.map((row) => row.projectId)).toEqual([ctx.beta.id]);
+ });
});