Skip to content

Commit 362c8c6

Browse files
committed
Restore Redis 4.3.0 accidental AUTH fallback behavior with a deprecation warning
1 parent 506f922 commit 362c8c6

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Unreleased
22

3+
* Restore the accidential auth behavior of redis-rb 4.3.0 with a warning. If provided with the `default` user password, but a wrong username,
4+
redis-rb will first try to connect as the provided user, but then will fallback to connect as the `default` user with the provided password.
5+
This behavior is deprecated and will be removed in Redis 4.6.0. Fix #1038.
6+
37
# 4.5.0
48

59
* Handle parts of the command using incompatible encodings. See #1037.

lib/redis/client.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ def connect
122122
rescue CommandError => err # Likely on Redis < 6
123123
if err.message.match?(/ERR wrong number of arguments for \'auth\' command/)
124124
call [:auth, password]
125+
elsif err.message.match?(/WRONGPASS invalid username-password pair/)
126+
begin
127+
call [:auth, password]
128+
rescue CommandError
129+
raise err
130+
end
131+
::Kernel.warn(
132+
"[redis-rb] The Redis connection was configured with username #{username.inspect}, but" \
133+
" the provided password was for the default user. This will start failing in redis-rb 4.6."
134+
)
125135
else
126136
raise
127137
end

test/connection_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,34 @@ def test_provides_a_meaningful_inspect
99
assert_equal "#<Redis client v#{Redis::VERSION} for redis://127.0.0.1:#{PORT}/15>", r.inspect
1010
end
1111

12+
def test_connection_with_user_and_password
13+
target_version "6.0" do
14+
with_acl do |username, password|
15+
redis = Redis.new(OPTIONS.merge(username: username, password: password))
16+
assert_equal "PONG", redis.ping
17+
end
18+
end
19+
end
20+
21+
def test_connection_with_default_user_and_password
22+
target_version "6.0" do
23+
with_default_user_password do |_username, password|
24+
redis = Redis.new(OPTIONS.merge(password: password))
25+
assert_equal "PONG", redis.ping
26+
end
27+
end
28+
end
29+
30+
def test_connection_with_wrong_user_and_password
31+
target_version "6.0" do
32+
with_default_user_password do |_username, password|
33+
Kernel.expects(:warn).once
34+
redis = Redis.new(OPTIONS.merge(username: "does-not-exist", password: password))
35+
assert_equal "PONG", redis.ping
36+
end
37+
end
38+
end
39+
1240
def test_connection_information
1341
assert_equal "127.0.0.1", r.connection.fetch(:host)
1442
assert_equal 6381, r.connection.fetch(:port)

test/helper.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,19 @@ def with_acl
172172
'+ping', '+select', '+command', '+cluster|slots', '+cluster|nodes',
173173
'>mysecret')
174174
yield('johndoe', 'mysecret')
175+
ensure
175176
admin.acl('DELUSER', 'johndoe')
176177
admin.close
177178
end
179+
180+
def with_default_user_password
181+
admin = _new_client
182+
admin.acl('SETUSER', 'default', '>mysecret')
183+
yield('default', 'mysecret')
184+
ensure
185+
admin.acl('SETUSER', 'default', 'nopass')
186+
admin.close
187+
end
178188
end
179189

180190
module Client

0 commit comments

Comments
 (0)