Skip to content

Commit 93573ae

Browse files
Alejo Sanchezfruch
authored andcommitted
Check port range
Only allow valid TCP port numbers. Signed-off-by: Alejo Sanchez <[email protected]>
1 parent 8dc0764 commit 93573ae

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

cassandra/cluster.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,9 @@ def __init__(self,
11511151
raise ValueError("Only numeric values are supported for port (%s)" % port)
11521152
port = int(port)
11531153

1154+
if port < 1 or port > 65535:
1155+
raise ValueError("Invalid port number (%s) (1-65535)" % port)
1156+
11541157
if connection_class is not None:
11551158
self.connection_class = connection_class
11561159

tests/unit/test_cluster.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ def test_port_str(self):
132132
cluster = Cluster(contact_points=['127.0.0.1'], port='string')
133133

134134

135+
def test_port_range(self):
136+
for invalid_port in [0, 65536, -1]:
137+
with self.assertRaises(ValueError):
138+
cluster = Cluster(contact_points=['127.0.0.1'], port=invalid_port)
139+
140+
135141
class SchedulerTest(unittest.TestCase):
136142
# TODO: this suite could be expanded; for now just adding a test covering a ticket
137143

0 commit comments

Comments
 (0)