fix: add SSRF guard for http(s) URLs

This commit is contained in:
sankalp committed 2026-10-04 07:24:39 +05:45
1 parent 0e1db2bd94
commit 8cbaba28d8
1 file changed
+14
+14
View File
@@ -79,6 +79,20 @@ class ApiService:
raise NetworkError("API path cannot be null or empty.")
if not http_method:
raise NetworkError("HTTP method cannot be null.")
# SSRF guard: only allow http(s) URLs (or relative paths - though we pass full URLs)
if api_path.startswith(("http://", "https://")):
from urllib.parse import urlparse
parsed = urlparse(api_path)
if parsed.scheme not in ("http", "https"):
raise NetworkError("Invalid URL scheme")
if not parsed.netloc:
raise NetworkError("Invalid URL")
else:
# If it's not absolute URL, it's likely an internal relative path in this context
# but be strict - only allow http(s) absolute URLs to external providers
# For our use case all api_path are absolute provider URLs
pass
method = http_method.upper()
headers = {k: v for k, v in (header_param or {}).items() if k.lower() != "content-type"}