Skip to content

Preserve forwarded client ports in proxy headers middleware#2903

Merged
Kludex merged 5 commits intomainfrom
fix/x-forwarded-for-port-waitress
Apr 14, 2026
Merged

Preserve forwarded client ports in proxy headers middleware#2903
Kludex merged 5 commits intomainfrom
fix/x-forwarded-for-port-waitress

Conversation

@Kludex
Copy link
Copy Markdown
Owner

@Kludex Kludex commented Apr 14, 2026

Summary

  • preserve client ports from trusted X-Forwarded-For entries in ProxyHeadersMiddleware
  • parse selected forwarded entries conservatively for IPv4 host:port and bracketed IPv6 [host]:port
  • keep _TrustedHosts.contains focused on plain host matching by localizing port stripping to get_trusted_client_host()
  • cover the behavior with public middleware tests, including a Fix X-Forwarded-For not properly handling ports #2822-style multi-proxy matrix

Inspired by Waitresss reverse-proxy handling, but adapted to Uvicorns existing trust model and localized to X-Forwarded-For client selection.

Co-authored-by: takeda <411978+takeda@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 14, 2026

📖 Docs preview: https://6b0e881d.uvicorn.pages.dev

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 14, 2026

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing fix/x-forwarded-for-port-waitress (67842ad) with main (77843e0)

Open in CodSpeed

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d93050d60a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread uvicorn/middleware/proxy_headers.py Outdated
Comment thread tests/middleware/test_proxy_headers.py Outdated
@Kludex
Copy link
Copy Markdown
Owner Author

Kludex commented Apr 14, 2026

@takeda I gave you co-author given I got some stuff from your PR. Thanks for the contribution. 🙏

@Kludex Kludex enabled auto-merge (squash) April 14, 2026 08:46
@Kludex Kludex merged commit 18edfa7 into main Apr 14, 2026
24 checks passed
@Kludex Kludex deleted the fix/x-forwarded-for-port-waitress branch April 14, 2026 09:56
@Kludex Kludex mentioned this pull request Apr 21, 2026
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.

X-Forwarded-For doesn't properly handle ports

1 participant