Skip to content

Commit 77206d8

Browse files
authored
fix: Parse Server databaseOptions nested keys incorrectly identified as invalid (#9213)
1 parent f384767 commit 77206d8

File tree

7 files changed

+195
-46
lines changed

7 files changed

+195
-46
lines changed

Diff for: spec/ParseConfigKey.spec.js

+81-45
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,88 @@
11
const Config = require('../lib/Config');
2-
const ParseServer = require('../lib/index').ParseServer;
32

43
describe('Config Keys', () => {
5-
const tests = [
6-
{
7-
name: 'Invalid Root Keys',
8-
options: { unknow: 'val', masterKeyIPs: '' },
9-
error: 'unknow, masterKeyIPs',
10-
},
11-
{ name: 'Invalid Schema Keys', options: { schema: { Strict: 'val' } }, error: 'schema.Strict' },
12-
{
13-
name: 'Invalid Pages Keys',
14-
options: { pages: { customUrls: { EmailVerificationSendFail: 'val' } } },
15-
error: 'pages.customUrls.EmailVerificationSendFail',
16-
},
17-
{
18-
name: 'Invalid LiveQueryServerOptions Keys',
19-
options: { liveQueryServerOptions: { MasterKey: 'value' } },
20-
error: 'liveQueryServerOptions.MasterKey',
21-
},
22-
{
23-
name: 'Invalid RateLimit Keys - Array Item',
24-
options: { rateLimit: [{ RequestPath: '' }, { RequestTimeWindow: '' }] },
25-
error: 'rateLimit[0].RequestPath, rateLimit[1].RequestTimeWindow',
26-
},
27-
];
28-
29-
tests.forEach(test => {
30-
it(test.name, async () => {
31-
const logger = require('../lib/logger').logger;
32-
spyOn(logger, 'error').and.callThrough();
33-
spyOn(Config, 'validateOptions').and.callFake(() => {});
34-
35-
new ParseServer({
36-
...defaultConfiguration,
37-
...test.options,
38-
});
39-
expect(logger.error).toHaveBeenCalledWith(`Invalid Option Keys Found: ${test.error}`);
40-
});
4+
const invalidKeyErrorMessage = 'Invalid key\\(s\\) found in Parse Server configuration';
5+
let loggerErrorSpy;
6+
7+
beforeEach(async () => {
8+
const logger = require('../lib/logger').logger;
9+
loggerErrorSpy = spyOn(logger, 'error').and.callThrough();
10+
spyOn(Config, 'validateOptions').and.callFake(() => {});
11+
});
12+
13+
it('recognizes invalid keys in root', async () => {
14+
await expectAsync(reconfigureServer({
15+
...defaultConfiguration,
16+
invalidKey: 1,
17+
})).toBeResolved();
18+
const error = loggerErrorSpy.calls.all().reduce((s, call) => s += call.args[0], '');
19+
expect(error).toMatch(invalidKeyErrorMessage);
20+
});
21+
22+
it('recognizes invalid keys in pages.customUrls', async () => {
23+
await expectAsync(reconfigureServer({
24+
...defaultConfiguration,
25+
pages: {
26+
customUrls: {
27+
invalidKey: 1,
28+
EmailVerificationSendFail: 1,
29+
}
30+
}
31+
})).toBeResolved();
32+
const error = loggerErrorSpy.calls.all().reduce((s, call) => s += call.args[0], '');
33+
expect(error).toMatch(invalidKeyErrorMessage);
34+
expect(error).toMatch(`invalidKey`);
35+
expect(error).toMatch(`EmailVerificationSendFail`);
36+
});
37+
38+
it('recognizes invalid keys in liveQueryServerOptions', async () => {
39+
await expectAsync(reconfigureServer({
40+
...defaultConfiguration,
41+
liveQueryServerOptions: {
42+
invalidKey: 1,
43+
MasterKey: 1,
44+
}
45+
})).toBeResolved();
46+
const error = loggerErrorSpy.calls.all().reduce((s, call) => s += call.args[0], '');
47+
expect(error).toMatch(invalidKeyErrorMessage);
48+
expect(error).toMatch(`MasterKey`);
49+
});
50+
51+
it('recognizes invalid keys in rateLimit', async () => {
52+
await expectAsync(reconfigureServer({
53+
...defaultConfiguration,
54+
rateLimit: [
55+
{ invalidKey: 1 },
56+
{ RequestPath: 1 },
57+
{ RequestTimeWindow: 1 },
58+
]
59+
})).toBeRejected();
60+
const error = loggerErrorSpy.calls.all().reduce((s, call) => s += call.args[0], '');
61+
expect(error).toMatch(invalidKeyErrorMessage);
62+
expect(error).toMatch('rateLimit\\[0\\]\\.invalidKey');
63+
expect(error).toMatch('rateLimit\\[1\\]\\.RequestPath');
64+
expect(error).toMatch('rateLimit\\[2\\]\\.RequestTimeWindow');
65+
});
66+
67+
it('recognizes valid keys in default configuration', async () => {
68+
await expectAsync(reconfigureServer({
69+
...defaultConfiguration,
70+
})).toBeResolved();
71+
expect(loggerErrorSpy.calls.all().reduce((s, call) => s += call.args[0], '')).not.toMatch(invalidKeyErrorMessage);
4172
});
4273

43-
it('should run fine', async () => {
44-
try {
45-
await reconfigureServer({
46-
...defaultConfiguration,
47-
});
48-
} catch (err) {
49-
fail('Should run without error');
50-
}
74+
it_only_db('mongo')('recognizes valid keys in databaseOptions (MongoDB)', async () => {
75+
await expectAsync(reconfigureServer({
76+
databaseURI: 'mongodb://localhost:27017/parse',
77+
filesAdapter: null,
78+
databaseAdapter: null,
79+
databaseOptions: {
80+
retryWrites: true,
81+
maxTimeMS: 1000,
82+
maxStalenessSeconds: 10,
83+
maxPoolSize: 10,
84+
},
85+
})).toBeResolved();
86+
expect(loggerErrorSpy.calls.all().reduce((s, call) => s += call.args[0], '')).not.toMatch(invalidKeyErrorMessage);
5187
});
5288
});

Diff for: spec/Utils.spec.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const Utils = require('../src/Utils');
2+
3+
describe('Utils', () => {
4+
describe('addNestedKeysToRoot', () => {
5+
it('should move the nested keys to root of object', async () => {
6+
const obj = {
7+
a: 1,
8+
b: {
9+
c: 2,
10+
d: 3
11+
},
12+
e: 4
13+
};
14+
Utils.addNestedKeysToRoot(obj, 'b');
15+
expect(obj).toEqual({
16+
a: 1,
17+
c: 2,
18+
d: 3,
19+
e: 4
20+
});
21+
});
22+
23+
it('should not modify the object if the key does not exist', async () => {
24+
const obj = {
25+
a: 1,
26+
e: 4
27+
};
28+
Utils.addNestedKeysToRoot(obj, 'b');
29+
expect(obj).toEqual({
30+
a: 1,
31+
e: 4
32+
});
33+
});
34+
35+
it('should not modify the object if the key is not an object', () => {
36+
const obj = {
37+
a: 1,
38+
b: 2,
39+
e: 4
40+
};
41+
Utils.addNestedKeysToRoot(obj, 'b');
42+
expect(obj).toEqual({
43+
a: 1,
44+
b: 2,
45+
e: 4
46+
});
47+
});
48+
});
49+
});

Diff for: src/Options/Definitions.js

+23
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,29 @@ module.exports.DatabaseOptions = {
10511051
action: parsers.booleanParser,
10521052
default: false,
10531053
},
1054+
maxPoolSize: {
1055+
env: 'PARSE_SERVER_DATABASE_MAX_POOL_SIZE',
1056+
help:
1057+
'The MongoDB driver option to set the maximum number of opened, cached, ready-to-use database connections maintained by the driver.',
1058+
action: parsers.numberParser('maxPoolSize'),
1059+
},
1060+
maxStalenessSeconds: {
1061+
env: 'PARSE_SERVER_DATABASE_MAX_STALENESS_SECONDS',
1062+
help:
1063+
'The MongoDB driver option to set the maximum replication lag for reads from secondary nodes.',
1064+
action: parsers.numberParser('maxStalenessSeconds'),
1065+
},
1066+
maxTimeMS: {
1067+
env: 'PARSE_SERVER_DATABASE_MAX_TIME_MS',
1068+
help:
1069+
'The MongoDB driver option to set a cumulative time limit in milliseconds for processing operations on a cursor.',
1070+
action: parsers.numberParser('maxTimeMS'),
1071+
},
1072+
retryWrites: {
1073+
env: 'PARSE_SERVER_DATABASE_RETRY_WRITES',
1074+
help: 'The MongoDB driver option to set whether to retry failed writes.',
1075+
action: parsers.booleanParser,
1076+
},
10541077
schemaCacheTtl: {
10551078
env: 'PARSE_SERVER_DATABASE_SCHEMA_CACHE_TTL',
10561079
help:

Diff for: src/Options/docs.js

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/Options/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,14 @@ export interface DatabaseOptions {
596596
enableSchemaHooks: ?boolean;
597597
/* 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. */
598598
schemaCacheTtl: ?number;
599+
/* The MongoDB driver option to set whether to retry failed writes. */
600+
retryWrites: ?boolean;
601+
/* The MongoDB driver option to set a cumulative time limit in milliseconds for processing operations on a cursor. */
602+
maxTimeMS: ?number;
603+
/* The MongoDB driver option to set the maximum replication lag for reads from secondary nodes.*/
604+
maxStalenessSeconds: ?number;
605+
/* The MongoDB driver option to set the maximum number of opened, cached, ready-to-use database connections maintained by the driver. */
606+
maxPoolSize: ?number;
599607
}
600608

601609
export interface AuthAdapter {

Diff for: src/ParseServer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class ParseServer {
109109
const diff = validateKeyNames(options, optionsBlueprint);
110110
if (diff.length > 0) {
111111
const logger = logging.logger;
112-
logger.error(`Invalid Option Keys Found: ${diff.join(', ')}`);
112+
logger.error(`Invalid key(s) found in Parse Server configuration: ${diff.join(', ')}`);
113113
}
114114

115115
// Set option defaults

Diff for: src/Utils.js

+29
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,35 @@ class Utils {
370370
}
371371
}
372372
}
373+
374+
/**
375+
* Moves the nested keys of a specified key in an object to the root of the object.
376+
*
377+
* @param {Object} obj The object to modify.
378+
* @param {String} key The key whose nested keys will be moved to root.
379+
* @returns {Object} The modified object, or the original object if no modification happened.
380+
* @example
381+
* const obj = {
382+
* a: 1,
383+
* b: {
384+
* c: 2,
385+
* d: 3
386+
* },
387+
* e: 4
388+
* };
389+
* addNestedKeysToRoot(obj, 'b');
390+
* console.log(obj);
391+
* // Output: { a: 1, e: 4, c: 2, d: 3 }
392+
*/
393+
static addNestedKeysToRoot(obj, key) {
394+
if (obj[key] && typeof obj[key] === 'object') {
395+
// Add nested keys to root
396+
Object.assign(obj, { ...obj[key] });
397+
// Delete original nested key
398+
delete obj[key];
399+
}
400+
return obj;
401+
}
373402
}
374403

375404
module.exports = Utils;

0 commit comments

Comments
 (0)