XForwardedMiddleware

class safir.middleware.x_forwarded.XForwardedMiddleware(app, *, proxies=None)

Bases: object

ASGI middleware to update the request based on X-Forwarded-For.

The remote IP address will be replaced with the right-most IP address in X-Forwarded-For that is not contained within one of the trusted proxy networks.

If X-Forwarded-For is found and X-Forwarded-Proto is also present, the corresponding entry of X-Forwarded-Proto is used to replace the scheme in the request scope. If X-Forwarded-Proto only has one entry (ingress-nginx has this behavior), that one entry will become the new scheme in the request scope.

The contents of X-Forwarded-Host will be stored as forwarded_host in the request state if it and X-Forwarded-For are present. Normally this is not needed since NGINX will pass the original Host header without modification.

Parameters:

Methods Summary

__call__(scope, receive, send)

Call self as a function.

Methods Documentation

async __call__(scope, receive, send)

Call self as a function.

Parameters:
Return type:

None