Skip to content

Commit

Permalink
pythongh-100985: Consistently wrap IPv6 IP address during CONNECT
Browse files Browse the repository at this point in the history
Update _get_hostport to always remove square brackets
from IPv6 addresses. Then add them if needed
in "CONNECT .." and "Host: ".
  • Loading branch information
derekhiggins committed Jan 12, 2023
1 parent 762745a commit 67434cd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
16 changes: 10 additions & 6 deletions Lib/http/client.py
Expand Up @@ -905,17 +905,23 @@ def _get_hostport(self, host, port):
host = host[:i]
else:
port = self.default_port
if host and host[0] == '[' and host[-1] == ']':
host = host[1:-1]
if host and host[0] == '[' and host[-1] == ']':
host = host[1:-1]

return (host, port)

def set_debuglevel(self, level):
self.debuglevel = level

def _wrap_ipv6(self, ip):
if ip.find(b':') >= 0 and ip.find(b'[') != 0:
return b"[" + ip + b"]"
return ip

def _tunnel(self):
connect = b"CONNECT %s:%d HTTP/1.0\r\n" % (
self._tunnel_host.encode("ascii"), self._tunnel_port)
self._wrap_ipv6(self._tunnel_host.encode("ascii")),
self._tunnel_port)
headers = [connect]
for header, value in self._tunnel_headers.items():
headers.append(f"{header}: {value}\r\n".encode("latin-1"))
Expand Down Expand Up @@ -1177,9 +1183,7 @@ def putrequest(self, method, url, skip_host=False,

# As per RFC 273, IPv6 address should be wrapped with []
# when used as Host header

if host.find(':') >= 0:
host_enc = b'[' + host_enc + b']'
host_enc = self._wrap_ipv6(host_enc)

if port == self.default_port:
self.putheader('Host', host_enc)
Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_httplib.py
Expand Up @@ -2262,6 +2262,23 @@ def test_connect_put_request(self):
self.assertIn(b'CONNECT destination.com', self.conn.sock.data)
self.assertIn(b'Host: destination.com', self.conn.sock.data)

def test_connect_put_request_ipv6(self):
self.conn.set_tunnel('[1:2:3::4]', 1234)
self.conn.request('PUT', '/', '')
self.assertEqual(self.conn.sock.host, self.host)
self.assertEqual(self.conn.sock.port, client.HTTP_PORT)
self.assertIn(b'CONNECT [1:2:3::4]:1234', self.conn.sock.data)
self.assertIn(b'Host: [1:2:3::4]:1234', self.conn.sock.data)

def test_connect_put_request_ipv6_port(self):
self.conn.set_tunnel('[1:2:3::4]:1234')
self.conn.request('PUT', '/', '')
self.assertEqual(self.conn.sock.host, self.host)
self.assertEqual(self.conn.sock.port, client.HTTP_PORT)
self.assertIn(b'CONNECT [1:2:3::4]:1234', self.conn.sock.data)
self.assertIn(b'Host: [1:2:3::4]:1234', self.conn.sock.data)


def test_tunnel_debuglog(self):
expected_header = 'X-Dummy: 1'
response_text = 'HTTP/1.0 200 OK\r\n{}\r\n\r\n'.format(expected_header)
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Expand Up @@ -739,6 +739,7 @@ Raymond Hettinger
Lisa Hewus Fresh
Kevan Heydon
Wouter van Heyst
Derek Higgins
Kelsey Hightower
Jason Hildebrand
Ryan Hileman
Expand Down
@@ -0,0 +1,2 @@
Update HTTPSConnection to consistently wrap IPv6 Addresses when using a
proxy

0 comments on commit 67434cd

Please sign in to comment.