Decode the IPv6 zone delimiter when parsing a URL
Summary
A URL can name an IPv6 zone in two ways: [fe80::1%25en0], the RFC 6874 form, and the bare [fe80::1%en0]. The parser kept whatever was typed, so the two were different hosts:
import httpx2
url = httpx2.URL("http://[fe80::1%25en0]/")
print(url.host) # before: fe80::1%25en0, now: fe80::1%en0
print(url) # http://[fe80::1%25en0]/
The %25 form was passed to the socket as-is, so it never reached the interface. Against a server on fe80::1%lo0 (macOS):
| URL | Before | Now |
|---|---|---|
http://[fe80::1%lo0]:8768/ | 204 | 204 |
http://[fe80::1%25lo0]:8768/ | ConnectError: [Errno 65] No route to host | 204 |
The zone is now parsed once, in encode_host():
URL.hostandURL.raw_hosthold the decoded form,fe80::1%en0. That is what the socket needs.str(url)andURL.netlocalways write%25, the formcurland Go write too.- Both spellings give the same
URLand the sameOrigin.
Why it is safe for the same-origin check
#1134 kept the two spellings as distinct origins because the transport connected to different places for each. Decoding only inside Origin would have let a redirect keep Authorization while connecting elsewhere.
Here the decoding happens in the parser, so Origin and the transport read the same host. Only the %25 delimiter is decoded. [2001:db8::1%30] keeps the zone 30 and is still a different origin from [2001:db8::10].
Ambiguity
A bare zone that starts with 25, such as %251, is read as the RFC 6874 form, so the zone is 1. Write %25251 for a zone named 251. curl resolves it the same way.
This applies to URL text and to a bracketed host=. A host= without brackets is taken as the decoded form, so url.copy_with(host=url.host) never changes the zone.
Not included
- The
Hostheader still carries the zone. That is #1360. httpcore2still sends the zone in the proxyCONNECTtarget and as the TLSserver_hostname.- The IPv6 address itself is not canonicalised in
URL, only inOrigin, as before.
Checklist
- I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
- I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
- I've updated the documentation accordingly.
Session: pi --session 01a20ced-af54-7647-a636-b3d9c9458448