!348 fix CVE-2024-6923,CVE-2024-7592,CVE-2024-8088
From: @xinsheng3 Reviewed-by: @chen-huihan, @gaoruoshu Signed-off-by: @gaoruoshu
This commit is contained in:
commit
87b8fe2ac0
@ -0,0 +1,348 @@
|
|||||||
|
From 67067f7cca97cc980d31c5769f621b58ef876b7e Mon Sep 17 00:00:00 2001
|
||||||
|
From: xinsheng <xinsheng3@huawei.com>
|
||||||
|
Date: Tue, 3 Sep 2024 21:17:26 +0800
|
||||||
|
Subject: [PATCH] gh-121650: Encode newlines in headers, and verify headers are
|
||||||
|
sound (GH-122233)
|
||||||
|
|
||||||
|
---
|
||||||
|
Doc/library/email.errors.rst | 6 ++
|
||||||
|
Doc/library/email.policy.rst | 18 ++++++
|
||||||
|
Doc/whatsnew/3.11.rst | 13 ++++
|
||||||
|
Lib/email/_header_value_parser.py | 12 +++-
|
||||||
|
Lib/email/_policybase.py | 8 +++
|
||||||
|
Lib/email/errors.py | 4 ++
|
||||||
|
Lib/email/generator.py | 13 +++-
|
||||||
|
Lib/test/test_email/test_generator.py | 62 +++++++++++++++++++
|
||||||
|
Lib/test/test_email/test_policy.py | 26 ++++++++
|
||||||
|
...-07-27-16-10-41.gh-issue-121650.nf6oc9.rst | 5 ++
|
||||||
|
10 files changed, 163 insertions(+), 4 deletions(-)
|
||||||
|
create mode 100644 Misc/NEWS.d/next/Library/2024-07-27-16-10-41.gh-issue-121650.nf6oc9.rst
|
||||||
|
|
||||||
|
diff --git a/Doc/library/email.errors.rst b/Doc/library/email.errors.rst
|
||||||
|
index 194a986..f737f02 100644
|
||||||
|
--- a/Doc/library/email.errors.rst
|
||||||
|
+++ b/Doc/library/email.errors.rst
|
||||||
|
@@ -59,6 +59,12 @@ The following exception classes are defined in the :mod:`email.errors` module:
|
||||||
|
:class:`~email.mime.image.MIMEImage`).
|
||||||
|
|
||||||
|
|
||||||
|
+.. exception:: HeaderWriteError()
|
||||||
|
+
|
||||||
|
+ Raised when an error occurs when the :mod:`~email.generator` outputs
|
||||||
|
+ headers.
|
||||||
|
+
|
||||||
|
+
|
||||||
|
Here is the list of the defects that the :class:`~email.parser.FeedParser`
|
||||||
|
can find while parsing messages. Note that the defects are added to the message
|
||||||
|
where the problem was found, so for example, if a message nested inside a
|
||||||
|
diff --git a/Doc/library/email.policy.rst b/Doc/library/email.policy.rst
|
||||||
|
index bf53b95..79b1786 100644
|
||||||
|
--- a/Doc/library/email.policy.rst
|
||||||
|
+++ b/Doc/library/email.policy.rst
|
||||||
|
@@ -229,6 +229,24 @@ added matters. To illustrate::
|
||||||
|
|
||||||
|
.. versionadded:: 3.6
|
||||||
|
|
||||||
|
+
|
||||||
|
+ .. attribute:: verify_generated_headers
|
||||||
|
+
|
||||||
|
+ If ``True`` (the default), the generator will raise
|
||||||
|
+ :exc:`~email.errors.HeaderWriteError` instead of writing a header
|
||||||
|
+ that is improperly folded or delimited, such that it would
|
||||||
|
+ be parsed as multiple headers or joined with adjacent data.
|
||||||
|
+ Such headers can be generated by custom header classes or bugs
|
||||||
|
+ in the ``email`` module.
|
||||||
|
+
|
||||||
|
+ As it's a security feature, this defaults to ``True`` even in the
|
||||||
|
+ :class:`~email.policy.Compat32` policy.
|
||||||
|
+ For backwards compatible, but unsafe, behavior, it must be set to
|
||||||
|
+ ``False`` explicitly.
|
||||||
|
+
|
||||||
|
+ .. versionadded:: 3.11.10
|
||||||
|
+
|
||||||
|
+
|
||||||
|
The following :class:`Policy` method is intended to be called by code using
|
||||||
|
the email library to create policy instances with custom settings:
|
||||||
|
|
||||||
|
diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst
|
||||||
|
index c47c895..d46df98 100644
|
||||||
|
--- a/Doc/whatsnew/3.11.rst
|
||||||
|
+++ b/Doc/whatsnew/3.11.rst
|
||||||
|
@@ -2727,6 +2727,7 @@ OpenSSL
|
||||||
|
|
||||||
|
.. _libb2: https://www.blake2.net/
|
||||||
|
|
||||||
|
+
|
||||||
|
Notable changes in 3.11.10
|
||||||
|
==========================
|
||||||
|
|
||||||
|
@@ -2735,3 +2736,15 @@ ipaddress
|
||||||
|
|
||||||
|
* Fixed ``is_global`` and ``is_private`` behavior in ``IPv4Address``,
|
||||||
|
``IPv6Address``, ``IPv4Network`` and ``IPv6Network``.
|
||||||
|
+
|
||||||
|
+email
|
||||||
|
+-----
|
||||||
|
+
|
||||||
|
+* Headers with embedded newlines are now quoted on output.
|
||||||
|
+
|
||||||
|
+ The :mod:`~email.generator` will now refuse to serialize (write) headers
|
||||||
|
+ that are improperly folded or delimited, such that they would be parsed as
|
||||||
|
+ multiple headers or joined with adjacent data.
|
||||||
|
+ If you need to turn this safety feature off,
|
||||||
|
+ set :attr:`~email.policy.Policy.verify_generated_headers`.
|
||||||
|
+ (Contributed by Bas Bloemsaat and Petr Viktorin in :gh:`121650`.)
|
||||||
|
\ No newline at end of file
|
||||||
|
diff --git a/Lib/email/_header_value_parser.py b/Lib/email/_header_value_parser.py
|
||||||
|
index e637e6d..e1b99d5 100644
|
||||||
|
--- a/Lib/email/_header_value_parser.py
|
||||||
|
+++ b/Lib/email/_header_value_parser.py
|
||||||
|
@@ -92,6 +92,8 @@ TOKEN_ENDS = TSPECIALS | WSP
|
||||||
|
ASPECIALS = TSPECIALS | set("*'%")
|
||||||
|
ATTRIBUTE_ENDS = ASPECIALS | WSP
|
||||||
|
EXTENDED_ATTRIBUTE_ENDS = ATTRIBUTE_ENDS - set('%')
|
||||||
|
+NLSET = {'\n', '\r'}
|
||||||
|
+SPECIALSNL = SPECIALS | NLSET
|
||||||
|
|
||||||
|
def quote_string(value):
|
||||||
|
return '"'+str(value).replace('\\', '\\\\').replace('"', r'\"')+'"'
|
||||||
|
@@ -2778,9 +2780,13 @@ def _refold_parse_tree(parse_tree, *, policy):
|
||||||
|
wrap_as_ew_blocked -= 1
|
||||||
|
continue
|
||||||
|
tstr = str(part)
|
||||||
|
- if part.token_type == 'ptext' and set(tstr) & SPECIALS:
|
||||||
|
- # Encode if tstr contains special characters.
|
||||||
|
- want_encoding = True
|
||||||
|
+ if not want_encoding:
|
||||||
|
+ if part.token_type == 'ptext':
|
||||||
|
+ # Encode if tstr contains special characters.
|
||||||
|
+ want_encoding = not SPECIALSNL.isdisjoint(tstr)
|
||||||
|
+ else:
|
||||||
|
+ # Encode if tstr contains newlines.
|
||||||
|
+ want_encoding = not NLSET.isdisjoint(tstr)
|
||||||
|
try:
|
||||||
|
tstr.encode(encoding)
|
||||||
|
charset = encoding
|
||||||
|
diff --git a/Lib/email/_policybase.py b/Lib/email/_policybase.py
|
||||||
|
index c9cbadd..d1f4821 100644
|
||||||
|
--- a/Lib/email/_policybase.py
|
||||||
|
+++ b/Lib/email/_policybase.py
|
||||||
|
@@ -157,6 +157,13 @@ class Policy(_PolicyBase, metaclass=abc.ABCMeta):
|
||||||
|
message_factory -- the class to use to create new message objects.
|
||||||
|
If the value is None, the default is Message.
|
||||||
|
|
||||||
|
+ verify_generated_headers
|
||||||
|
+ -- if true, the generator verifies that each header
|
||||||
|
+ they are properly folded, so that a parser won't
|
||||||
|
+ treat it as multiple headers, start-of-body, or
|
||||||
|
+ part of another header.
|
||||||
|
+ This is a check against custom Header & fold()
|
||||||
|
+ implementations.
|
||||||
|
"""
|
||||||
|
|
||||||
|
raise_on_defect = False
|
||||||
|
@@ -165,6 +172,7 @@ class Policy(_PolicyBase, metaclass=abc.ABCMeta):
|
||||||
|
max_line_length = 78
|
||||||
|
mangle_from_ = False
|
||||||
|
message_factory = None
|
||||||
|
+ verify_generated_headers = True
|
||||||
|
|
||||||
|
def handle_defect(self, obj, defect):
|
||||||
|
"""Based on policy, either raise defect or call register_defect.
|
||||||
|
diff --git a/Lib/email/errors.py b/Lib/email/errors.py
|
||||||
|
index 3ad0056..02aa5ec 100644
|
||||||
|
--- a/Lib/email/errors.py
|
||||||
|
+++ b/Lib/email/errors.py
|
||||||
|
@@ -29,6 +29,10 @@ class CharsetError(MessageError):
|
||||||
|
"""An illegal charset was given."""
|
||||||
|
|
||||||
|
|
||||||
|
+class HeaderWriteError(MessageError):
|
||||||
|
+ """Error while writing headers."""
|
||||||
|
+
|
||||||
|
+
|
||||||
|
# These are parsing defects which the parser was able to work around.
|
||||||
|
class MessageDefect(ValueError):
|
||||||
|
"""Base class for a message defect."""
|
||||||
|
diff --git a/Lib/email/generator.py b/Lib/email/generator.py
|
||||||
|
index b8c1091..9cdd95f 100644
|
||||||
|
--- a/Lib/email/generator.py
|
||||||
|
+++ b/Lib/email/generator.py
|
||||||
|
@@ -14,12 +14,14 @@ import random
|
||||||
|
from copy import deepcopy
|
||||||
|
from io import StringIO, BytesIO
|
||||||
|
from email.utils import _has_surrogates
|
||||||
|
+from email.errors import HeaderWriteError
|
||||||
|
|
||||||
|
UNDERSCORE = '_'
|
||||||
|
NL = '\n' # XXX: no longer used by the code below.
|
||||||
|
|
||||||
|
NLCRE = re.compile(r'\r\n|\r|\n')
|
||||||
|
fcre = re.compile(r'^From ', re.MULTILINE)
|
||||||
|
+NEWLINE_WITHOUT_FWSP = re.compile(r'\r\n[^ \t]|\r[^ \n\t]|\n[^ \t]')
|
||||||
|
|
||||||
|
|
||||||
|
class Generator:
|
||||||
|
@@ -222,7 +224,16 @@ class Generator:
|
||||||
|
|
||||||
|
def _write_headers(self, msg):
|
||||||
|
for h, v in msg.raw_items():
|
||||||
|
- self.write(self.policy.fold(h, v))
|
||||||
|
+ folded = self.policy.fold(h, v)
|
||||||
|
+ if self.policy.verify_generated_headers:
|
||||||
|
+ linesep = self.policy.linesep
|
||||||
|
+ if not folded.endswith(self.policy.linesep):
|
||||||
|
+ raise HeaderWriteError(
|
||||||
|
+ f'folded header does not end with {linesep!r}: {folded!r}')
|
||||||
|
+ if NEWLINE_WITHOUT_FWSP.search(folded.removesuffix(linesep)):
|
||||||
|
+ raise HeaderWriteError(
|
||||||
|
+ f'folded header contains newline: {folded!r}')
|
||||||
|
+ self.write(folded)
|
||||||
|
# A blank line always separates headers from body
|
||||||
|
self.write(self._NL)
|
||||||
|
|
||||||
|
diff --git a/Lib/test/test_email/test_generator.py b/Lib/test/test_email/test_generator.py
|
||||||
|
index 89e7ede..d29400f 100644
|
||||||
|
--- a/Lib/test/test_email/test_generator.py
|
||||||
|
+++ b/Lib/test/test_email/test_generator.py
|
||||||
|
@@ -6,6 +6,7 @@ from email.message import EmailMessage
|
||||||
|
from email.generator import Generator, BytesGenerator
|
||||||
|
from email.headerregistry import Address
|
||||||
|
from email import policy
|
||||||
|
+import email.errors
|
||||||
|
from test.test_email import TestEmailBase, parameterize
|
||||||
|
|
||||||
|
|
||||||
|
@@ -216,6 +217,44 @@ class TestGeneratorBase:
|
||||||
|
g.flatten(msg)
|
||||||
|
self.assertEqual(s.getvalue(), self.typ(expected))
|
||||||
|
|
||||||
|
+ def test_keep_encoded_newlines(self):
|
||||||
|
+ msg = self.msgmaker(self.typ(textwrap.dedent("""\
|
||||||
|
+ To: nobody
|
||||||
|
+ Subject: Bad subject=?UTF-8?Q?=0A?=Bcc: injection@example.com
|
||||||
|
+
|
||||||
|
+ None
|
||||||
|
+ """)))
|
||||||
|
+ expected = textwrap.dedent("""\
|
||||||
|
+ To: nobody
|
||||||
|
+ Subject: Bad subject=?UTF-8?Q?=0A?=Bcc: injection@example.com
|
||||||
|
+
|
||||||
|
+ None
|
||||||
|
+ """)
|
||||||
|
+ s = self.ioclass()
|
||||||
|
+ g = self.genclass(s, policy=self.policy.clone(max_line_length=80))
|
||||||
|
+ g.flatten(msg)
|
||||||
|
+ self.assertEqual(s.getvalue(), self.typ(expected))
|
||||||
|
+
|
||||||
|
+ def test_keep_long_encoded_newlines(self):
|
||||||
|
+ msg = self.msgmaker(self.typ(textwrap.dedent("""\
|
||||||
|
+ To: nobody
|
||||||
|
+ Subject: Bad subject=?UTF-8?Q?=0A?=Bcc: injection@example.com
|
||||||
|
+
|
||||||
|
+ None
|
||||||
|
+ """)))
|
||||||
|
+ expected = textwrap.dedent("""\
|
||||||
|
+ To: nobody
|
||||||
|
+ Subject: Bad subject
|
||||||
|
+ =?utf-8?q?=0A?=Bcc:
|
||||||
|
+ injection@example.com
|
||||||
|
+
|
||||||
|
+ None
|
||||||
|
+ """)
|
||||||
|
+ s = self.ioclass()
|
||||||
|
+ g = self.genclass(s, policy=self.policy.clone(max_line_length=30))
|
||||||
|
+ g.flatten(msg)
|
||||||
|
+ self.assertEqual(s.getvalue(), self.typ(expected))
|
||||||
|
+
|
||||||
|
|
||||||
|
class TestGenerator(TestGeneratorBase, TestEmailBase):
|
||||||
|
|
||||||
|
@@ -224,6 +263,29 @@ class TestGenerator(TestGeneratorBase, TestEmailBase):
|
||||||
|
ioclass = io.StringIO
|
||||||
|
typ = str
|
||||||
|
|
||||||
|
+ def test_verify_generated_headers(self):
|
||||||
|
+ """gh-121650: by default the generator prevents header injection"""
|
||||||
|
+ class LiteralHeader(str):
|
||||||
|
+ name = 'Header'
|
||||||
|
+ def fold(self, **kwargs):
|
||||||
|
+ return self
|
||||||
|
+
|
||||||
|
+ for text in (
|
||||||
|
+ 'Value\r\nBad Injection\r\n',
|
||||||
|
+ 'NoNewLine'
|
||||||
|
+ ):
|
||||||
|
+ with self.subTest(text=text):
|
||||||
|
+ message = message_from_string(
|
||||||
|
+ "Header: Value\r\n\r\nBody",
|
||||||
|
+ policy=self.policy,
|
||||||
|
+ )
|
||||||
|
+
|
||||||
|
+ del message['Header']
|
||||||
|
+ message['Header'] = LiteralHeader(text)
|
||||||
|
+
|
||||||
|
+ with self.assertRaises(email.errors.HeaderWriteError):
|
||||||
|
+ message.as_string()
|
||||||
|
+
|
||||||
|
|
||||||
|
class TestBytesGenerator(TestGeneratorBase, TestEmailBase):
|
||||||
|
|
||||||
|
diff --git a/Lib/test/test_email/test_policy.py b/Lib/test/test_email/test_policy.py
|
||||||
|
index e87c275..ff1ddf7 100644
|
||||||
|
--- a/Lib/test/test_email/test_policy.py
|
||||||
|
+++ b/Lib/test/test_email/test_policy.py
|
||||||
|
@@ -26,6 +26,7 @@ class PolicyAPITests(unittest.TestCase):
|
||||||
|
'raise_on_defect': False,
|
||||||
|
'mangle_from_': True,
|
||||||
|
'message_factory': None,
|
||||||
|
+ 'verify_generated_headers': True,
|
||||||
|
}
|
||||||
|
# These default values are the ones set on email.policy.default.
|
||||||
|
# If any of these defaults change, the docs must be updated.
|
||||||
|
@@ -277,6 +278,31 @@ class PolicyAPITests(unittest.TestCase):
|
||||||
|
with self.assertRaises(email.errors.HeaderParseError):
|
||||||
|
policy.fold("Subject", subject)
|
||||||
|
|
||||||
|
+ def test_verify_generated_headers(self):
|
||||||
|
+ """Turning protection off allows header injection"""
|
||||||
|
+ policy = email.policy.default.clone(verify_generated_headers=False)
|
||||||
|
+ for text in (
|
||||||
|
+ 'Header: Value\r\nBad: Injection\r\n',
|
||||||
|
+ 'Header: NoNewLine'
|
||||||
|
+ ):
|
||||||
|
+ with self.subTest(text=text):
|
||||||
|
+ message = email.message_from_string(
|
||||||
|
+ "Header: Value\r\n\r\nBody",
|
||||||
|
+ policy=policy,
|
||||||
|
+ )
|
||||||
|
+ class LiteralHeader(str):
|
||||||
|
+ name = 'Header'
|
||||||
|
+ def fold(self, **kwargs):
|
||||||
|
+ return self
|
||||||
|
+
|
||||||
|
+ del message['Header']
|
||||||
|
+ message['Header'] = LiteralHeader(text)
|
||||||
|
+
|
||||||
|
+ self.assertEqual(
|
||||||
|
+ message.as_string(),
|
||||||
|
+ f"{text}\nBody",
|
||||||
|
+ )
|
||||||
|
+
|
||||||
|
# XXX: Need subclassing tests.
|
||||||
|
# For adding subclassed objects, make sure the usual rules apply (subclass
|
||||||
|
# wins), but that the order still works (right overrides left).
|
||||||
|
diff --git a/Misc/NEWS.d/next/Library/2024-07-27-16-10-41.gh-issue-121650.nf6oc9.rst b/Misc/NEWS.d/next/Library/2024-07-27-16-10-41.gh-issue-121650.nf6oc9.rst
|
||||||
|
new file mode 100644
|
||||||
|
index 0000000..83dd28d
|
||||||
|
--- /dev/null
|
||||||
|
+++ b/Misc/NEWS.d/next/Library/2024-07-27-16-10-41.gh-issue-121650.nf6oc9.rst
|
||||||
|
@@ -0,0 +1,5 @@
|
||||||
|
+:mod:`email` headers with embedded newlines are now quoted on output. The
|
||||||
|
+:mod:`~email.generator` will now refuse to serialize (write) headers that
|
||||||
|
+are unsafely folded or delimited; see
|
||||||
|
+:attr:`~email.policy.Policy.verify_generated_headers`. (Contributed by Bas
|
||||||
|
+Bloemsaat and Petr Viktorin in :gh:`121650`.)
|
||||||
|
--
|
||||||
|
2.43.0
|
||||||
|
|
||||||
@ -0,0 +1,131 @@
|
|||||||
|
From cc22eb02afab967715da592536fc5eeab7d7c765 Mon Sep 17 00:00:00 2001
|
||||||
|
From: xinsheng <xinsheng3@huawei.com>
|
||||||
|
Date: Tue, 3 Sep 2024 14:58:02 +0800
|
||||||
|
Subject: [PATCH] gh-123067: Fix quadratic complexity in parsing ^Cuoted cookie
|
||||||
|
values with backslashes
|
||||||
|
|
||||||
|
---
|
||||||
|
Lib/http/cookies.py | 34 ++++-------------
|
||||||
|
Lib/test/test_http_cookies.py | 38 +++++++++++++++++++
|
||||||
|
...-08-16-19-13-21.gh-issue-123067.Nx9O4R.rst | 1 +
|
||||||
|
3 files changed, 47 insertions(+), 26 deletions(-)
|
||||||
|
create mode 100644 Misc/NEWS.d/next/Library/2024-08-16-19-13-21.gh-issue-123067.Nx9O4R.rst
|
||||||
|
|
||||||
|
diff --git a/Lib/http/cookies.py b/Lib/http/cookies.py
|
||||||
|
index 35ac2dc..2c1f021 100644
|
||||||
|
--- a/Lib/http/cookies.py
|
||||||
|
+++ b/Lib/http/cookies.py
|
||||||
|
@@ -184,8 +184,13 @@ def _quote(str):
|
||||||
|
return '"' + str.translate(_Translator) + '"'
|
||||||
|
|
||||||
|
|
||||||
|
-_OctalPatt = re.compile(r"\\[0-3][0-7][0-7]")
|
||||||
|
-_QuotePatt = re.compile(r"[\\].")
|
||||||
|
+_unquote_sub = re.compile(r'\\(?:([0-3][0-7][0-7])|(.))').sub
|
||||||
|
+
|
||||||
|
+def _unquote_replace(m):
|
||||||
|
+ if m[1]:
|
||||||
|
+ return chr(int(m[1], 8))
|
||||||
|
+ else:
|
||||||
|
+ return m[2]
|
||||||
|
|
||||||
|
def _unquote(str):
|
||||||
|
# If there aren't any doublequotes,
|
||||||
|
@@ -205,30 +210,7 @@ def _unquote(str):
|
||||||
|
# \012 --> \n
|
||||||
|
# \" --> "
|
||||||
|
#
|
||||||
|
- i = 0
|
||||||
|
- n = len(str)
|
||||||
|
- res = []
|
||||||
|
- while 0 <= i < n:
|
||||||
|
- o_match = _OctalPatt.search(str, i)
|
||||||
|
- q_match = _QuotePatt.search(str, i)
|
||||||
|
- if not o_match and not q_match: # Neither matched
|
||||||
|
- res.append(str[i:])
|
||||||
|
- break
|
||||||
|
- # else:
|
||||||
|
- j = k = -1
|
||||||
|
- if o_match:
|
||||||
|
- j = o_match.start(0)
|
||||||
|
- if q_match:
|
||||||
|
- k = q_match.start(0)
|
||||||
|
- if q_match and (not o_match or k < j): # QuotePatt matched
|
||||||
|
- res.append(str[i:k])
|
||||||
|
- res.append(str[k+1])
|
||||||
|
- i = k + 2
|
||||||
|
- else: # OctalPatt matched
|
||||||
|
- res.append(str[i:j])
|
||||||
|
- res.append(chr(int(str[j+1:j+4], 8)))
|
||||||
|
- i = j + 4
|
||||||
|
- return _nulljoin(res)
|
||||||
|
+ return _unquote_sub(_unquote_replace, str)
|
||||||
|
|
||||||
|
# The _getdate() routine is used to set the expiration time in the cookie's HTTP
|
||||||
|
# header. By default, _getdate() returns the current time in the appropriate
|
||||||
|
diff --git a/Lib/test/test_http_cookies.py b/Lib/test/test_http_cookies.py
|
||||||
|
index 925c869..8879902 100644
|
||||||
|
--- a/Lib/test/test_http_cookies.py
|
||||||
|
+++ b/Lib/test/test_http_cookies.py
|
||||||
|
@@ -5,6 +5,7 @@ import unittest
|
||||||
|
import doctest
|
||||||
|
from http import cookies
|
||||||
|
import pickle
|
||||||
|
+from test import support
|
||||||
|
|
||||||
|
|
||||||
|
class CookieTests(unittest.TestCase):
|
||||||
|
@@ -58,6 +59,43 @@ class CookieTests(unittest.TestCase):
|
||||||
|
for k, v in sorted(case['dict'].items()):
|
||||||
|
self.assertEqual(C[k].value, v)
|
||||||
|
|
||||||
|
+ def test_unquote(self):
|
||||||
|
+ cases = [
|
||||||
|
+ (r'a="b=\""', 'b="'),
|
||||||
|
+ (r'a="b=\\"', 'b=\\'),
|
||||||
|
+ (r'a="b=\="', 'b=='),
|
||||||
|
+ (r'a="b=\n"', 'b=n'),
|
||||||
|
+ (r'a="b=\042"', 'b="'),
|
||||||
|
+ (r'a="b=\134"', 'b=\\'),
|
||||||
|
+ (r'a="b=\377"', 'b=\xff'),
|
||||||
|
+ (r'a="b=\400"', 'b=400'),
|
||||||
|
+ (r'a="b=\42"', 'b=42'),
|
||||||
|
+ (r'a="b=\\042"', 'b=\\042'),
|
||||||
|
+ (r'a="b=\\134"', 'b=\\134'),
|
||||||
|
+ (r'a="b=\\\""', 'b=\\"'),
|
||||||
|
+ (r'a="b=\\\042"', 'b=\\"'),
|
||||||
|
+ (r'a="b=\134\""', 'b=\\"'),
|
||||||
|
+ (r'a="b=\134\042"', 'b=\\"'),
|
||||||
|
+ ]
|
||||||
|
+ for encoded, decoded in cases:
|
||||||
|
+ with self.subTest(encoded):
|
||||||
|
+ C = cookies.SimpleCookie()
|
||||||
|
+ C.load(encoded)
|
||||||
|
+ self.assertEqual(C['a'].value, decoded)
|
||||||
|
+
|
||||||
|
+ @support.requires_resource('cpu')
|
||||||
|
+ def test_unquote_large(self):
|
||||||
|
+ n = 10**6
|
||||||
|
+ for encoded in r'\\', r'\134':
|
||||||
|
+ with self.subTest(encoded):
|
||||||
|
+ data = 'a="b=' + encoded*n + ';"'
|
||||||
|
+ C = cookies.SimpleCookie()
|
||||||
|
+ C.load(data)
|
||||||
|
+ value = C['a'].value
|
||||||
|
+ self.assertEqual(value[:3], 'b=\\')
|
||||||
|
+ self.assertEqual(value[-2:], '\\;')
|
||||||
|
+ self.assertEqual(len(value), n + 3)
|
||||||
|
+
|
||||||
|
def test_load(self):
|
||||||
|
C = cookies.SimpleCookie()
|
||||||
|
C.load('Customer="WILE_E_COYOTE"; Version=1; Path=/acme')
|
||||||
|
diff --git a/Misc/NEWS.d/next/Library/2024-08-16-19-13-21.gh-issue-123067.Nx9O4R.rst b/Misc/NEWS.d/next/Library/2024-08-16-19-13-21.gh-issue-123067.Nx9O4R.rst
|
||||||
|
new file mode 100644
|
||||||
|
index 0000000..6a23456
|
||||||
|
--- /dev/null
|
||||||
|
+++ b/Misc/NEWS.d/next/Library/2024-08-16-19-13-21.gh-issue-123067.Nx9O4R.rst
|
||||||
|
@@ -0,0 +1 @@
|
||||||
|
+Fix quadratic complexity in parsing ``"``-quoted cookie values with backslashes by :mod:`http.cookies`.
|
||||||
|
--
|
||||||
|
2.43.0
|
||||||
|
|
||||||
@ -0,0 +1,150 @@
|
|||||||
|
From 148c957b6f1fe22ed5465f4b05adb87db361fb0f Mon Sep 17 00:00:00 2001
|
||||||
|
From: xinsheng <xinsheng3@huawei.com>
|
||||||
|
Date: Wed, 4 Sep 2024 19:21:41 +0800
|
||||||
|
Subject: [PATCH] gh-123270: Replaced SanitizedNames with a more surgical fix.
|
||||||
|
(GH-123354)
|
||||||
|
|
||||||
|
---
|
||||||
|
Lib/test/test_zipfile.py | 77 +++++++++++++++++++
|
||||||
|
Lib/zipfile.py | 10 ++-
|
||||||
|
...-08-26-13-45-20.gh-issue-123270.gXHvNJ.rst | 3 +
|
||||||
|
3 files changed, 88 insertions(+), 2 deletions(-)
|
||||||
|
create mode 100644 Misc/NEWS.d/next/Library/2024-08-26-13-45-20.gh-issue-123270.gXHvNJ.rst
|
||||||
|
|
||||||
|
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
|
||||||
|
index c8e0159..669e494 100644
|
||||||
|
--- a/Lib/test/test_zipfile.py
|
||||||
|
+++ b/Lib/test/test_zipfile.py
|
||||||
|
@@ -3512,6 +3512,83 @@ with zipfile.ZipFile(io.BytesIO(), "w") as zf:
|
||||||
|
zipfile.Path(zf)
|
||||||
|
zf.extractall(source_path.parent)
|
||||||
|
|
||||||
|
+ def test_malformed_paths(self):
|
||||||
|
+ """
|
||||||
|
+ Path should handle malformed paths gracefully.
|
||||||
|
+
|
||||||
|
+ Paths with leading slashes are not visible.
|
||||||
|
+
|
||||||
|
+ Paths with dots are treated like regular files.
|
||||||
|
+ """
|
||||||
|
+ data = io.BytesIO()
|
||||||
|
+ zf = zipfile.ZipFile(data, "w")
|
||||||
|
+ zf.writestr("/one-slash.txt", b"content")
|
||||||
|
+ zf.writestr("//two-slash.txt", b"content")
|
||||||
|
+ zf.writestr("../parent.txt", b"content")
|
||||||
|
+ zf.filename = ''
|
||||||
|
+ root = zipfile.Path(zf)
|
||||||
|
+ assert list(map(str, root.iterdir())) == ['../']
|
||||||
|
+ assert root.joinpath('..').joinpath('parent.txt').read_bytes() == b'content'
|
||||||
|
+
|
||||||
|
+ def test_unsupported_names(self):
|
||||||
|
+ """
|
||||||
|
+ Path segments with special characters are readable.
|
||||||
|
+
|
||||||
|
+ On some platforms or file systems, characters like
|
||||||
|
+ ``:`` and ``?`` are not allowed, but they are valid
|
||||||
|
+ in the zip file.
|
||||||
|
+ """
|
||||||
|
+ data = io.BytesIO()
|
||||||
|
+ zf = zipfile.ZipFile(data, "w")
|
||||||
|
+ zf.writestr("path?", b"content")
|
||||||
|
+ zf.writestr("V: NMS.flac", b"fLaC...")
|
||||||
|
+ zf.filename = ''
|
||||||
|
+ root = zipfile.Path(zf)
|
||||||
|
+ contents = root.iterdir()
|
||||||
|
+ assert next(contents).name == 'path?'
|
||||||
|
+ assert next(contents).name == 'V: NMS.flac'
|
||||||
|
+ assert root.joinpath('V: NMS.flac').read_bytes() == b"fLaC..."
|
||||||
|
+
|
||||||
|
+ def test_backslash_not_separator(self):
|
||||||
|
+ """
|
||||||
|
+ In a zip file, backslashes are not separators.
|
||||||
|
+ """
|
||||||
|
+ data = io.BytesIO()
|
||||||
|
+ zf = zipfile.ZipFile(data, "w")
|
||||||
|
+ zf.writestr(DirtyZipInfo.for_name("foo\\bar", zf), b"content")
|
||||||
|
+ zf.filename = ''
|
||||||
|
+ root = zipfile.Path(zf)
|
||||||
|
+ (first,) = root.iterdir()
|
||||||
|
+ assert not first.is_dir()
|
||||||
|
+ assert first.name == 'foo\\bar'
|
||||||
|
+
|
||||||
|
+
|
||||||
|
+class DirtyZipInfo(zipfile.ZipInfo):
|
||||||
|
+ """
|
||||||
|
+ Bypass name sanitization.
|
||||||
|
+ """
|
||||||
|
+
|
||||||
|
+ def __init__(self, filename, *args, **kwargs):
|
||||||
|
+ super().__init__(filename, *args, **kwargs)
|
||||||
|
+ self.filename = filename
|
||||||
|
+
|
||||||
|
+ @classmethod
|
||||||
|
+ def for_name(cls, name, archive):
|
||||||
|
+ """
|
||||||
|
+ Construct the same way that ZipFile.writestr does.
|
||||||
|
+
|
||||||
|
+ TODO: extract this functionality and re-use
|
||||||
|
+ """
|
||||||
|
+ self = cls(filename=name, date_time=time.localtime(time.time())[:6])
|
||||||
|
+ self.compress_type = archive.compression
|
||||||
|
+ self.compress_level = archive.compresslevel
|
||||||
|
+ if self.filename.endswith('/'): # pragma: no cover
|
||||||
|
+ self.external_attr = 0o40775 << 16 # drwxrwxr-x
|
||||||
|
+ self.external_attr |= 0x10 # MS-DOS directory flag
|
||||||
|
+ else:
|
||||||
|
+ self.external_attr = 0o600 << 16 # ?rw-------
|
||||||
|
+ return self
|
||||||
|
+
|
||||||
|
|
||||||
|
class EncodedMetadataTests(unittest.TestCase):
|
||||||
|
file_names = ['\u4e00', '\u4e8c', '\u4e09'] # Han 'one', 'two', 'three'
|
||||||
|
diff --git a/Lib/zipfile.py b/Lib/zipfile.py
|
||||||
|
index 6189db5..622a3ee 100644
|
||||||
|
--- a/Lib/zipfile.py
|
||||||
|
+++ b/Lib/zipfile.py
|
||||||
|
@@ -9,6 +9,7 @@ import io
|
||||||
|
import itertools
|
||||||
|
import os
|
||||||
|
import posixpath
|
||||||
|
+import re
|
||||||
|
import shutil
|
||||||
|
import stat
|
||||||
|
import struct
|
||||||
|
@@ -2192,7 +2193,7 @@ def _parents(path):
|
||||||
|
def _ancestry(path):
|
||||||
|
"""
|
||||||
|
Given a path with elements separated by
|
||||||
|
- posixpath.sep, generate all elements of that path
|
||||||
|
+ posixpath.sep, generate all elements of that path.
|
||||||
|
|
||||||
|
>>> list(_ancestry('b/d'))
|
||||||
|
['b/d', 'b']
|
||||||
|
@@ -2204,9 +2205,14 @@ def _ancestry(path):
|
||||||
|
['b']
|
||||||
|
>>> list(_ancestry(''))
|
||||||
|
[]
|
||||||
|
+
|
||||||
|
+ Multiple separators are treated like a single.
|
||||||
|
+
|
||||||
|
+ >>> list(_ancestry('//b//d///f//'))
|
||||||
|
+ ['//b//d///f', '//b//d', '//b']
|
||||||
|
"""
|
||||||
|
path = path.rstrip(posixpath.sep)
|
||||||
|
- while path and path != posixpath.sep:
|
||||||
|
+ while path.rstrip(posixpath.sep):
|
||||||
|
yield path
|
||||||
|
path, tail = posixpath.split(path)
|
||||||
|
|
||||||
|
diff --git a/Misc/NEWS.d/next/Library/2024-08-26-13-45-20.gh-issue-123270.gXHvNJ.rst b/Misc/NEWS.d/next/Library/2024-08-26-13-45-20.gh-issue-123270.gXHvNJ.rst
|
||||||
|
new file mode 100644
|
||||||
|
index 0000000..ee9fde6
|
||||||
|
--- /dev/null
|
||||||
|
+++ b/Misc/NEWS.d/next/Library/2024-08-26-13-45-20.gh-issue-123270.gXHvNJ.rst
|
||||||
|
@@ -0,0 +1,3 @@
|
||||||
|
+Applied a more surgical fix for malformed payloads in :class:`zipfile.Path`
|
||||||
|
+causing infinite loops (gh-122905) without breaking contents using
|
||||||
|
+legitimate characters.
|
||||||
|
--
|
||||||
|
2.43.0
|
||||||
|
|
||||||
33
python3.spec
33
python3.spec
@ -3,7 +3,7 @@ Summary: Interpreter of the Python3 programming language
|
|||||||
URL: https://www.python.org/
|
URL: https://www.python.org/
|
||||||
|
|
||||||
Version: 3.11.6
|
Version: 3.11.6
|
||||||
Release: 4
|
Release: 5
|
||||||
License: Python-2.0
|
License: Python-2.0
|
||||||
|
|
||||||
%global branchversion 3.11
|
%global branchversion 3.11
|
||||||
@ -92,12 +92,16 @@ Source1: pyconfig.h
|
|||||||
Patch1: 00001-rpath.patch
|
Patch1: 00001-rpath.patch
|
||||||
Patch251: 00251-change-user-install-location.patch
|
Patch251: 00251-change-user-install-location.patch
|
||||||
|
|
||||||
|
Patch6000: backport-3.11-gh-114572-Fix-locking-in-cert_store_stats-and-g.patch
|
||||||
|
Patch6001: backport-3.11-gh-113171-gh-65056-Fix-private-non-global-IP-ad.patch
|
||||||
|
Patch6002: backport-3.11-gh-115133-Fix-tests-for-XMLPullParser-with-Expa.patch
|
||||||
|
Patch6003: backport-gh-121650-Encode-newlines-in-headers-and-verify-head.patch
|
||||||
|
Patch6004: backport-gh-123067-Fix-quadratic-complexity-in-parsing-quoted.patch
|
||||||
|
Patch6005: backport-gh-123270-Replaced-SanitizedNames-with-a-more-surgic.patch
|
||||||
|
|
||||||
Patch9000: add-the-sm3-method-for-obtaining-the-salt-value.patch
|
Patch9000: add-the-sm3-method-for-obtaining-the-salt-value.patch
|
||||||
Patch9001: 0001-add-loongarch64-support-for-python.patch
|
Patch9001: 0001-add-loongarch64-support-for-python.patch
|
||||||
Patch9002: backport-3.11-gh-114572-Fix-locking-in-cert_store_stats-and-g.patch
|
Patch9002: fix-check-error-with-CVEfixed-expat-2.5.0.patch
|
||||||
Patch9003: backport-3.11-gh-113171-gh-65056-Fix-private-non-global-IP-ad.patch
|
|
||||||
Patch9004: backport-3.11-gh-115133-Fix-tests-for-XMLPullParser-with-Expa.patch
|
|
||||||
Patch9005: fix-check-error-with-CVEfixed-expat-2.5.0.patch
|
|
||||||
|
|
||||||
Provides: python%{branchversion} = %{version}-%{release}
|
Provides: python%{branchversion} = %{version}-%{release}
|
||||||
Provides: python(abi) = %{branchversion}
|
Provides: python(abi) = %{branchversion}
|
||||||
@ -193,12 +197,16 @@ rm configure pyconfig.h.in
|
|||||||
%patch1 -p1
|
%patch1 -p1
|
||||||
%patch251 -p1
|
%patch251 -p1
|
||||||
|
|
||||||
|
%patch6000 -p1
|
||||||
|
%patch6001 -p1
|
||||||
|
%patch6002 -p1
|
||||||
|
%patch6003 -p1
|
||||||
|
%patch6004 -p1
|
||||||
|
%patch6005 -p1
|
||||||
|
|
||||||
%patch9000 -p1
|
%patch9000 -p1
|
||||||
%patch9001 -p1
|
%patch9001 -p1
|
||||||
%patch9002 -p1
|
%patch9002 -p1
|
||||||
%patch9003 -p1
|
|
||||||
%patch9004 -p1
|
|
||||||
%patch9005 -p1
|
|
||||||
|
|
||||||
%build
|
%build
|
||||||
autoconf
|
autoconf
|
||||||
@ -860,6 +868,15 @@ export BEP_GTDLIST="$BEP_GTDLIST_TMP"
|
|||||||
%{_mandir}/*/*
|
%{_mandir}/*/*
|
||||||
|
|
||||||
%changelog
|
%changelog
|
||||||
|
* Tue Sep 03 2024 xinsheng <xinsheng3@huawei.com> - 3.11.6-5
|
||||||
|
- Type:CVE
|
||||||
|
- CVE:NA
|
||||||
|
- SUG:NA
|
||||||
|
- DESC:fix CVE-2024-6923,CVE-2024-7592,CVE-2024-8088
|
||||||
|
- Encode newlines in headers, and verify headers are sound
|
||||||
|
- Fix quadratic complexity in parsing quoted cookie values with backslashes
|
||||||
|
- Replaced SanitizedNames with a more surgical fix
|
||||||
|
|
||||||
* Tue Jun 25 2024 peng.zou <peng.zou@shingroup.cn> - 3.11.6-4
|
* Tue Jun 25 2024 peng.zou <peng.zou@shingroup.cn> - 3.11.6-4
|
||||||
- Type:bugfix
|
- Type:bugfix
|
||||||
- CVE:NA
|
- CVE:NA
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user