Skip to content

Commit 5139520

Browse files
committed
python: validate data it reads from nodes
1 parent a1401b2 commit 5139520

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

python/alternator_lb.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ipaddress
12
import threading
23
import time
34
import logging
@@ -9,6 +10,7 @@
910
from concurrent.futures import ThreadPoolExecutor
1011

1112

13+
1214
class ExecutorPool:
1315
def __init__(self):
1416
self._executor = None
@@ -45,6 +47,7 @@ class Config:
4547
port: int = 8080
4648
datacenter: str = None
4749
rack: str = None
50+
client_cert: str = None
4851
aws_region_name: str = "fake-alternator-lb-region"
4952
aws_access_key_id: str = "fake-alternator-lb-access-key-id"
5053
aws_secret_access_key: str = "fake-alternator-lb-secret-access-key"
@@ -105,10 +108,12 @@ def __init__(self, config: Config):
105108
self._next_update_time = 0
106109

107110
@staticmethod
108-
def _validate_uri(uri: str):
109-
parsed_uri = urlparse(uri)
110-
if not parsed_uri.scheme or not parsed_uri.netloc:
111-
raise ValueError(f"Invalid URI: {uri}")
111+
def _validate_node(node: str) -> bool:
112+
try:
113+
ipaddress.ip_address(node)
114+
return True
115+
except ValueError:
116+
return False
112117

113118
def _update_nodes_if_needed(self):
114119
if self._updating:
@@ -162,7 +167,7 @@ def _get_nodes(self, uri: str) -> List[str]:
162167
return []
163168

164169
nodes = response.json()
165-
return [self._host_to_uri(host) for host in nodes if host]
170+
return [self._host_to_uri(host) for host in nodes if host and self._validate_node(host)]
166171
except Exception as e:
167172
self._logger.warning(f"Failed to fetch nodes from {uri}: {e}")
168173
return []

0 commit comments

Comments
 (0)