socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

Connect and disconnect event can be fired many times by send packet from client.

Open toro-ponz opened this issue 4 years ago • 1 comments

Describe the bug

Every time send CONNECT(40) socket.io message by client, server fires connect (and disconnect) event. Use this, malicious user can confuse calculation to socket.io server by send CONNECT packet. I think that those events must fire just once on one connection.

In addition, reproduced similarly when EIO4 client connect to EIO3 server. Because EIO4 client send CONNECT, EIO3 server received it and fires connect event on duplicate.

To Reproduce

Socket.IO Client lib code.

patches this code.

      key: "onopen",
      value: function onopen() {
        var _this3 = this;

        if (typeof this.auth == "function") {
          this.auth(function (data) {
            _this3.packet({
              type: PacketType.CONNECT,
              data: data
            });
          });
        } else {
          this.packet({
            type: PacketType.CONNECT,
            data: this.auth
          });
+         this.packet({
+           type: PacketType.CONNECT,
+           data: this.auth
+         });
        }
      }

Socket.IO server version: <=4.5.1

Server code
import { Server } from "socket.io";

const io = new Server(3000, {});

io.on("connection", (socket) => {
  console.log(`connect ${socket.id}`);

  socket.on("disconnect", () => {
    console.log(`disconnect ${socket.id}`);
  });
});

Socket.IO client version: <=4.5.1

Client code
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <!-- link to patched socket.io client -->
    <script src="./socket.io.js"></script>

    <script>
        const socket = io("http://localhost:3000", {
            transports: ["websocket"],
        });
    </script>
</body>
</html>

On v3-v4 server behavior

Server fires many times connect and disconnect events. And changes sid each event.

connect RiHh72AeSqX4mT4RAAAE
connect FhJsZUhKFj4jVbI9AAAF

## close connection
disconnect RiHh72AeSqX4mT4RAAAE
disconnect FhJsZUhKFj4jVbI9AAAF

On v1-v2 server behavior

Server fires many times connect event same sid. But disconnect event are fired just once.

connect KVDcpM5rQibe4Ak4AAAJ
connect KVDcpM5rQibe4Ak4AAAJ

## close connection
disconnect KVDcpM5rQibe4Ak4AAAJ

Expected behavior

Connect and disconnect event are fires just once each connection.

Platform:

  • NodeJS Server x any client.

Additional context

If it is not bugs, could you tell me avoid this probrem.
Do I need to implements that assume connection event fires many times?

toro-ponz avatar Jul 04 '22 03:07 toro-ponz

This should be fixed by https://github.com/socketio/socket.io/commit/0b35dc77c07bfcf02c28643432affffbfc411038, included in version 4.5.0.

Thanks for the heads-up! :trophy:

darrachequesne avatar Oct 14 '22 08:10 darrachequesne