Skip to content

Commit

Permalink
Merge pull request from GHSA-2xm2-xj2q-qgpj
Browse files Browse the repository at this point in the history
* Test case and fixes

* Change requestTimeout default to 5s

* Document new function argument
  • Loading branch information
davimacedo authored Oct 21, 2020
1 parent ef2e54c commit 78b59fb
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 12 deletions.
42 changes: 42 additions & 0 deletions spec/ParseLiveQuery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,48 @@ describe('ParseLiveQuery', function () {
});
});

it('should not broadcast event to client with invalid session token - avisory GHSA-2xm2-xj2q-qgpj', async done => {
await reconfigureServer({
liveQuery: {
classNames: ['TestObject'],
},
liveQueryServerOptions: {
cacheTimeout: 100,
},
startLiveQueryServer: true,
verbose: false,
silent: true,
cacheTTL: 100,
});
const user = new Parse.User();
user.setUsername('username');
user.setPassword('password');
await user.signUp();
const obj1 = new Parse.Object('TestObject');
const obj1ACL = new Parse.ACL();
obj1ACL.setPublicReadAccess(false);
obj1ACL.setReadAccess(user, true);
obj1.setACL(obj1ACL);
const obj2 = new Parse.Object('TestObject');
const obj2ACL = new Parse.ACL();
obj2ACL.setPublicReadAccess(false);
obj2ACL.setReadAccess(user, true);
obj2.setACL(obj2ACL);
const query = new Parse.Query('TestObject');
const subscription = await query.subscribe();
subscription.on('create', obj => {
if (obj.id !== obj1.id) {
done.fail('should not fire');
}
});
await obj1.save();
await Parse.User.logOut();
await new Promise(resolve => setTimeout(resolve, 200));
await obj2.save();
await new Promise(resolve => setTimeout(resolve, 200));
done();
});

