airbrake / pybrake

Python exception notifier for Airbrake
https://airbrake.io
Other
37 stars 17 forks source link

Airbrake Starlette middleware throws error when running behind a reverse proxy #268

Open pouellet opened 1 year ago

pouellet commented 1 year ago

The Airbrake Starlette middleware makes the assumption that the request client will always be set: https://github.com/airbrake/pybrake/blob/f50658879447d142e490bd23297c34144d62222e/src/pybrake/middleware/starlette.py#L50

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.