Socket io using io.sockets.emit



我是Socket io世界的新手,想知道这是否存在安全问题:

我也在使用Coffeescript。

服务器。

io.sockets.emit('UserInfo', {player1: AllData1, player2: AllData2}) 
AllData1基本上是玩家

1的敏感信息,AllData2是玩家2的敏感信息。

客户。

 myName = 'snugglePuff'
 socket.on('UserInfo', (data) ->
        if data.player1.name = myName
          alert(data.player1.secret)
      )

所以我的问题是:看到服务器正在广播到连接的每个套接字,"player2"会以某种方式使用他们的浏览器看到data.player1.secret吗?

是的,这是一个巨大的安全问题。

每个客户都可以看到您广播的任何内容。对于用户来说,编辑其页面上的脚本并抓取广播以获取诸如此类的额外数据是微不足道的。

如果您必须发送敏感信息,请确保它仅发送给其所有者。或者,不要发送任何敏感内容,并研究将所有敏感内容保留在服务器端的方法(例如,使用安全随机生成的ID来识别每个用户的会话)。

我们有很多方法可以发送给客户,

所以在你的代码中,玩家2可以看到玩家1.secret。

// sending to sender-client only
socket.emit('message', "this is a test");
// sending to all clients, include sender
io.emit('message', "this is a test");
// sending to all clients except sender
socket.broadcast.emit('message', "this is a test");
// sending to all clients in 'game' room(channel) except sender
socket.broadcast.to('game').emit('message', 'nice game');
// sending to all clients in 'game' room(channel), include sender
io.in('game').emit('message', 'cool game');
// sending to sender client, only if they are in 'game' room(channel)
socket.to('game').emit('message', 'enjoy the game');
// sending to all clients in namespace 'myNamespace', include sender
io.of('myNamespace').emit('message', 'gg');
// sending to individual socketid
socket.broadcast.to(socketid).emit('message', 'for your eyes only');

最新更新