fix(auth): allow Remote-User SSO for DRF API endpoints - #777
Merged
Merged
Conversation
DRF's ``SessionAuthentication`` enforces CSRF on unsafe methods, which rejects proxy-authenticated API clients that legitimately carry no CSRF token. Add ``HttpRemoteUserAuthentication`` (subclass of DRF's stock ``RemoteUserAuthentication`` reading ``HTTP_REMOTE_USER`` instead of ``REMOTE_USER``) and conditionally prepend it to ``DEFAULT_AUTHENTICATION_CLASSES`` when ``CODEX_AUTH_REMOTE_USER`` is enabled, so proxy-forwarded ``Remote-User`` API requests authenticate through the existing ``RemoteUserBackend`` without hitting CSRF. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
Reverse-proxy
Remote-UserSSO (enabled viaCODEX_AUTH_REMOTE_USER) workedfor Django views but DRF API endpoints (
/api/v3/...) returned 403 onproxy-authenticated requests that carried no CSRF token — typical of non-browser
clients sitting behind Authentik / tinyauth / SWAG.
Root cause: DRF's only configured auth classes were
SessionAuthentication(which enforces CSRF on unsafe methods, the proxy is the auth authority so
clients legitimately have no token) and
BearerTokenAuthentication(irrelevantto Remote-User). The middleware set
request.user, but DRF never had an authclass that read the header and skipped CSRF.
HttpRemoteUserAuthenticationincodex/authentication.py— a subclassof DRF's stock
RemoteUserAuthenticationthat readsHTTP_REMOTE_USER(the WSGI/ASGI normalization of the proxy-forwarded
Remote-Userheader)instead of
REMOTE_USER(which only the on-machine socket path populates).REST_FRAMEWORK["DEFAULT_AUTHENTICATION_CLASSES"]when
AUTH_REMOTE_USERis on. Order matters: it must run beforeSessionAuthenticationso Remote-User clients authenticate via the existingRemoteUserBackendwithout hitting CSRF.No behavior change when
CODEX_AUTH_REMOTE_USERis off.Closes the bug report about
403 Forbiddenfrom/api/v3/auth/profile/andother DRF endpoints behind Authentik / SWAG / NGINX forward-auth setups.
Test plan
make fixcleanmake lintclean (ruff, basedpyright, vulture, complexipy, codespell, hadolint, actionlint)make testclean (257 backend + 69 frontend tests)Remote-User, hit/api/v3/auth/profile/as a non-session client (curl), confirm 200 instead of 403🤖 Generated with Claude Code