Skip to content

Commit 457c932

Browse files
author
DanielePalaia
committed
check destination url for publishers and consumers
1 parent b35315d commit 457c932

File tree

5 files changed

+57
-1
lines changed

5 files changed

+57
-1
lines changed

rabbitmq_amqp_python_client/address_helper.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,9 @@ def binding_path_with_exchange_queue(
7272
+ ";args="
7373
)
7474
return binding_path_wth_exchange_queue_key
75+
76+
77+
def validate_address(address: str) -> bool:
78+
if address.startswith("/queues") or address.startswith("/exchanges"):
79+
return True
80+
return False

rabbitmq_amqp_python_client/connection.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import logging
22
from typing import Optional
33

4+
from .address_helper import validate_address
45
from .consumer import Consumer
6+
from .exceptions import ArgumentOutOfRangeException
57
from .management import Management
68
from .publisher import Publisher
79
from .qpid.proton._handlers import MessagingHandler
@@ -35,11 +37,19 @@ def close(self) -> None:
3537
self._conn.close()
3638

3739
def publisher(self, destination: str) -> Publisher:
40+
if validate_address(destination) is False:
41+
raise ArgumentOutOfRangeException(
42+
"destination address must start with /queue or /exchanges"
43+
)
3844
publisher = Publisher(self._conn, destination)
3945
return publisher
4046

4147
def consumer(
4248
self, destination: str, handler: Optional[MessagingHandler] = None
4349
) -> Consumer:
50+
if validate_address(destination) is False:
51+
raise ArgumentOutOfRangeException(
52+
"destination address must start with /queue or /exchanges"
53+
)
4454
consumer = Consumer(self._conn, destination, handler)
4555
return consumer

rabbitmq_amqp_python_client/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
def validate_annotations(annotations: []) -> bool: # type: ignore
22
validated = True
33
for annotation in annotations:
4-
if len(annotation) > 0 and annotation[:2] == "x-":
4+
if annotation.startswith("x-"):
55
pass
66
else:
77
validated = False

tests/test_consumer.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,24 @@ def test_consumer_sync_queue_accept(connection: Connection) -> None:
5353
assert consumed > 0
5454

5555

56+
def test_consumer_invalid_destination(connection: Connection) -> None:
57+
58+
queue_name = "test-queue-sync-invalid-accept"
59+
raised = False
60+
consumer = None
61+
try:
62+
consumer = connection.consumer("/invalid-destination/" + queue_name)
63+
except ArgumentOutOfRangeException:
64+
raised = True
65+
except Exception:
66+
raised = False
67+
68+
if consumer is not None:
69+
consumer.close()
70+
71+
assert raised is True
72+
73+
5674
def test_consumer_async_queue_accept(connection: Connection) -> None:
5775

5876
messages_to_send = 1000

tests/test_publisher.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from rabbitmq_amqp_python_client import (
22
AddressHelper,
3+
ArgumentOutOfRangeException,
34
BindingSpecification,
45
Connection,
56
ExchangeSpecification,
@@ -31,6 +32,27 @@ def test_publish_queue(connection: Connection) -> None:
3132
assert raised is False
3233

3334

35+
def test_publish_to_invalid_destination(connection: Connection) -> None:
36+
37+
queue_name = "test-queue"
38+
39+
raised = False
40+
41+
publisher = None
42+
try:
43+
publisher = connection.publisher("/invalid-destination/" + queue_name)
44+
publisher.publish(Message(body="test"))
45+
except ArgumentOutOfRangeException:
46+
raised = True
47+
except Exception:
48+
raised = False
49+
50+
if publisher is not None:
51+
publisher.close()
52+
53+
assert raised is True
54+
55+
3456
def test_publish_exchange(connection: Connection) -> None:
3557

3658
exchange_name = "test-exchange"

0 commit comments

Comments
 (0)