airbrake/pybrake

Airbrake Starlette middleware throws error when running behind a reverse proxy

Opened this issue · 0 comments

The Airbrake Starlette middleware makes the assumption that the request client will always be set:

ctx["userAddr"] = request.client.host

but Starlette defines the field as optional:

    @property
    def client(self) -> typing.Optional[Address]:
        # client is a 2 item tuple of (host, port), None or missing
        host_port = self.scope.get("client")
        if host_port is not None:
            return Address(*host_port)
        return None

When running FastAPI behind a remote proxy (Nginx) that is not configured to forward headers, the client will resolve to None, which cause the middleware to throw an error when trying to access request.client.host as it attempts to notify about an unhandled error.