diff --git a/gradio/routes.py b/gradio/routes.py index 263f1d70ed51..09e899758c23 100644 --- a/gradio/routes.py +++ b/gradio/routes.py @@ -217,7 +217,7 @@ def app_id(request: fastapi.Request) -> dict: @app.post("/login") @app.post("/login/") def login(form_data: OAuth2PasswordRequestForm = Depends()): - username, password = form_data.username, form_data.password + username, password = form_data.username.strip(), form_data.password if app.auth is None: return RedirectResponse(url="/", status_code=status.HTTP_302_FOUND) if ( diff --git a/test/test_routes.py b/test/test_routes.py index 42a5951ce6d1..4db29d247644 100644 --- a/test/test_routes.py +++ b/test/test_routes.py @@ -473,6 +473,22 @@ def test_post_login(self): ) assert response.status_code == 400 + def test_login_strip_username(self): + io = Interface(lambda x: x, "text", "text") + app, _, _ = io.launch( + auth=("test", "correct_password"), + prevent_thread_lock=True, + enable_queue=False, + ) + client = TestClient(app) + + response = client.post( + "/login", + data={"username": " test ", "password": "correct_password"}, + ) + assert response.status_code == 200 + assert app.tokens[response.json()["access-token"]] == "test" + class TestQueueRoutes: @pytest.mark.asyncio