fix(api): pass explicit permission:undefined to better-auth hasPermis…#2751
Merged
Conversation
…sion
Every cookie-authenticated request was 403'ing with "Unable to verify
permissions" because better-auth 1.4.x's `hasPermission` body schema
is a discriminated union that requires both keys present, with the
unused side set to an explicit `undefined`. zod 4 (which better-auth's
plugin uses internally) rejects "key is absent" — only "key is
undefined" passes the `z.undefined()` branch:
z.union([
z.object({ permission: z.record(...), permissions: z.undefined() }),
z.object({ permission: z.undefined(), permissions: z.record(...) }),
])
Without `permission: undefined` in the body, the schema rejects every
request with "[body] Invalid input"; the catch in canActivate turns
that into a generic 403 and customers can't load the tasks page,
findings, or any other endpoint protected by PermissionGuard.
API key requests aren't affected because they go through the
scope-checking path earlier in the guard, which is why this only
showed up for cookie sessions on customer accounts.
Test pins the body shape so a future refactor that drops the field
gets caught.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
|
🎉 This PR is included in version 3.43.1 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…sion
Every cookie-authenticated request was 403'ing with "Unable to verify permissions" because better-auth 1.4.x's
hasPermissionbody schema is a discriminated union that requires both keys present, with the unused side set to an explicitundefined. zod 4 (which better-auth's plugin uses internally) rejects "key is absent" — only "key is undefined" passes thez.undefined()branch:z.union([
z.object({ permission: z.record(...), permissions: z.undefined() }),
z.object({ permission: z.undefined(), permissions: z.record(...) }),
])
Without
permission: undefinedin the body, the schema rejects every request with "[body] Invalid input"; the catch in canActivate turns that into a generic 403 and customers can't load the tasks page, findings, or any other endpoint protected by PermissionGuard.API key requests aren't affected because they go through the scope-checking path earlier in the guard, which is why this only showed up for cookie sessions on customer accounts.
Test pins the body shape so a future refactor that drops the field gets caught.
What does this PR do?
Visual Demo (For contributors especially)
A visual demonstration is strongly recommended, for both the original and new change (video / image - any one).
Video Demo (if applicable):
Image Demo (if applicable):
Mandatory Tasks (DO NOT REMOVE)
How should this be tested?
Checklist
Summary by cubic
Fix 403s for cookie-authenticated requests by sending
permission: undefinedalongsidepermissionstobetter-authhasPermission. This satisfies thezodunion schema and restores all PermissionGuard-protected endpoints; API key flows were unaffected.permission: undefinedin thehasPermissionbody to matchbetter-auth1.4.x schema and avoid “[body] Invalid input” 403s.Written for commit 832afdb. Summary will update on new commits.