emmett-framework / granian

A Rust HTTP server for Python applications
BSD 3-Clause "New" or "Revised" License
2.61k stars 77 forks source link

Process X-Forwarded-* headers #389

Open jvllmr opened 2 weeks ago

jvllmr commented 2 weeks ago

It would be great if granian could handle X-Forwarded headers for a smooth experience with reverse proxies without depending on a special setup or another dependency. Just declare your trusted proxies and you're good to go.

Here is how uvicorn handles these headers with its middleware which is baked into uvicorn: https://github.com/encode/uvicorn/blob/47304d9ae76321f0f5f649ff4f73e09b17085933/uvicorn/middleware/proxy_headers.py#L19

gi0baro commented 2 weeks ago

As discussed in #384, Granian won't strip any of those headers from the request, you should be able to see them.

Can you specify wether you'd like to have a control feature to strip those headers if not allowed or to have Granian update the ASGI scope based on forwarded headers? (or both?)

In the meantime, since Uvicorn uses a middleware, you can copy that code and wrap your application into that middleware to have 1:1 functionality implemented.

jvllmr commented 2 weeks ago

I'd firstly like to have a feature that updates the scope of the application similar to the middleware in uvicorn. I also think omitting these headers if they are not allowed could also be useful as it prevents spoofing attacks on logic that lives in the deployed application and uses the headers.

Furthermore, I want to list a few benefits from having this feature built-in that came to my mind just now: