Skip to content

Commit 41543b4

Browse files
committed
Prohibit bindings to topic exchange with empty binding key
1 parent ee8ece2 commit 41543b4

3 files changed

Lines changed: 44 additions & 6 deletions

File tree

deps/rabbit/src/rabbit_exchange_type_topic.erl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ route(#exchange{name = XName}, Msg, Opts) ->
4646
validate(_X) ->
4747
ok.
4848

49+
validate_binding(_X, #binding{key = <<>>}) ->
50+
{error, {binding_invalid, "topic exchange binding key cannot be empty", []}};
4951
validate_binding(_X, _B) ->
5052
ok.
5153

deps/rabbit/test/bindings_SUITE.erl

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ all_tests() ->
4646
unbind_from_volatile_queue,
4747
binding_args_direct_exchange,
4848
binding_args_fanout_exchange,
49+
empty_binding_key_queue_to_topic_exchange,
4950

5051
%% Exchange bindings
5152
bind_and_unbind_direct_exchange,
5253
bind_and_unbind_fanout_exchange,
5354
bind_and_delete_exchange_source,
5455
bind_and_delete_exchange_destination,
55-
bind_to_unknown_exchange
56+
bind_to_unknown_exchange,
57+
empty_binding_key_exchange_to_topic_exchange
5658
].
5759

5860
%% -------------------------------------------------------------------
@@ -842,6 +844,38 @@ bind_and_delete_exchange_source(Config) ->
842844
rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_binding, list, [<<"/">>])),
843845
ok.
844846

847+
empty_binding_key_queue_to_topic_exchange(Config) ->
848+
Server = rabbit_ct_broker_helpers:get_node_config(Config, 0, nodename),
849+
Ch = rabbit_ct_client_helpers:open_channel(Config, Server),
850+
Q = ?config(queue_name, Config),
851+
852+
#'queue.declare_ok'{} = declare(Ch, Q, []),
853+
?assertExit(
854+
{{shutdown,
855+
{server_initiated_close, 406,
856+
<<"PRECONDITION_FAILED - topic exchange binding key cannot be empty">>}},
857+
_},
858+
amqp_channel:call(Ch, #'queue.bind'{exchange = <<"amq.topic">>,
859+
queue = Q,
860+
routing_key = <<"">>})),
861+
?assertEqual([], rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_binding, list_explicit, [])).
862+
863+
empty_binding_key_exchange_to_topic_exchange(Config) ->
864+
Server = rabbit_ct_broker_helpers:get_node_config(Config, 0, nodename),
865+
Ch = rabbit_ct_client_helpers:open_channel(Config, Server),
866+
X = ?config(exchange_name, Config),
867+
868+
#'exchange.declare_ok'{} = amqp_channel:call(Ch, #'exchange.declare'{exchange = X}),
869+
?assertExit(
870+
{{shutdown,
871+
{server_initiated_close, 406,
872+
<<"PRECONDITION_FAILED - topic exchange binding key cannot be empty">>}},
873+
_},
874+
amqp_channel:call(Ch, #'exchange.bind'{source = <<"amq.topic">>,
875+
destination = X,
876+
routing_key = <<"">>})),
877+
?assertEqual([], rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_binding, list_explicit, [])).
878+
845879
%% Internal
846880

847881
delete_queues() ->

deps/rabbit/test/rabbit_db_topic_exchange_SUITE.erl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ topic_match_empty_routing_key(Config) ->
432432
%% Empty routing key should only match '#'
433433
?assertEqual(
434434
[QHash],
435-
sort_dests(do_match(Config, XName, <<>>, #{}))),
435+
do_match(Config, XName, <<>>, #{})),
436436
%% Non-empty routing key should also match '#'
437437
?assertEqual(
438438
sort_dests([QExact, QHash]),
@@ -905,10 +905,12 @@ topic_gen() ->
905905
list_to_binary(lists:join(".", Words))).
906906

907907
topic_filter_gen() ->
908-
frequency([{3, ?LET(Pat, resize(5, list(mqtt_segment_gen())),
909-
mk_filter_binary_mqtt(Pat))},
910-
{1, ?LET(Pat, resize(5, list(amqp_segment_gen())),
911-
mk_filter_binary_amqp(Pat))}]).
908+
?SUCHTHAT(Filter,
909+
frequency([{3, ?LET(Pat, resize(5, list(mqtt_segment_gen())),
910+
mk_filter_binary_mqtt(Pat))},
911+
{1, ?LET(Pat, resize(5, list(amqp_segment_gen())),
912+
mk_filter_binary_amqp(Pat))}]),
913+
Filter =/= <<>>).
912914

913915
topic_word_gen() ->
914916
oneof([<<"a">>, <<"b">>, <<"c">>, <<"d">>,

0 commit comments

Comments
 (0)