Hello everyone,
Before I start working on a PR for a feature I use on my own servers, I was curious to see what you all think of the feasibility.
I was thinking of this post (How to improve default security), and although we have CLPs, ACLs, etc, securing cloud functions is solely up to the developer.
Furthermore, the cloud code guide doesnât really have anything on validating requests, or the users making them. Even though these are simple, I think it can be overlooked for novice devs.
I personally have a validator function which I pass into the third parameter of Parse.Cloud.define (which I donât think validator functions are documented either, the SDK says define only has 2 parameters), but I was thinking of how we could make this more readily available.
Proposal:
Global Parse.Cloud.beforeCloud, where you can run any validation before a cloud function is ran, such as making sure a user is logged in,
Or, Parse.Cloud options:
Parse.Cloud.define('hello', () => {
return 'Hello world!';
}).withOptions({user:true,timeout:500,masterKey:true,revertKey:['a','b','c']});
.withOptions could trigger be a inbuilt validator, which runs based on the users options, such as:
static validateFunction(options,request) {
if (!options) {
return true;
}
if (options.user && !request.user) {
throw new Parse.Error(
Parse.Error.VALIDATION_ERROR,
'Validation failed. Please login to continue.'
);
}
if (options.master && !request.master) {
throw new Parse.Error(
Parse.Error.VALIDATION_ERROR,
'Validation failed. Master key is required to complete this request.'
);
}
for (const key in options.params) {
if (request.params[key] === null) {
throw new Parse.Error(
Parse.Error.VALIDATION_ERROR,
`Validation failed. Please specify data for ${key}.`
);
}
}
if (options.timeout) {
setTimeout(() => {
if (!request.complete) {
throw new Parse.Error(
Parse.Error.SCRIPT_FAILED,
`Function failed: "${functionName}. Error: Timeout."`
);
}
},parseFloat(options.timeout) * 100);
}
return true;
}
The inbuilt cloud function options validator could obviously be extended to provide the most benefit to our users.
What do you guys think, worth working on, or should I focus on the docs?