Skip to content

Commit

Permalink
Return unencrypted data (#156)
Browse files Browse the repository at this point in the history
refactor(crypto): return unencrypted data

Return source event data when a client configured with crypto is unable to decrypt it (can be not
encrypted data or encrypted with different options)
  • Loading branch information
parfeon authored Nov 23, 2023
1 parent e553ac2 commit 0acb1d8
Show file tree
Hide file tree
Showing 13 changed files with 535 additions and 212 deletions.
13 changes: 9 additions & 4 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
---
version: "5.3.1"
version: "5.3.2"
changelog:
- date: 2023-11-23
version: v5.3.2
changes:
- type: improvement
text: "Return source event data when a client configured with crypto is unable to decrypt it (can be not encrypted data or encrypted with different options)."
- date: 2023-11-03
version: v5.3.1
changes:
Expand Down Expand Up @@ -675,7 +680,7 @@ sdks:
- x86-64
- distribution-type: package
distribution-repository: RubyGems
package-name: pubnub-5.3.1.gem
package-name: pubnub-5.3.2.gem
location: https://rubygems.org/gems/pubnub
requires:
- name: addressable
Expand Down Expand Up @@ -780,8 +785,8 @@ sdks:
- x86-64
- distribution-type: library
distribution-repository: GitHub release
package-name: pubnub-5.3.1.gem
location: https://github.com/pubnub/ruby/releases/download/v5.3.1/pubnub-5.3.1.gem
package-name: pubnub-5.3.2.gem
location: https://github.com/pubnub/ruby/releases/download/v5.3.2/pubnub-5.3.2.gem
requires:
- name: addressable
min-version: 2.0.0
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v5.3.2
November 23 2023

#### Modified
- Return source event data when a client configured with crypto is unable to decrypt it (can be not encrypted data or encrypted with different options).

## v5.3.1
November 03 2023

Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
pubnub (5.3.1)
pubnub (5.3.2)
addressable (>= 2.0.0)
concurrent-ruby (~> 1.1.5)
concurrent-ruby-edge (~> 0.5.0)
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.3.1
5.3.2
81 changes: 81 additions & 0 deletions fixtures/vcr_cassettes/examples/history/crypto_1.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions fixtures/vcr_cassettes/examples/history/crypto_2.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions fixtures/vcr_cassettes/examples/history/crypto_3.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 6 additions & 10 deletions lib/pubnub/events/history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,26 @@ def parameters(*_args)

def decrypt_history(message, crypto)
if @include_token || @include_meta
encrypted_message = Base64.decode64(message['message'])
encrypted_message = Base64.strict_decode64(message['message'])
message['message'] = JSON.parse(crypto.decrypt(encrypted_message), quirks_mode: true)

message
else
encrypted_message = Base64.decode64(message)
encrypted_message = Base64.strict_decode64(message)
JSON.parse(crypto.decrypt(encrypted_message), quirks_mode: true)
end
rescue StandardError => e
puts "Pubnub :: DECRYPTION ERROR: #{e}"
message['decrypt_error'] = true if @include_token || @include_meta
message
end

def valid_envelope(parsed_response, req_res_objects)
messages = parsed_response[0]

# TODO: Uncomment code below when cryptor implementations will be added.
if crypto_module && messages
crypto = crypto_module
messages = messages.map { |message| decrypt_history(message, crypto) }
end
# if (@cipher_key || @app.env[:cipher_key] || @cipher_key_selector || @app.env[:cipher_key_selector]) && messages
# cipher_key = compute_cipher_key(parsed_response)
# random_iv = compute_random_iv(parsed_response)
# crypto = Crypto.new(cipher_key, random_iv)
# messages = messages.map { |message| decrypt_history(message, crypto) }
# end

start = parsed_response[1]
finish = parsed_response[2]
Expand Down
1 change: 0 additions & 1 deletion lib/pubnub/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def format_uuid(uuids, should_encode = true)
end
end

# TODO: Uncomment code below when cryptor implementations will be added.
# Transforms message to json and encode it.
#
# @param message [Hash, String, Integer, Boolean] Message data which
Expand Down
3 changes: 1 addition & 2 deletions lib/pubnub/subscribe_event/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ def build_error_envelopes(_parsed_response, error, req_res_objects)
end

def decipher_payload(message)
# TODO: Uncomment code below when cryptor implementations will be added.
return message[:payload] if message[:channel].end_with?('-pnpres') || crypto_module.nil?

encrypted_message = Base64.decode64(message[:payload])
encrypted_message = Base64.strict_decode64(message[:payload])
JSON.parse(crypto_module.decrypt(encrypted_message), quirks_mode: true)
rescue StandardError, UnknownCryptorError
message[:payload]
Expand Down
2 changes: 1 addition & 1 deletion lib/pubnub/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Toplevel Pubnub module.
module Pubnub
VERSION = '5.3.1'.freeze
VERSION = '5.3.2'.freeze
end
Loading

0 comments on commit 0acb1d8

Please sign in to comment.