Adds username validation for socketio event handler as well

This commit is contained in:
Saptak S 2021-11-14 23:28:17 +05:30
parent 2a7c3d6867
commit 6429392a40
No known key found for this signature in database
GPG key ID: 7B7F1772C0C6FCBF
2 changed files with 28 additions and 18 deletions

View file

@ -93,6 +93,8 @@ var updateUsername = function (socket) {
console.log(response);
if (response.success && response.username == username) {
socket.emit('update_username', { username: username });
} else {
addStatusMessage("Failed to updated username.")
}
});
return username;

View file

@ -47,6 +47,13 @@ class ChatModeWeb:
self.define_routes()
def validate_username(self, username):
return (
username
and username not in self.connected_users
and len(username) < 128
)
def define_routes(self):
"""
The web app routes for chatting
@ -78,11 +85,7 @@ class ChatModeWeb:
def update_session_username():
history_id = self.cur_history_id
data = request.get_json()
if (
data.get("username", "")
and data.get("username", "") not in self.connected_users
and len(data.get("username", "")) < 128
):
if self.validate_username(data.get("username", "")):
session["name"] = data.get("username", session.get("name"))
self.web.add_request(
request.path,
@ -141,7 +144,7 @@ class ChatModeWeb:
"""Sent by a client when the user updates their username.
The message is sent to all people in the server."""
current_name = session.get("name")
if message.get("username", ""):
if self.validate_username(message.get("username", "")):
session["name"] = message["username"]
self.connected_users[
self.connected_users.index(current_name)
@ -158,6 +161,11 @@ class ChatModeWeb:
},
broadcast=True,
)
else:
emit(
"status",
{"msg": "Failed to update username."},
)
@self.web.socketio.on("disconnect", namespace="/chat")
def disconnect():