Skip to content

Commit f8100f9

Browse files
author
Tony Kovanen
committed
Added a maximum buffer size to received data from polling. Settable with the maxHttpBufferSize option
1 parent 98f8071 commit f8100f9

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ to a single process.
193193
consider the connection closed (`60000`)
194194
- `pingInterval` (`Number`): how many ms before sending a new ping
195195
packet (`25000`)
196+
- `maxHttpBufferSize` (`Number`): how many bytes or characters a message
197+
can be when polling, before closing the session (to avoid DoS). Default
198+
value is `10E7`.
196199
- `transports` (`<Array> String`): transports to allow connections
197200
to (`['polling', 'websocket', 'flashsocket']`)
198201
- `allowUpgrades` (`Boolean`): whether to allow transport upgrades

Diff for: lib/server.js

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ function Server(opts){
3535
this.pingTimeout = opts.pingTimeout || 60000;
3636
this.pingInterval = opts.pingInterval || 25000;
3737
this.upgradeTimeout = opts.upgradeTimeout || 10000;
38+
this.maxHttpBufferSize = opts.maxHttpBufferSize || 10E7;
3839
this.transports = opts.transports || Object.keys(transports);
3940
this.allowUpgrades = false !== opts.allowUpgrades;
4041
this.cookie = false !== opts.cookie ? (opts.cookie || 'io') : false;
@@ -208,8 +209,13 @@ Server.prototype.handshake = function(transport, req){
208209

209210
debug('handshaking client "%s"', id);
210211

212+
var transportName = transport;
211213
try {
212214
var transport = new transports[transport](req);
215+
if ('polling' == transportName) {
216+
transport.maxHttpBufferSize = this.maxHttpBufferSize;
217+
}
218+
213219
if (req.query && req.query.b64) {
214220
transport.supportsBinary = false;
215221
} else {

Diff for: lib/transports/polling.js

+8
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,18 @@ Polling.prototype.onDataRequest = function (req, res) {
139139
}
140140

141141
function onData (data) {
142+
var contentLength;
142143
if (typeof data == 'string') {
143144
chunks += data;
145+
contentLength = Buffer.byteLength(chunks);
144146
} else {
145147
chunks = Buffer.concat([chunks, data]);
148+
contentLength = chunks.length;
149+
}
150+
151+
if (contentLength > self.maxHttpBufferSize) {
152+
chunks = '';
153+
req.connection.destroy();
146154
}
147155
}
148156

Diff for: test/server.js

+36-1
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,8 @@ describe('server', function () {
792792
});
793793

794794
describe('messages', function () {
795+
this.timeout(5000);
796+
795797
it('should arrive from server to client', function (done) {
796798
var engine = listen({ allowUpgrades: false }, function (port) {
797799
var socket = new eioc.Socket('ws://localhost:%d'.s(port));
@@ -844,6 +846,39 @@ describe('server', function () {
844846
});
845847
});
846848

849+
it('should not be receiving data when getting a message longer than maxHttpBufferSize when polling', function(done) {
850+
var opts = { allowUpgrades: false, transports: ['polling'], maxHttpBufferSize: 5 };
851+
var engine = listen(opts, function (port) {
852+
var socket = new eioc.Socket('ws://localhost:%d'.s(port));
853+
engine.on('connection', function (conn) {
854+
conn.on('message', function(msg) {
855+
console.log(msg);
856+
});
857+
});
858+
socket.on('open', function () {
859+
socket.send('aasdasdakjhasdkjhasdkjhasdkjhasdkjhasdkjhasdkjha');
860+
});
861+
});
862+
setTimeout(done, 1000);
863+
});
864+
865+
it('should receive data when getting a message shorter than maxHttpBufferSize when polling', function(done) {
866+
var opts = { allowUpgrades: false, transports: ['polling'], maxHttpBufferSize: 5 };
867+
var engine = listen(opts, function (port) {
868+
var socket = new eioc.Socket('ws://localhost:%d'.s(port));
869+
engine.on('connection', function (conn) {
870+
conn.on('message', function(msg) {
871+
expect(msg).to.be('a');
872+
done();
873+
});
874+
});
875+
socket.on('open', function () {
876+
socket.send('a');
877+
});
878+
});
879+
});
880+
881+
847882
it('should arrive from server to client (ws)', function (done) {
848883
var opts = { allowUpgrades: false, transports: ['websocket'] };
849884
var engine = listen(opts, function (port) {
@@ -999,7 +1034,7 @@ describe('server', function () {
9991034
var opts = { allowUpgrades: false, transports: ['websocket'] };
10001035
var engine = listen(opts, function(port) {
10011036
var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['websocket'] });
1002-
1037+
10031038
engine.on('connection', function (conn) {
10041039
conn.send(binaryData);
10051040
});

0 commit comments

Comments
 (0)