Repository navigation
fix(events): enforce role checks for host and moderator in joinEvent - #2657
prakashchoudhary07 merged 11 commits into
Conversation
|
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configuration
You can disable this status message by setting the Use the checkbox below for a quick retry:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
Summary by CodeRabbit
WalkthroughThe join endpoint now returns HTTP 403 when a user fails the authorization check for a host or moderator role. Integration tests verify both cases and confirm that the endpoint does not request an auth token. ChangesEvent role authorization
Priority: ⬆️ High Estimated code review effort: 2 (Simple) | ~8 minutes Change: Bug fix · Severity of issue fixed: High Merge Risk: ⚪ Minimal · up to
|
| it("should return 403 and no token when a super user without member role requests the moderator role", function (done) { | ||
| service = sinon.stub(EventAPIService.prototype, "get").returns({ data: [event1Data] }); |
There was a problem hiding this comment.
When is a super_user not a member?
Can there be a super user without a member role?
There was a problem hiding this comment.
- super_user and member are separate flags in user.roles, and nothing sets member automatically when someone gets super_user.
- yes
There was a problem hiding this comment.
This needs to be looked at. Need to get the confirmation once. Let's track this separately, please
There was a problem hiding this comment.
Sure, created #2660 to track this. I've kept the test as is for now since it reflects the current behaviour; will update it based on the outcome of the issue.
There was a problem hiding this comment.
Need to check this part: how the event codes are retrieved
REF: #2658
Created an issue for it; can look into it later
| @@ -143,6 +143,10 @@ | |||
| message: "Token generated successfully!", | |||
| }); | |||
There was a problem hiding this comment.
NIT: Also, see if we can simplify this part, it's confusing
There was a problem hiding this comment.
simplified.
|
|
||
| return res.status(403).json({ | ||
| message: "Unauthorized, you don't have permission to join as host or moderator!", | ||
| }); |
There was a problem hiding this comment.
If we want to make a member a host, is that possible currently
There was a problem hiding this comment.
No. Host requires super_user in both the UI and the API.
Updated the PR template |
|
Note Unit test generation is a beta feature. Expect some limitations and changes as we gather feedback and continue to improve it. Generating unit tests... This may take up to 20 minutes. |
|
🤖 Coding Agent task started for unit test generation. |
| return res.status(201).json({ | ||
| token, | ||
| message: "Token generated successfully!", | ||
| if (!hasPermission) { |
There was a problem hiding this comment.
This makes it sound like if no permission, then don't allow token creation.
But later, we do create tokens.
Like we discussed, please clean up that rather long and confusing if block using simplified guard clauses @geekyvaishnavi
There was a problem hiding this comment.
Removed hasPermission and replaced the nested block with guard clauses.
| } | ||
|
|
||
| const token = tokenService.getAuthToken({ ...payload, roomId }); | ||
| const token = tokenService.getAuthToken({ userId, role, roomId }); |
There was a problem hiding this comment.
Can users with other roles get a token?
Can new users get the token?
There was a problem hiding this comment.
No. The validator only accepts host, moderator, guest and maven. Any other role gets a 400 before it reaches the controller.
Not as host or moderator. A new user (first-time signup) has only { archived: false, in_discord: false } and no member or super_user role, so authorizeRoles blocks them on /join-admin. Like anyone else, they can only join as a guest, or as a maven with a valid event code.
There was a problem hiding this comment.
What happens if the role is "guest" ?
There was a problem hiding this comment.
guest skips all the role checks and gets a token directly
There was a problem hiding this comment.
What is the expected behaviour for a guest?
There was a problem hiding this comment.
A guest is the open role, so anyone can join an event as a guest without logging in. That's why there's no check they get a token straight away with role "guest", which only gives them guest-level access in the room.
Issue Ticket Number
Closes RealDevSquad/website-www#1180
Tech Doc Link
doc
Business Doc Link
N/A
Description
POST /events/join-adminwas giving out tokens for roles the user is not allowed to have. A member who is not a super_user could get a host token, and a super_user who is not a member could get a moderator token. The UI blocks both cases, but calling the API directly skipped that check.When the role checks in the host/moderator block of
joinEventfailed, the request fell through to the guest section, which created a token using therolefrom the request body. This PR returns403with no token at the end of that block incontrollers/events.js.Refactored
joinEvent: the token is now created in one place instead of four, the host/moderator checks are grouped under oneisPrivilegedRoleblock, and the maven event code check uses.some().POST /events/joinwithhost/moderatorand no login now returns401instead of400, matching theauthenticatemiddleware andres.boom.unauthorizedused across the app.Checklist
Screenshots
Screenshot
Before the fix: the API gives a member a host token.

After the fix: the same request is rejected.

Test Coverage
Test report
Added 6 integration tests in

test/integration/events.test.jsfor the 403 (wrong role), 401 (not logged in) and maven event code cases. All 32 tests in the file pass.Additional Notes
No UI change is needed, because the UI already enforces these rules in live.js. A super_user who is not a member now gets 403 for the moderator role, which matches the UI. Super users can still join as host.