Skip to content

Commit c269b75

Browse files
author
Ruben Bridgewater
committed
Check that the password is from type string
1 parent e24f056 commit c269b75

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");
@@ -911,6 +911,16 @@ RedisClient.prototype.select = RedisClient.prototype.SELECT = function (db, call
911911

912912
// Stash auth for connect and reconnect. Send immediately if already connected.
913913
RedisClient.prototype.auth = RedisClient.prototype.AUTH = function (pass, callback) {
914+
if (typeof pass !== 'string') {
915+
var err = new Error('The password has to be of type "string"');
916+
err.command_used = 'AUTH';
917+
if (callback) {
918+
callback(err);
919+
} else {
920+
this.emit('error', err);
921+
}
922+
return;
923+
}
914924
this.auth_pass = pass;
915925
this.auth_callback = callback;
916926
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)