8000 Fixed the timing attack in twilio.util.RequestValidator. · gustafa/twilio-python@022ea4c · GitHub
[go: up one dir, main page]

Skip to content

Commit 022ea4c

Browse files
committed
Fixed the timing attack in twilio.util.RequestValidator.
1 parent a5eca3a commit 022ea4c

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

twilio/util.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import base64
22
import hmac
3+
import itertools
34
import time
45
from hashlib import sha1
56

@@ -43,7 +44,23 @@ def validate(self, uri, params, signature):
4344
4445
:returns: True if the request passes validation, False if not
4546
"""
46-
return self.compute_signature(uri, params) == signature
47+
return secure_compare(self.compute_signature(uri, params), signature)
48+
49+
50+
def secure_compare(string1, string2):
51+
"""Compare two strings while protecting against timing attacks
52+
53+
:param string1: the first string
54+
:param string2: the second string
55+
56+
:returns: True if the strings are equal, False if not
57+
"""
58+
if len(string1) != len(string2):
59+
return False
60+
result = True
61+
for c1, c2 in itertools.izip(string1, string2):
62+
result &= c1 == c2
63+
return result
4764

4865

4966
class TwilioCapability(object):

0 commit comments

Comments
 (0)
0