Skip to content

Commit ebee966

Browse files
Joshua MedvinskyJoshua Medvinsky
authored andcommitted
fix: add SSRF protection and enable TLS verification in URI source
1 parent 7013484 commit ebee966

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

llmstack/common/utils/text_extract.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import base64
2+
import ipaddress
23
import logging
34
import re
45
from io import BytesIO
@@ -59,11 +60,34 @@ def connection(self):
5960
return self._connection
6061

6162

63+
def _is_private_url(url):
64+
"""Check if a URL points to a private/internal IP address."""
65+
try:
66+
from urllib.parse import urlparse as _urlparse
67+
parsed = _urlparse(url)
68+
hostname = parsed.hostname
69+
if not hostname:
70+
return True
71+
if hostname in ('localhost', 'metadata.google.internal', 'metadata.internal'):
72+
return True
73+
try:
74+
ip = ipaddress.ip_address(hostname)
75+
if ip.is_private or ip.is_loopback or ip.is_link_local or ip.is_reserved:
76+
return True
77+
except ValueError:
78+
pass
79+
return False
80+
except Exception:
81+
return True
82+
83+
6284
def get_url_content_type(url, connection=None):
85+
if _is_private_url(url):
86+
raise ValueError("URLs pointing to private/internal addresses are not allowed")
6387
response = requests.head(
6488
url,
6589
allow_redirects=True,
66-
verify=False,
90+
verify=True,
6791
_connection=connection,
6892
)
6993

0 commit comments

Comments
 (0)