<feed xmlns='http://www.w3.org/2005/Atom'>
<title>delta/cpython-git.git/Lib/test/test_http_cookiejar.py, branch main</title>
<subtitle>github.com: python/cpython.git
</subtitle>
<link rel='alternate' type='text/html' href='http://91.123.203.49/cgit/delta/cpython-git.git/'/>
<entry>
<title>gh-99576: Fix cookiejar file that was not truncated for some classes (GH-99616)</title>
<updated>2022-12-20T16:51:26+00:00</updated>
<author>
<name>Nikita Sobolev</name>
<email>mail@sobolevn.me</email>
</author>
<published>2022-12-20T16:51:26+00:00</published>
<link rel='alternate' type='text/html' href='http://91.123.203.49/cgit/delta/cpython-git.git/commit/?id=44892d45b038f919b0378590a776580a9d73b291'/>
<id>44892d45b038f919b0378590a776580a9d73b291</id>
<content type='text'>
Co-authored-by: Łukasz Langa &lt;lukasz@langa.pl&gt;</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Co-authored-by: Łukasz Langa &lt;lukasz@langa.pl&gt;</pre>
</div>
</content>
</entry>
<entry>
<title>gh-84623: Remove unused imports in tests (#93772)</title>
<updated>2022-06-13T14:56:03+00:00</updated>
<author>
<name>Victor Stinner</name>
<email>vstinner@python.org</email>
</author>
<published>2022-06-13T14:56:03+00:00</published>
<link rel='alternate' type='text/html' href='http://91.123.203.49/cgit/delta/cpython-git.git/commit/?id=3ceb4b8d3ad48101e67da8fb8f581d3377863bfa'/>
<id>3ceb4b8d3ad48101e67da8fb8f581d3377863bfa</id>
<content type='text'>
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
</pre>
</div>
</content>
</entry>
<entry>
<title>gh-79096: Fix/improve http cookiejar tests (GH-93614)</title>
<updated>2022-06-08T21:51:04+00:00</updated>
<author>
<name>Christian Heimes</name>
<email>christian@python.org</email>
</author>
<published>2022-06-08T21:51:04+00:00</published>
<link rel='alternate' type='text/html' href='http://91.123.203.49/cgit/delta/cpython-git.git/commit/?id=68f6a5da199826fef1098f869d8d2260aaccff8e'/>
<id>68f6a5da199826fef1098f869d8d2260aaccff8e</id>
<content type='text'>
Fixup of GH-93463:
- remove stray print
- use proper way to check file mode
- add working chmod decorator

Co-authored-by: Łukasz Langa &lt;lukasz@langa.pl&gt;</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Fixup of GH-93463:
- remove stray print
- use proper way to check file mode
- add working chmod decorator

Co-authored-by: Łukasz Langa &lt;lukasz@langa.pl&gt;</pre>
</div>
</content>
</entry>
<entry>
<title>gh-79096: Protect cookie file created by {LWP,Mozilla}CookieJar.save() (GH-93463)</title>
<updated>2022-06-07T08:11:03+00:00</updated>
<author>
<name>Pascal Wittmann</name>
<email>mail@pascal-wittmann.de</email>
</author>
<published>2022-06-07T08:11:03+00:00</published>
<link rel='alternate' type='text/html' href='http://91.123.203.49/cgit/delta/cpython-git.git/commit/?id=c6f6ede728df144c6c39858f558ea65a2aac7cda'/>
<id>c6f6ede728df144c6c39858f558ea65a2aac7cda</id>
<content type='text'>
Note: This change is not effective on Microsoft Windows.

Cookies can store sensitive information and should therefore be protected
against unauthorized third parties. This is also described in issue #79096.

The filesystem permissions are currently set to 644, everyone can read the
file. This commit changes the permissions to 600, only the creater of the file
can read and modify it. This improves security, because it reduces the attack
surface. Now the attacker needs control of the user that created the cookie or
a ways to circumvent the filesystems permissions.

This change is backwards incompatible. Systems that rely on world-readable
cookies will breake. However, one could argue that those are misconfigured in
the first place.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Note: This change is not effective on Microsoft Windows.

Cookies can store sensitive information and should therefore be protected
against unauthorized third parties. This is also described in issue #79096.

The filesystem permissions are currently set to 644, everyone can read the
file. This commit changes the permissions to 600, only the creater of the file
can read and modify it. This improves security, because it reduces the attack
surface. Now the attacker needs control of the user that created the cookie or
a ways to circumvent the filesystems permissions.

This change is backwards incompatible. Systems that rely on world-readable
cookies will breake. However, one could argue that those are misconfigured in
the first place.
</pre>
</div>
</content>
</entry>
<entry>
<title>bpo-42066: CookieJar cookies should not be sorted (GH-22745)</title>
<updated>2022-04-21T01:45:24+00:00</updated>
<author>
<name>Iman Kermani</name>
<email>55282537+IKermani@users.noreply.github.com</email>
</author>
<published>2022-04-21T01:45:24+00:00</published>
<link rel='alternate' type='text/html' href='http://91.123.203.49/cgit/delta/cpython-git.git/commit/?id=615b24c80b0bbbc7b70aa1e9c28f9c4463c8c1ed'/>
<id>615b24c80b0bbbc7b70aa1e9c28f9c4463c8c1ed</id>
<content type='text'>
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
</pre>
</div>
</content>
</entry>
<entry>
<title>bpo-46075: Store localhost cookies in CookieJar (#30108)</title>
<updated>2022-04-19T20:08:06+00:00</updated>
<author>
<name>Nick</name>
<email>keddad@yandex.ru</email>
</author>
<published>2022-04-19T20:08:06+00:00</published>
<link rel='alternate' type='text/html' href='http://91.123.203.49/cgit/delta/cpython-git.git/commit/?id=b6d5e3c3c92374598b327918c8e66e748a1a92a0'/>
<id>b6d5e3c3c92374598b327918c8e66e748a1a92a0</id>
<content type='text'>
Co-authored-by: Jelle Zijlstra &lt;jelle.zijlstra@gmail.com&gt;</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Co-authored-by: Jelle Zijlstra &lt;jelle.zijlstra@gmail.com&gt;</pre>
</div>
</content>
</entry>
<entry>
<title>bpo-45229: Make test_http_cookiejar discoverable (GH-29004)</title>
<updated>2021-10-17T12:06:40+00:00</updated>
<author>
<name>Serhiy Storchaka</name>
<email>storchaka@gmail.com</email>
</author>
<published>2021-10-17T12:06:40+00:00</published>
<link rel='alternate' type='text/html' href='http://91.123.203.49/cgit/delta/cpython-git.git/commit/?id=b3f0ceae919c1627094ff628c87184684a5cedd6'/>
<id>b3f0ceae919c1627094ff628c87184684a5cedd6</id>
<content type='text'>
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
</pre>
</div>
</content>
</entry>
<entry>
<title>bpo-38976: Add support for HTTP Only flag in MozillaCookieJar (#17471)</title>
<updated>2020-10-23T22:48:55+00:00</updated>
<author>
<name>Jacob Neil Taylor</name>
<email>me@jacobtaylor.id.au</email>
</author>
<published>2020-10-23T22:48:55+00:00</published>
<link rel='alternate' type='text/html' href='http://91.123.203.49/cgit/delta/cpython-git.git/commit/?id=16ee68da6e12bb2d79751b32cc37523fe4f4bb48'/>
<id>16ee68da6e12bb2d79751b32cc37523fe4f4bb48</id>
<content type='text'>
Add support for HTTP Only flag in MozillaCookieJar

Co-authored-by: blurb-it[bot] &lt;43283697+blurb-it[bot]@users.noreply.github.com&gt;</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Add support for HTTP Only flag in MozillaCookieJar

Co-authored-by: blurb-it[bot] &lt;43283697+blurb-it[bot]@users.noreply.github.com&gt;</pre>
</div>
</content>
</entry>
<entry>
<title>bpo-40275: Use new test.support helper submodules in tests (GH-21743)</title>
<updated>2020-08-06T11:51:29+00:00</updated>
<author>
<name>Hai Shi</name>
<email>shihai1992@gmail.com</email>
</author>
<published>2020-08-06T11:51:29+00:00</published>
<link rel='alternate' type='text/html' href='http://91.123.203.49/cgit/delta/cpython-git.git/commit/?id=79bb2c93f2d81702fdf1f93720369e18a76b7d1a'/>
<id>79bb2c93f2d81702fdf1f93720369e18a76b7d1a</id>
<content type='text'>
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
</pre>
</div>
</content>
</entry>
<entry>
<title>bpo-38804: Fix REDoS in http.cookiejar (GH-17157)</title>
<updated>2019-11-22T14:22:11+00:00</updated>
<author>
<name>bcaller</name>
<email>bcaller@users.noreply.github.com</email>
</author>
<published>2019-11-22T14:22:11+00:00</published>
<link rel='alternate' type='text/html' href='http://91.123.203.49/cgit/delta/cpython-git.git/commit/?id=1b779bfb8593739b11cbb988ef82a883ec9d077e'/>
<id>1b779bfb8593739b11cbb988ef82a883ec9d077e</id>
<content type='text'>
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) &gt; 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.</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
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) &gt; 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.</pre>
</div>
</content>
</entry>
</feed>
