Skip to content

fix: support IPv6 addresses in TrustedHostMiddleware - #3379

Open
tianrking wants to merge 3 commits into
Kludex:mainfrom
tianrking:agent/allow-ipv6-trusted-hosts
Open

fix: support IPv6 addresses in TrustedHostMiddleware#3379
tianrking wants to merge 3 commits into
Kludex:mainfrom
tianrking:agent/allow-ipv6-trusted-hosts

Conversation

@tianrking

@tianrking tianrking commented Jul 16, 2026

Copy link
Copy Markdown

Summary

  • preserve bracketed IPv6 literals while removing an optional port from Host
  • add TrustedHostMiddleware coverage for [::1]:8000

Root cause

The middleware split Host on the first colon, converting [::1]:8000 to [. A configured [::1] host was therefore rejected.

Fixes #3357

Validation

  • python -m pytest tests/middleware/test_trusted_host.py -q
  • ruff check starlette/middleware/trustedhost.py tests/middleware/test_trusted_host.py
  • ruff format --check starlette/middleware/trustedhost.py tests/middleware/test_trusted_host.py
  • mypy starlette/middleware/trustedhost.py tests/middleware/test_trusted_host.py

Review in cubic

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

Copy link
Copy Markdown

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: 255bd37301

ℹ️ 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 starlette/middleware/trustedhost.py Outdated
host = headers.get("host", "")
if host.startswith("["):
closing_bracket = host.find("]")
if closing_bracket != -1 and (closing_bracket == len(host) - 1 or host[closing_bracket + 1] == ":"):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Reject non-port suffixes after IPv6 literals

When allowed_hosts contains an IPv6 literal such as [::1], this condition strips everything after ] as long as the next character is :, so a malformed Host header like [::1]:attacker is treated as [::1] and the request is allowed through. Since this middleware is meant to reject incorrectly set Host headers, the IPv6 path should only ignore a numeric port (or otherwise reject the malformed suffix) instead of accepting arbitrary text after ]:.

Useful? React with 👍 / 👎.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No issues found across 2 files

Re-trigger cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 2 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread starlette/middleware/trustedhost.py Outdated
Samielakkad

This comment was marked as resolved.

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.

TrustedHostMiddleware Doesn't handle IPv6 properly

2 participants