-
-
Notifications
You must be signed in to change notification settings - Fork 879
Allow handling proxy headers from AF_UNIX sockets #2760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bemoody
wants to merge
8
commits into
Kludex:main
Choose a base branch
from
bemoody:proxy-unix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Author
|
Added a couple of fixes for type-checking. The thoroughness of the CI in this project is impressive; kudos. |
Author
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
The
--forwarded-allow-ipsoption allows uvicorn to identify "trusted" proxies by their IP address. However, if a proxy is connecting to uvicorn via an AF_UNIX socket (usinguvicorn --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-ipsincludes the literal stringunix:.Discussion thread: #2743
Checklist