fix: scope org-only v1 API key auth#7961
fix: scope org-only v1 API key auth#7961xernobyl wants to merge 5 commits intomainformbricks/formbricks:mainfrom chore/readonly_org_fixformbricks/formbricks:chore/readonly_org_fixCopy head branch name to clipboard
Conversation
WalkthroughThis PR adds organization-only API key support to the Formbricks API authentication system. It introduces a new 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@apps/web/app/api/v1/management/me/lib/api-key-response.test.ts`:
- Around line 35-160: Add a test to document the behavior where
organizationAccess.accessControl.read is true and environmentPermissions.length
=== 1: call buildApiKeyMeResponse with baseAuthentication plus
organizationAccess { accessControl: { read: true, write: false } } and a single
environmentPermission (use environmentPermission("env-id","read")), mock
getEnvironment to return the environment object (include createdAt/updatedAt
dates and projectId), and assert that the response is the legacy
environment-shaped payload (status 200 and JSON matching id, type,
createdAt/updatedAt ISO strings, appSetupCompleted, and project { id, name })
and that getEnvironment was called with "env-id"; place the test alongside the
other buildApiKeyMeResponse tests.
In `@apps/web/app/api/v1/management/me/lib/api-key-response.ts`:
- Around line 21-29: The current branching silently falls through when
hasOrganizationAccess(authentication, OrganizationAccessType.Read) is true and
authentication.environmentPermissions.length === 1, causing inconsistent
legacy-shaped responses; change the logic so that if hasOrganizationAccess(...,
OrganizationAccessType.Read) then always return
Response.json(buildApiKeyMetadataResponse(authentication)) (no nested length
check), otherwise keep the existing environmentPermissions length !== 1 =>
return null check and fall through to the legacy environment-shaped response for
length === 1; update the code paths around hasOrganizationAccess and
buildApiKeyMetadataResponse and add a unit test that constructs an
authentication with hasOrganizationAccess true and environmentPermissions.length
=== 1 asserting the metadata response is returned.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 5beede62-61d0-4727-8408-eb8a59afcc0d
📒 Files selected for processing (14)
apps/web/app/api/v1/auth.test.tsapps/web/app/api/v1/auth.tsapps/web/app/api/v1/management/me/lib/api-key-response.test.tsapps/web/app/api/v1/management/me/lib/api-key-response.tsapps/web/app/api/v1/management/me/route.test.tsapps/web/app/api/v1/management/me/route.tsapps/web/app/api/v1/management/surveys/lib/access.test.tsapps/web/app/api/v1/management/surveys/lib/access.tsapps/web/app/api/v1/management/surveys/route.test.tsapps/web/app/api/v1/management/surveys/route.tsapps/web/app/lib/api/with-api-logging.test.tsapps/web/app/lib/api/with-api-logging.tsapps/web/lib/environment/organization.test.tsapps/web/lib/environment/organization.ts
|
What does this PR do?
Fixes ENG-857
This PR fixes read-only organization API keys returning errors for the scoped v1 GET endpoints:
GET /api/v1/management/meGET /api/v1/management/surveysIt allows organization-only API keys to authenticate only where explicitly supported, instead of broadening v1 API-key behavior globally.
/management/meopts in directly, and/management/surveysopts in through the v1 API wrapper. Other v1 management routes keep the previous environment-permission requirement.How should this be tested?
pnpm --filter @formbricks/web test app/api/v1/auth.test.ts app/lib/api/with-api-logging.test.ts app/api/v1/management/me/route.test.tsManually create or modify an API key so it has:
Verify these endpoints return
200:curl -i -H "x-api-key: YOUR_KEY" http://localhost:3000/api/v1/management/action-classesExpected:
401.Checklist
Required
pnpm buildconsole.logsgit pull origin mainAppreciated