Skip to content

Fix server crash on invalid LiveQuery socket event #4533

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

Merged
merged 5 commits into from
Feb 16, 2018
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
39 changes: 29 additions & 10 deletions spec/ParseLiveQueryServer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ describe('ParseLiveQueryServer', function() {
// Mock matchesQuery
var mockMatchesQuery = jasmine.createSpy('matchesQuery').and.returnValue(true);
jasmine.mockLibrary('../src/LiveQuery/QueryTools', 'matchesQuery', mockMatchesQuery);
// Mock tv4
var mockValidate = function() {
return true;
}
jasmine.mockLibrary('tv4', 'validate', mockValidate);
// Mock ParsePubSub
var mockParsePubSub = {
createPublisher: function() {
Expand Down Expand Up @@ -350,7 +345,8 @@ describe('ParseLiveQueryServer', function() {

// Check connect request
var connectRequest = {
op: 'connect'
op: 'connect',
applicationId: '1'
};
// Trigger message event
parseWebSocket.emit('message', connectRequest);
Expand All @@ -370,7 +366,11 @@ describe('ParseLiveQueryServer', function() {
parseLiveQueryServer._onConnect(parseWebSocket);

// Check subscribe request
var subscribeRequest = '{"op":"subscribe"}';
var subscribeRequest = JSON.stringify({
op: 'subscribe',
requestId: 1,
query: {className: 'Test', where: {}}
});
// Trigger message event
parseWebSocket.emit('message', subscribeRequest);
// Make sure _handleSubscribe is called
Expand All @@ -390,7 +390,7 @@ describe('ParseLiveQueryServer', function() {
parseLiveQueryServer._onConnect(parseWebSocket);

// Check unsubscribe request
var unsubscribeRequest = '{"op":"unsubscribe"}';
var unsubscribeRequest = JSON.stringify({op: 'unsubscribe', requestId: 1});
// Trigger message event
parseWebSocket.emit('message', unsubscribeRequest);
// Make sure _handleUnsubscribe is called
Expand All @@ -414,7 +414,11 @@ describe('ParseLiveQueryServer', function() {
parseLiveQueryServer._onConnect(parseWebSocket);

// Check updateRequest request
var updateRequest = '{"op":"update"}';
var updateRequest = JSON.stringify({
op: 'update',
requestId: 1,
query: {className: 'Test', where: {}}
});
// Trigger message event
parseWebSocket.emit('message', updateRequest);
// Make sure _handleUnsubscribe is called
Expand All @@ -428,6 +432,22 @@ describe('ParseLiveQueryServer', function() {
expect(parseLiveQueryServer._handleSubscribe).toHaveBeenCalled();
});

it('can set missing command message handler for a parseWebSocket', function() {
var parseLiveQueryServer = new ParseLiveQueryServer(10, 10, {});
// Make mock parseWebsocket
var EventEmitter = require('events');
var parseWebSocket = new EventEmitter();
// Register message handlers for the parseWebSocket
parseLiveQueryServer._onConnect(parseWebSocket);

// Check invalid request
var invalidRequest = '{}';
// Trigger message event
parseWebSocket.emit('message', invalidRequest);
var Client = require('../src/LiveQuery/Client').Client;
expect(Client.pushError).toHaveBeenCalled();
});

it('can set unknown command message handler for a parseWebSocket', function() {
var parseLiveQueryServer = new ParseLiveQueryServer(10, 10, {});
// Make mock parseWebsocket
Expand Down Expand Up @@ -1193,7 +1213,6 @@ describe('ParseLiveQueryServer', function() {
jasmine.restoreLibrary('../src/LiveQuery/Subscription', 'Subscription');
jasmine.restoreLibrary('../src/LiveQuery/QueryTools', 'queryHash');
jasmine.restoreLibrary('../src/LiveQuery/QueryTools', 'matchesQuery');
jasmine.restoreLibrary('tv4', 'validate');
jasmine.restoreLibrary('../src/LiveQuery/ParsePubSub', 'ParsePubSub');
jasmine.restoreLibrary('../src/LiveQuery/SessionTokenCache', 'SessionTokenCache');
});
Expand Down
1 change: 1 addition & 0 deletions src/LiveQuery/RequestSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const general = {
'enum': ['connect', 'subscribe', 'unsubscribe', 'update']
},
},
'required': ['op']
};

const connect = {
Expand Down