Skip to content

fix: socket auth + stop trusting client for sender identity (#52)#60

Open
yashrajpurohit7 wants to merge 1 commit into
0rigin-c0de:mainfrom
yashrajpurohit7:fix/socket-auth-and-sender-verification
Open

fix: socket auth + stop trusting client for sender identity (#52)#60
yashrajpurohit7 wants to merge 1 commit into
0rigin-c0de:mainfrom
yashrajpurohit7:fix/socket-auth-and-sender-verification

Conversation

@yashrajpurohit7
Copy link
Copy Markdown

Fixes #52

While going through the socket code I noticed two things that
stood out:

  1. The socket connection had zero authentication anyone could
    connect and start emitting events with no verification at all.

  2. send_message was taking sender_name, sender_tag, sender_pic
    directly from the client payload and forwarding them as-is.
    Basically anyone could put any name/pic and impersonate someone.

What I changed:

  • Added a Socket.IO middleware (io.use) that checks the JWT token
    from the handshake before the connection is established. If the
    token is missing or invalid, the connection is rejected.

  • send_message now ignores the client-provided sender fields and
    instead reads username, tag, profile_pic from the verified token
    stored in socket.data.user

  • get_userid no longer trusts the user_id sent by the client
    uses the verified ID from the token instead

  • Also merged the two separate io.on("connection") blocks into
    one since having them split serves no purpose

Files changed:

  • server/socket/index.js
  • frontend/src/components/socket/Socket.jsx

Note: join_chat membership check (also mentioned in #52) needs
DB queries to verify server membership I can follow up on that
separately if needed.

Tested locally socket connects with token, messages show
correct sender info.

@netlify
Copy link
Copy Markdown

netlify Bot commented May 15, 2026

👷 Deploy request for piperchat01 pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 270a514

Copy link
Copy Markdown
Owner

@0rigin-c0de 0rigin-c0de left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@yashrajpurohit7
Copy link
Copy Markdown
Author

@0rigin-c0de sir merge it if possible

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.

[Security]: Socket events rely on client-provided data without server-side verification

2 participants