Skip to content

feat(auth): add membership and role checks to server mutation routes#89

Open
varun29sharma wants to merge 4 commits into
0rigin-c0de:mainfrom
varun29sharma:feat/server-role-auth-issue-19
Open

feat(auth): add membership and role checks to server mutation routes#89
varun29sharma wants to merge 4 commits into
0rigin-c0de:mainfrom
varun29sharma:feat/server-role-auth-issue-19

Conversation

@varun29sharma
Copy link
Copy Markdown
Contributor

Summary

Closes #19

Adds authentication, server membership verification, and role-based access
control to the three unprotected mutation routes in server/routes/servers.js.
Previously any anonymous HTTP request could create channels, categories, or
delete a server entirely.

Changes

New file: server/middleware/verifyServerRole.js

A three-layer guard middleware that runs in sequence:

  1. Verifies JWT from x-auth-token header → 401 if missing or invalid
  2. Calls existing checkServerInUser() to confirm user belongs to the server → 403 if not a member
  3. Checks server_role on the User document is owner or admin403 if regular member

Attaches req.user and req.serverMembership for downstream use.

Modified: server/routes/servers.js

Route Before After
POST /add_new_channel No auth at all validate()verifyServerRole() → handler
POST /add_new_category No auth at all validate()verifyServerRole() → handler
POST /delete_server No auth at all validate()verifyServerRole() → handler

Reuses the existing checkServerInUser() from serverService.js — the same
pattern already used correctly in /server_info.
Socket.IO server_updated events are completely unchanged.

Test evidence

All tests run with curl against local instance (port 2000):

  • POST /add_new_channel with no token → 401
  • POST /add_new_channel with fake token → 401
  • POST /add_new_channel with valid token but wrong server → 403
  • POST /add_new_category with owner token + real server → 200

Compatibility

All existing happy-path response shapes unchanged. No frontend code modified.
validate() middleware from PR #82 runs first, verifyServerRole() second —
the two middlewares compose cleanly.So everything is intact and working. : )

@netlify
Copy link
Copy Markdown

netlify Bot commented May 17, 2026

👷 Deploy request for piperchat01 pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 0dad0ea

@varun29sharma
Copy link
Copy Markdown
Contributor Author

Note for reviewer: @0rigin-c0de server/middleware/validate.js appears in this diff
because PR #82 (Issue #18) hasn't been merged into main yet. Once #82
merges, this file will no longer show as a change here. Both files are
intentional — validate.js runs first, verifyServerRole.js second.
Thankyou

- Add server/middleware/verifyServerRole.js with 3-layer guard:
  401 for missing/invalid JWT
  403 for non-members of the target server
  403 for members without owner/admin role
- Apply verifyServerRole to /add_new_channel, /add_new_category, /delete_server
- Reuses existing checkServerInUser() from serverService.js
- validate() from Issue 18 runs first, verifyServerRole second
- Socket.IO server_updated events completely unchanged

Closes 0rigin-c0de#19
@varun29sharma varun29sharma force-pushed the feat/server-role-auth-issue-19 branch from 596541d to 9a3af9e Compare May 17, 2026 08:01
@vercel
Copy link
Copy Markdown

vercel Bot commented May 22, 2026

@varun29sharma is attempting to deploy a commit to the Sunil Kumar's projects Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prevent unauthorized channel and category changes by non-members

1 participant