hypercorn icon indicating copy to clipboard operation
hypercorn copied to clipboard

Flask CORS + Hypercorn middleware not working on invalid origin requests

Open abhay-apella opened this issue 2 years ago • 2 comments

I am trying to get Flask CORs middleware working along with hypercorn and I get the following error:

  File ".../lib/python3.9/site-packages/hypercorn/asyncio/task_group.py", line 27, in _handle
    await app(scope, receive, send, sync_spawn, call_soon)
  File ".../lib/python3.9/site-packages/hypercorn/app_wrappers.py", line 51, in __call__
    await self.handle_http(scope, receive, send, sync_spawn, call_soon)
  File ".../lib/python3.9/site-packages/hypercorn/app_wrappers.py", line 84, in handle_http
    await send({"type": "http.response.body", "body": b"", "more_body": False})
  File "/.../lib/python3.9/site-packages/hypercorn/protocol/http_stream.py", line 168, in app_send
AttributeError: 'HTTPStream' object has no attribute 'response'

I instantiate my server and middleware like so:

    wsgi_app = Flask(import_name, static_folder=None)
    uvloop.install()
    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
    CORS(app=wsgi_app, origins=permitted_cors_origins, expose_headers=["Start-Time", "End-Time"])

    asyncio.run(serve(
        app=wsgi_app,
        config=config,
        mode="wsgi",
    ))

In order to test my server I issue a preflight request to my webserver. Going through the http_stream.py code, it seems its not handling an options request. Is this an issue with me not correctly instantiating hypercorn?

abhay-apella avatar Jan 26 '24 02:01 abhay-apella

I have the same problem, I use Django without CORS. In my case, staying with version hypercorn==0.15.0, does not trigger the above error.

mstojke avatar Feb 14 '24 12:02 mstojke

we need a solution :)

julian7 avatar Mar 07 '24 11:03 julian7

Fixed in e47757d5471f705d7c3d9018e436be205ce3f05c

pgjones avatar May 27 '24 16:05 pgjones