go-macaroon-bakery / py-macaroon-bakery

Python code to implement the httpbakery conventions
GNU Lesser General Public License v3.0
9 stars 24 forks source link

Unable to save macaroons in MozillaCookieJar() under python3.10 #88

Open stevebeattie opened 2 years ago

stevebeattie commented 2 years ago

OS: Ubuntu 22.04 LTS with python 3.10.4 and py-macaroon-bakery 1.3.1

With the move to python 3.10, attempts to store macaroons in http.cookiejar.MozillaCookieJar format files fail. Essentally, I have code that does the following:

    client = httpbakery.Client(cookies=MozillaCookieJar(".cooklefile"))

    if os.path.exists(client.cookies.filename):
        client.cookies.load(ignore_discard=True)

    response = client.request("POST", url=url, json=payload)
    client.cookies.save(ignore_discard=True)

Under python 3.10, this causes a traceback in the call to client.cookies.save() like so:

[ELIDED]
  File "/usr/lib/python3.10/http/cookiejar.py", line 2120, in save
    if cookie.has_nonstandard_attr(HTTPONLY_ATTR):
  File "/usr/lib/python3.10/http/cookiejar.py", line 805, in has_nonstandard_attr
    return name in self._rest
TypeError: argument of type 'NoneType' is not iterable

In python 3.10 the commit https://github.com/python/cpython/commit/16ee68da6e12bb2d79751b32cc37523fe4f4bb48 adds a check for the HTTP Only flag in the MozillaCookieJar class, and in particular, this is checked for before writing out the cookie file via Cookie.has_nonstandard_attr() like so:

                if cookie.has_nonstandard_attr(HTTPONLY_ATTR):
                    domain = HTTPONLY_PREFIX + domain

(see https://github.com/python/cpython/blob/16ee68da6e12bb2d79751b32cc37523fe4f4bb48/Lib/http/cookiejar.py#L2120 )

Cookie.has_nonstandard_attr() is implemented as the following:

    def has_nonstandard_attr(self, name):
        return name in self._rest

(see https://github.com/python/cpython/blame/main/Lib/http/cookiejar.py#L803 )

Unfortunately, macaroonbakery/_utils/__init__.py.cookie() creates a Cookie class with rest=None https://github.com/go-macaroon-bakery/py-macaroon-bakery/blob/df0e18f91267640b2970b8fd0788be4a5e6c0d1c/macaroonbakery/_utils/__init__.py#L163 and that causes the traceback seen above, because you can't test for values present in None.

Fixing (or working around, depending on whether you believe this is a bug in python's cookiejar) this in py-macaroon-bakery should be as simple as:

diff --git a/macaroonbakery/_utils/__init__.py b/macaroonbakery/_utils/__init__.py
index 977cdbe..d1afe31 100644
--- a/macaroonbakery/_utils/__init__.py
+++ b/macaroonbakery/_utils/__init__.py
@@ -160,7 +160,7 @@ def cookie(
         discard=False,
         comment=None,
         comment_url=None,
-        rest=None,
+        rest={},
         rfc2109=False,
     )
eslerm commented 1 year ago

Big thanks @stevebeattie :pray:

I encountered the same scenario which was resolved with the suggested fix.

On my setup, snap Firefox opens and allows me to sign in. After authentication the error occurs.