From 49aa426cc562cfa1e0a0df52b68ec1d4b029fbb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20B=C3=B6rjesson?= Date: Mon, 23 Dec 2024 11:28:08 +0100 Subject: [PATCH] Prevent MQTT entities to be bound to AMQP exchanges This is a temporary "hack" that can be removed when MQTT Exchanges and Queues no longer inherit from AMQP entitites. --- src/lavinmq/amqp/exchange/exchange.cr | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/lavinmq/amqp/exchange/exchange.cr b/src/lavinmq/amqp/exchange/exchange.cr index 83e6cefe5a..b230d38259 100644 --- a/src/lavinmq/amqp/exchange/exchange.cr +++ b/src/lavinmq/amqp/exchange/exchange.cr @@ -137,6 +137,30 @@ module LavinMQ notify_observers(ExchangeEvent::Deleted) end + # This outer macro will add a finished macro hook to all inherited classes + # in LavinMQ::AMQP namespace. + macro inherited + {% if @type.name.starts_with?("LavinMQ::AMQP::") %} + # This macro will find the "bind" method of classes inhering from this class + # and redfine them to raise AccessRefused exception if the first argument + # isn't a type in LavinMQ::AMQP namespace. + # + # TODO remove this when LavinMQ::MQTT::Session no longer inherit from + # LavinMQ::AMQP::Queue and LavinMQ::MQTT::Exchange no longer inherit from + # lavinMQ::AMQP::Exchange + macro finished + \{% if (m = @type.methods.find(&.name.== "bind")) %} + def bind(\{{ m.args.map(&.id).join(",").id}}) : Bool + unless \{{m.args[0].name.id}}.class.name.starts_with?("LavinMQ::AMQP::") + raise AccessRefused.new(self) + end + \{{ m.body }} + end + \{% end %} + end + {% end %} + end + def bind(destination : LavinMQ::Destination, routing_key, headers = nil) : Bool raise AccessRefused.new(self) end