afterEach(async function (done) {
const client = await Parse.CoreManager.getLiveQueryController().getDefaultLiveQueryClient();
client.close();
Expand Down
12 changes: 7 additions & 5 deletions src/LiveQuery/ParseLiveQueryServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ class ParseLiveQueryServer {
// The subscriber we use to get object update from publisher
subscriber: Object;

constructor(server: any, config: any = {}) {
constructor(server: any, config: any = {}, parseServerConfig: any = {}) {
this.server = server;
this.clients = new Map();
this.subscriptions = new Map();
this.config = config;

config.appId = config.appId || Parse.applicationId;
config.masterKey = config.masterKey || Parse.masterKey;
Expand All @@ -54,13 +55,15 @@ class ParseLiveQueryServer {

// The cache controller is a proper cache controller
// with access to User and Roles
this.cacheController = getCacheController(config);
this.cacheController = getCacheController(parseServerConfig);

config.cacheTimeout = config.cacheTimeout || 5 * 1000; // 5s

// This auth cache stores the promises for each auth resolution.
// The main benefit is to be able to reuse the same user / session token resolution.
this.authCache = new LRU({
max: 500, // 500 concurrent
maxAge: 60 * 60 * 1000, // 1h
maxAge: config.cacheTimeout,
});
// Initialize websocket server
this.parseWebSocketServer = new ParseWebSocketServer(
Expand Down Expand Up @@ -510,12 +513,11 @@ class ParseLiveQueryServer {
// There was an error with the session token
const result = {};
if (error && error.code === Parse.Error.INVALID_SESSION_TOKEN) {
// Store a resolved promise with the error for 10 minutes
result.error = error;
this.authCache.set(
sessionToken,
Promise.resolve(result),
60 * 10 * 1000
this.config.cacheTimeout
);
} else {
this.authCache.del(sessionToken);
Expand Down
2 changes: 1 addition & 1 deletion src/Options/Definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ module.exports.LiveQueryServerOptions = {
cacheTimeout: {
env: 'PARSE_LIVE_QUERY_SERVER_CACHE_TIMEOUT',
help:
"Number in milliseconds. When clients provide the sessionToken to the LiveQuery server, the LiveQuery server will try to fetch its ParseUser's objectId from parse server and store it in the cache. The value defines the duration of the cache. Check the following Security section and our protocol specification for details, defaults to 30 * 24 * 60 * 60 * 1000 ms (~30 days).",
"Number in milliseconds. When clients provide the sessionToken to the LiveQuery server, the LiveQuery server will try to fetch its ParseUser's objectId from parse server and store it in the cache. The value defines the duration of the cache. Check the following Security section and our protocol specification for details, defaults to 5 * 1000 ms (5 seconds).",
action: parsers.numberParser('cacheTimeout'),
},
keyPairs: {
Expand Down
2 changes: 1 addition & 1 deletion src/Options/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
/**
* @interface LiveQueryServerOptions
* @property {String} appId This string should match the appId in use by your Parse Server. If you deploy the LiveQuery server alongside Parse Server, the LiveQuery server will try to use the same appId.
* @property {Number} cacheTimeout Number in milliseconds. When clients provide the sessionToken to the LiveQuery server, the LiveQuery server will try to fetch its ParseUser's objectId from parse server and store it in the cache. The value defines the duration of the cache. Check the following Security section and our protocol specification for details, defaults to 30 * 24 * 60 * 60 * 1000 ms (~30 days).
* @property {Number} cacheTimeout Number in milliseconds. When clients provide the sessionToken to the LiveQuery server, the LiveQuery server will try to fetch its ParseUser's objectId from parse server and store it in the cache. The value defines the duration of the cache. Check the following Security section and our protocol specification for details, defaults to 5 * 1000 ms (5 seconds).
* @property {Any} keyPairs A JSON object that serves as a whitelist of keys. It is used for validating clients when they try to connect to the LiveQuery server. Check the following Security section and our protocol specification for details.
* @property {String} logLevel This string defines the log level of the LiveQuery server. We support VERBOSE, INFO, ERROR, NONE, defaults to INFO.
* @property {String} masterKey This string should match the masterKey in use by your Parse Server. If you deploy the LiveQuery server alongside Parse Server, the LiveQuery server will try to use the same masterKey.
Expand Down
2 changes: 1 addition & 1 deletion src/Options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export interface LiveQueryServerOptions {
keyPairs: ?any;
/* Number of milliseconds between ping/pong frames. The WebSocket server sends ping/pong frames to the clients to keep the WebSocket alive. This value defines the interval of the ping/pong frame from the server to clients, defaults to 10 * 1000 ms (10 s).*/
websocketTimeout: ?number;
/* Number in milliseconds. When clients provide the sessionToken to the LiveQuery server, the LiveQuery server will try to fetch its ParseUser's objectId from parse server and store it in the cache. The value defines the duration of the cache. Check the following Security section and our protocol specification for details, defaults to 30 * 24 * 60 * 60 * 1000 ms (~30 days).*/
/* Number in milliseconds. When clients provide the sessionToken to the LiveQuery server, the LiveQuery server will try to fetch its ParseUser's objectId from parse server and store it in the cache. The value defines the duration of the cache. Check the following Security section and our protocol specification for details, defaults to 5 * 1000 ms (5 seconds).*/
cacheTimeout: ?number;
/* This string defines the log level of the LiveQuery server. We support VERBOSE, INFO, ERROR, NONE, defaults to INFO.*/
logLevel: ?string;
Expand Down
14 changes: 10 additions & 4 deletions src/ParseServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ class ParseServer {
if (options.startLiveQueryServer || options.liveQueryServerOptions) {
this.liveQueryServer = ParseServer.createLiveQueryServer(
server,
options.liveQueryServerOptions
options.liveQueryServerOptions,
options
);
}
/* istanbul ignore next */
Expand All @@ -324,16 +325,21 @@ class ParseServer {
* Helper method to create a liveQuery server
* @static
* @param {Server} httpServer an optional http server to pass
* @param {LiveQueryServerOptions} config options fot he liveQueryServer
* @param {LiveQueryServerOptions} config options for the liveQueryServer
* @param {ParseServerOptions} options options for the ParseServer
* @returns {ParseLiveQueryServer} the live query server instance
*/
static createLiveQueryServer(httpServer, config: LiveQueryServerOptions) {
static createLiveQueryServer(
httpServer,
config: LiveQueryServerOptions,
options: ParseServerOptions
) {
if (!httpServer || (config && config.port)) {
var app = express();
httpServer = require('http').createServer(app);
httpServer.listen(config.port);
}
return new ParseLiveQueryServer(httpServer, config);
return new ParseLiveQueryServer(httpServer, config, options);
}

static verifyServerUrl(callback) {
Expand Down

0 comments on commit 78b59fb

Please sign in to comment.