Fix OAuth sign-in redirect loop

#4
by EmmaScharfmann HF Staff - opened

Fixes the "too many redirects" error on "Sign in with Hugging Face".

Root cause: gradio's built-in OAuth login (/login/huggingface) stores CSRF
state in the Starlette session cookie. A known authlib bug (linked from
gradio's own oauth.py, https://github.com/lepture/authlib/issues/622) grows
that cookie on repeated login attempts until it is truncated past the 4KB
cookie limit, losing the state value. Every subsequent login attempt then
fails its state check and bounces back into another attempt with the same
oversized cookie still attached — which the browser reports as an infinite
redirect loop. gradio's own 2-retry escape hatch does not help because the
stale cookie survives it.

Fix: wrap the Gradio Blocks in a thin FastAPI shell via the documented
gr.mount_gradio_app, and add one middleware that strips any session
cookie on the way into /login/huggingface, so every login attempt starts
clean. No new dependencies (fastapi/uvicorn already ship with
gradio[oauth]).

EmmaScharfmann changed pull request status to open
EmmaScharfmann changed pull request status to merged

Sign up or log in to comment