Commit 125b4e1a authored by Arnout Kazemier's avatar Arnout Kazemier

Merge pull request #160 from MakerAndrew/master

Add key validation to getMulti requests (Issue #159)
parents 01b68784 0d679625
......@@ -864,9 +864,11 @@ Client.config = {
memcached.command(function getMultiCommand(noreply) {
return {
callback: handle
, multi:true
, multi: true
, type: 'get'
, command: 'get ' + key.join(' ')
, key: keys
, validate: [['key', Array], ['callback', Function]]
};
}, server);
});
......
......@@ -29,7 +29,17 @@ exports.validateArg = function validateArg (args, config) {
if (toString.call(value) !== '[object Array]') {
err = 'Argument "' + key + '" is not a valid Array.';
}
if (!err && key === 'key') {
for (var vKey in value) {
var vValue = value[vKey];
var result = validateKeySize(config, vKey, vValue);
if (result.err) {
err = result.err;
} else {
args.command = args.command.replace(vValue, result['value']);
}
}
}
break;
case Object:
......@@ -52,17 +62,11 @@ exports.validateArg = function validateArg (args, config) {
}
if (!err && key === 'key') {
if (value.length > config.maxKeySize) {
if (config.keyCompression){
args[key] = createHash('md5').update(value).digest('hex');
// also make sure you update the command
args.command = args.command.replace(value, args[key]);
} else {
err = 'Argument "' + key + '" is longer than the maximum allowed length of ' + config.maxKeySize;
}
} else if (/[\s\n\r]/.test(value)) {
err = 'The key should not contain any whitespace or new lines';
var result = validateKeySize(config, key, value);
if (result.err) {
err = result.err;
} else {
args.command = args.command.replace(value, result['value']);
}
}
break;
......@@ -75,16 +79,30 @@ exports.validateArg = function validateArg (args, config) {
});
if (err){
if(args.callback) args.callback(new Error(err));
if (args.callback) args.callback(new Error(err));
return false;
}
return true;
};
var validateKeySize = function validateKeySize(config, key, value) {
if (value.length > config.maxKeySize) {
if (config.keyCompression){
return { err: false, value: createHash('md5').update(value).digest('hex') };
} else {
return { err: 'Argument "' + key + '" is longer than the maximum allowed length of ' + config.maxKeySize };
}
} else if (/[\s\n\r]/.test(value)) {
return { err: 'The key should not contain any whitespace or new lines' };
} else {
return { err: false, value: value };
}
};
// a small util to use an object for eventEmitter
exports.fuse = function fuse (target, handlers) {
for(var i in handlers)
for (var i in handlers)
if (handlers.hasOwnProperty(i)){
target.on(i, handlers[i]);
}
......
......@@ -604,4 +604,25 @@ describe("Memcached GET SET", function() {
done();
});
});
/*
Make sure that getMulti calls work for very long keys.
If the keys aren't hashed because they are too long, memcached will throw exceptions, so we need to make sure that exceptions aren't thrown.
*/
it("make sure you can getMulti really long keys", function(done) {
var memcached = new Memcached(common.servers.single)
, message = 'My value is not relevant'
, testnr1 = "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"+(++global.testnumbers)
, testnr2 = "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"+(global.testnumbers)+"a"
, callbacks = 0;
memcached.getMulti([ testnr1, testnr2 ], function(error, ok) {
++callbacks;
assert.ok(!error);
memcached.end();
assert.equal(callbacks, 1);
done();
});
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment