ecordell / pymacaroons

A Python Macaroon Library
pymacaroons.readthedocs.org
MIT License
79 stars 23 forks source link

Crashes while doing .inspect() #17

Closed facundobatista closed 8 years ago

facundobatista commented 8 years ago

See:

Python 3.4.3+ (default, Oct 14 2015, 16:03:50) 
[GCC 5.2.1 20151010] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymacaroons
>>> pymacaroons.__version__
'0.9.0'
>>> m = pymacaroons.Macaroon(location="location", identifier="identifier", key="key")
>>> m.add_third_party_caveat("other location", "caveat key", "identifier")
<pymacaroons.macaroon.Macaroon object at 0x7ff9474fc978>
>>> m.inspect()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/facundo/.local/share/fades/664c588e-be8a-4843-8e95-dfdba5a36353/lib/python3.4/site-packages/pymacaroons/macaroon.py", line 85, in inspect
    if caveat.verification_key_id and caveat.location:
  File "/home/facundo/.local/share/fades/664c588e-be8a-4843-8e95-dfdba5a36353/lib/python3.4/site-packages/pymacaroons/caveat.py", line 22, in verification_key_id
    return convert_to_string(self._verification_key_id)
  File "/home/facundo/.local/share/fades/664c588e-be8a-4843-8e95-dfdba5a36353/lib/python3.4/site-packages/pymacaroons/utils.py", line 25, in convert_to_string
    return string_or_bytes.decode('ascii')
UnicodeDecodeError: 'ascii' codec can't decode byte 0x89 in position 1: ordinal not in range(128)
ecordell commented 8 years ago

Thanks for the bug report!

To me this looks like the verification_id is being generated with a non-ascii value. That value comes from the secret_box method in libsodium; could you tell me what version of libsodium you're running?

I'm having trouble reproducing it so any more information would be helpful. Does it happen every time you run or only sometimes? (The secret_box method generates a random nonce every time, it's possible that's the culprit).

Also just to mention, there is a passing unit test for this case.

facundobatista commented 8 years ago

Thanks for the fast response!

The libsodium lib I have installed in the system (I guess that this is the one pymacaroon uses, even if pymacaroon is installed in a venv is):

$ apt-cache policy libsodium13
libsodium13:
  Installed: 1.0.3-1

It happens every time. The call that makes convert_to_string() crash is passing this as string_or_bytes:

b'\xaa\xa1\x14\xfa\x99\xc3\x83\xe4by\xb5>\x8f\xf2.\x18\x07A/\xf1\xc6\x10\xee\xf4Q{a\x1c\xe8/\xc9e\xf4\xbe\x9b[\xd1\x8e\xcb\x12\xfc\xdc\x89\x8cp=\x8e\xdd\xec4f\xcd\xb4\xf2\xd2\xbb\xb8\xdc\xfe\x87\x03\x99\xeb\xd2\xab\xda\x98\x1d\xe9\x1cG%'

I'm happy to debug anything and help you to fix this.

Regards,

ecordell commented 8 years ago

@facundobatista I realized that the fixes that were on master for inspect were not in 0.9.0 on pypi!

I've released a new version, 0.9.1, which should fix the problems you're seeing. Could you try with 0.9.1 and see?

facundobatista commented 8 years ago

Solved!!

$ fades -d  pymacaroons==0.9.1
*** fades ***  2016-02-11 15:46:39,055  INFO     Need to install a dependency with pip, but no builtin, doing it manually (just wait a little, all should go well)
*** fades ***  2016-02-11 15:46:41,923  INFO     Installing dependency: pymacaroons==0.9.1
Python 3.4.3 (default, Oct 14 2015, 20:33:09) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymacaroons
>>> m = pymacaroons.Macaroon(location="location", identifier="identifier", key="key")
>>> m.add_third_party_caveat("other location", "caveat key", "identifier")
<pymacaroons.macaroon.Macaroon object at 0xb707e80c>
>>> m.inspect()
'location location\nidentifier identifier\ncid identifier\nvid vLcWEYbQDJxlpohwSc6QHVqGbGL9OPjU9bfUan88VbkccJin6hVYDL1ZfXYVhumS+5A+lxrS0hGy5JIWBm+ZiHCnZ6ippn8X\ncl other location\nsignature a857f71e833772bd31beeb3b2aaf37c4a602be52547fc8cf76e69dfff4e8b868'
>>> print(_)
location location
identifier identifier
cid identifier
vid vLcWEYbQDJxlpohwSc6QHVqGbGL9OPjU9bfUan88VbkccJin6hVYDL1ZfXYVhumS+5A+lxrS0hGy5JIWBm+ZiHCnZ6ippn8X
cl other location
signature a857f71e833772bd31beeb3b2aaf37c4a602be52547fc8cf76e69dfff4e8b868
facundobatista commented 8 years ago

And thanks for this work!!