Skip to content

quart crashes when port is not in the host header #19

@klarose

Description

@klarose

I have a service using quart. I tried to enable json_logging on it, but it fails every request like this:

Traceback (most recent call last):
  File "/usr/lib/python3.6/logging/__init__.py", line 994, in emit
    msg = self.format(record)
  File "/usr/lib/python3.6/logging/__init__.py", line 840, in format
    return fmt.format(record)
  File "/home/kyle/.local/lib/python3.6/site-packages/json_logging/__init__.py", line 221, in format
    "remote_port": request_adapter.get_remote_port(request),
  File "/home/kyle/.local/lib/python3.6/site-packages/json_logging/framework/quart/__init__.py", line 115, in get_remote_port
    return request.host.split(":", 2)[1]
IndexError: list index out of range

The reason is that the "Host" header does not have a port on it in most cases. Indeed, it is optional according to the standard: https://tools.ietf.org/html/rfc2616?spm=5176.doc32013.2.3.Aimyd7#page-128

Take the example quart service provided in README.md. Modify it to listen on port 80. curl localhost/ will cause it to crash.

Or, keep it at the default port(5000), and run `curl localhost:5000 -H "Host: localhost". It will again crash

Ultimately, we cannot rely on the listening port being in a header. Some options for a fix:

  1. Return "-" if the split fails.
  2. Try to extract the listening port from quart. Note that this may not be the port the client connected to the system on, since there may be a reverse proxy/DNAT in front of the service.
  3. Don't expose the port at all (what value does it give? The service will log where it's listening when it starts).

As it stands, I cannot use this at all for quart, since I can't override the formatter for request context logs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions