Skip to content

Commit 179ebca

Browse files
authored
Remove default subscriptions setup when a device connects (#2296)
All devices, when connecting to a topic ("device", "console", "extensions"), are lumped into a PubSub topic using the channel name (the name used in join/3). Since these names are generic, what ends up happening is that every device is subscribed to a shared topic name that has fastlaning enabled, meaning messages can be sent directly to devices. I believe this could be a slight security issue, as pubsub messages can be sent to all connected devices with just a simple: ```elixir Phoenix.Channel.Server.broadcast(NervesHub.PubSub, "device", "any_event_name", %{just: "a payload"}) ``` Additionally, this topic isn't needed or used, so we can unsubscribe from it.
1 parent 02971dd commit 179ebca

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

lib/nerves_hub_web/channels/console_channel.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ defmodule NervesHubWeb.ConsoleChannel do
6262
def handle_info({:after_join, payload}, socket) do
6363
socket = assign(socket, :version, payload["console_version"])
6464

65+
# all devices are lumped into a `console` topic (the name used in join/3)
66+
# this can be a security issue as pubsub messages can be sent to all connected devices
67+
# additionally, this topic isn't needed or used, so we can unsubscribe from it
68+
socket.endpoint.unsubscribe("console")
69+
6570
socket.endpoint.subscribe("device:console:#{socket.assigns.device.id}")
6671

6772
socket.endpoint.broadcast!(

lib/nerves_hub_web/channels/device_channel.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ defmodule NervesHubWeb.DeviceChannel do
5454

5555
deployment_channel = deployment_channel(device)
5656

57+
# all devices are lumped into a `device` topic (the name used in join/3)
58+
# this can be a security issue as pubsub messages can be sent to all connected devices
59+
# additionally, this topic isn't needed or used, so we can unsubscribe from it
60+
unsubscribe("device")
61+
5762
subscribe("device:#{device.id}")
5863
subscribe(deployment_channel)
5964

lib/nerves_hub_web/channels/extensions_channel.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,13 @@ defmodule NervesHubWeb.ExtensionsChannel do
2525
send(self(), :init_extensions)
2626
end
2727

28+
# all devices are lumped into a `extensions` topic (the name used in join/3)
29+
# this can be a security issue as pubsub messages can be sent to all connected devices
30+
# additionally, this topic isn't needed or used, so we can unsubscribe from it
31+
:ok = socket.endpoint.unsubscribe("extensions")
32+
2833
topic = "device:#{socket.assigns.device.id}:extensions"
29-
:ok = PubSub.subscribe(NervesHub.PubSub, topic)
34+
:ok = socket.endpoint.subscribe(topic)
3035

3136
{:ok, attach_list, socket}
3237
end

0 commit comments

Comments
 (0)