|
| 1 | +import { postgresTest } from "@internal/testcontainers"; |
| 2 | +import plugin from "@trigger.dev/rbac"; |
| 3 | +import { type PrismaClient } from "@trigger.dev/database"; |
| 4 | +import { describe, expect, vi } from "vitest"; |
| 5 | +import { |
| 6 | + createTestOrgProjectWithMember, |
| 7 | + createTestUser, |
| 8 | +} from "./fixtures/environmentVariablesFixtures"; |
| 9 | + |
| 10 | +vi.setConfig({ testTimeout: 60_000 }); |
| 11 | + |
| 12 | +// The RBAC fallback ability is permissive (`can: () => true` for a non-admin), so |
| 13 | +// `ability.can` is not a tenant floor. `authenticateSession` is the gate every |
| 14 | +// org-scoped dashboard route relies on; a non-member in an org context must be |
| 15 | +// denied here, or a permissive ability lets them act on any org whose slug they |
| 16 | +// know. The route-level e2e (auth-dashboard.e2e.full) covers the HTTP path; this |
| 17 | +// pins the fallback gate directly since that path can't run without a container. |
| 18 | +function fallback(prisma: PrismaClient) { |
| 19 | + // forceFallback skips the closed-source plugin and uses the in-repo fallback. |
| 20 | + return plugin.create({ primary: prisma, replica: prisma }, { forceFallback: true }); |
| 21 | +} |
| 22 | + |
| 23 | +const request = new Request("https://app.trigger.dev/orgs/x/settings/roles"); |
| 24 | + |
| 25 | +describe("RBAC fallback authenticateSession — org membership floor", () => { |
| 26 | + postgresTest("denies a non-member in an org context", async ({ prisma }) => { |
| 27 | + const { organization } = await createTestOrgProjectWithMember(prisma); |
| 28 | + const outsider = await createTestUser(prisma); |
| 29 | + |
| 30 | + const result = await fallback(prisma).authenticateSession(request, { |
| 31 | + userId: outsider.id, |
| 32 | + organizationId: organization.id, |
| 33 | + }); |
| 34 | + |
| 35 | + expect(result).toMatchObject({ ok: false, reason: "unauthorized" }); |
| 36 | + }); |
| 37 | + |
| 38 | + postgresTest("allows a member in an org context", async ({ prisma }) => { |
| 39 | + const { user, organization } = await createTestOrgProjectWithMember(prisma); |
| 40 | + |
| 41 | + const result = await fallback(prisma).authenticateSession(request, { |
| 42 | + userId: user.id, |
| 43 | + organizationId: organization.id, |
| 44 | + }); |
| 45 | + |
| 46 | + expect(result.ok).toBe(true); |
| 47 | + }); |
| 48 | + |
| 49 | + postgresTest( |
| 50 | + "stays permissive with no org context, even for a non-member", |
| 51 | + async ({ prisma }) => { |
| 52 | + // Identity-only checks (no organizationId) predate any scope, so the floor |
| 53 | + // does not apply and the permissive baseline is preserved. |
| 54 | + const outsider = await createTestUser(prisma); |
| 55 | + |
| 56 | + const result = await fallback(prisma).authenticateSession(request, { userId: outsider.id }); |
| 57 | + |
| 58 | + expect(result.ok).toBe(true); |
| 59 | + } |
| 60 | + ); |
| 61 | + |
| 62 | + // A project-only scope is still a tenant claim, so the floor resolves the |
| 63 | + // project's organization rather than letting the context through unchecked. |
| 64 | + postgresTest("denies a non-member scoped only to a project", async ({ prisma }) => { |
| 65 | + const { project } = await createTestOrgProjectWithMember(prisma); |
| 66 | + const outsider = await createTestUser(prisma); |
| 67 | + |
| 68 | + const result = await fallback(prisma).authenticateSession(request, { |
| 69 | + userId: outsider.id, |
| 70 | + projectId: project.id, |
| 71 | + }); |
| 72 | + |
| 73 | + expect(result).toMatchObject({ ok: false, reason: "unauthorized" }); |
| 74 | + }); |
| 75 | + |
| 76 | + postgresTest("allows a member scoped only to a project", async ({ prisma }) => { |
| 77 | + const { user, project } = await createTestOrgProjectWithMember(prisma); |
| 78 | + |
| 79 | + const result = await fallback(prisma).authenticateSession(request, { |
| 80 | + userId: user.id, |
| 81 | + projectId: project.id, |
| 82 | + }); |
| 83 | + |
| 84 | + expect(result.ok).toBe(true); |
| 85 | + }); |
| 86 | + |
| 87 | + // The membership probe reads the replica first and the primary on a miss, so a |
| 88 | + // member whose row has not replicated yet is not bounced. Modelled by giving |
| 89 | + // the controller a replica that cannot see the row and a primary that can. |
| 90 | + postgresTest("allows a member the replica has not caught up on", async ({ prisma }) => { |
| 91 | + const { user, organization } = await createTestOrgProjectWithMember(prisma); |
| 92 | + const blindReplica = { |
| 93 | + ...prisma, |
| 94 | + orgMember: { findFirst: async () => null }, |
| 95 | + user: prisma.user, |
| 96 | + project: prisma.project, |
| 97 | + } as unknown as PrismaClient; |
| 98 | + |
| 99 | + const controller = plugin.create( |
| 100 | + { primary: prisma, replica: blindReplica }, |
| 101 | + { forceFallback: true } |
| 102 | + ); |
| 103 | + const result = await controller.authenticateSession(request, { |
| 104 | + userId: user.id, |
| 105 | + organizationId: organization.id, |
| 106 | + }); |
| 107 | + |
| 108 | + expect(result.ok).toBe(true); |
| 109 | + }); |
| 110 | +}); |
0 commit comments