Fix critical issues: Redis host/port parsing, Ollama URL sanitization, and Hugging Face provider initialization
Browse files- core/providers/huggingface.py +1 -1
- core/redis_client.py +16 -6
- utils/config.py +19 -8
- utils/config_validator.py +13 -3
core/providers/huggingface.py
CHANGED
|
@@ -25,7 +25,7 @@ class HuggingFaceProvider(LLMProvider):
|
|
| 25 |
if not config.hf_token:
|
| 26 |
raise ValueError("HF_TOKEN not set - required for Hugging Face provider")
|
| 27 |
|
| 28 |
-
# Fixed OpenAI client instantiation
|
| 29 |
self.client = OpenAI(
|
| 30 |
base_url=config.hf_api_url,
|
| 31 |
api_key=config.hf_token
|
|
|
|
| 25 |
if not config.hf_token:
|
| 26 |
raise ValueError("HF_TOKEN not set - required for Hugging Face provider")
|
| 27 |
|
| 28 |
+
# Fixed OpenAI client instantiation - removed deprecated proxies parameter
|
| 29 |
self.client = OpenAI(
|
| 30 |
base_url=config.hf_api_url,
|
| 31 |
api_key=config.hf_token
|
core/redis_client.py
CHANGED
|
@@ -82,18 +82,28 @@ class RedisClient:
|
|
| 82 |
|
| 83 |
def _parse_host_port(self, host_string: str, default_port: int) -> tuple:
|
| 84 |
"""Parse host and port from host string"""
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
| 86 |
host_string = host_string.strip()
|
|
|
|
| 87 |
|
| 88 |
-
#
|
| 89 |
-
if ':' in host_string and not host_string.startswith('['): # IPv6
|
| 90 |
-
|
|
|
|
| 91 |
try:
|
| 92 |
host = parts[0]
|
| 93 |
port = int(parts[1])
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
except ValueError:
|
| 96 |
-
# Port is not a valid integer
|
| 97 |
return host_string, default_port
|
| 98 |
|
| 99 |
return host_string, default_port
|
|
|
|
| 82 |
|
| 83 |
def _parse_host_port(self, host_string: str, default_port: int) -> tuple:
|
| 84 |
"""Parse host and port from host string"""
|
| 85 |
+
if not host_string:
|
| 86 |
+
return "localhost", default_port
|
| 87 |
+
|
| 88 |
+
# Remove any whitespace and control characters
|
| 89 |
host_string = host_string.strip()
|
| 90 |
+
host_string = re.sub(r'[\r\n\t\0]+', '', host_string)
|
| 91 |
|
| 92 |
+
# Handle case where port is included in REDIS_HOST (e.g., "host:port")
|
| 93 |
+
if ':' in host_string and not host_string.startswith('['): # Not IPv6
|
| 94 |
+
# Check if the part after : is a valid port number
|
| 95 |
+
parts = host_string.rsplit(':', 1)
|
| 96 |
try:
|
| 97 |
host = parts[0]
|
| 98 |
port = int(parts[1])
|
| 99 |
+
# Validate that this looks like a port (0-65535)
|
| 100 |
+
if 1 <= port <= 65535:
|
| 101 |
+
return host, port
|
| 102 |
+
else:
|
| 103 |
+
# Invalid port, use default
|
| 104 |
+
return host_string, default_port
|
| 105 |
except ValueError:
|
| 106 |
+
# Port is not a valid integer, use default
|
| 107 |
return host_string, default_port
|
| 108 |
|
| 109 |
return host_string, default_port
|
utils/config.py
CHANGED
|
@@ -43,14 +43,22 @@ class Config:
|
|
| 43 |
# Remove leading/trailing whitespace and control characters
|
| 44 |
url = url.strip()
|
| 45 |
|
| 46 |
-
# Remove any
|
| 47 |
-
url = re.sub(r'[\r\n\t\0]+', '', url)
|
| 48 |
|
| 49 |
-
#
|
|
|
|
|
|
|
|
|
|
| 50 |
if url and not re.match(r'^https?://', url):
|
| 51 |
-
#
|
| 52 |
-
url
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
return url
|
| 55 |
|
| 56 |
def _sanitize_host(self, host: str) -> str:
|
|
@@ -61,8 +69,11 @@ class Config:
|
|
| 61 |
# Remove leading/trailing whitespace and control characters
|
| 62 |
host = host.strip()
|
| 63 |
|
| 64 |
-
# Remove any newlines
|
| 65 |
-
host = re.sub(r'[\r\n\t\0]+', '', host)
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
return host
|
| 68 |
|
|
|
|
| 43 |
# Remove leading/trailing whitespace and control characters
|
| 44 |
url = url.strip()
|
| 45 |
|
| 46 |
+
# Remove any newlines, carriage returns, tabs, and null bytes
|
| 47 |
+
url = re.sub(r'[\r\n\t\0\x0b\x0c]+', '', url)
|
| 48 |
|
| 49 |
+
# Remove any trailing URL encoding artifacts
|
| 50 |
+
url = url.rstrip('%0a').rstrip('%0d')
|
| 51 |
+
|
| 52 |
+
# Validate URL format - if it looks like a URL but missing scheme
|
| 53 |
if url and not re.match(r'^https?://', url):
|
| 54 |
+
# Check if it looks like a domain
|
| 55 |
+
if re.match(r'^[a-zA-Z0-9.-]+(?:\.[a-zA-Z]{2,})+', url) or 'ngrok' in url:
|
| 56 |
+
# Assume https for ngrok and domains
|
| 57 |
+
url = 'https://' + url
|
| 58 |
+
else:
|
| 59 |
+
# Otherwise default to http
|
| 60 |
+
url = 'http://' + url
|
| 61 |
+
|
| 62 |
return url
|
| 63 |
|
| 64 |
def _sanitize_host(self, host: str) -> str:
|
|
|
|
| 69 |
# Remove leading/trailing whitespace and control characters
|
| 70 |
host = host.strip()
|
| 71 |
|
| 72 |
+
# Remove any newlines, carriage returns, tabs, and null bytes
|
| 73 |
+
host = re.sub(r'[\r\n\t\0\x0b\x0c]+', '', host)
|
| 74 |
+
|
| 75 |
+
# Remove any trailing URL encoding artifacts
|
| 76 |
+
host = host.rstrip('%0a').rstrip('%0d')
|
| 77 |
|
| 78 |
return host
|
| 79 |
|
utils/config_validator.py
CHANGED
|
@@ -17,9 +17,18 @@ class ConfigValidator:
|
|
| 17 |
'warnings': []
|
| 18 |
}
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
# Validate Ollama host
|
| 21 |
if config.ollama_host:
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
report['errors'].append(f"Invalid OLLAMA_HOST format: {config.ollama_host}")
|
| 24 |
report['valid'] = False
|
| 25 |
else:
|
|
@@ -27,8 +36,9 @@ class ConfigValidator:
|
|
| 27 |
|
| 28 |
# Validate Redis configuration
|
| 29 |
if config.redis_host:
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
else:
|
| 33 |
report['warnings'].append("Redis not configured, using in-memory storage")
|
| 34 |
|
|
|
|
| 17 |
'warnings': []
|
| 18 |
}
|
| 19 |
|
| 20 |
+
# Log current configuration for debugging
|
| 21 |
+
logger.info(f"Current configuration:")
|
| 22 |
+
logger.info(f" OLLAMA_HOST: '{config.ollama_host}'")
|
| 23 |
+
logger.info(f" REDIS_HOST: '{config.redis_host}'")
|
| 24 |
+
logger.info(f" REDIS_PORT: {config.redis_port}")
|
| 25 |
+
|
| 26 |
# Validate Ollama host
|
| 27 |
if config.ollama_host:
|
| 28 |
+
sanitized = config._sanitize_url(config.ollama_host)
|
| 29 |
+
if sanitized != config.ollama_host:
|
| 30 |
+
logger.info(f"Sanitized OLLAMA_HOST from '{config.ollama_host}' to '{sanitized}'")
|
| 31 |
+
if not ConfigValidator._is_valid_url(sanitized):
|
| 32 |
report['errors'].append(f"Invalid OLLAMA_HOST format: {config.ollama_host}")
|
| 33 |
report['valid'] = False
|
| 34 |
else:
|
|
|
|
| 36 |
|
| 37 |
# Validate Redis configuration
|
| 38 |
if config.redis_host:
|
| 39 |
+
sanitized_host = config._sanitize_host(config.redis_host)
|
| 40 |
+
if sanitized_host != config.redis_host:
|
| 41 |
+
logger.info(f"Sanitized REDIS_HOST from '{config.redis_host}' to '{sanitized_host}'")
|
| 42 |
else:
|
| 43 |
report['warnings'].append("Redis not configured, using in-memory storage")
|
| 44 |
|