From d8a5a01ceaae5c53f57595d31d2686f77c94c414 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Fri, 14 Jun 2024 17:01:25 +0530 Subject: [PATCH] Pass multiple allowed Khoj domains via KHOJ_DOMAIN env var To add multiple allowed Khoj domains pass them as a comma separated list of domains via the KHOJ_DOMAIN environment variable Resolve comment in issue #662 --- src/khoj/main.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/khoj/main.py b/src/khoj/main.py index 64cfc194..76f5f87f 100644 --- a/src/khoj/main.py +++ b/src/khoj/main.py @@ -3,6 +3,7 @@ """ from contextlib import redirect_stdout +import itertools import logging import io import os @@ -66,19 +67,25 @@ else: django_app = get_asgi_application() # Add CORS middleware -KHOJ_DOMAIN = os.getenv("KHOJ_DOMAIN", "app.khoj.dev") +KHOJ_DOMAIN = os.getenv("KHOJ_DOMAIN", "app.khoj.dev").split(",") +scheme = "https" if not is_env_var_true("KHOJ_NO_HTTPS") else "http" +custom_origins = list( + itertools.chain.from_iterable( + [[f"{scheme}://{domain.strip()}", f"{scheme}://{domain.strip()}:*"] for domain in KHOJ_DOMAIN] + ) +) +default_origins = [ + "app://obsidian.md", # To allow access from Obsidian desktop app + "capacitor://localhost", # To allow access from Obsidian iOS app using Capacitor.JS + "http://localhost", # To allow access from Obsidian Android app + "http://localhost:*", # To allow access from localhost + "http://127.0.0.1:*", # To allow access from localhost + "app://khoj.dev", # To allow access from Khoj desktop app +] + app.add_middleware( CORSMiddleware, - allow_origins=[ - "app://obsidian.md", - "capacitor://localhost", # To allow access from Obsidian iOS app using Capacitor.JS - "http://localhost", # To allow access from Obsidian Android app - "http://localhost:*", - "http://127.0.0.1:*", - f"https://{KHOJ_DOMAIN}" if not is_env_var_true("KHOJ_NO_HTTPS") else f"http://{KHOJ_DOMAIN}", - f"https://{KHOJ_DOMAIN}:*" if not is_env_var_true("KHOJ_NO_HTTPS") else f"http://{KHOJ_DOMAIN}:*", - "app://khoj.dev", - ], + allow_origins=default_origins + custom_origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"],