Skip to content

Commit 838bf23

Browse files
Merge pull request carlos-jenkins#5 from kpriceyahoo/allow-compare-digest-fallback
Add hmac.compare_digest fallback path for Python versions < 2.7.7
2 parents b08f5cb + c225d20 commit 838bf23

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

webhooks.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# under the License.
1717

1818
import logging
19-
from sys import stderr
19+
from sys import stderr, hexversion
2020
logging.basicConfig(stream=stderr)
2121

2222
import hmac
@@ -75,8 +75,16 @@ def index():
7575

7676
# HMAC requires the key to be bytes, but data is string
7777
mac = hmac.new(str(secret), msg=request.data, digestmod=sha1)
78-
if not hmac.compare_digest(str(mac.hexdigest()), str(signature)):
79-
abort(403)
78+
79+
# Python prior to 2.7.7 does not have hmac.compare_digest
80+
if hexversion >= 0x020707F0:
81+
if not hmac.compare_digest(str(mac.hexdigest()), str(signature)):
82+
abort(403)
83+
else:
84+
# What compare_digest provides is protection against timing attacks; we
85+
# can live without this protection for a web-based application
86+
if not str(mac.hexdigest()) == str(signature):
87+
abort(403)
8088

8189
# Implement ping
8290
event = request.headers.get('X-GitHub-Event', 'ping')

0 commit comments

Comments
 (0)