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

AttributeError: 'SHA512' object has no attribute 'update' #364

Closed
giovannicimolin opened this issue Aug 12, 2024 · 5 comments
Closed

AttributeError: 'SHA512' object has no attribute 'update' #364

giovannicimolin opened this issue Aug 12, 2024 · 5 comments

Comments

@giovannicimolin
Copy link
Contributor

giovannicimolin commented Aug 12, 2024

When migrating my project to version 5.x, I get the following error when running tests:

    response = handler(request, *args, **kwargs)
  File "/code/custom_auth/views.py", line 165, in post
    _, token = AuthToken.objects.create(
  File "/opt/venv/lib/python3.10/site-packages/knox/models.py", line 24, in create
    digest = crypto.hash_token(token)
  File "/opt/venv/lib/python3.10/site-packages/knox/crypto.py", line 30, in hash_token
    digest.update(make_hex_compatible(token))
AttributeError: 'SHA512' object has no attribute 'update'

Related code:

def make_hex_compatible(token: str) -> bytes:

Investigation ongoing.

@giovannicimolin
Copy link
Contributor Author

giovannicimolin commented Aug 12, 2024

I think this was caused by this switch alongside the code changes linked above: #230

@johnraz
Copy link
Collaborator

johnraz commented Aug 12, 2024

The related code is actually

digest.update(make_hex_compatible(token))

and digest is from

hash_func = knox_settings.SECURE_HASH_ALGORITHM

This is weird because the interface should have the update method according to the Python doc

https://docs.python.org/3/library/hashlib.html#hash-algorithms

What do you have in your settings ? Could you have a bad import there ?

Also the Python version might be a factor here

which version are you using?
edit: it’s clearly 3.10 from the traceback

I recommend we improve the test suite of knox to cover all the hash functions we support.

Cheers !

@giovannicimolin
Copy link
Contributor Author

@johnraz Yup, that's excatly the place having issues.

I'm using the old default cryptography.hazmat.primitives.hashes.SHA512, but it looks like it's broken now.
I'm investigating to figure out why this happened and why it was working before.

I recommend we improve the test suite of knox to cover all the hash functions we support.

👍 to this.
It was already in my plans but I guess I'll have to work on it earlier than expected.

@giovannicimolin
Copy link
Contributor Author

giovannicimolin commented Aug 13, 2024

In 4.2.0 the code was like this:

    digest = hashes.Hash(sha(), backend=default_backend())
    digest.update(binascii.unhexlify(token))
    return binascii.hexlify(digest.finalize()).decode()

With the signature change the cryptography functions don't work anymore.

I wanted to keep old tokens working with #362, but now I'm not 100% sure it'll work as is currently.

@giovannicimolin
Copy link
Contributor Author

I'll close this now since the issue was me trying to use a now incompatible hashing method.

If anyone is looking for a solution when bumping into this issue: just remove the line below and let the library use the default from hashlib.

    "SECURE_HASH_ALGORITHM": "cryptography.hazmat.primitives.hashes.SHA512",

I think we'll need to do a better job at documentation when changes like this come in. I'll be more attentive when reviewing PRs related to this now.

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

No branches or pull requests

2 participants