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

Handle unbracketed ipv6 addresses #538

Merged
merged 6 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
44 changes: 22 additions & 22 deletions lib/sshkit/host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,27 @@ def attributes

end

# @private
# :nodoc:
class IPv6HostWithPortParser < SimpleHostParser
IPV6_REGEX = /\A\[?([a-fA-F0-9:]+)\]?(?:\:(\d+))?\z/

def self.suitable?(host_string)
host_string.match(IPV6_REGEX)
end

def port
prt = @host_string.match(IPV6_REGEX)[2]
prt = prt.to_i unless prt.nil?
prt
end

def hostname
@host_string.match(IPV6_REGEX)[1]
end

end

class HostWithPortParser < SimpleHostParser

def self.suitable?(host_string)
Expand Down Expand Up @@ -148,27 +169,6 @@ def port
end
end

# @private
# :nodoc:
class IPv6HostWithPortParser < SimpleHostParser
IPV6_REGEX = /\[([a-fA-F0-9:]+)\](?:\:(\d+))?/

def self.suitable?(host_string)
host_string.match(IPV6_REGEX)
end

def port
prt = @host_string.match(IPV6_REGEX)[2]
prt = prt.to_i unless prt.nil?
prt
end

def hostname
@host_string.match(IPV6_REGEX)[1]
end

end

# @private
# :nodoc:
class HostWithUsernameParser < SimpleHostParser
Expand All @@ -185,9 +185,9 @@ def hostname

PARSERS = [
SimpleHostParser,
IPv6HostWithPortParser,
HostWithPortParser,
HostWithUsernameAndPortParser,
IPv6HostWithPortParser,
HostWithUsernameParser,
].freeze

Expand Down
6 changes: 6 additions & 0 deletions test/unit/test_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ def test_host_local
assert_equal 'localhost', h.hostname
end

def test_ipv6_without_brackets
h = Host.new '1fff:0:a88:85a3::ac1f'
assert_nil h.port
assert_equal '1fff:0:a88:85a3::ac1f', h.hostname
end

def test_does_not_confuse_ipv6_hosts_with_port_specification
h = Host.new '[1fff:0:a88:85a3::ac1f]:8001'
assert_equal 8001, h.port
Expand Down