uvicorn icon indicating copy to clipboard operation
uvicorn copied to clipboard

Allow handling proxy headers from AF_UNIX sockets

Open bemoody opened this issue 1 month ago • 2 comments

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: https://github.com/Kludex/uvicorn/discussions/2743

Checklist

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

bemoody avatar Nov 12 '25 21:11 bemoody

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

bemoody avatar Nov 13 '25 17:11 bemoody

Note that the first patch here is equivalent in essence to https://github.com/Kludex/uvicorn/pull/2561 . I didn't see that pull request and would be happy to rebase on top of that if you prefer.

bemoody avatar Nov 18 '25 20:11 bemoody