Rembg is a tool to remove images background
Fix SSRF and CORS misconfiguration in HTTP server (GHSA-55v6-g8pm-pw4c)
Daniel Gatis07ad0d493057
diff --git a/rembg/commands/s_command.py b/rembg/commands/s_command.pyindex 6f836a4..6096eb2 100644--- a/rembg/commands/s_command.py+++ b/rembg/commands/s_command.py@@ -1,14 +1,17 @@+import ipaddressimport jsonimport os+import socketimport webbrowserfrom typing import Optional, Tuple, cast+from urllib.parse import urlparseimport aiohttpimport clickimport gradio as grimport uvicornfrom asyncer import asyncify-from fastapi import Depends, FastAPI, File, Form, Query+from fastapi import Depends, FastAPI, File, Form, HTTPException, Queryfrom fastapi.middleware.cors import CORSMiddlewarefrom starlette.responses import Response@@ -99,7 +102,7 @@ def s_command(port: int, host: str, log_level: str, threads: int, no_ui: bool) -app.add_middleware(CORSMiddleware,- allow_credentials=True,+ allow_credentials=False,allow_origins=["*"],allow_methods=["*"],allow_headers=["*"],@@ -238,6 +241,34 @@ def s_command(port: int, host: str, log_level: str, threads: int, no_ui: bool) -RunVar("_default_thread_limiter").set(CapacityLimiter(threads))+ def _is_private_ip(host: str) -> bool:+ try:+ resolved = socket.getaddrinfo(host, None)+ for item in resolved:+ addr = item[4][0]+ ip = ipaddress.ip_address(addr)+ if (+ ip.is_private+ or ip.is_loopback+ or ip.is_link_local+ or ip.is_reserved+ ):+ return True+ except Exception:+ return True+ return False++ def _validate_url(url: str) -> None:+ parsed = urlparse(url)+ if parsed.scheme not in ("http", "https"):+ raise ValueError("Only http and https URLs are allowed.")+ if not parsed.hostname:+ raise ValueError("Invalid URL: missing hostname.")+ if _is_private_ip(parsed.hostname):+ raise ValueError(+ "Requests to private/internal addresses are not allowed."+ )+@app.get(path="/api/remove",tags=["Background Removal"],@@ -250,6 +281,11 @@ def s_command(port: int, host: str, log_level: str, threads: int, no_ui: bool) -),commons: CommonQueryParams = Depends(),):+ try:+ _validate_url(url)+ except ValueError as e:+ raise HTTPException(status_code=400, detail=str(e))+async with aiohttp.ClientSession() as session:async with session.get(url) as response:file = await response.read()