Skip to content
This repository has been archived by the owner on Mar 8, 2020. It is now read-only.

Commit

Permalink
[0.19.x] Rest server can be terminated by client (#4531)
Browse files Browse the repository at this point in the history
A client listening on a web socket can terminate the rest server
if it is badly behaved and doesn’t cleanly disconnect

Signed-off-by: Dave Kelsey <[email protected]>
  • Loading branch information
Dave Kelsey authored Dec 6, 2018
1 parent 84e89bd commit dab2b14
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/composer-rest-server/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ module.exports = function (composer) {
clientTracking: true
});

// Add a dummy error handler for a ws connection so bad websocket client
// doesn't kill the rest server
wss.on('connection', (ws) => {
ws.on('error', () => {
// do nothing
});
});

// Add a broadcast method that sends data to all connected clients.
wss.broadcast = (data) => {
wss.clients.forEach((client) => {
Expand Down
22 changes: 22 additions & 0 deletions packages/composer-rest-server/test/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const IdCard = require('composer-common').IdCard;
const path = require('path');
const server = require('../../server/server');
const WebSocket = require('ws');
const EventEmitter = require('events');

const chai = require('chai');
const should = chai.should();
Expand Down Expand Up @@ -341,4 +342,25 @@ describe('server', () => {
});
});

it('should register an error handler on connection to a websocket and it shoud no nothing when fired', () => {
composerConfig.websockets = true;
const stubWS = new EventEmitter();
const wsOnSpy = sinon.spy(stubWS, 'on');


return server(composerConfig)
.then((result) => {
result.app.should.exist;
result.server.should.exist;
const wss = result.app.get('wss');
wss.should.be.an.instanceOf(WebSocket.Server);
wss.emit('connection', stubWS);
sinon.assert.calledOnce(wsOnSpy);
sinon.assert.calledWith(wsOnSpy, 'error');

// fire the error event
stubWS.emit('error');
});
});

});

0 comments on commit dab2b14

Please sign in to comment.