utils.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import asyncio
  2. from datetime import datetime, time, timedelta
  3. import socket
  4. import ssl
  5. import urllib.parse
  6. import certifi
  7. import validators
  8. from typing import AsyncIterator, Dict, List, Optional, Union, Sequence, Iterator
  9. from langchain_community.document_loaders import (
  10. WebBaseLoader,
  11. PlaywrightURLLoader
  12. )
  13. from langchain_core.documents import Document
  14. from open_webui.constants import ERROR_MESSAGES
  15. from open_webui.config import ENABLE_RAG_LOCAL_WEB_FETCH, RAG_WEB_LOADER
  16. from open_webui.env import SRC_LOG_LEVELS
  17. import logging
  18. log = logging.getLogger(__name__)
  19. log.setLevel(SRC_LOG_LEVELS["RAG"])
  20. def validate_url(url: Union[str, Sequence[str]]):
  21. if isinstance(url, str):
  22. if isinstance(validators.url(url), validators.ValidationError):
  23. raise ValueError(ERROR_MESSAGES.INVALID_URL)
  24. if not ENABLE_RAG_LOCAL_WEB_FETCH:
  25. # Local web fetch is disabled, filter out any URLs that resolve to private IP addresses
  26. parsed_url = urllib.parse.urlparse(url)
  27. # Get IPv4 and IPv6 addresses
  28. ipv4_addresses, ipv6_addresses = resolve_hostname(parsed_url.hostname)
  29. # Check if any of the resolved addresses are private
  30. # This is technically still vulnerable to DNS rebinding attacks, as we don't control WebBaseLoader
  31. for ip in ipv4_addresses:
  32. if validators.ipv4(ip, private=True):
  33. raise ValueError(ERROR_MESSAGES.INVALID_URL)
  34. for ip in ipv6_addresses:
  35. if validators.ipv6(ip, private=True):
  36. raise ValueError(ERROR_MESSAGES.INVALID_URL)
  37. return True
  38. elif isinstance(url, Sequence):
  39. return all(validate_url(u) for u in url)
  40. else:
  41. return False
  42. def safe_validate_urls(url: Sequence[str]) -> Sequence[str]:
  43. valid_urls = []
  44. for u in url:
  45. try:
  46. if validate_url(u):
  47. valid_urls.append(u)
  48. except ValueError:
  49. continue
  50. return valid_urls
  51. def resolve_hostname(hostname):
  52. # Get address information
  53. addr_info = socket.getaddrinfo(hostname, None)
  54. # Extract IP addresses from address information
  55. ipv4_addresses = [info[4][0] for info in addr_info if info[0] == socket.AF_INET]
  56. ipv6_addresses = [info[4][0] for info in addr_info if info[0] == socket.AF_INET6]
  57. return ipv4_addresses, ipv6_addresses
  58. def extract_metadata(soup, url):
  59. metadata = {
  60. "source": url
  61. }
  62. if title := soup.find("title"):
  63. metadata["title"] = title.get_text()
  64. if description := soup.find("meta", attrs={"name": "description"}):
  65. metadata["description"] = description.get(
  66. "content", "No description found."
  67. )
  68. if html := soup.find("html"):
  69. metadata["language"] = html.get("lang", "No language found.")
  70. return metadata
  71. class SafePlaywrightURLLoader(PlaywrightURLLoader):
  72. """Load HTML pages safely with Playwright, supporting SSL verification and rate limiting.
  73. Attributes:
  74. urls (List[str]): List of URLs to load.
  75. verify_ssl (bool): If True, verify SSL certificates.
  76. requests_per_second (Optional[float]): Number of requests per second to limit to.
  77. continue_on_failure (bool): If True, continue loading other URLs on failure.
  78. headless (bool): If True, the browser will run in headless mode.
  79. """
  80. def __init__(
  81. self,
  82. urls: List[str],
  83. verify_ssl: bool = True,
  84. requests_per_second: Optional[float] = None,
  85. continue_on_failure: bool = True,
  86. headless: bool = True,
  87. remove_selectors: Optional[List[str]] = None,
  88. proxy: Optional[Dict[str, str]] = None
  89. ):
  90. """Initialize with additional safety parameters."""
  91. super().__init__(
  92. urls=urls,
  93. continue_on_failure=continue_on_failure,
  94. headless=headless,
  95. remove_selectors=remove_selectors,
  96. proxy=proxy
  97. )
  98. self.verify_ssl = verify_ssl
  99. self.requests_per_second = requests_per_second
  100. self.last_request_time = None
  101. def _verify_ssl_cert(self, url: str) -> bool:
  102. """Verify SSL certificate for the given URL."""
  103. if not url.startswith("https://"):
  104. return True
  105. try:
  106. hostname = url.split("://")[-1].split("/")[0]
  107. context = ssl.create_default_context(cafile=certifi.where())
  108. with context.wrap_socket(ssl.socket(), server_hostname=hostname) as s:
  109. s.connect((hostname, 443))
  110. return True
  111. except ssl.SSLError:
  112. return False
  113. except Exception as e:
  114. log.warning(f"SSL verification failed for {url}: {str(e)}")
  115. return False
  116. async def _wait_for_rate_limit(self):
  117. """Wait to respect the rate limit if specified."""
  118. if self.requests_per_second and self.last_request_time:
  119. min_interval = timedelta(seconds=1.0 / self.requests_per_second)
  120. time_since_last = datetime.now() - self.last_request_time
  121. if time_since_last < min_interval:
  122. await asyncio.sleep((min_interval - time_since_last).total_seconds())
  123. self.last_request_time = datetime.now()
  124. def _sync_wait_for_rate_limit(self):
  125. """Synchronous version of rate limit wait."""
  126. if self.requests_per_second and self.last_request_time:
  127. min_interval = timedelta(seconds=1.0 / self.requests_per_second)
  128. time_since_last = datetime.now() - self.last_request_time
  129. if time_since_last < min_interval:
  130. time.sleep((min_interval - time_since_last).total_seconds())
  131. self.last_request_time = datetime.now()
  132. async def _safe_process_url(self, url: str) -> bool:
  133. """Perform safety checks before processing a URL."""
  134. if self.verify_ssl and not self._verify_ssl_cert(url):
  135. raise ValueError(f"SSL certificate verification failed for {url}")
  136. await self._wait_for_rate_limit()
  137. return True
  138. def _safe_process_url_sync(self, url: str) -> bool:
  139. """Synchronous version of safety checks."""
  140. if self.verify_ssl and not self._verify_ssl_cert(url):
  141. raise ValueError(f"SSL certificate verification failed for {url}")
  142. self._sync_wait_for_rate_limit()
  143. return True
  144. async def alazy_load(self) -> AsyncIterator[Document]:
  145. """Safely load URLs asynchronously."""
  146. parent_iterator = super().alazy_load()
  147. async for document in parent_iterator:
  148. url = document.metadata["source"]
  149. try:
  150. await self._safe_process_url(url)
  151. yield document
  152. except Exception as e:
  153. if self.continue_on_failure:
  154. log.error(f"Error processing {url}, exception: {e}")
  155. continue
  156. raise e
  157. def lazy_load(self) -> Iterator[Document]:
  158. """Safely load URLs synchronously."""
  159. parent_iterator = super().lazy_load()
  160. for document in parent_iterator:
  161. url = document.metadata["source"]
  162. try:
  163. self._safe_process_url_sync(url)
  164. yield document
  165. except Exception as e:
  166. if self.continue_on_failure:
  167. log.error(f"Error processing {url}, exception: {e}")
  168. continue
  169. raise e
  170. class SafeWebBaseLoader(WebBaseLoader):
  171. """WebBaseLoader with enhanced error handling for URLs."""
  172. def lazy_load(self) -> Iterator[Document]:
  173. """Lazy load text from the url(s) in web_path with error handling."""
  174. for path in self.web_paths:
  175. try:
  176. soup = self._scrape(path, bs_kwargs=self.bs_kwargs)
  177. text = soup.get_text(**self.bs_get_text_kwargs)
  178. # Build metadata
  179. metadata = extract_metadata(soup, path)
  180. yield Document(page_content=text, metadata=metadata)
  181. except Exception as e:
  182. # Log the error and continue with the next URL
  183. log.error(f"Error loading {path}: {e}")
  184. def get_web_loader(
  185. urls: Union[str, Sequence[str]],
  186. verify_ssl: bool = True,
  187. requests_per_second: int = 2,
  188. ):
  189. # Check if the URL is valid
  190. safe_urls = safe_validate_urls([urls] if isinstance(urls, str) else urls)
  191. if RAG_WEB_LOADER.value == "chromium":
  192. log.info("Using SafePlaywrightURLLoader")
  193. return SafePlaywrightURLLoader(
  194. safe_urls,
  195. verify_ssl=verify_ssl,
  196. requests_per_second=requests_per_second,
  197. continue_on_failure=True,
  198. )
  199. else:
  200. log.info("Using SafeWebBaseLoader")
  201. return SafeWebBaseLoader(
  202. safe_urls,
  203. verify_ssl=verify_ssl,
  204. requests_per_second=requests_per_second,
  205. continue_on_failure=True,
  206. )