fix: Prevent admin role self-assignment via registration endpoint#1950
Open
fennhelloworld wants to merge 2 commits into
Open
fix: Prevent admin role self-assignment via registration endpoint#1950fennhelloworld wants to merge 2 commits into
fennhelloworld wants to merge 2 commits into
Conversation
…cureBananaLabs#1823) - Remove 'role' field from registerSchema to prevent privilege escalation - Add .strict() to registerSchema to reject any unexpected fields - Default all new registrations to 'client' role in authService - Add ZodError handling in authController and error middleware - Add tests proving role self-assignment is blocked
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.
Fix for #1823: Admin role self-assignment via registration endpoint
Vulnerability Description
The registration endpoint (
POST /api/auth/register) accepted arolefield in the request body, allowing any user to self-assign privileged roles including"admin". The vulnerable code was inapps/api/src/validators/auth.js:An attacker could simply send:
And the service would create an account with
role: "admin"and issue a JWT token with admin claims, granting full administrative access.Fix Summary
Defense in depth — three layers of protection:
Schema-level (validators/auth.js): Removed
rolefromregisterSchemaand added.strict()to reject any unexpected fields. Sending"role"now results in a validation error (HTTP 400).Service-level (services/authService.js):
registerUser()no longer readspayload.role. All new registrations are hardcoded to"client"via aDEFAULT_ROLEconstant. Even if schema validation were bypassed, the service layer would not use a client-supplied role.Error handling (controllers/authController.js, middleware/errorHandler.js): Added proper
ZodErrorcatch blocks in the controller and error middleware so validation errors return clean 400 responses instead of unhandled rejections or 500s.Files Changed
apps/api/src/validators/auth.jsrolefield, added.strict()apps/api/src/services/authService.jsDEFAULT_ROLE = "client", no longer readspayload.roleapps/api/src/controllers/authController.jsapps/api/src/middleware/errorHandler.jsapps/api/src/tests/register.role-prevention.test.jsTest Results
Closes #1823