Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
stevebeattie opened this issue Apr 25, 2022 · 1 comment · May be fixed by #103
Open

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

stevebeattie opened this issue Apr 25, 2022 · 1 comment · May be fixed by #103

Comments

@stevebeattie
Copy link

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 python/cpython@16ee68d 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

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
Copy link

eslerm commented Oct 6, 2022

Big thanks @stevebeattie 🙏

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.

@lengau lengau linked a pull request Sep 19, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants