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

Conversation

supercaracal
Copy link
Contributor

@supercaracal supercaracal commented Feb 20, 2022

This PR fixes #1074 .

We can pass server URI strings as option to client like this:

r = Redis.new(cluster: %w[redis://username:password@127.0.0.1:7000])

The username and password may include special characters such that ! & # $ ^ < > -. It needs percent encoding in that case. The URI strings are converted to hash objects and they are passed to internal client object. However, the strings remain escaped. It has to be unescaped before it is passed.

irb(main):001:0> require 'uri'
=> true

irb(main):002:0> URI("redis://foobar:#{URI.encode_www_form_component('!&<123-abc>')}@127.0.0.1:6379").password
=> "%21%26%3C123-abc%3E"

def parse_node_url(addr)
uri = URI(addr)
raise InvalidClientOptionError, "Invalid uri scheme #{addr}" unless VALID_SCHEMES.include?(uri.scheme)
db = uri.path.split('/')[1]&.to_i
{ scheme: uri.scheme, username: uri.user, password: uri.password, host: uri.host, port: uri.port, db: db }
.reject { |_, v| v.nil? || v == '' }
rescue URI::InvalidURIError => err
raise InvalidClientOptionError, err.message
end

def build_clients(options)
clients = options.map do |node_key, option|
next if replica_disabled? && slave?(node_key)
option = option.merge(readonly: true) if slave?(node_key)
client = Client.new(option)
[node_key, client]
end
clients.compact.to_h
end

@supercaracal supercaracal marked this pull request as ready for review February 20, 2022 13:15
@byroot byroot merged commit 8650985 into redis:master Feb 21, 2022
@supercaracal supercaracal deleted the prevent-double-unescaping-for-cluster branch May 2, 2024 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Connecting to Azure Redis Cluster
2 participants