fix: socket auth + stop trusting client for sender identity (#52)#60
Open
yashrajpurohit7 wants to merge 1 commit into
Open
Conversation
👷 Deploy request for piperchat01 pending review.Visit the deploys page to approve it
|
Author
|
@0rigin-c0de sir merge it if possible |
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.
Fixes #52
While going through the socket code I noticed two things that
stood out:
The socket connection had zero authentication anyone could
connect and start emitting events with no verification at all.
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:
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.