Skip to content

Bug: sendMessage accepts username from request body — authenticated users can impersonate any teammate in chat #160

@anshul23102

Description

@anshul23102

Description

In backend/controllers/chat.controller.js, the sendMessage handler reads the username field from the request body and stores it in Supabase without any server-side identity verification:

const { text, username, image, audio } = req.body;
const { data, error } = await supabase
    .from('messages')
    .insert([{ text, username, image, audio, status: 'sent' }])
    .select();

Although the route requires authenticateUser, the authenticated user's identity is never applied to the stored message. Any authenticated user can provide username: "other_person" in the request body to post chat messages under a different user's name.

To Reproduce

  1. Authenticate as User A.
  2. Send POST /api/messages with { "text": "Hello team", "username": "UserB" }.
  3. The message is broadcast and stored as if UserB sent it.

Expected Behavior

The username should come from the authenticated session, not the request body. If the auth middleware attaches req.user:

const username = req.user?.username || req.user?.email || 'Anonymous';

Actual Behavior

Any authenticated user can send messages attributed to any arbitrary username.

Desktop

  • Backend: Node.js / Express / Supabase
  • File: backend/controllers/chat.controller.js, function: sendMessage

Additional context

Expected NSOC points: level2 (security - identity impersonation)

Labels: bug, NSoC'26, level2

Checklist:

  • Searched existing issues - not a duplicate
  • Read CONTRIBUTING.md and NSoC rules
  • No AI/Claude mentions
  • No em dashes or double hyphens
  • Repository verified as NSOC

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions