Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions pubnub/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ def decrypt(self, key, msg, use_random_iv=False):
plain = self.depad((cipher.decrypt(extracted_message)).decode('utf-8'))
except UnicodeDecodeError as e:
if not self.fallback_mode:
raise e
logging.debug(f'Decryption failed: {e}')
raise PubNubException('decryption error')

cipher = AES.new(bytes(secret[0:32], "utf-8"), self.fallback_mode, initialization_vector)
plain = self.depad((cipher.decrypt(extracted_message)).decode('utf-8'))
try:
cipher = AES.new(bytes(secret[0:32], "utf-8"), self.fallback_mode, initialization_vector)
plain = self.depad((cipher.decrypt(extracted_message)).decode('utf-8'))
except (UnicodeDecodeError, ValueError, IndexError) as fallback_error:
logging.debug(f'Decryption failed: {fallback_error}')
raise PubNubException('decryption error')

try:
return json.loads(plain)
Expand Down Expand Up @@ -103,11 +108,16 @@ def decrypt(self, key, file, use_random_iv=True):
try:
cipher = AES.new(bytes(secret[0:32], "utf-8"), self.mode, initialization_vector)
result = unpad(cipher.decrypt(extracted_file), 16)
except ValueError:
if not self.fallback_mode: # No fallback mode so we return the original content
return file
cipher = AES.new(bytes(secret[0:32], "utf-8"), self.fallback_mode, initialization_vector)
result = unpad(cipher.decrypt(extracted_file), 16)
except ValueError as e:
if not self.fallback_mode:
logging.debug(f'File decryption failed: {e}')
raise PubNubException('decryption error')
try:
cipher = AES.new(bytes(secret[0:32], "utf-8"), self.fallback_mode, initialization_vector)
result = unpad(cipher.decrypt(extracted_file), 16)
except ValueError as fallback_error:
logging.debug(f'File decryption failed: {fallback_error}')
raise PubNubException('decryption error')

return result

Expand Down
30 changes: 22 additions & 8 deletions pubnub/crypto_core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import hashlib
import json
import logging
import secrets

