Skip to content

Commit eea9d2a

Browse files
committed
Merge pull request #847 from fintura/check-password
Check that the password is from type string
2 parents 30f227b + c269b75 commit eea9d2a

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ RedisClient.prototype.on_connect = function () {
252252

253253
this.init_parser();
254254

255-
if (this.auth_pass) {
255+
if (typeof this.auth_pass === 'string') {
256256
this.do_auth();
257257
} else {
258258
this.emit("connect");
@@ -935,6 +935,16 @@ RedisClient.prototype.select = RedisClient.prototype.SELECT = function (db, call
935935

936936
// Stash auth for connect and reconnect. Send immediately if already connected.
937937
RedisClient.prototype.auth = RedisClient.prototype.AUTH = function (pass, callback) {
938+
if (typeof pass !== 'string') {
939+
var err = new Error('The password has to be of type "string"');
940+
err.command_used = 'AUTH';
941+
if (callback) {
942+
callback(err);
943+
} else {
944+
this.emit('error', err);
945+
}
946+
return;
947+
}
938948
this.auth_pass = pass;
939949
this.auth_callback = callback;
940950
debug("Saving auth as " + this.auth_pass);

test/auth.spec.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ describe("client authentication", function () {
4848
client.auth(auth + 'bad');
4949
});
5050

51-
it("returns an error when auth is bad with a callback", function (done) {
51+
it("returns an error when auth is bad (empty string) with a callback", function (done) {
5252
if (helper.redisProcess().spawnFailed()) this.skip();
5353

5454
client = redis.createClient.apply(redis.createClient, args);
5555

56-
client.auth(auth + 'bad', function (err, res) {
56+
client.auth('', function (err, res) {
5757
assert.strictEqual(err.command_used, 'AUTH');
5858
assert.ok(/ERR invalid password/.test(err.message));
5959
done();
@@ -122,6 +122,30 @@ describe("client authentication", function () {
122122
}
123123
});
124124
});
125+
126+
it('should return an error if the password is not of type string and a callback has been provided', function (done) {
127+
if (helper.redisProcess().spawnFailed()) this.skip();
128+
129+
client = redis.createClient.apply(redis.createClient, args);
130+
client.auth(undefined, function(err, res) {
131+
assert.strictEqual(err.message, 'The password has to be of type "string"');
132+
assert.strictEqual(err.command_used, 'AUTH');
133+
assert.strictEqual(res, undefined);
134+
done();
135+
});
136+
});
137+
138+
it('should emit an error if the password is not of type string and no callback has been provided', function (done) {
139+
if (helper.redisProcess().spawnFailed()) this.skip();
140+
141+
client = redis.createClient.apply(redis.createClient, args);
142+
client.on('error', function (err) {
143+
assert.strictEqual(err.message, 'The password has to be of type "string"');
144+
assert.strictEqual(err.command_used, 'AUTH');
145+
done();
146+
});
147+
client.auth(234567);
148+
});
125149
});
126150
});
127151

0 commit comments

Comments
 (0)