XForwardedFiltered

class gafaelfawr.x_forwarded.XForwardedFiltered(trusted: Sequence[_BaseNetwork])

Bases: aiohttp_remotes.x_forwarded.XForwardedBase

Middleware to update the request based on X-Forwarded-For.

The semantics we want aren’t supported by either of the aiohttp_remotes middleware classes, so we implement our own. This is similar to XForwardedRelaxed except that it takes the rightmost IP address that is not contained within one of the trusted networks.

Parameters:trusted (Sequence[Union[ipaddress.IPv4Network, ipaddress.IPv6Network]]) – List of trusted networks that should be skipped over when finding the actual client IP address.

Methods Summary

get_forwarded_for(headers)
get_forwarded_host(headers)
get_forwarded_proto(headers)
middleware(request, handler) Replace request information with details from proxy.
raise_error(request)
setup(app)

Methods Documentation

get_forwarded_for(headers)
get_forwarded_host(headers)
get_forwarded_proto(headers)
middleware(request: web.Request, handler: Handler) → web.StreamResponse

Replace request information with details from proxy.

Honor X-Forwarded-For and related headers.

Parameters:
Returns:

response – The response with a new logger key attached to it.

Return type:

aiohttp.web.StreamResponse

Notes

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 networks. The last entry of X-Forwarded-Proto and the contents of X-Forwarded-Host will be used unconditionally if they are present and X-Forwarded-For is also present.

raise_error(request)
setup(app)