|
| 1 | +/** |
| 2 | + * The environments route is the agent's cross-project sweep: an org-wide user-actor token lists |
| 3 | + * any project of its own organization, and nothing outside it. Driven through the real loader |
| 4 | + * against a real database, because membership — not the claim — is the tenant floor. |
| 5 | + */ |
| 6 | + |
| 7 | +import { postgresTest } from "@internal/testcontainers"; |
| 8 | +import type { PrismaClient } from "@trigger.dev/database"; |
| 9 | +import { buildJwtAbility, signUserActorToken } from "@trigger.dev/rbac"; |
| 10 | +import { expect, vi } from "vitest"; |
| 11 | + |
| 12 | +const SESSION_SECRET = "test-session-secret"; |
| 13 | + |
| 14 | +const ctx = vi.hoisted(() => ({ prisma: undefined as unknown as PrismaClient })); |
| 15 | + |
| 16 | +vi.mock("~/db.server", () => { |
| 17 | + const proxy = new Proxy( |
| 18 | + {}, |
| 19 | + { get: (_target, prop) => (ctx.prisma as unknown as Record<string, unknown>)[prop as string] } |
| 20 | + ); |
| 21 | + return { prisma: proxy, $replica: proxy, sqlDatabaseSchema: undefined }; |
| 22 | +}); |
| 23 | + |
| 24 | +const mocks = vi.hoisted(() => ({ authenticateUserActor: vi.fn() })); |
| 25 | + |
| 26 | +vi.mock("~/services/rbac.server", () => ({ |
| 27 | + rbac: { authenticateUserActor: mocks.authenticateUserActor, authenticatePat: vi.fn() }, |
| 28 | +})); |
| 29 | +vi.mock("~/env.server", () => ({ env: { SESSION_SECRET: "test-session-secret" } })); |
| 30 | +vi.mock("~/services/personalAccessToken.server", async () => { |
| 31 | + const { verifyUserActorToken } = await import("@trigger.dev/rbac"); |
| 32 | + return { |
| 33 | + updateLastAccessedAtIfStale: vi.fn(), |
| 34 | + resolveAndRecheckUserActorClaims: async (claims: unknown, bearer: string) => |
| 35 | + claims ?? (await verifyUserActorToken(SESSION_SECRET, bearer)), |
| 36 | + }; |
| 37 | +}); |
| 38 | +vi.mock("~/services/authTelemetry.server", () => ({ |
| 39 | + authenticateBearerWithTelemetry: vi.fn(), |
| 40 | +})); |
| 41 | +vi.mock("~/services/logger.server", () => ({ |
| 42 | + logger: { debug: vi.fn(), error: vi.fn(), warn: vi.fn(), info: vi.fn() }, |
| 43 | +})); |
| 44 | +vi.mock("~/services/tenantContext.server", () => ({ |
| 45 | + tenantContext: { enrich: vi.fn() }, |
| 46 | + tenantContextFromAuthEnvironment: vi.fn(), |
| 47 | +})); |
| 48 | +vi.mock("~/v3/services/worker/workerGroupTokenService.server", () => ({ |
| 49 | + WorkerGroupTokenService: class {}, |
| 50 | +})); |
| 51 | +vi.mock("~/v3/services/common.server", () => ({ |
| 52 | + ServiceValidationError: class extends Error {}, |
| 53 | +})); |
| 54 | +vi.mock("@internal/run-engine", () => ({ |
| 55 | + EngineServiceValidationError: class extends Error {}, |
| 56 | +})); |
| 57 | + |
| 58 | +const { loader } = await import("~/routes/api.v1.projects.$projectRef.environments"); |
| 59 | + |
| 60 | +function suffix() { |
| 61 | + return Math.random().toString(36).slice(2, 10); |
| 62 | +} |
| 63 | + |
| 64 | +/** An org with one project, prod/staging/dev environments, a member and an outsider. */ |
| 65 | +async function seedOrg(prisma: PrismaClient) { |
| 66 | + const slug = `orgenvs_${suffix()}`; |
| 67 | + const member = await prisma.user.create({ |
| 68 | + data: { email: `${slug}-member@example.com`, authenticationMethod: "MAGIC_LINK" }, |
| 69 | + }); |
| 70 | + const outsider = await prisma.user.create({ |
| 71 | + data: { email: `${slug}-outsider@example.com`, authenticationMethod: "MAGIC_LINK" }, |
| 72 | + }); |
| 73 | + const organization = await prisma.organization.create({ data: { title: slug, slug } }); |
| 74 | + const orgMember = await prisma.orgMember.create({ |
| 75 | + data: { organizationId: organization.id, userId: member.id, role: "ADMIN" }, |
| 76 | + }); |
| 77 | + const project = await prisma.project.create({ |
| 78 | + data: { name: slug, slug, organizationId: organization.id, externalRef: `proj_${slug}` }, |
| 79 | + }); |
| 80 | + const environmentFor = (envSlug: string, type: "PRODUCTION" | "STAGING" | "DEVELOPMENT") => |
| 81 | + prisma.runtimeEnvironment.create({ |
| 82 | + data: { |
| 83 | + slug: envSlug, |
| 84 | + type, |
| 85 | + projectId: project.id, |
| 86 | + organizationId: organization.id, |
| 87 | + apiKey: `tr_${envSlug}_${slug}`, |
| 88 | + pkApiKey: `pk_${envSlug}_${slug}`, |
| 89 | + shortcode: `${envSlug}${suffix()}`, |
| 90 | + ...(type === "DEVELOPMENT" ? { orgMemberId: orgMember.id } : {}), |
| 91 | + }, |
| 92 | + }); |
| 93 | + |
| 94 | + return { |
| 95 | + member, |
| 96 | + outsider, |
| 97 | + organization, |
| 98 | + project, |
| 99 | + prod: await environmentFor("prod", "PRODUCTION"), |
| 100 | + staging: await environmentFor("stg", "STAGING"), |
| 101 | + dev: await environmentFor("dev", "DEVELOPMENT"), |
| 102 | + }; |
| 103 | +} |
| 104 | + |
| 105 | +async function callLoader(opts: { |
| 106 | + projectRef: string; |
| 107 | + userId: string; |
| 108 | + organizationId?: string; |
| 109 | + environmentId?: string; |
| 110 | +}): Promise<{ status: number; body: any }> { |
| 111 | + const claims = { |
| 112 | + userId: opts.userId, |
| 113 | + client: "dashboard-agent", |
| 114 | + ...(opts.organizationId ? { organizationId: opts.organizationId } : {}), |
| 115 | + ...(opts.environmentId ? { environmentId: opts.environmentId } : {}), |
| 116 | + }; |
| 117 | + const token = await signUserActorToken(SESSION_SECRET, { |
| 118 | + ...claims, |
| 119 | + cap: ["read:environments"], |
| 120 | + }); |
| 121 | + mocks.authenticateUserActor.mockImplementation(async () => ({ |
| 122 | + ok: true, |
| 123 | + userId: opts.userId, |
| 124 | + claims, |
| 125 | + subject: { type: "userActor", userId: opts.userId }, |
| 126 | + ability: buildJwtAbility(["read:environments"]), |
| 127 | + })); |
| 128 | + |
| 129 | + const response = await loader({ |
| 130 | + request: new Request( |
| 131 | + `https://api.trigger.dev/api/v1/projects/${opts.projectRef}/environments`, |
| 132 | + { headers: { Authorization: `Bearer ${token}` } } |
| 133 | + ), |
| 134 | + params: { projectRef: opts.projectRef }, |
| 135 | + context: {}, |
| 136 | + } as any); |
| 137 | + |
| 138 | + return { status: response.status, body: await response.json() }; |
| 139 | +} |
| 140 | + |
| 141 | +postgresTest( |
| 142 | + "org-wide user-actor token lists a sibling project's environments", |
| 143 | + async ({ prisma }) => { |
| 144 | + ctx.prisma = prisma; |
| 145 | + const orgA = await seedOrg(prisma); |
| 146 | + const orgB = await seedOrg(prisma); |
| 147 | + |
| 148 | + // Its own org's project: every environment, dev included — the whole point of the sweep. |
| 149 | + const own = await callLoader({ |
| 150 | + projectRef: orgA.project.externalRef, |
| 151 | + userId: orgA.member.id, |
| 152 | + organizationId: orgA.organization.id, |
| 153 | + }); |
| 154 | + expect(own.status).toBe(200); |
| 155 | + expect(own.body.map((env: any) => env.slug).sort()).toEqual(["dev", "prod", "stg"]); |
| 156 | + |
| 157 | + // A project outside the claimed organization is refused on the claim alone. |
| 158 | + const foreign = await callLoader({ |
| 159 | + projectRef: orgB.project.externalRef, |
| 160 | + userId: orgA.member.id, |
| 161 | + organizationId: orgA.organization.id, |
| 162 | + }); |
| 163 | + expect(foreign.status).toBe(403); |
| 164 | + expect(foreign.body.code).toBe("forbidden_environment"); |
| 165 | + |
| 166 | + // A claim naming the right org still needs membership of it. |
| 167 | + const outsider = await callLoader({ |
| 168 | + projectRef: orgA.project.externalRef, |
| 169 | + userId: orgB.outsider.id, |
| 170 | + organizationId: orgA.organization.id, |
| 171 | + }); |
| 172 | + expect(outsider.status).toBe(404); |
| 173 | + |
| 174 | + // The environment-claim path is unchanged: exactly its own environment, and nothing elsewhere. |
| 175 | + const scoped = await callLoader({ |
| 176 | + projectRef: orgA.project.externalRef, |
| 177 | + userId: orgA.member.id, |
| 178 | + environmentId: orgA.staging.id, |
| 179 | + }); |
| 180 | + expect(scoped.status).toBe(200); |
| 181 | + expect(scoped.body.map((env: any) => env.slug)).toEqual(["stg"]); |
| 182 | + |
| 183 | + const scopedForeign = await callLoader({ |
| 184 | + projectRef: orgB.project.externalRef, |
| 185 | + userId: orgA.member.id, |
| 186 | + environmentId: orgA.staging.id, |
| 187 | + }); |
| 188 | + expect(scopedForeign.status).toBe(403); |
| 189 | + expect(scopedForeign.body.code).toBe("forbidden_environment"); |
| 190 | + } |
| 191 | +); |
0 commit comments