From 9376191fc4500b9001bc0dc3ff9f0e0351797319 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Sun, 18 Apr 2021 13:53:43 +0200 Subject: Refuse to use already closed WebSocket objects We can't do anything useful with them anyway. --- core/rfb.js | 4 ++++ tests/test.rfb.js | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/core/rfb.js b/core/rfb.js index 4aedb0d..2257da3 100644 --- a/core/rfb.js +++ b/core/rfb.js @@ -480,6 +480,10 @@ export default class RFB extends EventTargetMixin { } catch (e) { this._fail("Error attaching channel (" + e + ")"); } + + if (this._sock.readyState === 'closed') { + this._fail("Cannot use already closed WebSocket/RTCDataChannel"); + } } // Make our elements part of the page diff --git a/tests/test.rfb.js b/tests/test.rfb.js index 5a1b713..2c112f3 100644 --- a/tests/test.rfb.js +++ b/tests/test.rfb.js @@ -183,6 +183,17 @@ describe('Remote Frame Buffer Protocol Client', function () { expect(attach).to.have.been.calledOnceWithExactly(sock); }); + it('should refuse closed WebSocket/RTCDataChannel objects', function () { + let sock = new FakeWebSocket('ws://HOST:8675/PATH', []); + sock.readyState = WebSocket.CLOSED; + const client = new RFB(document.createElement('div'), sock); + let callback = sinon.spy(); + client.addEventListener('disconnect', callback); + this.clock.tick(); + expect(callback).to.have.been.calledOnce; + expect(callback.args[0][0].detail.clean).to.be.false; + }); + it('should report attach problems via event', function () { attach.restore(); attach = sinon.stub(Websock.prototype, 'attach'); -- cgit v1.2.1