Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@
from ipaddress import ip_address, ip_network
from flask import Flask, request, abort

# Python prior to 2.7.7 does not have hmac.compare_digest
if hexversion >= 0x020707F0:
def constant_time_compare(val1, val2):
return hmac.compare_digest(val1, val2)
else:
def constant_time_compare(val1, val2):
if len(val1) != len(val2):
return False
result = 0
for x, y in zip(val1, val2):
result |= ord(x) ^ ord(y)
return result == 0

application = Flask(__name__)

Expand Down Expand Up @@ -82,18 +94,10 @@ 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:
if not hmac.compare_digest(str(mac.hexdigest()), str(signature)):
abort(403)
else:
# What compare_digest provides is protection against timing
# attacks; we can live without this protection for a web-based
# application
if not str(mac.hexdigest()) == str(signature):
abort(403)
if not constant_time_compare(str(mac.hexdigest()), str(signature)):
abort(403)

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