fix: Parse Server databaseOptions nested keys incorrectly identified as invalid (#9213)

This commit is contained in:
Manuel
2024-07-17 17:48:33 +02:00
committed by GitHub
parent f384767d68
commit 77206d8044
7 changed files with 194 additions and 45 deletions

View File

@@ -1,52 +1,88 @@
const Config = require('../lib/Config');
const ParseServer = require('../lib/index').ParseServer;
describe('Config Keys', () => {
const tests = [
{
name: 'Invalid Root Keys',
options: { unknow: 'val', masterKeyIPs: '' },
error: 'unknow, masterKeyIPs',
},
{ name: 'Invalid Schema Keys', options: { schema: { Strict: 'val' } }, error: 'schema.Strict' },
{
name: 'Invalid Pages Keys',
options: { pages: { customUrls: { EmailVerificationSendFail: 'val' } } },
error: 'pages.customUrls.EmailVerificationSendFail',
},
{
name: 'Invalid LiveQueryServerOptions Keys',
options: { liveQueryServerOptions: { MasterKey: 'value' } },
error: 'liveQueryServerOptions.MasterKey',
},
{
name: 'Invalid RateLimit Keys - Array Item',
options: { rateLimit: [{ RequestPath: '' }, { RequestTimeWindow: '' }] },
error: 'rateLimit[0].RequestPath, rateLimit[1].RequestTimeWindow',
},
];
const invalidKeyErrorMessage = 'Invalid key\\(s\\) found in Parse Server configuration';
let loggerErrorSpy;
tests.forEach(test => {
it(test.name, async () => {
const logger = require('../lib/logger').logger;
spyOn(logger, 'error').and.callThrough();
spyOn(Config, 'validateOptions').and.callFake(() => {});
new ParseServer({
...defaultConfiguration,
...test.options,
});
expect(logger.error).toHaveBeenCalledWith(`Invalid Option Keys Found: ${test.error}`);
});
beforeEach(async () => {
const logger = require('../lib/logger').logger;
loggerErrorSpy = spyOn(logger, 'error').and.callThrough();
spyOn(Config, 'validateOptions').and.callFake(() => {});
});
it('should run fine', async () => {
try {
await reconfigureServer({
...defaultConfiguration,
});
} catch (err) {
fail('Should run without error');
}
it('recognizes invalid keys in root', async () => {
await expectAsync(reconfigureServer({
...defaultConfiguration,
invalidKey: 1,
})).toBeResolved();
const error = loggerErrorSpy.calls.all().reduce((s, call) => s += call.args[0], '');
expect(error).toMatch(invalidKeyErrorMessage);
});
it('recognizes invalid keys in pages.customUrls', async () => {
await expectAsync(reconfigureServer({
...defaultConfiguration,
pages: {
customUrls: {
invalidKey: 1,
EmailVerificationSendFail: 1,
}
}
})).toBeResolved();
const error = loggerErrorSpy.calls.all().reduce((s, call) => s += call.args[0], '');
expect(error).toMatch(invalidKeyErrorMessage);
expect(error).toMatch(`invalidKey`);
expect(error).toMatch(`EmailVerificationSendFail`);
});
it('recognizes invalid keys in liveQueryServerOptions', async () => {
await expectAsync(reconfigureServer({
...defaultConfiguration,
liveQueryServerOptions: {
invalidKey: 1,
MasterKey: 1,
}
})).toBeResolved();
const error = loggerErrorSpy.calls.all().reduce((s, call) => s += call.args[0], '');
expect(error).toMatch(invalidKeyErrorMessage);
expect(error).toMatch(`MasterKey`);
});
it('recognizes invalid keys in rateLimit', async () => {
await expectAsync(reconfigureServer({
...defaultConfiguration,
rateLimit: [
{ invalidKey: 1 },
{ RequestPath: 1 },
{ RequestTimeWindow: 1 },
]
})).toBeRejected();
const error = loggerErrorSpy.calls.all().reduce((s, call) => s += call.args[0], '');
expect(error).toMatch(invalidKeyErrorMessage);
expect(error).toMatch('rateLimit\\[0\\]\\.invalidKey');
expect(error).toMatch('rateLimit\\[1\\]\\.RequestPath');
expect(error).toMatch('rateLimit\\[2\\]\\.RequestTimeWindow');
});
it('recognizes valid keys in default configuration', async () => {
await expectAsync(reconfigureServer({
...defaultConfiguration,
})).toBeResolved();
expect(loggerErrorSpy.calls.all().reduce((s, call) => s += call.args[0], '')).not.toMatch(invalidKeyErrorMessage);
});
it_only_db('mongo')('recognizes valid keys in databaseOptions (MongoDB)', async () => {
await expectAsync(reconfigureServer({
databaseURI: 'mongodb://localhost:27017/parse',
filesAdapter: null,
databaseAdapter: null,
databaseOptions: {
retryWrites: true,
maxTimeMS: 1000,
maxStalenessSeconds: 10,
maxPoolSize: 10,
},
})).toBeResolved();
expect(loggerErrorSpy.calls.all().reduce((s, call) => s += call.args[0], '')).not.toMatch(invalidKeyErrorMessage);
});
});

49
spec/Utils.spec.js Normal file
View File

@@ -0,0 +1,49 @@
const Utils = require('../src/Utils');
describe('Utils', () => {
describe('addNestedKeysToRoot', () => {
it('should move the nested keys to root of object', async () => {
const obj = {
a: 1,
b: {
c: 2,
d: 3
},
e: 4
};
Utils.addNestedKeysToRoot(obj, 'b');
expect(obj).toEqual({
a: 1,
c: 2,
d: 3,
e: 4
});
});
it('should not modify the object if the key does not exist', async () => {
const obj = {
a: 1,
e: 4
};
Utils.addNestedKeysToRoot(obj, 'b');
expect(obj).toEqual({
a: 1,
e: 4
});
});
it('should not modify the object if the key is not an object', () => {
const obj = {
a: 1,
b: 2,
e: 4
};
Utils.addNestedKeysToRoot(obj, 'b');
expect(obj).toEqual({
a: 1,
b: 2,
e: 4
});
});
});
});

View File

@@ -1051,6 +1051,29 @@ module.exports.DatabaseOptions = {
action: parsers.booleanParser,
default: false,
},
maxPoolSize: {
env: 'PARSE_SERVER_DATABASE_MAX_POOL_SIZE',
help:
'The MongoDB driver option to set the maximum number of opened, cached, ready-to-use database connections maintained by the driver.',
action: parsers.numberParser('maxPoolSize'),
},
maxStalenessSeconds: {
env: 'PARSE_SERVER_DATABASE_MAX_STALENESS_SECONDS',
help:
'The MongoDB driver option to set the maximum replication lag for reads from secondary nodes.',
action: parsers.numberParser('maxStalenessSeconds'),
},
maxTimeMS: {
env: 'PARSE_SERVER_DATABASE_MAX_TIME_MS',
help:
'The MongoDB driver option to set a cumulative time limit in milliseconds for processing operations on a cursor.',
action: parsers.numberParser('maxTimeMS'),
},
retryWrites: {
env: 'PARSE_SERVER_DATABASE_RETRY_WRITES',
help: 'The MongoDB driver option to set whether to retry failed writes.',
action: parsers.booleanParser,
},
schemaCacheTtl: {
env: 'PARSE_SERVER_DATABASE_SCHEMA_CACHE_TTL',
help:

View File

@@ -235,6 +235,10 @@
/**
* @interface DatabaseOptions
* @property {Boolean} enableSchemaHooks Enables database real-time hooks to update single schema cache. Set to `true` if using multiple Parse Servers instances connected to the same database. Failing to do so will cause a schema change to not propagate to all instances and re-syncing will only happen when the instances restart. To use this feature with MongoDB, a replica set cluster with [change stream](https://docs.mongodb.com/manual/changeStreams/#availability) support is required.
* @property {Number} maxPoolSize The MongoDB driver option to set the maximum number of opened, cached, ready-to-use database connections maintained by the driver.
* @property {Number} maxStalenessSeconds The MongoDB driver option to set the maximum replication lag for reads from secondary nodes.
* @property {Number} maxTimeMS The MongoDB driver option to set a cumulative time limit in milliseconds for processing operations on a cursor.
* @property {Boolean} retryWrites The MongoDB driver option to set whether to retry failed writes.
* @property {Number} schemaCacheTtl The duration in seconds after which the schema cache expires and will be refetched from the database. Use this option if using multiple Parse Servers instances connected to the same database. A low duration will cause the schema cache to be updated too often, causing unnecessary database reads. A high duration will cause the schema to be updated too rarely, increasing the time required until schema changes propagate to all server instances. This feature can be used as an alternative or in conjunction with the option `enableSchemaHooks`. Default is infinite which means the schema cache never expires.
*/

View File

@@ -596,6 +596,14 @@ export interface DatabaseOptions {
enableSchemaHooks: ?boolean;
/* The duration in seconds after which the schema cache expires and will be refetched from the database. Use this option if using multiple Parse Servers instances connected to the same database. A low duration will cause the schema cache to be updated too often, causing unnecessary database reads. A high duration will cause the schema to be updated too rarely, increasing the time required until schema changes propagate to all server instances. This feature can be used as an alternative or in conjunction with the option `enableSchemaHooks`. Default is infinite which means the schema cache never expires. */
schemaCacheTtl: ?number;
/* The MongoDB driver option to set whether to retry failed writes. */
retryWrites: ?boolean;
/* The MongoDB driver option to set a cumulative time limit in milliseconds for processing operations on a cursor. */
maxTimeMS: ?number;
/* The MongoDB driver option to set the maximum replication lag for reads from secondary nodes.*/
maxStalenessSeconds: ?number;
/* The MongoDB driver option to set the maximum number of opened, cached, ready-to-use database connections maintained by the driver. */
maxPoolSize: ?number;
}
export interface AuthAdapter {

View File

@@ -109,7 +109,7 @@ class ParseServer {
const diff = validateKeyNames(options, optionsBlueprint);
if (diff.length > 0) {
const logger = logging.logger;
logger.error(`Invalid Option Keys Found: ${diff.join(', ')}`);
logger.error(`Invalid key(s) found in Parse Server configuration: ${diff.join(', ')}`);
}
// Set option defaults

View File

@@ -370,6 +370,35 @@ class Utils {
}
}
}
/**
* Moves the nested keys of a specified key in an object to the root of the object.
*
* @param {Object} obj The object to modify.
* @param {String} key The key whose nested keys will be moved to root.
* @returns {Object} The modified object, or the original object if no modification happened.
* @example
* const obj = {
* a: 1,
* b: {
* c: 2,
* d: 3
* },
* e: 4
* };
* addNestedKeysToRoot(obj, 'b');
* console.log(obj);
* // Output: { a: 1, e: 4, c: 2, d: 3 }
*/
static addNestedKeysToRoot(obj, key) {
if (obj[key] && typeof obj[key] === 'object') {
// Add nested keys to root
Object.assign(obj, { ...obj[key] });
// Delete original nested key
delete obj[key];
}
return obj;
}
}
module.exports = Utils;