Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websocket: unhandle rejection #6418

Merged
merged 4 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions spec/ParseLiveQuery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,43 @@ describe('ParseLiveQuery', function() {
await object.save();
});

it('handle invalid websocket payload length', async done => {
await reconfigureServer({
liveQuery: {
classNames: ['TestObject'],
},
startLiveQueryServer: true,
verbose: false,
silent: true,
websocketTimeout: 100,
});
const object = new TestObject();
await object.save();

const query = new Parse.Query(TestObject);
query.equalTo('objectId', object.id);
const subscription = await query.subscribe();

// All control frames must have a payload length of 125 bytes or less.
// https://tools.ietf.org/html/rfc6455#section-5.5
//
// 0x89 = 10001001 = ping
// 0xfe = 11111110 = first bit is masking the remaining 7 are 1111110 or 126 the payload length
// https://tools.ietf.org/html/rfc6455#section-5.2
const client = await Parse.CoreManager.getLiveQueryController().getDefaultLiveQueryClient();
client.socket._socket.write(Buffer.from([0x89, 0xfe]));

subscription.on('update', async object => {
expect(object.get('foo')).toBe('bar');
done();
});
// Wait for Websocket timeout to reconnect
setTimeout(async () => {
object.set({ foo: 'bar' });
await object.save();
}, 1000);
});

afterEach(async function(done) {
const client = await Parse.CoreManager.getLiveQueryController().getDefaultLiveQueryClient();
client.close();
Expand Down
13 changes: 7 additions & 6 deletions spec/ParseWebSocketServer.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const {
ParseWebSocketServer,
} = require('../lib/LiveQuery/ParseWebSocketServer');
const EventEmitter = require('events');

describe('ParseWebSocketServer', function() {
beforeEach(function(done) {
// Mock ws server
const EventEmitter = require('events');

const mockServer = function() {
return new EventEmitter();
};
Expand All @@ -22,11 +23,11 @@ describe('ParseWebSocketServer', function() {
onConnectCallback,
{ websocketTimeout: 5 }
).server;
const ws = {
readyState: 0,
OPEN: 0,
ping: jasmine.createSpy('ping'),
};
const ws = new EventEmitter();
ws.readyState = 0;
ws.OPEN = 0;
ws.ping = jasmine.createSpy('ping');

parseWebSocketServer.onConnection(ws);

// Make sure callback is called
Expand Down
4 changes: 4 additions & 0 deletions src/LiveQuery/ParseWebSocketServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export class ParseWebSocketServer {
logger.info('Parse LiveQuery Server starts running');
};
wss.onConnection = ws => {
ws.on('error', error => {
logger.error(error.message);
logger.error(JSON.stringify(ws));
});
onConnect(new ParseWebSocket(ws));
// Send ping to client periodically
const pingIntervalId = setInterval(() => {
Expand Down