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

Commit dab2b14

Browse files
author
Dave Kelsey
authored
[0.19.x] Rest server can be terminated by client (#4531)
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 <d_kelsey@uk.ibm.com>
1 parent 84e89bd commit dab2b14

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

packages/composer-rest-server/server/server.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ module.exports = function (composer) {
192192
clientTracking: true
193193
});
194194

195+
// Add a dummy error handler for a ws connection so bad websocket client
196+
// doesn't kill the rest server
197+
wss.on('connection', (ws) => {
198+
ws.on('error', () => {
199+
// do nothing
200+
});
201+
});
202+
195203
// Add a broadcast method that sends data to all connected clients.
196204
wss.broadcast = (data) => {
197205
wss.clients.forEach((client) => {

packages/composer-rest-server/test/server/server.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const IdCard = require('composer-common').IdCard;
2323
const path = require('path');
2424
const server = require('../../server/server');
2525
const WebSocket = require('ws');
26+
const EventEmitter = require('events');
2627

2728
const chai = require('chai');
2829
const should = chai.should();
@@ -341,4 +342,25 @@ describe('server', () => {
341342
});
342343
});
343344

345+
it('should register an error handler on connection to a websocket and it shoud no nothing when fired', () => {
346+
composerConfig.websockets = true;
347+
const stubWS = new EventEmitter();
348+
const wsOnSpy = sinon.spy(stubWS, 'on');
349+
350+
351+
return server(composerConfig)
352+
.then((result) => {
353+
result.app.should.exist;
354+
result.server.should.exist;
355+
const wss = result.app.get('wss');
356+
wss.should.be.an.instanceOf(WebSocket.Server);
357+
wss.emit('connection', stubWS);
358+
sinon.assert.calledOnce(wsOnSpy);
359+
sinon.assert.calledWith(wsOnSpy, 'error');
360+
361+
// fire the error event
362+
stubWS.emit('error');
363+
});
364+
});
365+
344366
});

0 commit comments

Comments
 (0)