Skip to content

Fix infinite redirect loop with FQDN trailing dot#7326

Closed
Bahtya wants to merge 1 commit intopsf:mainfrom
Bahtya:fix-trailing-dot-fqdn
Closed

Fix infinite redirect loop with FQDN trailing dot#7326
Bahtya wants to merge 1 commit intopsf:mainfrom
Bahtya:fix-trailing-dot-fqdn

Conversation

@Bahtya
Copy link
Copy Markdown

@Bahtya Bahtya commented Apr 4, 2026

Problem

When making a request to a URL with a trailing dot FQDN (e.g. https://pyropus.ca./), requests gets stuck in an infinite redirect loop, eventually raising TooManyRedirects.

The server redirects to the non-FQDN version (https://pyropus.ca/), but the FQDN trailing dot in the Host header causes requests to see the same URL and loops infinitely.

Browsers and curl normalize FQDNs by stripping the trailing dot.

Fix

Strip trailing dot from host during URL preparation in prepare_url(). This ensures the Host header is sent without the trailing dot, preventing the redirect loop.
Also fix overly strict validation that rejected all hosts starting with . - only leading dots should be rejected ( not bare dots at the start of a label).
Add a comment explaining the RFC 1035 context.

Testing

Before:

requests.get("https://pyropus.ca./")  # TooManyRedirects

After

requests.get("https://pyropus.ca./")  # 200 OK

Closes #7209

When making requests to a URL with a trailing dot FQDN (e.g. https://pyropus.ca./),
the server redirects to the non-FQDN version (https://pyropus.ca/)
but requests keeps requesting the FQDN form, causing an infinite redirect loop.

 The fix normalizes the host by stripping the trailing dot during URL preparation,
 following the behavior of browsers and curl which already do.

This prevents infinite redirect loops and ensures consistent host comparison between original and redirect URLs.

 No trailing dot normalization also prevents the issue where host.startswith('.') incorrectly rejects all FQDNs - only host.startswith('.') should be rejected.

 Host='pyropus.ca.' was be correctly normalized.

 The netloc reconstruction preserves the
 stripped host for so the URL looks like:

https://pyropus.ca/ instead of https://pyropus.ca./.

Co Closes #7209
@Bahtya Bahtya closed this by deleting the head repository Apr 6, 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.

TooManyRedirects exception loop when requesting FQDN URLs with a trailing dot

1 participant