Skip to content

Commit 7c01f20

Browse files
committed
Improves key matching algorithm
1 parent 791c551 commit 7c01f20

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

src/middlewares.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,21 @@ function handleParseHeaders(req, res, next) {
9999

100100
// Client keys are not required in parse-server, but if any have been configured in the server, validate them
101101
// to preserve original behavior.
102-
var keyRequired = (req.config.clientKey
103-
|| req.config.javascriptKey
104-
|| req.config.dotNetKey
105-
|| req.config.restAPIKey);
106-
var keyHandled = false;
107-
if (keyRequired
108-
&& ((info.clientKey && req.config.clientKey && info.clientKey === req.config.clientKey)
109-
|| (info.javascriptKey && req.config.javascriptKey && info.javascriptKey === req.config.javascriptKey)
110-
|| (info.dotNetKey && req.config.dotNetKey && info.dotNetKey === req.config.dotNetKey)
111-
|| (info.restAPIKey && req.config.restAPIKey && info.restAPIKey === req.config.restAPIKey)
112-
)) {
113-
keyHandled = true;
114-
}
115-
if (keyRequired && !keyHandled) {
102+
var keyMismatch = ["clientKey", "javascriptKey", "dotNetKey", "restAPIKey"].reduce(function(mismatch, key){
103+
let configKey = req.config[key];
104+
// No key provided in the config, skip
105+
if (!configKey) {
106+
return mismatch;
107+
}
108+
// we haven't found a matching key
109+
if (mismatch) {
110+
// check the info key
111+
mismatch = info[key] !== configKey;
112+
}
113+
return mismatch;
114+
}, true);
115+
116+
if (keyMismatch) {
116117
return invalidRequest(req, res);
117118
}
118119

0 commit comments

Comments
 (0)