FEATURE: raise when attempting to publish to invalid targets

FEATURE: raise when attempting to publish to invalid targets

When a user attempts to publish to a an empty group/user or client id list odds are a mistake happened.

Erring on the side of safety we should publish nothing, previously everyone would get the message.

diff --git a/lib/message_bus.rb b/lib/message_bus.rb
index 841ac36..0976407 100644
--- a/lib/message_bus.rb
+++ b/lib/message_bus.rb
@@ -20,6 +20,7 @@ end
 module MessageBus; end
 MessageBus::BACKENDS = {}
 class MessageBus::InvalidMessage < StandardError; end
+class MessageBus::InvalidMessageTarget < MessageBus::InvalidMessage; end
 class MessageBus::BusDestroyed < StandardError; end
 
 # The main server-side interface to a message bus for the purposes of
@@ -329,6 +330,7 @@ module MessageBus::Implementation
   #
   # @raise [MessageBus::BusDestroyed] if the bus is destroyed
   # @raise [MessageBus::InvalidMessage] if attempting to put permission restrictions on a globally-published message
+  # @raise [MessageBus::InvalidMessageTarget] if attempting to publish to a empty group of users
   def publish(channel, data, opts = nil)
     return if @off
 
@@ -348,7 +350,13 @@ module MessageBus::Implementation
       site_id = opts[:site_id]
     end
 
-    raise ::MessageBus::InvalidMessage if (user_ids || group_ids) && global?(channel)
+    if (user_ids || group_ids) && global?(channel)
+      raise ::MessageBus::InvalidMessage
+    end
+
+    if (user_ids == []) || (group_ids == []) || (client_ids == [])
+      raise ::MessageBus::InvalidMessageTarget
+    end
 
     encoded_data = JSON.dump(
       data: data,
diff --git a/spec/lib/message_bus_spec.rb b/spec/lib/message_bus_spec.rb
index 1501b8a..d63af5e 100644
--- a/spec/lib/message_bus_spec.rb
+++ b/spec/lib/message_bus_spec.rb
@@ -266,15 +266,37 @@ describe MessageBus do
     end
 
     it "should exception if publishing restricted messages to user" do
-      lambda do
+      assert_raises(MessageBus::InvalidMessage) do
         @bus.publish("/global/test", "test", user_ids: [1])
-      end.must_raise(MessageBus::InvalidMessage)
+      end
     end
 
     it "should exception if publishing restricted messages to group" do
-      lambda do
+      assert_raises(MessageBus::InvalidMessage) do
         @bus.publish("/global/test", "test", user_ids: [1])
-      end.must_raise(MessageBus::InvalidMessage)
+      end
+    end
+
+    it "should raise if we publish to an empty group or user list" do
+      assert_raises(MessageBus::InvalidMessageTarget) do
+        @bus.publish "/foo", "bar", user_ids: []
+      end
+
+      assert_raises(MessageBus::InvalidMessageTarget) do
+        @bus.publish "/foo", "bar", group_ids: []
+      end
+
+      assert_raises(MessageBus::InvalidMessageTarget) do
+        @bus.publish "/foo", "bar", client_ids: []
+      end
+
+      assert_raises(MessageBus::InvalidMessageTarget) do
+        @bus.publish "/foo", "bar", group_ids: [], user_ids: [1]
+      end
+
+      assert_raises(MessageBus::InvalidMessageTarget) do
+        @bus.publish "/foo", "bar", group_ids: [1], user_ids: []
+      end
     end
   end
 

GitHub sha: fe340ade

1 Like