Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Lib/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -972,13 +972,22 @@ def _wrap_ipv6(self, ip):
return ip

def _tunnel(self):
if _contains_disallowed_url_pchar_re.search(self._tunnel_host):
raise ValueError('Tunnel host can\'t contain control characters %r'
% (self._tunnel_host,))
connect = b"CONNECT %s:%d %s\r\n" % (
self._wrap_ipv6(self._tunnel_host.encode("idna")),
self._tunnel_port,
self._http_vsn_str.encode("ascii"))
headers = [connect]
for header, value in self._tunnel_headers.items():
headers.append(f"{header}: {value}\r\n".encode("latin-1"))
header_bytes = header.encode("latin-1")
value_bytes = value.encode("latin-1")
if not _is_legal_header_name(header_bytes):
raise ValueError('Invalid header name %r' % (header_bytes,))
if _is_illegal_header_value(value_bytes):
raise ValueError('Invalid header value %r' % (value_bytes,))
headers.append(b"%s: %s\r\n" % (header_bytes, value_bytes))
headers.append(b"\r\n")
# Making a single send() call instead of one per line encourages
# the host OS to use a more optimal packet size instead of
Expand Down
45 changes: 45 additions & 0 deletions Lib/test/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,51 @@ def test_invalid_headers(self):
with self.assertRaisesRegex(ValueError, 'Invalid header'):
conn.putheader(name, value)

def test_invalid_tunnel_headers(self):
cases = (
('Invalid\r\nName', 'ValidValue'),
('Invalid\rName', 'ValidValue'),
('Invalid\nName', 'ValidValue'),
('\r\nInvalidName', 'ValidValue'),
('\rInvalidName', 'ValidValue'),
('\nInvalidName', 'ValidValue'),
(' InvalidName', 'ValidValue'),
('\tInvalidName', 'ValidValue'),
('Invalid:Name', 'ValidValue'),
(':InvalidName', 'ValidValue'),
('ValidName', 'Invalid\r\nValue'),
('ValidName', 'Invalid\rValue'),
('ValidName', 'Invalid\nValue'),
('ValidName', 'InvalidValue\r\n'),
('ValidName', 'InvalidValue\r'),
('ValidName', 'InvalidValue\n'),
)
for name, value in cases:
with self.subTest((name, value)):
conn = client.HTTPConnection('example.com')
conn.set_tunnel('tunnel', headers={
name: value
})
conn.sock = FakeSocket('')
with self.assertRaisesRegex(ValueError, 'Invalid header'):
conn._tunnel() # Called in .connect()

def test_invalid_tunnel_host(self):
cases = (
'invalid\r.host',
'\ninvalid.host',
'invalid.host\r\n',
'invalid.host\x00',
'invalid host',
)
for tunnel_host in cases:
with self.subTest(tunnel_host):
conn = client.HTTPConnection('example.com')
conn.set_tunnel(tunnel_host)
conn.sock = FakeSocket('')
with self.assertRaisesRegex(ValueError, 'Tunnel host can\'t contain control characters'):
conn._tunnel() # Called in .connect()

def test_headers_debuglevel(self):
body = (
b'HTTP/1.1 200 OK\r\n'
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_webbrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ def test_open_bad_new_parameter(self):
arguments=[URL],
kw=dict(new=999))

def test_reject_action_dash_prefixes(self):
browser = self.browser_class(name=CMD_NAME)
with self.assertRaises(ValueError):
browser.open('%action--incognito')
# new=1: action is "--new-window", so "%action" itself expands to
# a dash-prefixed flag even with no dash in the original URL.
with self.assertRaises(ValueError):
browser.open('%action', new=1)


class EdgeCommandTest(CommandTestMixin, unittest.TestCase):

Expand Down
5 changes: 3 additions & 2 deletions Lib/webbrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ def _invoke(self, args, remote, autoraise, url=None):

def open(self, url, new=0, autoraise=True):
sys.audit("webbrowser.open", url)
self._check_url(url)
if new == 0:
action = self.remote_action
elif new == 1:
Expand All @@ -288,7 +287,9 @@ def open(self, url, new=0, autoraise=True):
raise Error("Bad 'new' parameter to open(); "
f"expected 0, 1, or 2, got {new}")

args = [arg.replace("%s", url).replace("%action", action)
self._check_url(url.replace("%action", action))

args = [arg.replace("%action", action).replace("%s", url)
for arg in self.remote_args]
args = [arg for arg in args if arg]
success = self._invoke(args, True, autoraise, url)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Reject CR/LF characters in tunnel request headers for the
HTTPConnection.set_tunnel() method.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
A bypass in :mod:`webbrowser` allowed URLs prefixed with ``%action`` to pass
the dash-prefix safety check.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hardened :mod:`!_remote_debugging` by validating remote debug offset tables
before using them to size memory reads or interpret remote layouts.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fix a dangling input pointer in :class:`lzma.LZMADecompressor`,
:class:`bz2.BZ2Decompressor`, and internal :class:`!zlib._ZlibDecompressor`
when memory allocation fails with :exc:`MemoryError`, which could let a
subsequent :meth:`!decompress` call read or write through a stale pointer to
the already-released caller buffer.
1 change: 1 addition & 0 deletions Modules/_bz2module.c
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ decompress(BZ2Decompressor *d, char *data, size_t len, Py_ssize_t max_length)
return result;

error:
bzs->next_in = NULL;
Py_XDECREF(result);
return NULL;
}
Expand Down
1 change: 1 addition & 0 deletions Modules/_lzmamodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,7 @@ decompress(Decompressor *d, uint8_t *data, size_t len, Py_ssize_t max_length)
return result;

error:
lzs->next_in = NULL;
Py_XDECREF(result);
return NULL;
}
Expand Down
Loading