Spaces:
Running on Zero
Running on Zero
Update utils.py
Browse files29 unmodified lines303132333435365 unmodified lines4243444546474849505152535455565758596061626364656667686970717273106 unmodified lines180181182183184185186187188226 unmodified lines415416417418419420421109 unmodified lines531532533534535536537238 unmodified lines77677777877929 unmodified linesimport shutilimport subprocessimport jsonimport html as _htmlIS_ZERO_GPU = bool(os.getenv("SPACES_ZERO_GPU"))USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:127.0) Gecko/20100101 Firefox/127.0'5 unmodified linesdef validate_url(url: str) -> str:"""Validate URL protocol and block SSRF (localhost, private & cloud metadata IPs)."""url = url.strip()if not url:raise ValueError("URL cannot be empty.")parsed = urllib.parse.urlparse(url)if parsed.scheme not in ("http", "https"):raise ValueError(f"Invalid protocol '{parsed.scheme}'. Only HTTP/HTTPS are allowed.")hostname = (parsed.hostname or "").lower()if not hostname:raise ValueError("Invalid URL: missing hostname.")# SSRF Protection: Block loopback and local hostsif hostname in ("localhost", "0.0.0.0", "127.0.0.1", "::1"):raise ValueError("Access to local/loopback address is blocked.")# SSRF Protection: Block private and link-local / cloud metadata IPs (e.g. 169.254.169.254)try:ip = ipaddress.ip_address(hostname)if ip.is_private or ip.is_loopback or ip.is_link_local or ip.is_reserved:raise ValueError(f"Access to internal IP ({hostname}) is blocked.")except ValueError:pass # Standard domain name# Normalize Civitai domain aliasif hostname == "civitai.red" or hostname.endswith(".civitai.red"):url = url.replace("civitai.red", "civitai.com")106 unmodified linesReturn a compact HTML snippet (string) showing the parsed infoin a small font. Values are HTML-escaped."""# helper to escapeesc = _html.escaperows = [("Title", esc(str(self.title))),("Author", esc(str(self.author))),226 unmodified linesif not url:return None# SSRF & protocol validationtry:url = validate_url(url)except Exception as e:109 unmodified linesnew_lora_model_list = new_lora_model_list + DIFFUSERS_FORMAT_LORASmsg_lora = "Downloaded"if l_name:msg_lora += f": <b>{l_name}</b>"print(msg_lora)try:238 unmodified linesdef escape_html(text):"""Escapes HTML special characters in the input text."""return text.replace("<", "<").replace(">", ">").replace("\n", "<br>")29 unmodified lines303132333435365 unmodified lines4243444546474849505152535455565758596061626364656667686970717273106 unmodified lines180181182183184185226 unmodified lines412413414415416417109 unmodified lines527528529530531532533238 unmodified lines77277377477577677729 unmodified linesimport shutilimport subprocessimport jsonfrom html import escape as escIS_ZERO_GPU = bool(os.getenv("SPACES_ZERO_GPU"))USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:127.0) Gecko/20100101 Firefox/127.0'5 unmodified linesdef validate_url(url: str) -> str:"""Validate URL protocol and host."""url = url.strip()if not url:raise ValueError("URL cannot be empty.")parsed = urllib.parse.urlparse(url)if parsed.scheme != "https":raise ValueError(f"Invalid protocol '{parsed.scheme}'. Only HTTPS is allowed.")hostname = (parsed.hostname or "").lower()if not hostname:raise ValueError("Invalid URL: missing hostname.")# Restrict local and internal endpointsif hostname in ("localhost", "0.0.0.0", "127.0.0.1", "::1"):raise ValueError("Restricted address.")# Restrict private IP addressestry:ip = ipaddress.ip_address(hostname)if ip.is_private or ip.is_loopback or ip.is_link_local or ip.is_reserved:raise ValueError(f"Access to ({hostname}) is blocked.")except ValueError:pass # Standard domain name# Normalize domain aliasif hostname == "civitai.red" or hostname.endswith(".civitai.red"):url = url.replace("civitai.red", "civitai.com")106 unmodified linesReturn a compact HTML snippet (string) showing the parsed infoin a small font. Values are HTML-escaped."""rows = [("Title", esc(str(self.title))),("Author", esc(str(self.author))),226 unmodified linesif not url:return Nonetry:url = validate_url(url)except Exception as e:109 unmodified linesnew_lora_model_list = new_lora_model_list + DIFFUSERS_FORMAT_LORASmsg_lora = "Downloaded"if l_name:msg_lora += f": <b>{escape_html(os.path.basename(l_name))}</b>"print(msg_lora)try:238 unmodified linesdef escape_html(text):"""Escapes HTML special characters in the input text."""if text is None:return ""return esc(str(text), quote=True).replace("\n", "<br>")
utils.py+13-15