summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/http/cookiejar.py18
-rw-r--r--Lib/test/test_http_cookiejar.py13
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS.d/next/Security/2019-11-15-00-54-42.bpo-38804.vjbM8V.rst1
4 files changed, 27 insertions, 6 deletions
diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py
index adc7ed6..47ed5c3 100644
--- a/Lib/http/cookiejar.py
+++ b/Lib/http/cookiejar.py
@@ -214,10 +214,14 @@ LOOSE_HTTP_DATE_RE = re.compile(
(?::(\d\d))? # optional seconds
)? # optional clock
\s*
- ([-+]?\d{2,4}|(?![APap][Mm]\b)[A-Za-z]+)? # timezone
+ (?:
+ ([-+]?\d{2,4}|(?![APap][Mm]\b)[A-Za-z]+) # timezone
+ \s*
+ )?
+ (?:
+ \(\w+\) # ASCII representation of timezone in parens.
\s*
- (?:\(\w+\))? # ASCII representation of timezone in parens.
- \s*$""", re.X | re.ASCII)
+ )?$""", re.X | re.ASCII)
def http2time(text):
"""Returns time in seconds since epoch of time represented by a string.
@@ -287,9 +291,11 @@ ISO_DATE_RE = re.compile(
(?::?(\d\d(?:\.\d*)?))? # optional seconds (and fractional)
)? # optional clock
\s*
- ([-+]?\d\d?:?(:?\d\d)?
- |Z|z)? # timezone (Z is "zero meridian", i.e. GMT)
- \s*$""", re.X | re. ASCII)
+ (?:
+ ([-+]?\d\d?:?(:?\d\d)?
+ |Z|z) # timezone (Z is "zero meridian", i.e. GMT)
+ \s*
+ )?$""", re.X | re. ASCII)
def iso2time(text):
"""
As for http2time, but parses the ISO 8601 formats:
diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py
index 853a400..2d7077a 100644
--- a/Lib/test/test_http_cookiejar.py
+++ b/Lib/test/test_http_cookiejar.py
@@ -123,6 +123,13 @@ class DateTimeTests(unittest.TestCase):
"http2time(%s) is not None\n"
"http2time(test) %s" % (test, http2time(test)))
+ def test_http2time_redos_regression_actually_completes(self):
+ # LOOSE_HTTP_DATE_RE was vulnerable to malicious input which caused catastrophic backtracking (REDoS).
+ # If we regress to cubic complexity, this test will take a very long time to succeed.
+ # If fixed, it should complete within a fraction of a second.
+ http2time("01 Jan 1970{}00:00:00 GMT!".format(" " * 10 ** 5))
+ http2time("01 Jan 1970 00:00:00{}GMT!".format(" " * 10 ** 5))
+
def test_iso2time(self):
def parse_date(text):
return time.gmtime(iso2time(text))[:6]
@@ -180,6 +187,12 @@ class DateTimeTests(unittest.TestCase):
self.assertIsNone(iso2time(test),
"iso2time(%r)" % test)
+ def test_iso2time_performance_regression(self):
+ # If ISO_DATE_RE regresses to quadratic complexity, this test will take a very long time to succeed.
+ # If fixed, it should complete within a fraction of a second.
+ iso2time('1994-02-03{}14:15:29 -0100!'.format(' '*10**6))
+ iso2time('1994-02-03 14:15:29{}-0100!'.format(' '*10**6))
+
class HeaderTests(unittest.TestCase):
diff --git a/Misc/ACKS b/Misc/ACKS
index a2e8046..e52ae98 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -250,6 +250,7 @@ Zach Byrne
Vedran Čačić
Nicolas Cadou
Jp Calderone
+Ben Caller
Arnaud Calmettes
Daniel Calvelo
Tony Campbell
diff --git a/Misc/NEWS.d/next/Security/2019-11-15-00-54-42.bpo-38804.vjbM8V.rst b/Misc/NEWS.d/next/Security/2019-11-15-00-54-42.bpo-38804.vjbM8V.rst
new file mode 100644
index 0000000..1f45142
--- /dev/null
+++ b/Misc/NEWS.d/next/Security/2019-11-15-00-54-42.bpo-38804.vjbM8V.rst
@@ -0,0 +1 @@
+Fixes a ReDoS vulnerability in :mod:`http.cookiejar`. Patch by Ben Caller.