Skip to content

Commit f20612c

Browse files
committed
close connection when redis returns WRONGPASS
1 parent 7655bb8 commit f20612c

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

lib/redis_client.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,7 @@ def connect
850850
connect_error.set_backtrace(error.backtrace)
851851
raise connect_error
852852
rescue CommandError => error
853+
@raw_connection&.close
853854
if error.message.match?(/ERR unknown command ['`]HELLO['`]/)
854855
raise UnsupportedServer,
855856
"redis-client requires Redis 6+ with HELLO command available (#{config.server_url})"

test/shared/redis_client_tests.rb

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,12 @@ def test_command_missing
328328
end
329329

330330
def test_authentication
331-
@redis.call("ACL", "SETUSER", "AzureDiamond", ">hunter2", "on", "+PING")
331+
@redis.call("ACL", "DELUSER", "AzureDiamond")
332+
@redis.call("ACL", "SETUSER", "AzureDiamond", ">hunter2", "on", "+PING", "+CLIENT")
333+
@redis.call("ACL", "DELUSER", "backup_admin")
334+
@redis.call("ACL", "SETUSER", "backup_admin", ">hunter2", "on", "~*", "&*", "+@all")
335+
backup = new_client(username: "backup_admin", password: "hunter2")
336+
backup.call("ACL", "SETUSER", "default", "off")
332337

333338
client = new_client(username: "AzureDiamond", password: "hunter2")
334339
assert_equal "PONG", client.call("PING")
@@ -337,10 +342,70 @@ def test_authentication
337342
client.call("GET", "foo")
338343
end
339344

345+
# Wrong password
340346
client = new_client(username: "AzureDiamond", password: "trolilol")
341-
assert_raises RedisClient::AuthenticationError do
347+
error = assert_raises RedisClient::AuthenticationError do
342348
client.call("PING")
343349
end
350+
assert_match(/WRONGPASS invalid username-password pair/, error.message)
351+
352+
# The same error is raised, this shows that the client retried AUTH and didn't fall back to the default user
353+
error = assert_raises RedisClient::AuthenticationError do
354+
client.call("PING")
355+
end
356+
assert_match(/WRONGPASS invalid username-password pair/, error.message)
357+
358+
# Correct password, but user disabled
359+
backup.call("ACL", "SETUSER", "AzureDiamond", "<hunter2", ">trolilol", "off")
360+
error = assert_raises RedisClient::AuthenticationError do
361+
client.call_once("PING")
362+
end
363+
assert_match(/WRONGPASS invalid username-password pair/, error.message)
364+
365+
# Correct password, user enabled
366+
backup.call("ACL", "SETUSER", "AzureDiamond", "on")
367+
assert_equal "PONG", client.call_once("PING")
368+
assert_match(/user=AzureDiamond/, client.call("CLIENT", "INFO"))
369+
370+
# Wrong username
371+
client = new_client(username: "GreenOpal", password: "trolilol")
372+
error = assert_raises RedisClient::AuthenticationError do
373+
client.call("PING")
374+
end
375+
assert_match(/WRONGPASS invalid username-password pair/, error.message)
376+
ensure
377+
backup.call("ACL", "SETUSER", "default", "on")
378+
end
379+
380+
def test_prelude_failure
381+
client = new_client(db: 100)
382+
error = assert_raises RedisClient::CommandError do
383+
client.call("PING")
384+
end
385+
assert_match(/ERR DB index is out of range/, error.message)
386+
387+
error = assert_raises RedisClient::CommandError do
388+
client.call("PING")
389+
end
390+
assert_match(/ERR DB index is out of range/, error.message)
391+
end
392+
393+
def test_noauth
394+
@redis.call("ACL", "DELUSER", "AzureDiamond")
395+
@redis.call("ACL", "SETUSER", "AzureDiamond", ">hunter2", "on", "~*", "&*", "+@all")
396+
backup = new_client(username: "AzureDiamond", password: "hunter2")
397+
backup.call("ACL", "SETUSER", "default", "off")
398+
399+
client = new_client(protocol: 2)
400+
error = assert_raises RedisClient::CommandError do
401+
client.call("PING")
402+
end
403+
assert_match(/NOAUTH Authentication required/, error.message)
404+
405+
backup.call("ACL", "SETUSER", "default", "on")
406+
client.call("PING")
407+
ensure
408+
backup.call("ACL", "SETUSER", "default", "on")
344409
end
345410

346411
def test_transaction

0 commit comments

Comments
 (0)