Skip to content

Conversation

@bemoody
Copy link

@bemoody bemoody commented Nov 12, 2025

Summary

The --forwarded-allow-ips option allows uvicorn to identify "trusted" proxies by their IP address. However, if a proxy is connecting to uvicorn via an AF_UNIX socket (using uvicorn --uds), then it has no IP address. We still need a way to identify that the proxy is trusted in this case.

To allow middleware and applications to reliably tell that they are talking to an AF_UNIX peer, the ASGI spec says that scope["server"] should be set to [path, None]. (This was added by ASGI spec_version 2.2; earlier versions didn't mention Unix sockets. uvicorn claims to support spec_version 2.3.)

Then, in uvicorn's built-in middleware, treat an AF_UNIX peer as trusted if --forwarded-allow-ips includes the literal string unix:.

Discussion thread: #2743

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

Benjamin Moody added 8 commits November 12, 2025 15:51
When uvicorn is listening on an AF_UNIX socket (--uds), the "server"
scope parameter should be (socket_path, None).  This allows the
application/middleware to detect that a Unix socket is in use.

The test_get_local_addr test case needs to be updated accordingly;
previously it was assumed that get_local_addr would return None for a
Unix socket.

The test_get_local_addr_with_socket test case was nonsensical: if the
'family' is AF_UNIX then the 'sockname' must be a string.  Fix this
test case to check that get_local_addr works in both cases.
On Linux, if an AF_UNIX socket is bound to an abstract
address (starting with a NUL byte), then Python's socket API reports
the address as a 'bytes' object rather than a 'str'.

Check that this is handled correctly by get_local_addr.  The server
address reported to the ASGI application should always be a Unicode
string.
The test_get_remote_addr_with_socket test case was nonsensical.  If
the socket family is AF_UNIX then the peer address must be a
string (usually an empty string.)  Change this test case to
realistically reflect what happens when an AF_UNIX client is
connected.
When uvicorn is behind a reverse proxy, we want a way to report the
real client IP address and protocol based on the X-Forwarded-For and
X-Forwarded-Proto headers.  If we don't want people to be able to
impersonate arbitrary addresses, we need to identify which proxies are
trusted.

In order to allow AF_UNIX proxies to be trusted, use the string
"unix:" as a placeholder address.  Thus, "--forwarded-allow-ips=unix:"
means to trust AF_UNIX proxies but not IPv4 or IPv6 proxies.

(This syntax is borrowed from nginx: if an nginx proxy server is
itself listening on an AF_UNIX socket, "unix:" is what it will display
in X-Forwarded-For.  Thus, "--forwarded-allow-ips=unix:" allows us to
trust a chain of multiple AF_UNIX proxies.)
When the client connects via an AF_UNIX socket (scope["client"] is
None; scope["server"] is (socket_path, None)), ProxyHeadersMiddleware
should trust the proxy headers if either "unix:" or "*" is listed as a
trusted host.

When the client connects via an unknown socket type (scope["client"]
and scope["server"] are both None), ProxyHeadersMiddleware should only
trust the proxy headers if "*" is listed as a trusted host.
In ASGI, the "server" scope field is an optional field containing the
listening socket address and port number.

In ASGI spec_version 2.1, this field was defined as:

    server (Iterable[Unicode string, int]) - A two-item iterable of
    [host, port], where host is the listening address for this server,
    and port is the integer listening port. Optional; if missing
    defaults to None.

In ASGI spec_version 2.2, this was revised to:

    server (Iterable[Unicode string, Optional[int]]) - Either a
    two-item iterable of [host, port], where host is the listening
    address for this server, and port is the integer listening port,
    or [path, None] where path is that of the unix socket. Optional;
    if missing defaults to None.
@bemoody
Copy link
Author

bemoody commented Nov 13, 2025

Added a couple of fixes for type-checking. The thoroughness of the CI in this project is impressive; kudos.

@bemoody
Copy link
Author

bemoody commented Nov 18, 2025

Note that the first patch here is equivalent in essence to #2561 . I didn't see that pull request and would be happy to rebase on top of that if you prefer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant