utils.py 11 KB

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