8000 update request validator to remove port numbers from https urls (#394) · kaijiez/twilio-python@9f3a32f · GitHub
[go: up one dir, main page]

Skip to content

Commit 9f3a32f

Browse files
Brodancodejudas
authored andcommitted
update request validator to remove port numbers from https urls (twilio#394)
* update request validator to remove port numbers from https urls * fix typo
1 parent b3d9ae2 commit 9f3a32f

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

tests/unit/test_request_validator.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def setUp(self):
4141
"ToCountry": "US",
4242
"ToState": "CA",
4343
"ToZip": "94612",
44-
}
44+
}
4545

4646
def test_compute_signature_bytecode(self):
4747
expected = b("fF+xx6dTinOaCdZ0aIeNkHr/ZAA=")
@@ -60,3 +60,8 @@ def test_compute_signature_unicode(self):
6060
def test_validation(self):
6161
expected = "fF+xx6dTinOaCdZ0aIeNkHr/ZAA="
6262
assert_true(self.validator.validate(self.uri, self.params, expected))
63+
64+
def test_validation_removes_port_on_https(self):
65+
self.uri = "https://www.postbin.org:1234/1ed898x"
66+
expected = "Y7MeICc5ECftd1G11Fc8qoxAn0A="
67+
assert_true(self.validator.validate(self.uri, self.params, expected))

twilio/request_validator.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from six import PY3
66

7-
from twilio.compat import izip
7+
from twilio.compat import izip, urlparse
88

99

1010
def compare(string1, string2):
@@ -24,6 +24,19 @@ def compare(string1, string2):
2424
return result
2525

2626

27+
def remove_port(uri):
28+
"""Remove the port number from a URI
29+
30+
:param uri: full URI that Twilio requested on your server
31+
32+
:returns: full URI without a port number
33+
:rtype: str
34+
"""
35+
new_netloc = uri.netloc.split(':')[0]
36+
new_uri = uri._replace(netloc=new_netloc)
37+
return new_uri.geturl()
38+
39+
2740
class RequestValidator(object):
2841

2942
def __init__(self, token):
@@ -60,4 +73,7 @@ def validate(self, uri, params, signature):
6073
6174
:returns: True if the request passes validation, False if not
6275
"""
76+
parsed_uri = urlparse(uri)
77+
if parsed_uri.scheme == "https" and parsed_uri.port:
78+
uri = remove_port(parsed_uri)
6379
return compare(self.compute_signature(uri, params), signature)

0 commit comments

Comments
 (0)
0