fix(dashboard): move CORSMiddleware outermost so OPTIONS preflight bypasses auth - #59072
Conversation
…passes auth app.add_middleware(CORSMiddleware, ...) was registered before the @app.middleware(http) auth decorators, making CORS the innermost layer in Starlette's onion model. A cross-origin OPTIONS preflight hit auth first → 401, and the CORS headers were never sent. Moving the add_middleware call after all @app.middleware registrations makes CORS outermost, so preflights are answered before auth runs. Fixes NousResearch#59052
|
Thanks for the focused regression fix. Current The proposed reorder directly makes CORS outermost without changing authentication of the actual follow-up request. The added tests cover allowed and disallowed preflights plus authenticated/same-origin GET behavior; current dashboard tests cover the token-gated GET path but do not cover this preflight regression. Automated hermes-sweeper review. |
What does this PR do?
Moves the
CORSMiddlewareregistration inhermes_cli/web_server.pyfrom before the@app.middleware("http")auth decorators to after them. In Starlette's onion model, the last-added middleware is the outermost (runs first). Previously, CORS was innermost — anOPTIONSpreflight from a cross-origin SPA hit the auth middlewares first, which returned 401 before CORS could answer with the properAccess-Control-Allow-*headers.Related Issue
Fixes #59052
Type of Change
Changes Made
hermes_cli/web_server.py— Movedapp.add_middleware(CORSMiddleware, …)from line 289 (before@app.middleware("http")registrations) to after the last@app.middleware("http")decorator (_token_auth_seam), making CORS the outermost middleware. Added comment explaining the ordering rationale.tests/hermes_cli/test_web_server_cors_preflight.py(new) — 5 regression tests verifying OPTIONS preflight returns CORS headers, allowed/disallowed origin handling, and same-origin requests are unaffected.How to Test
python -m pytest tests/hermes_cli/test_web_server_cors_preflight.py -v— all 5 tests should pass.python -m pytest tests/hermes_cli/ -v— all existing dashboard tests should still pass (auth tests verify 401 for unauthenticated non-OPTIONS requests is unchanged).hermes dashboard, then from a different localhost port:HTTP/1.1 200 OKwithAccess-Control-Allow-Origin: http://127.0.0.1:8092. Before this fix:HTTP/1.1 401 Unauthorized.Checklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/ -qand all tests passDocumentation & Housekeeping
docs/, docstrings) — or N/Acli-config.yaml.exampleif I added/changed config keys — or N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — or N/A