Repository navigation
Add a csrf_trusted_origins setting for reverse proxies - #978
Merged
Merged
Conversation
A user behind a reverse proxy got CSRF 403s and worked around them by removing CsrfViewMiddleware. That is unsafe and doesn't even work for the API: DRF's SessionAuthentication enforces CSRF itself. Codex never set Django's CSRF_TRUSTED_ORIGINS, so a proxy that serves Codex under another scheme or host (usually one missing X-Forwarded-Proto) had no fix. [server] csrf_trusted_origins (TOML array) or CODEX_CSRF_TRUSTED_ORIGINS (comma-separated) now feeds CSRF_TRUSTED_ORIGINS. Entries lose a trailing slash and are deduped; a scheme-less entry, which could never match an Origin header, is dropped with a warning that points at the X-Forwarded-* headers first. The default is empty, so existing installs are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Oct 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A user behind a reverse proxy got CSRF 403s and worked around them by patching
CsrfViewMiddlewareout of settings. That is unsafe, and it doesn't fix the API either: DRF'sSessionAuthenticationenforces CSRF on its own. Codex never set Django'sCSRF_TRUSTED_ORIGINS, so there was no supported fix when a proxy serves Codex under a different scheme or host.[server] csrf_trusted_origins(TOML array) /CODEX_CSRF_TRUSTED_ORIGINS(comma-separated) feedsCSRF_TRUSTED_ORIGINS. The default is empty, so existing installs behave the same.get_str_listincodex/settings/config.pyreads a TOML array or a comma string.normalize_csrf_trusted_originsstrips a trailing slash, keepshttps://*.example.comwildcards, dedupes in order, and drops a scheme-less entry (it could never match anOriginheader) with a warning that says to checkX-Forwarded-Proto/X-Forwarded-Hostfirst.codex.toml.defaultand in the README copy of it, the env var in the README list, and a new "CSRF 403 behind a proxy" section under Reverse Proxy that tells users to check the forwarded headers first and not to remove the middleware.Origins are host names, so they stay out of the telemeter.
Test plan
tests/test_csrf_trusted_origins.py: loader and normalizer cases, env-over-TOML precedence (including an empty env value), the warning on a scheme-less entry, and an end-to-endClient(enforce_csrf_checks=True)PUT /api/v4/auth/timezonewith a mismatchedOrigin: 403CSRF Failed … Origin checking failedwithout the setting, 204 with it.CSRF_TRUSTED_ORIGINS == []with the setting unset; with the env var set, it normalizes the list and logs the warning.make fix,make lint,make tyclean. The two existingcover.py"unreachable" warnings are untouched.make test-python: 1683 passed, 1 xfailed.🤖 Generated with Claude Code