Skip to content

Commit 923e771

Browse files
authored
Merge pull request #52 from web-push-libs/bug/crypto
bug: baseline cryptography library to 1.8.2
2 parents 83abcf0 + bd5f31d commit 923e771

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

python/py_vapid/__init__.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from cryptography.hazmat.primitives import serialization
1414

1515
from cryptography.hazmat.primitives import hashes
16+
from cryptography.exceptions import InvalidSignature
1617

1718
from py_vapid.utils import b64urldecode, b64urlencode
1819
from py_vapid.jwt import sign
@@ -112,7 +113,8 @@ def from_file(cls, private_key_file=None):
112113
vapid.generate_keys()
113114
vapid.save_key(private_key_file)
114115
return vapid
115-
private_key = open(private_key_file, 'r').read()
116+
with open(private_key_file, 'r') as file:
117+
private_key = file.read()
116118
try:
117119
if "-----BEGIN" in private_key:
118120
vapid = cls.from_pem(private_key.encode('utf8'))
@@ -218,11 +220,15 @@ def verify_token(self, validation_token, verification_token):
218220
hsig = b64urldecode(verification_token.encode('utf8'))
219221
r = int(binascii.hexlify(hsig[:32]), 16)
220222
s = int(binascii.hexlify(hsig[32:]), 16)
221-
return self.public_key.verify(
222-
ecutils.encode_dss_signature(r, s),
223-
validation_token,
224-
signature_algorithm=ec.ECDSA(hashes.SHA256())
225-
)
223+
try:
224+
self.public_key.verify(
225+
ecutils.encode_dss_signature(r, s),
226+
validation_token,
227+
signature_algorithm=ec.ECDSA(hashes.SHA256())
228+
)
229+
return True
230+
except InvalidSignature:
231+
return False
226232

227233
def _base_sign(self, claims):
228234
if not claims.get('exp'):

python/py_vapid/tests/test_vapid.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,24 @@ def test_integration(self):
169169
# These values were taken from a test page. DO NOT ALTER!
170170
key = ("BDd3_hVL9fZi9Ybo2UUzA284WG5FZR30_95YeZJsiApwXKpNcF1rRPF3foI"
171171
"iBHXRdJI2Qhumhf6_LFTeZaNndIo")
172-
173172
auth = ("WebPush eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJhdWQiOiJod"
174173
"HRwczovL3VwZGF0ZXMucHVzaC5zZXJ2aWNlcy5tb3ppbGxhLmNvbSIsImV"
175174
"4cCI6MTQ5NDY3MTQ3MCwic3ViIjoibWFpbHRvOnNpbXBsZS1wdXNoLWRlb"
176175
"W9AZ2F1bnRmYWNlLmNvLnVrIn0.LqPi86T-HJ71TXHAYFptZEHD7Wlfjcc"
177176
"4u5jYZ17WpqOlqDcW-5Wtx3x1OgYX19alhJ9oLumlS2VzEvNioZolQA")
178177
ok_(Vapid01.verify(key=key, auth=auth))
179178

179+
def test_bad_integration(self):
180+
# These values were taken from a test page. DO NOT ALTER!
181+
key = ("BDd3_hVL9fZi9Ybo2UUzA284WG5FZR30_95YeZJsiApwXKpNcF1rRPF3foI"
182+
"iBHXRdJI2Qhumhf6_LFTeZaNndIo")
183+
auth = ("WebPush eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJhdWQiOiJod"
184+
"HRwczovL3VwZGF0ZXMucHVzaC5zZXJ2aWNlcy5tb3ppbGxhLmNvbSIsImV"
185+
"4cCI6MTQ5NDY3MTQ3MCwic3ViIjoibWFpbHRvOnNpbXBsZS1wdXNoLWRlb"
186+
"W9AZ2F1bnRmYWNlLmNvLnVrIn0.LqPi86T-HJ71TXHAYFptZEHD7Wlfjcc"
187+
"4u5jYZ17WpqOlqDcW-5Wtx3x1OgYX19alhJ9oLumlS2VzEvNioZ_BAD")
188+
eq_(Vapid01.verify(key=key, auth=auth), False)
189+
180190
def test_bad_sign(self):
181191
v = Vapid01.from_file("/tmp/private")
182192
self.assertRaises(VapidException,

python/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
ecdsa==0.13
2-
cryptography==1.8.2
2+
cryptography>=1.8.2,<=1.10

python/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from setuptools import setup, find_packages
55

6-
__version__ = "1.2.4"
6+
__version__ = "1.2.5"
77

88

99
def read_from(file):

0 commit comments

Comments
 (0)