Skip to content

Commit 2875d2c

Browse files
committedOct 13, 2020
feat: do not reuse the Engine.IO id
In previous versions, the Socket#id attribute was equal (or derived, for a non-default namespace) to the underlying Engine.IO id, which is used as a mean to authenticate the user throughout the Engine.IO session and thus is sensitive information that should be kept secret. The problem with reusing the Engine.IO id is that users could be tempted to transmit this id to other clients, in order to implement private messaging for example. So we'll now generate a new random id for each new socket. Please note that this id will now be different from the one found in the query parameters of the HTTP requests.
1 parent 3289f7e commit 2875d2c

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed
 

‎dist/socket.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const socket_io_parser_1 = require("socket.io-parser");
99
const has_binary2_1 = __importDefault(require("has-binary2"));
1010
const url_1 = __importDefault(require("url"));
1111
const debug_1 = __importDefault(require("debug"));
12+
const base64id_1 = __importDefault(require("base64id"));
1213
const debug = debug_1.default("socket.io:socket");
1314
/**
1415
* Blacklisted events.
@@ -40,7 +41,7 @@ class Socket extends events_1.EventEmitter {
4041
this._rooms = new Set();
4142
this.server = nsp.server;
4243
this.adapter = this.nsp.adapter;
43-
this.id = nsp.name !== "/" ? nsp.name + "#" + client.id : client.id;
44+
this.id = base64id_1.default.generateId(); // don't reuse the Engine.IO id because it's sensitive information
4445
this.connected = true;
4546
this.disconnected = false;
4647
this.handshake = this.buildHandshake(auth);
@@ -206,7 +207,7 @@ class Socket extends events_1.EventEmitter {
206207
debug("socket connected - writing packet");
207208
this.nsp.connected.set(this.id, this);
208209
this.join(this.id);
209-
this.packet({ type: socket_io_parser_1.PacketType.CONNECT });
210+
this.packet({ type: socket_io_parser_1.PacketType.CONNECT, data: { sid: this.id } });
210211
}
211212
/**
212213
* Called with each packet. Called by `Client`.

‎lib/socket.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import debugModule from "debug";
66
import { Client, Namespace, Server } from "./index";
77
import { IncomingMessage } from "http";
88
import { Adapter, BroadcastFlags, Room, SocketId } from "socket.io-adapter";
9+
import base64id from "base64id";
910

1011
const debug = debugModule("socket.io:socket");
1112

@@ -100,7 +101,7 @@ export class Socket extends EventEmitter {
100101
super();
101102
this.server = nsp.server;
102103
this.adapter = this.nsp.adapter;
103-
this.id = nsp.name !== "/" ? nsp.name + "#" + client.id : client.id;
104+
this.id = base64id.generateId(); // don't reuse the Engine.IO id because it's sensitive information
104105
this.connected = true;
105106
this.disconnected = false;
106107
this.handshake = this.buildHandshake(auth);
@@ -288,7 +289,7 @@ export class Socket extends EventEmitter {
288289
debug("socket connected - writing packet");
289290
this.nsp.connected.set(this.id, this);
290291
this.join(this.id);
291-
this.packet({ type: PacketType.CONNECT });
292+
this.packet({ type: PacketType.CONNECT, data: { sid: this.id } });
292293
}
293294

294295
/**

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"format:fix": "prettier --write 'lib/**/*.ts' 'test/**/*.ts'"
2828
},
2929
"dependencies": {
30+
"base64id": "~2.0.0",
3031
"debug": "~4.1.0",
3132
"engine.io": "~4.0.0",
3233
"has-binary2": "~1.0.2",

0 commit comments

Comments
 (0)
Please sign in to comment.