|
@@ -3,6 +3,8 @@ import sys
|
|
|
import logging
|
|
|
import importlib.metadata
|
|
|
import pkgutil
|
|
|
+from urllib.parse import urlparse
|
|
|
+
|
|
|
import chromadb
|
|
|
from chromadb import Settings
|
|
|
from bs4 import BeautifulSoup
|
|
@@ -840,6 +842,35 @@ ENABLE_COMMUNITY_SHARING = PersistentConfig(
|
|
|
os.environ.get("ENABLE_COMMUNITY_SHARING", "True").lower() == "true",
|
|
|
)
|
|
|
|
|
|
+def validate_cors_origins(origins):
|
|
|
+ for origin in origins:
|
|
|
+ if origin != "*":
|
|
|
+ validate_cors_origin(origin)
|
|
|
+
|
|
|
+
|
|
|
+def validate_cors_origin(origin):
|
|
|
+ parsed_url = urlparse(origin)
|
|
|
+
|
|
|
+ # Check if the scheme is either http or https
|
|
|
+ if parsed_url.scheme not in ["http", "https"]:
|
|
|
+ raise ValueError(f"Invalid scheme in CORS_ALLOW_ORIGIN: '{origin}'. Only 'http' and 'https' are allowed.")
|
|
|
+
|
|
|
+ # Ensure that the netloc (domain + port) is present, indicating it's a valid URL
|
|
|
+ if not parsed_url.netloc:
|
|
|
+ raise ValueError(f"Invalid URL structure in CORS_ALLOW_ORIGIN: '{origin}'.")
|
|
|
+
|
|
|
+
|
|
|
+# For production, you should only need one host as
|
|
|
+# fastapi serves the svelte-kit built frontend and backend from the same host and port.
|
|
|
+# To test CORS_ALLOW_ORIGIN locally, you can set something like
|
|
|
+# CORS_ALLOW_ORIGIN=http://localhost:5173;http://localhost:8080
|
|
|
+# in your .env file depending on your frontend port, 5173 in this case.
|
|
|
+CORS_ALLOW_ORIGIN = os.environ.get("CORS_ALLOW_ORIGIN", "*").split(";")
|
|
|
+
|
|
|
+if "*" in CORS_ALLOW_ORIGIN:
|
|
|
+ log.warning("\n\nWARNING: CORS_ALLOW_ORIGIN IS SET TO '*' - NOT RECOMMENDED FOR PRODUCTION DEPLOYMENTS.\n")
|
|
|
+
|
|
|
+validate_cors_origins(CORS_ALLOW_ORIGIN)
|
|
|
|
|
|
class BannerModel(BaseModel):
|
|
|
id: str
|