@@ -92,18 +92,28 @@ defmodule NervesHubWeb.DeviceSocket do
9292
9393 # Used by Devices connecting with HMAC Shared Secrets
9494 @ decorate with_span ( "Channels.DeviceSocket.connect" )
95- def connect ( _params , socket , % { x_headers: x_headers } )
95+ def connect ( _params , socket , % { x_headers: x_headers } = connect_info )
9696 when is_list ( x_headers ) and length ( x_headers ) > 0 do
9797 headers = Map . new ( x_headers )
9898
99- with :ok <- check_shared_secret_enabled ( ) ,
99+ with :ok <- check_source_enabled ( connect_info [ :source ] ) ,
100+ :ok <- check_shared_secret_enabled ( ) ,
100101 { :ok , key , salt , verification_opts } <- decode_from_headers ( headers ) ,
101102 { :ok , auth } <- get_shared_secret_auth ( key ) ,
102103 { :ok , signature } <- Map . fetch ( headers , "x-nh-signature" ) ,
103104 { :ok , identifier } <- Crypto . verify ( auth . secret , salt , signature , verification_opts ) ,
104105 { :ok , device } <- get_or_maybe_create_device ( auth , identifier ) do
105106 socket_and_assigns ( socket , device )
106107 else
108+ { :error , :check_uri } = error ->
109+ :telemetry . execute ( [ :nerves_hub , :devices , :invalid_auth ] , % { count: 1 } , % {
110+ auth: :shared_secrets ,
111+ reason: error ,
112+ product_key: Map . get ( headers , "x-nh-key" , "*empty*" )
113+ } )
114+
115+ error
116+
107117 error ->
108118 :telemetry . execute ( [ :nerves_hub , :devices , :invalid_auth ] , % { count: 1 } , % {
109119 auth: :shared_secrets ,
@@ -188,6 +198,14 @@ defmodule NervesHubWeb.DeviceSocket do
188198 end
189199 end
190200
201+ defp check_source_enabled ( source ) do
202+ if source_enabled? ( source ) do
203+ :ok
204+ else
205+ { :error , :check_uri }
206+ end
207+ end
208+
191209 defp socket_and_assigns ( socket , device ) do
192210 # disconnect devices using the same identifier
193211 _ = socket . endpoint . broadcast_from ( self ( ) , "device_socket:#{ device . id } " , "disconnect" , % { } )
@@ -279,4 +297,13 @@ defmodule NervesHubWeb.DeviceSocket do
279297 |> Keyword . get ( :shared_secrets , [ ] )
280298 |> Keyword . get ( :enabled , false )
281299 end
300+
301+ def source_enabled? ( nil ) do
302+ true
303+ end
304+
305+ def source_enabled? ( NervesHubWeb.Endpoint ) do
306+ Application . get_env ( :nerves_hub , __MODULE__ , [ ] )
307+ |> Keyword . get ( :web_endpoint_supported , true )
308+ end
282309end
0 commit comments