Skip to content

Commit 9c59d70

Browse files
authored
Merge pull request #67 from web-push-libs/feat/79
feat: Deep copy the claims before fixup
2 parents 9f15984 + f32719d commit 9c59d70

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

python/py_vapid/__init__.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import binascii
88
import time
99
import re
10+
import copy
1011

1112
from cryptography.hazmat.backends import default_backend
1213
from cryptography.hazmat.primitives.asymmetric import ec, utils as ecutils
@@ -254,23 +255,24 @@ def verify_token(self, validation_token, verification_token):
254255
return False
255256

256257
def _base_sign(self, claims):
257-
if not claims.get('exp'):
258-
claims['exp'] = str(int(time.time()) + 86400)
258+
cclaims = copy.deepcopy(claims)
259+
if not cclaims.get('exp'):
260+
cclaims['exp'] = str(int(time.time()) + 86400)
259261
if not re.match("mailto:.+@.+\..+",
260-
claims.get('sub', ''),
262+
cclaims.get('sub', ''),
261263
re.IGNORECASE):
262264
raise VapidException(
263265
"Missing 'sub' from claims. "
264266
"'sub' is your admin email as a mailto: link.")
265267
if not re.match("^https?:\/\/[^\/\.:]+\.[^\/:]+(:\d+)?$",
266-
claims.get("aud", ""),
268+
cclaims.get("aud", ""),
267269
re.IGNORECASE):
268270
raise VapidException(
269271
"Missing 'aud' from claims. "
270272
"'aud' is the scheme, host and optional port for this "
271273
"transaction e.g. https://example.com:8080")
272274

273-
return claims
275+
return cclaims
274276

275277
def sign(self, claims, crypto_key=None):
276278
"""Sign a set of claims.
@@ -284,8 +286,7 @@ def sign(self, claims, crypto_key=None):
284286
:rtype: dict
285287
286288
"""
287-
claims = self._base_sign(claims)
288-
sig = sign(claims, self.private_key)
289+
sig = sign(self._base_sign(claims), self.private_key)
289290
pkey = 'p256ecdsa='
290291
pkey += b64urlencode(
291292
self.public_key.public_numbers().encode_point())
@@ -307,8 +308,7 @@ class Vapid02(Vapid01):
307308
_schema = "vapid"
308309

309310
def sign(self, claims, crypto_key=None):
310-
claims = self._base_sign(claims)
311-
sig = sign(claims, self.private_key)
311+
sig = sign(self._base_sign(claims), self.private_key)
312312
pkey = self.public_key.public_numbers().encode_point()
313313
return{
314314
"Authorization": "{schema} t={t},k={k}".format(

python/py_vapid/tests/test_vapid.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import binascii
22
import base64
3+
import copy
34
import os
45
import json
56
import unittest
@@ -139,7 +140,8 @@ def test_sign_01(self):
139140
v.public_key.public_numbers().encode_point()
140141
).decode('utf8').replace('+', '-').replace('/', '_').strip()
141142
items = decode(result['Authorization'].split(' ')[1], pkey)
142-
eq_(items, claims)
143+
for k in claims:
144+
eq_(items[k], claims[k])
143145
result = v.sign(claims)
144146
eq_(result['Crypto-Key'],
145147
'p256ecdsa=' + T_PUBLIC_RAW.decode('utf8'))
@@ -155,6 +157,7 @@ def test_sign_02(self):
155157
claims = {"aud": "https://example.com",
156158
"sub": "mailto:[email protected]",
157159
"foo": "extra value"}
160+
claim_check = copy.deepcopy(claims)
158161
result = v.sign(claims, "id=previous")
159162
auth = result['Authorization']
160163
eq_(auth[:6], 'vapid ')
@@ -168,6 +171,7 @@ def test_sign_02(self):
168171
k_val = binascii.a2b_base64(self.repad(parts[1][2:]))
169172
eq_(binascii.hexlify(k_val)[:2], b'04')
170173
eq_(len(k_val), 65)
174+
eq_(claims, claim_check)
171175
for k in claims:
172176
eq_(t_val[k], claims[k])
173177

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.3.1"
6+
__version__ = "1.4.0"
77

88

99
def read_from(file):

0 commit comments

Comments
 (0)