Spaces:
Running
Running
Fix OAuth sign-in redirect loop
#4
by EmmaScharfmann HF Staff - opened
app.py
CHANGED
|
@@ -472,5 +472,50 @@ with gr.Blocks(title="MecCog submission", delete_cache=(1800, 3600)) as demo:
|
|
| 472 |
busy(a_yes, on_approve, a_user, [a_out, waiting])
|
| 473 |
busy(a_no, on_reject, a_user, [a_out, waiting])
|
| 474 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 475 |
if __name__ == "__main__":
|
| 476 |
demo.queue().launch(max_file_size="10mb", theme=THEME, css=CSS, head=HEAD, js=JS)
|
|
|
|
| 472 |
busy(a_yes, on_approve, a_user, [a_out, waiting])
|
| 473 |
busy(a_no, on_reject, a_user, [a_out, waiting])
|
| 474 |
|
| 475 |
+
# gradio's built-in OAuth login (/login/huggingface) stashes CSRF state in the
|
| 476 |
+
# Starlette "session" cookie. A known authlib bug (linked from gradio's own
|
| 477 |
+
# oauth.py, https://github.com/lepture/authlib/issues/622) grows that cookie on
|
| 478 |
+
# repeated login attempts until it's truncated past the 4KB cookie limit, losing
|
| 479 |
+
# the state value — which then makes every subsequent login attempt fail its state
|
| 480 |
+
# check and bounce the user straight back into another login attempt, with the
|
| 481 |
+
# same oversized cookie still attached. That reads to the browser as an infinite
|
| 482 |
+
# redirect loop ("too many redirects"), and gradio's own 2-retry escape hatch
|
| 483 |
+
# doesn't help because the stale cookie survives it. Since there's never a reason
|
| 484 |
+
# to carry old OAuth state into a fresh login click, drop the "session" cookie on
|
| 485 |
+
# the way into /login/huggingface so every attempt starts clean.
|
| 486 |
+
#
|
| 487 |
+
# demo.launch() builds its FastAPI app (with OAuth routes + SessionMiddleware
|
| 488 |
+
# already attached) and only then hands it to gradio.http_server.start_server,
|
| 489 |
+
# which does the actual binding — including the port-search/retry logic a Space
|
| 490 |
+
# redeploy depends on. Middleware can't be added once that app has started
|
| 491 |
+
# serving (Starlette rejects it), so the one safe seam is to add ours from
|
| 492 |
+
# inside start_server itself, right before it binds. http_server.start_server is
|
| 493 |
+
# looked up as a module attribute at call time (`from gradio import http_server`
|
| 494 |
+
# then `http_server.start_server(...)`), so patching it here — before launch()
|
| 495 |
+
# runs — is picked up without touching gradio's own binding code at all.
|
| 496 |
+
import gradio.http_server as _gr_http_server
|
| 497 |
+
|
| 498 |
+
_gradio_start_server = _gr_http_server.start_server
|
| 499 |
+
|
| 500 |
+
|
| 501 |
+
def _start_server_with_oauth_fix(app, *args, **kwargs):
|
| 502 |
+
@app.middleware("http")
|
| 503 |
+
async def _drop_stale_oauth_cookie(request, call_next):
|
| 504 |
+
if request.url.path == "/login/huggingface":
|
| 505 |
+
headers = []
|
| 506 |
+
for name, value in request.scope["headers"]:
|
| 507 |
+
if name == b"cookie":
|
| 508 |
+
value = re.sub(rb"(^|;\s*)session=[^;]*", b"", value).strip(b"; ")
|
| 509 |
+
if not value:
|
| 510 |
+
continue
|
| 511 |
+
headers.append((name, value))
|
| 512 |
+
request.scope["headers"] = headers
|
| 513 |
+
return await call_next(request)
|
| 514 |
+
|
| 515 |
+
return _gradio_start_server(app, *args, **kwargs)
|
| 516 |
+
|
| 517 |
+
|
| 518 |
+
_gr_http_server.start_server = _start_server_with_oauth_fix
|
| 519 |
+
|
| 520 |
if __name__ == "__main__":
|
| 521 |
demo.queue().launch(max_file_size="10mb", theme=THEME, css=CSS, head=HEAD, js=JS)
|