Neoteroi / BlackSheep

Fast ASGI web framework for Python
https://www.neoteroi.dev/blacksheep/
MIT License
1.88k stars 78 forks source link

ForwardedHeadersMiddleware - no ability to configure accept_only_proxied_requests #274

Open virajkanwade opened 2 years ago

virajkanwade commented 2 years ago

Describe the bug A description of what the bug is, possibly including how to reproduce it.

The flag, accept_only_proxied_requests, is hardcoded to True https://github.com/Neoteroi/BlackSheep/blob/a3656fb7eb0dbb9582f276bf1a61d4d14bbcb077/blacksheep/server/remotes/forwarding.py#L46

Override should be allowed in the constructor.

Would be useful in cloud where load balancer will be used, but monitoring scripts would use the instance local ip.

RobertoPrevato commented 2 years ago

Hi! Thank You for reporting this issue. You are right that the class should allow configuring that property in the constructor, this can be easily fixed.

It is not a blocking bug, though, as it's always possible to set the property after instantiating the class:

@app.on_middlewares_configuration
def configure_forwarded_headers(app):
    forward_middleware = ForwardedHeadersMiddleware()
    forward_middleware.accept_only_proxied_requests = False

    app.middlewares.insert(
        0,
        forward_middleware,
    )

instead of:

@app.on_middlewares_configuration
def configure_forwarded_headers(app):
    app.middlewares.insert(
        0,
        ForwardedHeadersMiddleware(),
    )