python-github-webhooks icon indicating copy to clipboard operation
python-github-webhooks copied to clipboard

not work with secret

Open ionoff opened this issue 8 years ago • 4 comments
trafficstars

File "/app/webhooks.py", line 82, in index
    mac = hmac.new(str(secret), msg=request.data, digestmod='sha1')
  File "/usr/local/lib/python2.7/hmac.py", line 136, in new
    return HMAC(key, msg, digestmod)
  File "/usr/local/lib/python2.7/hmac.py", line 52, in __init__
    self.outer = self.digest_cons()
  File "/usr/local/lib/python2.7/hmac.py", line 50, in <lambda>
    self.digest_cons = lambda d='': digestmod.new(d)
AttributeError: 'str' object has no attribute 'new'

ionoff avatar Jul 09 '17 14:07 ionoff

confirmed here too.

fvanderbiest avatar Jul 30 '17 14:07 fvanderbiest

The same here

ygini avatar Aug 23 '17 06:08 ygini

Here is the fix

--- a/webhooks.py
+++ b/webhooks.py
@@ -79,7 +79,7 @@ def index():
             abort(501)
 
         # HMAC requires the key to be bytes, but data is string
-        mac = hmac.new(str(secret), msg=request.data, digestmod='sha1')
+        mac = hmac.new(str(secret), msg=request.data, digestmod=sha1)
 
         # Python prior to 2.7.7 does not have hmac.compare_digest
         if hexversion >= 0x020707F0:

ygini avatar Aug 23 '17 06:08 ygini

I was still getting below error (FreeBSD)

  File "webhooks.py", line 87, in index
    mac = hmac.new(str(secret), msg=request.data, digestmod=sha1)
  File "/usr/local/lib/python3.7/hmac.py", line 153, in new
    return HMAC(key, msg, digestmod)
  File "/usr/local/lib/python3.7/hmac.py", line 49, in __init__
    raise TypeError("key: expected bytes or bytearray, but got %r" % type(key).__name__)
TypeError: key: expected bytes or bytearray, but got 'str'

Go it to work with

mac = hmac.new(secret.encode('utf-8'), msg=request.data, digestmod=sha1)

richsuca avatar Jan 25 '21 00:01 richsuca