Skip to content

Commit 75c70e6

Browse files
larsindonv
andauthored
tls hostname verification (#125)
Co-authored-by: Uwe Kubosch <[email protected]>
1 parent 52ef5ff commit 75c70e6

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

Diff for: lib/mqtt/client.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class Client
2424
# @see OpenSSL::SSL::SSLContext::METHODS
2525
attr_accessor :ssl
2626

27+
# Set to false to skip tls hostname verification
28+
attr_accessor :verify_host
29+
2730
# Time (in seconds) between pings to remote server (default is 15 seconds)
2831
attr_accessor :keep_alive
2932

@@ -75,7 +78,8 @@ class Client
7578
:will_payload => nil,
7679
:will_qos => 0,
7780
:will_retain => false,
78-
:ssl => false
81+
:ssl => false,
82+
:verify_host => true
7983
}
8084

8185
# Create and connect a new MQTT Client
@@ -248,6 +252,8 @@ def connect(clientid = nil)
248252
@socket.hostname = @host if @socket.respond_to?(:hostname=)
249253

250254
@socket.connect
255+
256+
@socket.post_connection_check(@host) if @verify_host
251257
else
252258
@socket = tcp_socket
253259
end

Diff for: mqtt.gemspec

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ Gem::Specification.new do |gem|
2929
gem.add_development_dependency 'rubocop', '~> 1.45'
3030
elsif Gem.ruby_version > Gem::Version.new('2.0')
3131
gem.add_development_dependency 'bundler', '>= 1.11.2'
32-
gem.add_development_dependency 'rake', '>= 10.2.2'
33-
gem.add_development_dependency 'yard', '>= 0.9.11'
32+
gem.add_development_dependency 'rake', '>= 12.3.3'
33+
gem.add_development_dependency 'yard', '>= 0.9.20'
3434
gem.add_development_dependency 'rspec', '>= 3.5.0'
3535
gem.add_development_dependency 'simplecov','>= 0.9.2'
3636
gem.add_development_dependency 'rubocop', '~> 0.48.0'

Diff for: spec/mqtt_client_spec.rb

+13
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ def now
442442
it "should use ssl if it enabled using the :ssl => true parameter" do
443443
expect(OpenSSL::SSL::SSLSocket).to receive(:new).and_return(ssl_socket)
444444
expect(ssl_socket).to receive(:connect)
445+
expect(ssl_socket).to receive(:post_connection_check).with('mqtt.example.com')
445446

446447
client = MQTT::Client.new('mqtt.example.com', :ssl => true)
447448
allow(client).to receive(:receive_connack)
@@ -451,6 +452,7 @@ def now
451452
it "should use ssl if it enabled using the mqtts:// scheme" do
452453
expect(OpenSSL::SSL::SSLSocket).to receive(:new).and_return(ssl_socket)
453454
expect(ssl_socket).to receive(:connect)
455+
expect(ssl_socket).to receive(:post_connection_check).with('mqtt.example.com')
454456

455457
client = MQTT::Client.new('mqtts://mqtt.example.com')
456458
allow(client).to receive(:receive_connack)
@@ -460,6 +462,7 @@ def now
460462
it "should use set the SSL version, if the :ssl parameter is a symbol" do
461463
expect(OpenSSL::SSL::SSLSocket).to receive(:new).and_return(ssl_socket)
462464
expect(ssl_socket).to receive(:connect)
465+
expect(ssl_socket).to receive(:post_connection_check).with('mqtt.example.com')
463466

464467
client = MQTT::Client.new('mqtt.example.com', :ssl => :TLSv1)
465468
expect(client.ssl_context).to receive('ssl_version=').with(:TLSv1)
@@ -470,11 +473,21 @@ def now
470473
it "should use set hostname on the SSL socket for SNI" do
471474
expect(OpenSSL::SSL::SSLSocket).to receive(:new).and_return(ssl_socket)
472475
expect(ssl_socket).to receive(:hostname=).with('mqtt.example.com')
476+
expect(ssl_socket).to receive(:post_connection_check).with('mqtt.example.com')
473477

474478
client = MQTT::Client.new('mqtts://mqtt.example.com')
475479
allow(client).to receive(:receive_connack)
476480
client.connect
477481
end
482+
483+
it "should skip host verification" do
484+
expect(OpenSSL::SSL::SSLSocket).to receive(:new).and_return(ssl_socket)
485+
expect(ssl_socket).to receive(:connect)
486+
487+
client = MQTT::Client.new('mqtt.example.com', :ssl => true, :verify_host => false)
488+
allow(client).to receive(:receive_connack)
489+
client.connect
490+
end
478491
end
479492

480493
context "with a last will and testament set" do

0 commit comments

Comments
 (0)