Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: websockets/ws
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 7.2.2
Choose a base ref
...
head repository: websockets/ws
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 7.2.3
Choose a head ref
  • 2 commits
  • 3 files changed
  • 1 contributor

Commits on Mar 9, 2020

  1. [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.
    lpinca committed Mar 9, 2020

    Verified

    This commit was signed with the committer’s verified signature.
    Copy the full SHA
    e54f08d View commit details
  2. [dist] 7.2.3

    lpinca committed Mar 9, 2020
    Copy the full SHA
    5fcdc42 View commit details
Showing with 61 additions and 2 deletions.
  1. +15 −1 lib/sender.js
  2. +1 −1 package.json
  3. +45 −0 test/websocket.test.js
16 changes: 15 additions & 1 deletion lib/sender.js
Original file line number Diff line number Diff line change
@@ -110,7 +110,13 @@ class Sender {
buf = Buffer.allocUnsafe(2);
buf.writeUInt16BE(code, 0);
} else {
buf = Buffer.allocUnsafe(2 + Buffer.byteLength(data));
const length = Buffer.byteLength(data);

if (length > 123) {
throw new RangeError('The message must not be greater than 123 bytes');
}

buf = Buffer.allocUnsafe(2 + length);
buf.writeUInt16BE(code, 0);
buf.write(data, 2);
}
@@ -154,6 +160,10 @@ class Sender {
ping(data, mask, cb) {
const buf = toBuffer(data);

if (buf.length > 125) {
throw new RangeError('The data size must not be greater than 125 bytes');
}

if (this._deflating) {
this.enqueue([this.doPing, buf, mask, toBuffer.readOnly, cb]);
} else {
@@ -194,6 +204,10 @@ class Sender {
pong(data, mask, cb) {
const buf = toBuffer(data);

if (buf.length > 125) {
throw new RangeError('The data size must not be greater than 125 bytes');
}

if (this._deflating) {
this.enqueue([this.doPong, buf, mask, toBuffer.readOnly, cb]);
} else {
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ws",
"version": "7.2.2",
"version": "7.2.3",
"description": "Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js",
"keywords": [
"HyBi",
45 changes: 45 additions & 0 deletions test/websocket.test.js
Original file line number Diff line number Diff line change
@@ -878,6 +878,21 @@ describe('WebSocket', () => {
});
});
});

it('throws an error if the data size is greater than 125 bytes', (done) => {
const wss = new WebSocket.Server({ port: 0 }, () => {
const ws = new WebSocket(`ws://localhost:${wss.address().port}`);

ws.on('open', () => {
assert.throws(
() => ws.ping(Buffer.alloc(126)),
/^RangeError: The data size must not be greater than 125 bytes$/
);

wss.close(done);
});
});
});
});

describe('#pong', () => {
@@ -1019,6 +1034,21 @@ describe('WebSocket', () => {
});
});
});

it('throws an error if the data size is greater than 125 bytes', (done) => {
const wss = new WebSocket.Server({ port: 0 }, () => {
const ws = new WebSocket(`ws://localhost:${wss.address().port}`);

ws.on('open', () => {
assert.throws(
() => ws.pong(Buffer.alloc(126)),
/^RangeError: The data size must not be greater than 125 bytes$/
);

wss.close(done);
});
});
});
});

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

it('throws an error if the message is greater than 123 bytes', (done) => {
const wss = new WebSocket.Server({ port: 0 }, () => {
const ws = new WebSocket(`ws://localhost:${wss.address().port}`);

ws.on('open', () => {
assert.throws(
() => ws.close(1000, 'a'.repeat(124)),
/^RangeError: The message must not be greater than 123 bytes$/
);

wss.close(done);
});
});
});

it('sends the close status code only when necessary', (done) => {
let sent;
const wss = new WebSocket.Server({ port: 0 }, () => {