Skip to content

Commit 29293ed

Browse files
committed
[fix] Prevent allocation of memory through ping frames
1 parent 753937f commit 29293ed

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

lib/Sender.js

+8
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ Sender.prototype.frameAndSend = function(opcode, data, finalFragment, maskData,
155155
if (data && (typeof data.byteLength !== 'undefined' || typeof data.buffer !== 'undefined')) {
156156
data = getArrayBuffer(data);
157157
} else {
158+
//
159+
// If people want to send a number, this would allocate the number in
160+
// bytes as memory size instead of storing the number as buffer value. So
161+
// we need to transform it to string in order to prevent possible
162+
// vulnerabilities / memory attacks.
163+
//
164+
if (typeof data === 'number') data = data.toString();
165+
158166
data = new Buffer(data);
159167
}
160168
}

lib/WebSocket.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ function cleanupWebsocketResources(error) {
918918
this._closeTimer = null;
919919

920920
if (emitClose) {
921-
// If the connection was closed abnormally (with an error), or if
921+
// If the connection was closed abnormally (with an error), or if
922922
// the close control frame was not received then the close code
923923
// must default to 1006.
924924
if (error || !this._closeReceived) {

test/WebSocket.test.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('WebSocket', function() {
4040
done();
4141
}
4242
});
43-
43+
4444
it('should return a new instance if called without new', function(done) {
4545
var ws = WebSocket('ws://localhost:' + port);
4646
ws.should.be.an.instanceOf(WebSocket);
@@ -585,6 +585,23 @@ describe('WebSocket', function() {
585585
});
586586
});
587587

588+
it('can send safely receive numbers as ping payload', function(done) {
589+
server.createServer(++port, function(srv) {
590+
var ws = new WebSocket('ws://localhost:' + port);
591+
592+
ws.on('open', function() {
593+
ws.ping(200);
594+
});
595+
596+
srv.on('ping', function(message) {
597+
assert.equal('200', message);
598+
srv.close();
599+
ws.terminate();
600+
done();
601+
});
602+
});
603+
});
604+
588605
it('with encoded message is successfully transmitted to the server', function(done) {
589606
server.createServer(++port, function(srv) {
590607
var ws = new WebSocket('ws://localhost:' + port);

0 commit comments

Comments
 (0)