Skip to content

Commit 6d72953

Browse files
MuMu
Mu
authored and
Mu
committed
Default Message
1 parent 759b67e commit 6d72953

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

webhooks.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@
3131
from ipaddress import ip_address, ip_network
3232
from flask import Flask, request, abort
3333

34+
# Python prior to 2.7.7 does not have hmac.compare_digest
35+
if hexversion >= 0x020707F0:
36+
def constant_time_compare(val1, val2):
37+
return hmac.compare_digest(val1, val2)
38+
else:
39+
def constant_time_compare(val1, val2):
40+
if len(val1) != len(val2):
41+
return False
42+
result = 0
43+
for x, y in zip(val1, val2):
44+
result |= ord(x) ^ ord(y)
45+
return result == 0
3446

3547
application = Flask(__name__)
3648

@@ -84,16 +96,8 @@ def index():
8496
# HMAC requires the key to be bytes, but data is string
8597
mac = hmac.new(str(secret), msg=request.data, digestmod='sha1')
8698

87-
# Python prior to 2.7.7 does not have hmac.compare_digest
88-
if hexversion >= 0x020707F0:
89-
if not hmac.compare_digest(str(mac.hexdigest()), str(signature)):
90-
abort(403)
91-
else:
92-
# What compare_digest provides is protection against timing
93-
# attacks; we can live without this protection for a web-based
94-
# application
95-
if not str(mac.hexdigest()) == str(signature):
96-
abort(403)
99+
if not constant_time_compare(str(mac.hexdigest()), str(signature)):
100+
abort(403)
97101

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

0 commit comments

Comments
 (0)