Skip to content

Commit 5a3c7b1

Browse files
committed
Decouple Django CSRF, ALLOWED_HOST settings for more complex setups
- Set KHOJ_ALLOWED_DOMAIN to the domain that Khoj is accessible on from the host machine. This can be the internal i.p or domain of the host machine. It can be used by your load balancer/reverse_proxy to access Khoj. For example, if the load balancer service is in the khoj docker network, KHOJ_DOMAIN will be `server' (i.e service name). - Set KHOJ_DOMAIN to your externally accessible DOMAIN or I.P to avoid CSRF trusted origin or unset cookie issue when trying to access the khoj admin panel. Resolves #1114
1 parent bb0828b commit 5a3c7b1

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

docker-compose.yml

+7-1
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,18 @@ services:
8181
# - OLOSTEP_API_KEY=your_olostep_api_key
8282
#
8383
# Uncomment the necessary lines below to make your instance publicly accessible.
84-
# Replace the KHOJ_DOMAIN with either your domain or IP address (no http/https prefix).
8584
# Proceed with caution, especially if you are using anonymous mode.
8685
# ---
8786
# - KHOJ_NO_HTTPS=True
87+
# Replace the KHOJ_DOMAIN with the server's externally accessible domain or I.P address from a remote machie (no http/https prefix).
88+
# Ensure this is set correctly to avoid CSRF trusted origin or unset cookie issue when trying to access the admin panel.
8889
# - KHOJ_DOMAIN=192.168.0.104
8990
# - KHOJ_DOMAIN=khoj.example.com
91+
# Replace the KHOJ_ALLOWED_DOMAIN with the server's internally accessible domain or I.P address on the host machine (no http/https prefix).
92+
# Only set if using a load balancer/reverse_proxy in front of your Khoj server. If unset, it defaults to KHOJ_DOMAIN.
93+
# For example, if the load balancer service is added to the khoj docker network, set KHOJ_ALLOWED_DOMAIN to khoj's docker service name: `server'.
94+
# - KHOJ_ALLOWED_DOMAIN=server
95+
# - KHOJ_ALLOWED_DOMAIN=127.0.0.1
9096
# Uncomment the line below to disable telemetry.
9197
# Telemetry helps us prioritize feature development and understand how people are using Khoj
9298
# Read more at https://docs.khoj.dev/miscellaneous/telemetry

documentation/docs/get-started/setup.mdx

+6-1
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,14 @@ Go to http://localhost:42110/server/admin and login with the admin credentials y
283283
Ensure you are using **localhost, not 127.0.0.1**, to access the admin panel to avoid the CSRF error.
284284
:::
285285

286+
:::info[CSRF Trusted Origin or Unset Cookie Error]
287+
If using a load balancer/reverse_proxy in front of your Khoj server: Set the environment variable KHOJ_ALLOWED_DOMAIN=your-internal-ip-or-domain to avoid this error.
288+
If unset, it defaults to KHOJ_DOMAIN.
289+
:::
290+
286291
:::info[DISALLOWED HOST or Bad Request (400) Error]
287292
You may hit this if you try access Khoj exposed on a custom domain (e.g. 192.168.12.3 or example.com) or over HTTP.
288-
Set the environment variables KHOJ_DOMAIN=your-domain and KHOJ_NO_HTTPS=True if required to avoid this error.
293+
Set the environment variables KHOJ_DOMAIN=your-external-ip-or-domain and KHOJ_NO_HTTPS=True if required to avoid this error.
289294
:::
290295

291296
:::tip[Note]

src/khoj/app/settings.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232

3333
# All Subdomains of KHOJ_DOMAIN are trusted
3434
KHOJ_DOMAIN = os.getenv("KHOJ_DOMAIN", "khoj.dev")
35-
ALLOWED_HOSTS = [f".{KHOJ_DOMAIN}", "localhost", "127.0.0.1", "[::1]", f"{KHOJ_DOMAIN}"]
35+
KHOJ_ALLOWED_DOMAIN = os.getenv("KHOJ_ALLOWED_DOMAIN", KHOJ_DOMAIN)
36+
ALLOWED_HOSTS = [f".{KHOJ_ALLOWED_DOMAIN}", "localhost", "127.0.0.1", "[::1]", f"{KHOJ_ALLOWED_DOMAIN}"]
3637

3738
CSRF_TRUSTED_ORIGINS = [
3839
f"https://*.{KHOJ_DOMAIN}",
@@ -45,7 +46,7 @@
4546
DISABLE_HTTPS = is_env_var_true("KHOJ_NO_HTTPS")
4647

4748
COOKIE_SAMESITE = "None"
48-
if DEBUG or os.getenv("KHOJ_DOMAIN") == None:
49+
if DEBUG and os.getenv("KHOJ_DOMAIN") == None:
4950
SESSION_COOKIE_DOMAIN = "localhost"
5051
CSRF_COOKIE_DOMAIN = "localhost"
5152
else:

0 commit comments

Comments
 (0)