Skip to content

fix(url): normalizeHost with ipv6 hostname#201

Open
chimurai wants to merge 1 commit into
httptoolkit:mainfrom
chimurai:fix-ipv6-bracket-literals
Open

fix(url): normalizeHost with ipv6 hostname#201
chimurai wants to merge 1 commit into
httptoolkit:mainfrom
chimurai:fix-ipv6-bracket-literals

Conversation

@chimurai
Copy link
Copy Markdown

@chimurai chimurai commented May 19, 2026

Hi,

Been using mockttp for a long time in http-proxy-middleware to mock the proxy target server.

Great work on this library 🙏

Recently I updated httpxy (dependency of http-proxy-middleware) with improved IPv6 support: unjs/httpxy#136

After updating httpxy some of existing IPv6 tests started to fail: https://github.com/chimurai/http-proxy-middleware/blob/ffbf5bd59712704c9df178d5df61d96062a11b22/test/e2e/ipv6.spec.ts

These tests are using mockttp with as target server.

I applied the fixes from this PR in this draft PR: chimurai/http-proxy-middleware#1234

Looks like it fixes the issue.

For full transparency, I used Copilot to help with analysis and creating this PR and the description below.


Summary

Fix IPv6 host serialization when reconstructing absolute request URLs from parsed destination data.

Mockttp already parses bracketed IPv6 Host headers correctly, but when rebuilding host[:port] strings it was dropping the brackets. For requests like Host: [::1]:8000, that produced invalid absolute URLs such as http://::1:8000/api, which fail URL parsing and trigger a 400 before request handlers run.

Changes

  • Update host normalization to re-add brackets when serializing IPv6 literal hostnames
  • Preserve existing default-port behavior so :80 for HTTP and :443 for HTTPS are still omitted
  • Leave parsing behavior unchanged for bracketed IPv6 input
  • Add regression coverage for:
    • IPv6 normalization with non-default HTTP and HTTPS ports
    • IPv6 normalization with default ports
    • Request preprocessing from Host: [::1]:8000
    • Existing IPv4 and DNS host formatting behavior

Example

Before:

Host: [::1]:8000
reconstructed URL: http://::1:8000/api
URL parsing fails, request returns 400

After:

Host: [::1]:8000
reconstructed URL: http://[::1]:8000/api
request is processed normally

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 19, 2026

CLA assistant check
All committers have signed the CLA.

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.

2 participants