Skip to content

fix(auth): allow Remote-User SSO for DRF API endpoints - #777

Merged
ajslater merged 1 commit into
developfrom
fix-remote-user-drf-auth
May 25, 2026
Merged

ajslater merged 1 commit into
developfrom
fix-remote-user-drf-auth

Conversation

@ajslater

Copy link
Copy Markdown
Owner

Summary

Reverse-proxy Remote-User SSO (enabled via CODEX_AUTH_REMOTE_USER) worked
for Django views but DRF API endpoints (/api/v3/...) returned 403 on
proxy-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 (irrelevant
to Remote-User). The middleware set request.user, but DRF never had an auth
class that read the header and skipped CSRF.

  • Adds HttpRemoteUserAuthentication in codex/authentication.py — a subclass
    of DRF's stock RemoteUserAuthentication that reads HTTP_REMOTE_USER
    (the WSGI/ASGI normalization of the proxy-forwarded Remote-User header)
    instead of REMOTE_USER (which only the on-machine socket path populates).
  • Conditionally prepends it to REST_FRAMEWORK["DEFAULT_AUTHENTICATION_CLASSES"]
    when AUTH_REMOTE_USER is on. Order matters: it must run before
    SessionAuthentication so Remote-User clients authenticate via the existing
    RemoteUserBackend without hitting CSRF.

No behavior change when CODEX_AUTH_REMOTE_USER is off.

Closes the bug report about 403 Forbidden from /api/v3/auth/profile/ and
other DRF endpoints behind Authentik / SWAG / NGINX forward-auth setups.

Test plan

  • make fix clean
  • make lint clean (ruff, basedpyright, vulture, complexipy, codespell, hadolint, actionlint)
  • make test clean (257 backend + 69 frontend tests)
  • Manual: stand up Codex behind a proxy that forwards Remote-User, hit /api/v3/auth/profile/ as a non-session client (curl), confirm 200 instead of 403

🤖 Generated with Claude Code

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>
@ajslater
ajslater merged commit 41ef2d6 into develop May 25, 2026
3 checks passed
@ajslater
ajslater deleted the fix-remote-user-drf-auth branch May 25, 2026 10:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant