refactor: simplify _validate_url function

This commit is contained in:
Re-bin 2026-02-03 17:13:30 +00:00
parent 1660d7b261
commit 1a784fca1e

View File

@ -31,30 +31,16 @@ def _normalize(text: str) -> str:
def _validate_url(url: str) -> tuple[bool, str]: def _validate_url(url: str) -> tuple[bool, str]:
""" """Validate URL: must be http(s) with valid domain."""
Validate URL for security.
Returns:
(is_valid, error_message): Tuple of validation result and error message if invalid.
"""
try: try:
parsed = urlparse(url) p = urlparse(url)
if p.scheme not in ('http', 'https'):
# Check if scheme exists return False, f"Only http/https allowed, got '{p.scheme or 'none'}'"
if not parsed.scheme: if not p.netloc:
return False, "URL must include a scheme (http:// or https://)" return False, "Missing domain"
# Only allow http and https schemes
if parsed.scheme.lower() not in ('http', 'https'):
return False, f"Invalid URL scheme '{parsed.scheme}'. Only http:// and https:// are allowed"
# Check if netloc (domain) exists
if not parsed.netloc:
return False, "URL must include a valid domain"
return True, "" return True, ""
except Exception as e: except Exception as e:
return False, f"Invalid URL format: {str(e)}" return False, str(e)
class WebSearchTool(Tool): class WebSearchTool(Tool):