Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a option parsing bug for cluster mode that percent encoded string is passed to server unintended #1076

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/redis/cluster/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ def parse_node_url(addr)
raise InvalidClientOptionError, "Invalid uri scheme #{addr}" unless VALID_SCHEMES.include?(uri.scheme)

db = uri.path.split('/')[1]&.to_i
username = uri.user ? URI.decode_www_form_component(uri.user) : nil
password = uri.password ? URI.decode_www_form_component(uri.password) : nil

{ scheme: uri.scheme, username: uri.user, password: uri.password, host: uri.host, port: uri.port, db: db }
{ scheme: uri.scheme, username: username, password: password, host: uri.host, port: uri.port, db: db }
.reject { |_, v| v.nil? || v == '' }
rescue URI::InvalidURIError => err
raise InvalidClientOptionError, err.message
Expand Down
9 changes: 8 additions & 1 deletion test/cluster_client_options_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative 'helper'
require 'uri'
require 'helper'

# ruby -w -Itest test/cluster_client_options_test.rb
class TestClusterClientOptions < Minitest::Test
Expand Down Expand Up @@ -31,6 +32,12 @@ def test_option_class
option = Redis::Cluster::Option.new(cluster: %w[redis://:bazzap@127.0.0.1:7000], password: 'foobar')
assert_equal({ '127.0.0.1:7000' => { scheme: 'redis', password: 'bazzap', host: '127.0.0.1', port: 7000 } }, option.per_node_key)

option = Redis::Cluster::Option.new(cluster: %W[redis://#{URI.encode_www_form_component('!&<123-abc>')}:@127.0.0.1:7000])
assert_equal({ '127.0.0.1:7000' => { scheme: 'redis', username: '!&<123-abc>', host: '127.0.0.1', port: 7000 } }, option.per_node_key)

option = Redis::Cluster::Option.new(cluster: %W[redis://:#{URI.encode_www_form_component('!&<123-abc>')}@127.0.0.1:7000])
assert_equal({ '127.0.0.1:7000' => { scheme: 'redis', password: '!&<123-abc>', host: '127.0.0.1', port: 7000 } }, option.per_node_key)

option = Redis::Cluster::Option.new(cluster: %w[redis://127.0.0.1:7000/0], db: 1)
assert_equal({ '127.0.0.1:7000' => { scheme: 'redis', host: '127.0.0.1', port: 7000, db: 0 } }, option.per_node_key)

Expand Down