Module: Mongo::Address::Validator Private

Included in:
Srv::Result, URI
Defined in:
lib/mongo/address/validator.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0

Instance Method Summary collapse

Instance Method Details

#validate_address_str!(address_str) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Takes an address string in ipv4/ipv6/hostname/socket path format and validates its format.

Error messages describe the component at fault without quoting the address: a malformed connection string may not have had its credentials delimited, in which case the text being validated is itself the credential. See "Disclosure of Sensitive Values" in the connection string specification.

Since:

  • 2.0.0



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mongo/address/validator.rb', line 29

def validate_address_str!(address_str)
  case address_str
  when /\A\[[\d:]+\](?::(\d+))?\z/
    # ipv6 with optional port
    if (port_str = ::Regexp.last_match(1))
      validate_port_str!(port_str)
    end
  when %r{\A/}, /\.sock\z/
    # Unix socket path.
    # Spec requires us to validate that the path has no unescaped
    # slashes, but if this were to be the case, parsing would have
    # already failed elsewhere because the URI would've been split in
    # a weird place.
    # The spec also allows relative socket paths and requires that
    # socket paths end in ".sock". We accept all paths but special case
    # the .sock extension to avoid relative paths falling into the
    # host:port case below.
  when %r{[/\[\]]}
    # Not a host:port nor an ipv4 address with optional port.
    # Possibly botched ipv6 address with e.g. port delimiter present and
    # port missing, or extra junk before or after.
    raise Error::InvalidAddress, 'Invalid host identifier.'
  when /:.*:/m
    raise Error::InvalidAddress,
          'Multiple port delimiters are not allowed in the host identifier.'
  else
    # host:port or ipv4 address with optional port number
    host, port = address_str.split(':')
    raise Error::InvalidAddress, 'Host is empty.' if host.empty?

    validate_hostname!(host)

    raise Error::InvalidAddress, 'Port is empty.' if port && port.empty?

    validate_port_str!(port)
  end
end