Validate user-methods parameters (#924)

* Validate user-methods parameters

* Switch homemade typeof operator for real one.

* Fix array prototype access
This commit is contained in:
Kristian Dupont 2017-03-20 12:52:41 +01:00 committed by GitHub
parent 78caae7b17
commit 5bece3d5c4
2 changed files with 35 additions and 12 deletions

View File

@ -24,6 +24,15 @@ function node_require(module) {
return require(module);
}
function checkTypes(args, types) {
args = Array.prototype.slice.call(args);
for (var i = 0; i < types.length; ++i) {
if (typeof args[i] !== types[i]) {
throw new TypeError('param ' + i + ' must be of type ' + types[i]);
}
}
}
const performFetch = typeof fetch === 'undefined' ? node_require('node-fetch') : fetch;
const url_parse = require('url-parse');
@ -133,6 +142,7 @@ module.exports = {
},
adminUser(token) {
checkTypes(arguments, ['string']);
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
@ -142,6 +152,7 @@ module.exports = {
},
register(server, username, password, callback) {
checkTypes(arguments, ['string', 'string', 'string', 'function']);
_authenticate(this, server, {
provider: 'password',
user_info: { password: password, register: true },
@ -150,6 +161,7 @@ module.exports = {
},
login(server, username, password, callback) {
checkTypes(arguments, ['string', 'string', 'string', 'function']);
_authenticate(this, server, {
provider: 'password',
user_info: { password: password },
@ -162,11 +174,14 @@ module.exports = {
// Compatibility with previous signature:
// registerWithProvider(server, provider, providerToken, callback)
if (arguments.length === 4) {
checkTypes(arguments, ['string', 'string', 'string', 'function']);
options = {
provider: arguments[1],
providerToken: arguments[2]
};
callback = arguments[3];
} else {
checkTypes(arguments, ['string', 'object', 'function']);
}
let reqOptions = {

View File

@ -134,17 +134,21 @@ module.exports = {
},
testRegisterMissingUsername() {
return callbackTest((callback) => Realm.Sync.User.register('http://localhost:9080', undefined, 'password', callback), (error, user) => {
assertIsAuthError(error, 602, 'https://realm.io/docs/object-server/problems/missing-parameters');
TestCase.assertUndefined(user);
return new Promise((resolve, _reject) => {
TestCase.assertThrows(() => {
Realm.Sync.User.register('http://localhost:9080', undefined, 'password', () => {});
});
resolve();
});
},
testRegisterMissingPassword() {
var username = uuid();
return callbackTest((callback) => Realm.Sync.User.register('http://localhost:9080', username, undefined, callback), (error, user) => {
assertIsAuthError(error, 602, 'https://realm.io/docs/object-server/problems/missing-parameters');
TestCase.assertUndefined(user);
return new Promise((resolve, _reject) => {
TestCase.assertThrows(() => {
Realm.Sync.User.register('http://localhost:9080', username, undefined, () => {});
});
resolve();
});
},
@ -180,17 +184,21 @@ module.exports = {
},
testLoginMissingUsername() {
return callbackTest((callback) => Realm.Sync.User.login('http://localhost:9080', undefined, 'password', callback), (error, user) => {
assertIsAuthError(error, 602, 'https://realm.io/docs/object-server/problems/missing-parameters');
TestCase.assertUndefined(user);
return new Promise((resolve, _reject) => {
TestCase.assertThrows(() => {
Realm.Sync.User.login('http://localhost:9080', undefined, 'password', () => {});
});
resolve();
});
},
testLoginMissingPassword() {
var username = uuid();
return callbackTest((callback) => Realm.Sync.User.login('http://localhost:9080', username, undefined, callback), (error, user) => {
assertIsAuthError(error, 602, 'https://realm.io/docs/object-server/problems/missing-parameters');
TestCase.assertUndefined(user);
return new Promise((resolve, _reject) => {
TestCase.assertThrows(() => {
Realm.Sync.User.login('http://localhost:9080', username, undefined, () => {});
});
resolve();
});
},