Skip to content

Commit e54f08d

Browse files
committedMar 9, 2020
[fix] Make WebSocket#{p{i,o}ng,close}() check the data size
Throw an error if the data size is too large to fit in a control frame.
1 parent af4f722 commit e54f08d

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed
 

‎lib/sender.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,13 @@ class Sender {
110110
buf = Buffer.allocUnsafe(2);
111111
buf.writeUInt16BE(code, 0);
112112
} else {
113-
buf = Buffer.allocUnsafe(2 + Buffer.byteLength(data));
113+
const length = Buffer.byteLength(data);
114+
115+
if (length > 123) {
116+
throw new RangeError('The message must not be greater than 123 bytes');
117+
}
118+
119+
buf = Buffer.allocUnsafe(2 + length);
114120
buf.writeUInt16BE(code, 0);
115121
buf.write(data, 2);
116122
}
@@ -154,6 +160,10 @@ class Sender {
154160
ping(data, mask, cb) {
155161
const buf = toBuffer(data);
156162

163+
if (buf.length > 125) {
164+
throw new RangeError('The data size must not be greater than 125 bytes');
165+
}
166+
157167
if (this._deflating) {
158168
this.enqueue([this.doPing, buf, mask, toBuffer.readOnly, cb]);
159169
} else {
@@ -194,6 +204,10 @@ class Sender {
194204
pong(data, mask, cb) {
195205
const buf = toBuffer(data);
196206

207+
if (buf.length > 125) {
208+
throw new RangeError('The data size must not be greater than 125 bytes');
209+
}
210+
197211
if (this._deflating) {
198212
this.enqueue([this.doPong, buf, mask, toBuffer.readOnly, cb]);
199213
} else {

‎test/websocket.test.js

+45
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,21 @@ describe('WebSocket', () => {
878878
});
879879
});
880880
});
881+
882+
it('throws an error if the data size is greater than 125 bytes', (done) => {
883+
const wss = new WebSocket.Server({ port: 0 }, () => {
884+
const ws = new WebSocket(`ws://localhost:${wss.address().port}`);
885+
886+
ws.on('open', () => {
887+
assert.throws(
888+
() => ws.ping(Buffer.alloc(126)),
889+
/^RangeError: The data size must not be greater than 125 bytes$/
890+
);
891+
892+
wss.close(done);
893+
});
894+
});
895+
});
881896
});
882897

883898
describe('#pong', () => {
@@ -1019,6 +1034,21 @@ describe('WebSocket', () => {
10191034
});
10201035
});
10211036
});
1037+
1038+
it('throws an error if the data size is greater than 125 bytes', (done) => {
1039+
const wss = new WebSocket.Server({ port: 0 }, () => {
1040+
const ws = new WebSocket(`ws://localhost:${wss.address().port}`);
1041+
1042+
ws.on('open', () => {
1043+
assert.throws(
1044+
() => ws.pong(Buffer.alloc(126)),
1045+
/^RangeError: The data size must not be greater than 125 bytes$/
1046+
);
1047+
1048+
wss.close(done);
1049+
});
1050+
});
1051+
});
10221052
});
10231053

10241054
describe('#send', () => {
@@ -1429,6 +1459,21 @@ describe('WebSocket', () => {
14291459
});
14301460
});
14311461

1462+
it('throws an error if the message is greater than 123 bytes', (done) => {
1463+
const wss = new WebSocket.Server({ port: 0 }, () => {
1464+
const ws = new WebSocket(`ws://localhost:${wss.address().port}`);
1465+
1466+
ws.on('open', () => {
1467+
assert.throws(
1468+
() => ws.close(1000, 'a'.repeat(124)),
1469+
/^RangeError: The message must not be greater than 123 bytes$/
1470+
);
1471+
1472+
wss.close(done);
1473+
});
1474+
});
1475+
});
1476+
14321477
it('sends the close status code only when necessary', (done) => {
14331478
let sent;
14341479
const wss = new WebSocket.Server({ port: 0 }, () => {

0 commit comments

Comments
 (0)
Please sign in to comment.