Skip to content

Commit f5b6bfa

Browse files
committed
tls hostname verification
see njh#125
1 parent ca5c69a commit f5b6bfa

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/mqtt/client.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class Client
2626
# @see OpenSSL::SSL::SSLContext::METHODS
2727
attr_accessor :ssl
2828

29+
# Set to false to skip tls hostname verification
30+
attr_accessor :verify_host
31+
2932
# Time (in seconds) between pings to remote server (default is 15 seconds)
3033
attr_accessor :keep_alive
3134

@@ -91,7 +94,8 @@ class Client
9194
will_payload: nil,
9295
will_qos: 0,
9396
will_retain: false,
94-
ssl: false
97+
ssl: false,
98+
verify_host: true
9599
}.freeze
96100

97101
# Create and connect a new MQTT Client
@@ -505,6 +509,8 @@ def connect_internal
505509
@socket.hostname = @host if @socket.respond_to?(:hostname=)
506510

507511
@socket.connect
512+
513+
@socket.post_connection_check(@host) if @verify_host
508514
else
509515
@socket = tcp_socket
510516
end

spec/mqtt/client_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@
404404
it "uses ssl if it enabled using the ssl: true parameter" do
405405
expect(OpenSSL::SSL::SSLSocket).to receive(:new).and_return(ssl_socket)
406406
expect(ssl_socket).to receive(:connect)
407+
expect(ssl_socket).to receive(:post_connection_check).with("mqtt.example.com")
407408

408409
client = MQTT::Client.new("mqtt.example.com", ssl: true)
409410
allow(client).to receive(:receive_connack)
@@ -413,6 +414,7 @@
413414
it "uses ssl if it enabled using the mqtts:// scheme" do
414415
expect(OpenSSL::SSL::SSLSocket).to receive(:new).and_return(ssl_socket)
415416
expect(ssl_socket).to receive(:connect)
417+
expect(ssl_socket).to receive(:post_connection_check).with("mqtt.example.com")
416418

417419
client = MQTT::Client.new("mqtts://mqtt.example.com")
418420
allow(client).to receive(:receive_connack)
@@ -422,6 +424,7 @@
422424
it "uses set the SSL version, if the :ssl parameter is a symbol" do
423425
expect(OpenSSL::SSL::SSLSocket).to receive(:new).and_return(ssl_socket)
424426
expect(ssl_socket).to receive(:connect)
427+
expect(ssl_socket).to receive(:post_connection_check).with("mqtt.example.com")
425428

426429
client = MQTT::Client.new("mqtt.example.com", ssl: :TLSv1)
427430
expect(client.ssl_context).to receive("ssl_version=").with(:TLSv1)
@@ -432,11 +435,21 @@
432435
it "uses set hostname on the SSL socket for SNI" do
433436
expect(OpenSSL::SSL::SSLSocket).to receive(:new).and_return(ssl_socket)
434437
expect(ssl_socket).to receive(:hostname=).with("mqtt.example.com")
438+
expect(ssl_socket).to receive(:post_connection_check).with("mqtt.example.com")
435439

436440
client = MQTT::Client.new("mqtts://mqtt.example.com")
437441
allow(client).to receive(:receive_connack)
438442
client.connect
439443
end
444+
445+
it "skips host verification" do
446+
expect(OpenSSL::SSL::SSLSocket).to receive(:new).and_return(ssl_socket)
447+
expect(ssl_socket).to receive(:connect)
448+
449+
client = MQTT::Client.new("mqtt.example.com", ssl: true, verify_host: false)
450+
allow(client).to receive(:receive_connack)
451+
client.connect
452+
end
440453
end
441454

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

0 commit comments

Comments
 (0)