⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
test(security): cover API keys without workspace targets
Tin Sever34313f980d02
diff --git a/tests/api-integration/invitation-owner-api-key.test.ts b/tests/api-integration/invitation-owner-api-key.test.tsindex a8bf921b9..169ddecc4 100644--- a/tests/api-integration/invitation-owner-api-key.test.ts+++ b/tests/api-integration/invitation-owner-api-key.test.ts@@ -1,5 +1,6 @@+import { randomUUID } from "node:crypto";import * as email from "@kaneo/email";-import { eq } from "drizzle-orm";+import { eq, inArray } from "drizzle-orm";import { beforeEach, describe, expect, it, vi } from "vite-plus/test";import db, { schema } from "../../apps/api/src/database";import { resetTestDatabase } from "./helpers/database";@@ -126,3 +127,151 @@ describe("invitation ownership with API keys", () => {},);});++describe("API key invitation and removal targets", () => {+ const cases = [false, true].flatMap((bearer) =>+ ["admin, owner", "owner"].map((role) => ({ bearer, role })),+ );++ it.each(cases)(+ "does not inherit an active workspace (bearer=$bearer, role=$role)",+ async ({ bearer, role }) => {+ const ctx = await invitationSecurityFixture("owner");+ const keys: Awaited<ReturnType<typeof createKey>>[] = [];+ for (const permissions of [+ { invitation: ["create"], member: ["delete"] },+ null,+ ]) {+ keys.push(await createKey(ctx, ctx.actor.cookies, permissions));+ }+ await db+ .update(schema.workspaceUserTable)+ .set({ role })+ .where(eq(schema.workspaceUserTable.id, ctx.membership.id));+ await db+ .update(schema.sessionTable)+ .set({ activeOrganizationId: ctx.workspace.id })+ .where(+ inArray(schema.sessionTable.userId, [+ ctx.actor.userId,+ ctx.owner.userId,+ ]),+ );+ // A second canonical owner prevents the last-owner check from masking+ // an unauthorized removal if the key ever inherits a browser target.+ const [otherOwner] = await db+ .insert(schema.userTable)+ .values({+ id: randomUUID(),+ email: "[email protected]",+ emailVerified: true,+ name: "Other owner",+ })+ .returning();+ await db.insert(schema.workspaceUserTable).values({+ workspaceId: ctx.workspace.id,+ userId: otherOwner.id,+ role: "owner",+ joinedAt: new Date(),+ });+ const pending = await ctx.pending("member, owner");+ const members = await db+ .select()+ .from(schema.workspaceUserTable)+ .orderBy(schema.workspaceUserTable.id);+ const send = vi.spyOn(email, "sendWorkspaceInvitationEmail");++ for (const key of keys) {+ const headers = {+ cookie: ctx.owner.cookies,+ ...(bearer+ ? { authorization: `Bearer ${key.key}` }+ : { "x-api-key": key.key }),+ };+ const session = await ctx.app.request("/api/auth/get-session", {+ headers,+ });+ expect(session.status).toBe(200);+ const viewer = await session.json();+ expect(viewer).toMatchObject({+ user: { id: ctx.actor.userId },+ session: { id: key.id },+ });+ expect(viewer.session.activeOrganizationId).toBeUndefined();++ for (const organizationId of [undefined, ""]) {+ const invited = await organizationRequest(+ ctx.app,+ headers,+ "invite-member",+ {+ organizationId,+ email: "[email protected]",+ role: "member",+ resend: true,+ },+ );+ expect(invited.status).toBe(400);+ expect(await invited.json()).toMatchObject({+ code: "ORGANIZATION_NOT_FOUND",+ });+ const removed = await organizationRequest(+ ctx.app,+ headers,+ "remove-member",+ { organizationId, memberIdOrEmail: "[email protected]" },+ );+ expect(removed.status).toBe(400);+ expect(await removed.json()).toMatchObject({+ code: "NO_ACTIVE_ORGANIZATION",+ });+ }++ // With an explicit target, the stored invitation guard runs even for+ // a true owner. A malformed actor is also denied before member removal.+ const invited = await organizationRequest(+ ctx.app,+ headers,+ "invite-member",+ {+ organizationId: ctx.workspace.id,+ email: "[email protected]",+ role: "member",+ resend: true,+ },+ );+ expect(invited.status).toBe(403);+ expect(await invited.json()).toMatchObject({+ code: "OWNER_ROLE_REVIEW_REQUIRED",+ });+ if (role !== "owner") {+ const removed = await organizationRequest(+ ctx.app,+ headers,+ "remove-member",+ {+ organizationId: ctx.workspace.id,+ memberIdOrEmail: "[email protected]",+ },+ );+ expect(removed.status).toBe(403);+ expect(await removed.json()).toMatchObject({+ code: "OWNER_ROLE_REVIEW_REQUIRED",+ });+ }+ }+ expect(+ await db.query.invitationTable.findFirst({+ where: eq(schema.invitationTable.id, pending.id),+ }),+ ).toEqual(pending);+ expect(+ await db+ .select()+ .from(schema.workspaceUserTable)+ .orderBy(schema.workspaceUserTable.id),+ ).toEqual(members);+ expect(send).not.toHaveBeenCalled();+ },+ );+});