简体   繁体   English

Socket.io ID 和用户信息安全

[英]Socket.io ID and user info security

What I want to do at the moment is when user leaves lobby I want to emit the username who left the lobby and then display that info in lobby chatroom.我现在想做的是当用户离开大厅时,我想发出离开大厅的用户名,然后在大厅聊天室中显示该信息。 This is the current emit:这是当前的发射:

  $("#lobby-leave-btn").on('click', () => {
    socket.emit('lobby-left', {
      username: username
    });
  });

The problem is that everyone can set whatever username they want in console, even if I check in serverside if the username exists in DB they can type existing users username who is not in lobby.问题是每个人都可以在控制台中设置他们想要的任何用户名,即使我在服务器端检查数据库中是否存在用户名,他们也可以输入不在大厅中的现有用户用户名。

Is there somehow possible way to do this without passing username ?有没有办法在不传递用户名的情况下做到这一点? More secure way ?更安全的方式? I can get socket.id in serverside but then again I can't get user info by socket id.我可以在服务器端获取socket.id但我又无法通过套接字 id 获取用户信息。 Any help is appreciated.任何帮助表示赞赏。

If you have usernames, then you should have them stored server-side, which I assume you're doing (either in a database, or just stored in memory on the server when the users connect).如果您有用户名,那么您应该将它们存储在服务器端,我假设您正在这样做(在数据库中,或者在用户连接时仅存储在服务器的内存中)。 In that case, as soon as you know who a user is -- that is, as soon as they either sign in (if you have a sign-in system) or connect to the socket -- you should store the username as a property on the socket itself on the server.在这种情况下,一旦您知道用户是谁——也就是说,只要他们登录(如果您有登录系统)或连接到套接字——您就应该将用户名存储为属性在服务器上的套接字本身上。 Then any socket data that comes in will automatically have the username attached to it, since it's inherently connected to that socket.然后任何传入的套接字数据将自动附加用户名,因为它固有地连接到该套接字。

The mantra of web security is that you should never trust user-submitted data if you can help it;网络安全的口头禅是,如果你能帮助它,你永远不应该相信用户提交的数据; so you should never let the user identify themselves in an action without some sort of verification.所以你不应该让用户在没有某种验证的情况下在操作中识别自己。 Hence, verify them once on sign-in or connection, then store their identity on the socket, don't let them tell you who they are whenever they want to make a change.因此,在登录或连接时验证他们一次,然后将他们的身份存储在套接字上,不要让他们在想要进行更改时告诉您他们是谁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM