Skip to content

Potential bug in connection.rb? #416

@slj

Description

@slj

Under Rails 3.1.2, I'm running a rake task that uses net-ldap 0.16.1, and this happens (info redacted for security):

rake aborted!
TypeError: no implicit conversion of Hash into String
/usr/local/rvm/gems/ruby-3.1.2.../gems/net-ldap-0.16.1/lib/net/ldap/connection.rb:709:in `new'
/usr/local/rvm/gems/ruby-3.1.2.../gems/net-ldap-0.16.1/lib/net/ldap/connection.rb:53:in `block in open_connection'
/usr/local/rvm/gems/ruby-3.1.2.../gems/net-ldap-0.16.1/lib/net/ldap/connection.rb:51:in `each'
/usr/local/rvm/gems/ruby-3.1.2.../gems/net-ldap-0.16.1/lib/net/ldap/connection.rb:51:in `open_connection'
/usr/local/rvm/gems/ruby-3.1.2.../gems/net-ldap-0.16.1/lib/net/ldap/connection.rb:698:in `socket'
/usr/local/rvm/gems/ruby-3.1.2.../gems/net-ldap-0.16.1/lib/net/ldap.rb:1321:in `new_connection'
/usr/local/rvm/gems/ruby-3.1.2.../gems/net-ldap-0.16.1/lib/net/ldap.rb:866:in `block in bind'
/usr/local/rvm/gems/ruby-3.1.2.../gems/net-ldap-0.16.1/lib/net/ldap/instrumentation.rb:19:in `instrument'
/usr/local/rvm/gems/ruby-3.1.2.../gems/net-ldap-0.16.1/lib/net/ldap.rb:860:in `bind'
<rake-task-filename>.rake:85:in `block (2 levels) in <top (required)>'
Tasks: TOP => import:ldap

The rake task attempts an LDAP connection as follows:

      conn = Net::LDAP.new(:host       => $ldap['host'],
                           :port       => $ldap['port'],
                           :base       => $ldap['base'],
                           :encryption => :simple_tls)
      conn.authenticate($ldap['bind_dn'], $ldap['password'])
      unless conn.bind    ############################ THIS IS LINE 85 mentioned in the error output
        $output << "LDAP import failed to connect (#{Rails.env})"
        runtime = Time.now - start
        Rails.logger.info("[#{Time.now}] - import:ldap rake task aborted after #{runtime} seconds (login failure)")
        ...
        exit
      end

Line 709 of /usr/local/rvm/gems/ruby-3.1.2.../gems/net-ldap-0.16.1/lib/net/ldap/connection.rb reads:

Socket.tcp(host, port, socket_opts)

This conflicts with https://ruby-doc.org/core-3.1.2/Socket.html#method-c-tcp, where the 3rd argument is a local_host value:

tcp(host, port, local_host=nil, local_port=nil, [opts]) {|socket| ... }
tcp(host, port, local_host=nil, local_port=nil, [opts])

Here's the actual method definition in our /usr/local/rvm/rubies/ruby-3.1.2/lib/ruby/3.1.0/socket.rb:

def self.tcp(host, port, local_host = nil, local_port = nil, connect_timeout: nil, resolv_timeout: nil) # :yield: socket

Clearly, net-ldap is calling Rails' Socket#tcp with a hash as the 3rd parameter, which ultimately crashes the rake task. The 3rd parameter is still a hash in 0.17.1:

  # Wrap around Socket.tcp to normalize with other Socket initializers
  class DefaultSocket
    def self.new(host, port, socket_opts = {})
      Socket.tcp(host, port, **socket_opts)
    end
  end

Please correct me if I'm missing something. Otherwise, can anyone suggest/execute a proper fix?

Thank you for your time and attention.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions