Skip to content

Commit 2e95413

Browse files
committed
Copy subscription before altering the keys.
Altering subscriptions reference by changing the keys from string to byte causes hard to debug issues in libraries. For exemple, trying to call `json.dumps()` after initializing `WebPusher` throws an error because json cannot handle bytes.
1 parent 7b58129 commit 2e95413

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

pywebpush/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

55
import base64
6+
from copy import deepcopy
67
import json
78
import os
89

@@ -116,7 +117,7 @@ def __init__(self, subscription_info, requests_session=None):
116117

117118
if 'endpoint' not in subscription_info:
118119
raise WebPushException("subscription_info missing endpoint URL")
119-
self.subscription_info = subscription_info
120+
self.subscription_info = deepcopy(subscription_info)
120121
self.auth_key = self.receiver_key = None
121122
if 'keys' in subscription_info:
122123
keys = self.subscription_info['keys']

pywebpush/tests/test_webpush.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import unittest
55

66
from mock import patch
7-
from nose.tools import eq_, ok_, assert_raises
7+
from nose.tools import eq_, ok_, assert_is_not, assert_raises
88
import http_ece
99
from cryptography.hazmat.primitives.asymmetric import ec
1010
from cryptography.hazmat.backends import default_backend
@@ -75,7 +75,10 @@ def test_init(self):
7575
"keys": {'p256dh': 'AAA=', 'auth': 'AAA='}})
7676

7777
push = WebPusher(subscription_info)
78-
eq_(push.subscription_info, subscription_info)
78+
assert_is_not(push.subscription_info, subscription_info)
79+
assert_is_not(push.subscription_info['keys'],
80+
subscription_info['keys'])
81+
eq_(push.subscription_info['endpoint'], subscription_info['endpoint'])
7982
eq_(push.receiver_key, rk_decode)
8083
eq_(push.auth_key, b'\x93\xc2U\xea\xc8\xddn\x10"\xd6}\xff,0K\xbc')
8184

0 commit comments

Comments
 (0)