Spaces:
Runtime error
Runtime error
| import uuid | |
| import time | |
| import logging | |
| import socket | |
| import ipaddress | |
| from urllib.parse import urlparse | |
| import gradio as gr | |
| logger = logging.getLogger(__name__) | |
| # Helper Functions | |
| def format_error(message): | |
| """Formats error messages for consistent user feedback.""" | |
| return {"error": message} | |
| # def toggle_visibility(show, *components): | |
| # """Toggles visibility for multiple Gradio components.""" | |
| # return [gr.update(visible=show) for _ in components] | |
| def generate_session_id(): | |
| """Generates a unique session ID for tracking inputs.""" | |
| return str(uuid.uuid4()) | |
| def log_runtime(start_time): | |
| """Logs the runtime of a process.""" | |
| elapsed_time = time.time() - start_time | |
| logger.info(f"Process completed in {elapsed_time:.2f} seconds.") | |
| return elapsed_time | |
| def is_public_ip(url): | |
| """ | |
| Checks whether the resolved IP address of a URL is public (non-local). | |
| Prevents SSRF by blocking internal addresses like 127.0.0.1 or 192.168.x.x. | |
| """ | |
| try: | |
| hostname = urlparse(url).hostname | |
| ip = socket.gethostbyname(hostname) | |
| ip_obj = ipaddress.ip_address(ip) | |
| return ip_obj.is_global # Only allow globally routable IPs | |
| except Exception as e: | |
| logger.warning(f"URL IP validation failed: {e}") | |
| return False |