OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
fix(api): only organization admins can add projects through onboarding
Carl-Gerhard Lindesvärd35c2a138fb9f
diff --git a/packages/core/src/modules/onboarding/onboarding.rpc.test.ts b/packages/core/src/modules/onboarding/onboarding.rpc.test.tsindex 8090a05d8..4e0746ef2 100644--- a/packages/core/src/modules/onboarding/onboarding.rpc.test.ts+++ b/packages/core/src/modules/onboarding/onboarding.rpc.test.ts@@ -1,8 +1,10 @@-// Only the "is anyone logged in" boundary is exercised here; no database.+// The auth boundaries only; no database.-import { expect, test } from 'bun:test';+import { expect, mock, test } from 'bun:test';import { stubHttpCtx } from '../../../test/rpc-fixtures';import { makeTrpcContext } from '../../rpc/base';+import { TRPCForbiddenError } from '../../rpc/errors';+import type { Services } from '../../services';import type { CookieOptions } from '../../shared/cookie';import { onboardingRouter } from './onboarding.rpc';@@ -46,3 +48,37 @@ test('project rejects an unauthenticated caller before creating anything', async})).rejects.toMatchObject({ code: 'UNAUTHORIZED' });});++test('project refuses an organization member who is not an admin', async () => {+ const requireOrganizationAdmin = mock(() =>+ Promise.reject(+ new TRPCForbiddenError('Only organization admins can create projects')+ )+ );+ const services = {+ auth: {+ getOrganizationAccess: () => Promise.resolve({ role: 'org:member' }),+ requireOrganizationAdmin,+ },+ } as unknown as Services;+ const { ctx } = stubHttpCtx({ services });+ const trpcCtx = await makeTrpcContext(ctx, new Headers(), {+ cookieOptions: COOKIE_OPTIONS,+ });+ const caller = onboardingRouter.createCaller(trpcCtx);++ await expect(+ caller.project({+ organizationId: 'org_1',+ project: 'website',+ domain: null,+ cors: [],+ website: false,+ app: false,+ backend: true,+ })+ ).rejects.toMatchObject({ code: 'FORBIDDEN' });+ expect(requireOrganizationAdmin).toHaveBeenCalledWith(+ expect.objectContaining({ userId: 'user_1', organizationId: 'org_1' })+ );+});diff --git a/packages/core/src/modules/onboarding/onboarding.rpc.ts b/packages/core/src/modules/onboarding/onboarding.rpc.tsindex 999ca01a3..f4fb45393 100644--- a/packages/core/src/modules/onboarding/onboarding.rpc.ts+++ b/packages/core/src/modules/onboarding/onboarding.rpc.ts@@ -1,19 +1,14 @@// `protectedProcedure` runs `enforceUserIsAuthed` + `enforceAccess` BEFORE the-// input parser. The explicit checks in the handlers below stay: `enforceAccess`-// only sees a TOP-LEVEL `projectId` / `organizationId`, so anything resolved-// from another id needs its own.-//-// `project`'s input can carry an existing `organizationId`, which-// `enforceAccess` gates on membership (not admin — joining an org you already-// belong to needs no more than that); this router repeats the check explicitly-// through the organization module's own lookup.+// input parser. `enforceAccess` gates a top-level `organizationId` on+// membership only, but adding a project and a write client to an existing+// organization is what `project.create` and `client.create` reserve for+// organization admins, so this router requires the same.import {createTRPCRouter,protectedProcedure,publicProcedure,} from '../../rpc/base';-import { TRPCForbiddenError } from '../../rpc/errors';import { zOnboardingProject } from './onboarding.constants';import {canSkipOnboarding,@@ -31,18 +26,11 @@ export const onboardingRouter = createTRPCRouter({const userId = ctx.session.userId;if (input.organizationId) {- // The auth service owns every access lookup; this procedure is- // protected but the organization is an input, so the builder cannot- // decide it.- const access = await ctx.services.auth.getOrganizationAccess({+ await ctx.services.auth.requireOrganizationAdmin({userId,organizationId: input.organizationId,+ message: 'Only organization admins can create projects',});- if (!access) {- throw new TRPCForbiddenError(- 'You do not have access to this organization'- );- }}return createOnboardingProject(ctx, input, userId);