from abc import abstractmethod
Expand Down Expand Up @@ -79,15 +80,24 @@ def decrypt(self, payload: CryptorPayload, key=None, use_random_iv=False, binary
raise PubNubException('decryption error')
cipher = AES.new(bytes(secret[0:32], "utf-8"), self.mode, initialization_vector)
if binary_mode:
return self.depad(cipher.decrypt(extracted_message), binary_mode)
try:
return self.depad(cipher.decrypt(extracted_message), binary_mode)
except (ValueError, IndexError) as e:
logging.debug(f'Decryption failed: {e}')
raise PubNubException('decryption error')
try:
plain = self.depad((cipher.decrypt(extracted_message)).decode('utf-8'), binary_mode)
except UnicodeDecodeError as e:
if not self.fallback_mode:
raise e
logging.debug(f'Decryption failed: {e}')
raise PubNubException('decryption error')

cipher = AES.new(bytes(secret[0:32], "utf-8"), self.fallback_mode, initialization_vector)
plain = self.depad((cipher.decrypt(extracted_message)).decode('utf-8'), binary_mode)
try:
cipher = AES.new(bytes(secret[0:32], "utf-8"), self.fallback_mode, initialization_vector)
plain = self.depad((cipher.decrypt(extracted_message)).decode('utf-8'), binary_mode)
except (UnicodeDecodeError, ValueError, IndexError) as fallback_error:
logging.debug(f'Decryption failed: {fallback_error}')
raise PubNubException('decryption error')

try:
return json.loads(plain)
Expand Down Expand Up @@ -156,7 +166,11 @@ def decrypt(self, payload: CryptorPayload, key=None, binary_mode: bool = False,

cipher = AES.new(secret, mode=self.mode, iv=iv)

if binary_mode:
return unpad(cipher.decrypt(payload['data']), AES.block_size)
else:
return unpad(cipher.decrypt(payload['data']), AES.block_size).decode()
try:
if binary_mode:
return unpad(cipher.decrypt(payload['data']), AES.block_size)
else:
return unpad(cipher.decrypt(payload['data']), AES.block_size).decode()
except (ValueError, UnicodeDecodeError) as e:
logging.debug(f'Decryption failed: {e}')
raise PubNubException('decryption error')
2 changes: 1 addition & 1 deletion pubnub/workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def _process_message(self, message_input):
try:
return self._pubnub.crypto.decrypt(message_input), None
except Exception as exception:
logger.warning("could not decrypt message: \"%s\", due to error %s" % (message_input, str(exception)))
logger.warning("could not decrypt message, due to error %s" % str(exception))

pn_status = PNStatus()
pn_status.category = PNStatusCategory.PNDecryptionErrorCategory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,4 @@ interactions:
status:
code: 200
message: OK
url: !!python/object/new:yarl.URL
state: !!python/tuple
- !!python/object/new:urllib.parse.SplitResult
- https
- ps.pndsn.com
- /v3/history/sub-key/sub-c-mock-key/channel/my-ch
- start=123&end=456&pnsdk=PubNub-Python-Asyncio%2F4.7.0&uuid=af9429d0-aa10-4919-9670-abe67a5c395f
- ''
version: 1
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,4 @@ interactions:
status:
code: 200
message: OK
url: !!python/object/new:yarl.URL
state: !!python/tuple
- !!python/object/new:urllib.parse.SplitResult
- https
- ps.pndsn.com
- /v3/history/sub-key/sub-c-mock-key/channel/my-ch-%20%7C.%2A%20%24
- start=123&end=456&pnsdk=PubNub-Python-Asyncio%2F4.7.0&uuid=fbbfbfbf-2b08-4561-bccb-3a0003b0b71b
- ''
version: 1
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,4 @@ interactions:
status:
code: 200
message: OK
url: !!python/object/new:yarl.URL
state: !!python/tuple
- !!python/object/new:urllib.parse.SplitResult
- http
- ps.pndsn.com
- /v1/objects/demo/spaces/value1/users
- count=True&include=custom,user,user.custom&pnsdk=PubNub-Python-Asyncio%2F4.1.0&uuid=f032239c-241a-45f7-ac74-02ebfe06a29e
- ''
version: 1
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,4 @@ interactions:
status:
code: 200
message: OK
url: !!python/object/new:yarl.URL
state: !!python/tuple
- !!python/object/new:urllib.parse.SplitResult
- http
- ps.pndsn.com
- /v1/objects/demo/users/mg3/spaces
- count=True&include=custom,space,space.custom&pnsdk=PubNub-Python-Asyncio%2F4.1.0&uuid=8d72a1a1-eec4-4b3f-84d6-53e88c80ded1
- ''
version: 1
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,4 @@ interactions:
status:
code: 200
message: OK
url: !!python/object/new:yarl.URL
state: !!python/tuple
- !!python/object/new:urllib.parse.SplitResult
- http
- ps.pndsn.com
- /v1/objects/demo/spaces/value1/users
- include=custom,user,user.custom&pnsdk=PubNub-Python-Asyncio%2F4.1.0&uuid=8cc8fb7d-6bb8-4109-a6b9-750490d89e7a
- ''
version: 1
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,4 @@ interactions:
status:
code: 200
message: OK
url: !!python/object/new:yarl.URL
state: !!python/tuple
- !!python/object/new:urllib.parse.SplitResult
- http
- ps.pndsn.com
- /v1/objects/demo/users/mg/spaces
- include=custom,space,space.custom&pnsdk=PubNub-Python-Asyncio%2F4.1.0&uuid=ca41ef07-c7db-4874-be1d-7039c919ef6f
- ''
version: 1
11 changes: 0 additions & 11 deletions tests/integrational/fixtures/asyncio/message_count/multi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ interactions:
Cache-Control: no-cache, Connection: keep-alive, Content-Length: '30', Content-Type: text/javascript;
charset="UTF-8", Date: 'Sun, 24 Feb 2019 20:13:16 GMT'}
status: {code: 200, message: OK}
url: !!python/object/new:yarl.URL
state: !!python/tuple
- !!python/object/new:urllib.parse.SplitResult [http, balancer1g.bronze.aws-pdx-1.ps.pn,
/publish/demo-36/demo-36/0/unique_asyncio_1/0/%22something%22, pnsdk=PubNub-Python-Asyncio%2F4.1.0&uuid=d2a546ca-037c-499a-9d87-35951bbbd289,
'']
- request:
body: null
headers:
Expand All @@ -30,10 +25,4 @@ interactions:
Content-Length: '109', Content-Type: text/javascript; charset="UTF-8", Date: 'Sun,
24 Feb 2019 20:13:16 GMT', Server: Pubnub}
status: {code: 200, message: OK}
url: !!python/object/new:yarl.URL
state: !!python/tuple
- !!python/object/new:urllib.parse.SplitResult [http, balancer1g.bronze.aws-pdx-1.ps.pn,
'/v3/history/sub-key/demo-36/message-counts/unique_asyncio_1,unique_asyncio_2',
'channelsTimetoken=15510391962937046,15510391962937046&pnsdk=PubNub-Python-Asyncio%2F4.1.0&uuid=d2a546ca-037c-499a-9d87-35951bbbd289&l_pub=0.37061548233032227',
'']
version: 1
10 changes: 0 additions & 10 deletions tests/integrational/fixtures/asyncio/message_count/single.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ interactions:
Cache-Control: no-cache, Connection: keep-alive, Content-Length: '30', Content-Type: text/javascript;
charset="UTF-8", Date: 'Sun, 24 Feb 2019 20:13:15 GMT'}
status: {code: 200, message: OK}
url: !!python/object/new:yarl.URL
state: !!python/tuple
- !!python/object/new:urllib.parse.SplitResult [http, balancer1g.bronze.aws-pdx-1.ps.pn,
/publish/demo-36/demo-36/0/unique_asyncio/0/%22bla%22, pnsdk=PubNub-Python-Asyncio%2F4.1.0&uuid=68f7b4f4-c169-4a49-b09d-7c68e22049b8,
'']
- request:
body: null
headers:
Expand All @@ -30,9 +25,4 @@ interactions:
Content-Length: '86', Content-Type: text/javascript; charset="UTF-8", Date: 'Sun,
24 Feb 2019 20:13:15 GMT', Server: Pubnub}
status: {code: 200, message: OK}
url: !!python/object/new:yarl.URL
state: !!python/tuple
- !!python/object/new:urllib.parse.SplitResult [http, balancer1g.bronze.aws-pdx-1.ps.pn,
/v3/history/sub-key/demo-36/message-counts/unique_asyncio, timetoken=15510391957007172&pnsdk=PubNub-Python-Asyncio%2F4.1.0&uuid=68f7b4f4-c169-4a49-b09d-7c68e22049b8&l_pub=0.4618048667907715,
'']
version: 1
16 changes: 0 additions & 16 deletions tests/integrational/fixtures/asyncio/pam/global_level.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ interactions:
status:
code: 200
message: OK
url: !!python/object/new:yarl.URL
state: !!python/tuple
- !!python/object/new:urllib.parse.SplitResult
- https
- ps.pndsn.com
- /v2/auth/grant/sub-key/sub-c-7ba2ac4c-4836-11e6-85a4-0619f8945a4f
- pnsdk=PubNub-Python-Asyncio%2F4.7.0&r=1&signature=v2.Um4OSe_f8tRtFo2tuw0lmwE6Rq5wgjTHmfblkIyoZ4I&timestamp=1606303468&uuid=my_uuid&w=1
- ''
- request:
body: null
headers:
Expand All @@ -53,12 +45,4 @@ interactions:
status:
code: 200
message: OK
url: !!python/object/new:yarl.URL
state: !!python/tuple
- !!python/object/new:urllib.parse.SplitResult
- https
- ps.pndsn.com
- /v2/auth/grant/sub-key/sub-c-7ba2ac4c-4836-11e6-85a4-0619f8945a4f
- g=0&j=0&l_pam=0.24709081649780273&m=0&pnsdk=PubNub-Python-Asyncio%2F4.7.0&r=0&signature=v2.NyyRFAQKOOpqAAAMlcN6wHg-cmHLwC6L7KgdEqwS7bY&timestamp=1606303468&u=0&uuid=my_uuid&w=0
- ''
version: 1
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,4 @@ interactions:
status:
code: 200
message: OK
url: !!python/object/new:yarl.URL
state: !!python/tuple
- !!python/object/new:urllib.parse.SplitResult
- http
- ps.pndsn.com
- /v2/auth/grant/sub-key/sub-c-7ba2ac4c-4836-11e6-85a4-0619f8945a4f
- channel-group=test-pam-asyncio-cg1,test-pam-asyncio-cg2&pnsdk=PubNub-Python-Asyncio%2F4.1.0&r=1&signature=v2.0eTFy_Kgi-Qiz6nD3NmfZlu4Z4ndtUT5pYHl57imcZI&timestamp=1577189139&uuid=my_uuid&w=1
- ''
version: 1
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,4 @@ interactions:
status:
code: 200
message: OK
url: !!python/object/new:yarl.URL
state: !!python/tuple
- !!python/object/new:urllib.parse.SplitResult
- http
- ps.pndsn.com
- /v2/auth/grant/sub-key/sub-c-7ba2ac4c-4836-11e6-85a4-0619f8945a4f
- auth=test-pam-asyncio-auth&channel-group=test-pam-asyncio-cg1,test-pam-asyncio-cg2&pnsdk=PubNub-Python-Asyncio%2F4.1.0&r=1&signature=v2.pjnKPVphocAsl8BsxLeirCZMbNVzNOQV3CS6mfm1Bbc&timestamp=1577189139&uuid=my_uuid&w=1
- ''
version: 1
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,4 @@ interactions:
status:
code: 200
message: OK
url: !!python/object/new:yarl.URL
state: !!python/tuple
- !!python/object/new:urllib.parse.SplitResult
- http
- ps.pndsn.com
- /v2/auth/grant/sub-key/sub-c-7ba2ac4c-4836-11e6-85a4-0619f8945a4f
- channel=test-pam-asyncio-ch1,test-pam-asyncio-ch2&pnsdk=PubNub-Python-Asyncio%2F4.1.0&r=1&signature=v2.z01_vYcxRHcQlLohU41PTYPzZOfaU8xWK4qXRF4bjK8&timestamp=1577189138&uuid=test-pam-asyncio-uuid&w=1
- ''
version: 1
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,4 @@ interactions:
status:
code: 200
message: OK
url: !!python/object/new:yarl.URL
state: !!python/tuple
- !!python/object/new:urllib.parse.SplitResult
- http
- ps.pndsn.com
- /v2/auth/grant/sub-key/sub-c-7ba2ac4c-4836-11e6-85a4-0619f8945a4f
- auth=test-pam-asyncio-auth&channel=test-pam-asyncio-ch1,test-pam-asyncio-ch2&pnsdk=PubNub-Python-Asyncio%2F4.1.0&r=1&signature=v2.YX_q8cliqGK-cMPUevjVQ1rRnEFAkKLLkutGJt9X1OY&timestamp=1577189138&uuid=my_uuid&w=1
- ''
version: 1
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,4 @@ interactions:
status:
code: 200
message: OK
url: !!python/object/new:yarl.URL
state: !!python/tuple
- !!python/object/new:urllib.parse.SplitResult
- http
- ps.pndsn.com
- /v2/auth/grant/sub-key/sub-c-7ba2ac4c-4836-11e6-85a4-0619f8945a4f
- channel=test-pam-asyncio-ch&pnsdk=PubNub-Python-Asyncio%2F4.1.0&r=1&signature=v2.fNqcroTl6ykcSUYDgrOmpGVe2b_11FKkOjU8_LMt7E8&timestamp=1577189138&uuid=my_uuid&w=1
- ''
version: 1
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,4 @@ interactions:
status:
code: 200
message: OK
url: !!python/object/new:yarl.URL
state: !!python/tuple
- !!python/object/new:urllib.parse.SplitResult
- http
- ps.pndsn.com
- /v2/auth/grant/sub-key/sub-c-7ba2ac4c-4836-11e6-85a4-0619f8945a4f
- channel-group=test-pam-asyncio-cg&pnsdk=PubNub-Python-Asyncio%2F4.1.0&r=1&signature=v2.BihlEpGJOoGHtVcTzIw1h0Jp7vqKoIdpkxaIYrvV1FU&timestamp=1577189138&uuid=test-pam-asyncio-uuid&w=1
- ''
version: 1
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,4 @@ interactions:
status:
code: 200
message: OK
url: !!python/object/new:yarl.URL
state: !!python/tuple
- !!python/object/new:urllib.parse.SplitResult
- http
- ps.pndsn.com
- /v2/auth/grant/sub-key/sub-c-7ba2ac4c-4836-11e6-85a4-0619f8945a4f
- auth=test-pam-asyncio-auth&channel-group=test-pam-asyncio-cg&pnsdk=PubNub-Python-Asyncio%2F4.1.0&r=1&signature=v2.Sjk_iz1y4xns4Mt3xuch3EWqgAJogNU6RJ6TCce-_3w&timestamp=1577189139&uuid=test-pam-asyncio-uuid&w=1
- ''
version: 1
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,4 @@ interactions:
status:
code: 200
message: OK
url: !!python/object/new:yarl.URL
state: !!python/tuple
- !!python/object/new:urllib.parse.SplitResult
- http
- ps.pndsn.com
- /v2/auth/grant/sub-key/sub-c-7ba2ac4c-4836-11e6-85a4-0619f8945a4f
- auth=test-pam-asyncio-auth&channel=test-pam-asyncio-ch&pnsdk=PubNub-Python-Asyncio%2F4.1.0&r=1&signature=v2.P1WnlQZkBoiO8ah1YE9CS_Cgq4Iyi34TmjCB9Hj0qUA&timestamp=1577189138&uuid=test-pam-asyncio-uuid&w=1
- ''
version: 1
8 changes: 0 additions & 8 deletions tests/integrational/fixtures/asyncio/signal/single.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,4 @@ interactions:
status:
code: 200
message: OK
url: !!python/object/new:yarl.URL
state: !!python/tuple
- !!python/object/new:urllib.parse.SplitResult
- http
- ps.pndsn.com
- /signal/demo/demo/0/unique_sync/0/%22test%22
- pnsdk=PubNub-Python-Asyncio%2F4.1.0&uuid=f5706789-e3a0-459e-871d-e4aed46e5458
- ''
version: 1
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,4 @@ interactions:
status:
code: 200
message: OK
url: !!python/object/new:yarl.URL
state: !!python/tuple
- !!python/object/new:urllib.parse.SplitResult
- http
- ps.pndsn.com
- /v1/objects/demo/spaces
- include=custom&pnsdk=PubNub-Python-Asyncio%2F4.1.0&uuid=30b485f7-38c6-4e5b-8911-06f5016d415d
- ''
version: 1
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,4 @@ interactions:
status:
code: 200
message: OK
url: !!python/object/new:yarl.URL
state: !!python/tuple
- !!python/object/new:urllib.parse.SplitResult
- http
- ps.pndsn.com
- /v1/objects/demo/spaces/in_space
- pnsdk=PubNub-Python-Asyncio%2F4.1.0&uuid=982fa2bc-479b-4f37-a3a0-1a44f7a00011
- ''
version: 1
Loading
Loading