Skip to content

Commit

Permalink
Fix/handle unencrypted message (#94)
Browse files Browse the repository at this point in the history
* Handle exception of decrypting unencrypted message

Co-authored-by: Mateusz Wiktor <[email protected]>

---------

Co-authored-by: PubNub Release Bot <[email protected]>
Co-authored-by: Mateusz Wiktor <[email protected]>
  • Loading branch information
3 people authored Nov 27, 2023
1 parent 00bb3b2 commit c2a2446
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 21 deletions.
11 changes: 8 additions & 3 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
name: php
version: 6.1.2
version: 6.1.3
schema: 1
scm: github.com/pubnub/php
changelog:
- date: 2023-11-27
version: v6.1.3
changes:
- type: bug
text: "Gracefully handle decrypting an unencrypted method. If a decryption error occurs when trying to decrypt plain text, the plain text message will be returned and an error field will be set in the response. This works for both history and subscription messages."
- date: 2023-11-02
version: v6.1.2
changes:
Expand Down Expand Up @@ -402,8 +407,8 @@ sdks:
- x86-64
- distribution-type: library
distribution-repository: GitHub release
package-name: php-6.1.2.zip
location: https://github.com/pubnub/php/releases/tag/v6.1.2
package-name: php-6.1.3.zip
location: https://github.com/pubnub/php/releases/tag/v6.1.3
requires:
- name: rmccue/requests
min-version: 1.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 @@
## v6.1.3
November 27 2023

#### Fixed
- Gracefully handle decrypting an unencrypted method. If a decryption error occurs when trying to decrypt plain text, the plain text message will be returned and an error field will be set in the response. This works for both history and subscription messages.

## v6.1.2
November 02 2023

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ You will need the publish and subscribe keys to authenticate your app. Get your
{
"require": {
<!-- include the latest version from the badge at the top -->
"pubnub/pubnub": "6.1.2"
"pubnub/pubnub": "6.1.3"
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"keywords": ["api", "real-time", "realtime", "real time", "ajax", "push"],
"homepage": "http://www.pubnub.com/",
"license": "proprietary",
"version": "6.1.2",
"version": "6.1.3",
"authors": [
{
"name": "PubNub",
Expand Down
38 changes: 38 additions & 0 deletions examples/cli/hist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

set_time_limit(0);

require('../../vendor/autoload.php');

use PubNub\PubNub;
use PubNub\PNConfiguration;
use PubNub\CryptoModule;

$pnUuid = 'pn-610da4553bb079.92567429'; //uniqid('pn-', true);

$pnConfig = new PNConfiguration();
$pnConfig->setPublishKey(getenv('PN_KEY_PUBLISH'));
$pnConfig->setSubscribeKey(getenv('PN_KEY_SUBSCRIBE'));
// $pnConfig->setSecretKey('sec-c-YjZiZWQzZWItMmZlNS00NjBlLTkyNTUtOGFhZjZiY2E1ZDc1');
$pnConfig->setUuid($pnUuid);
if (array_key_exists(2, $argv)) {
$pnConfig->setCrypto(CryptoModule::aesCbcCryptor($argv[2], true));
}

$pubnub = new PubNub($pnConfig);

$ts = $pubnub->time()->sync();

$channelName = $argv[1];
$history = $pubnub->history()
->channel($channelName)
->reverse(false)
->end((int)($ts->getTimetoken() - 3000000000))
->count(50)
->sync();

foreach ($history->getMessages() as $key => $message) {
print($message);
}
26 changes: 26 additions & 0 deletions examples/cli/pub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

set_time_limit(0);

require('../../vendor/autoload.php');


use PubNub\PubNub;
use PubNub\PNConfiguration;
use PubNub\CryptoModule;

$pnUuid = $argv[1] . '-pn-610da4553bb079.92567429';

$pnConfig = new PNConfiguration();
$pnConfig->setPublishKey(getenv('PN_KEY_PUBLISH'));
$pnConfig->setSubscribeKey(getenv('PN_KEY_SUBSCRIBE'));
$pnConfig->setUuid($pnUuid);
if (array_key_exists(4, $argv)) {
$pnConfig->setCrypto(CryptoModule::aesCbcCryptor($argv[4], true));
}

$pubnub = new PubNub($pnConfig);

$pubResult = $pubnub->publish()->channel($argv[2])->message($argv[3])->sync();
104 changes: 104 additions & 0 deletions examples/cli/sub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

declare(strict_types=1);

error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED);

set_time_limit(0);

require('../../vendor/autoload.php');

use PubNub\Callbacks\SubscribeCallback;
use PubNub\Exceptions\PubNubException;
use PubNub\Models\Consumer\PubSub\PNMessageResult;
use PubNub\Models\Consumer\PubSub\PNPresenceEventResult;
use PubNub\Models\ResponseHelpers\PNStatus;
use PubNub\PubNub;
use PubNub\PNConfiguration;
use PubNub\CryptoModule;


$pnUuid = 'pn-610da4553bb079.92567429';

$pnConfig = new PNConfiguration();
$pnConfig->setPublishKey(getenv('PN_KEY_PUBLISH'));
$pnConfig->setSubscribeKey(getenv('PN_KEY_SUBSCRIBE'));
if (array_key_exists(2, $argv)) {
$pnConfig->setCrypto(CryptoModule::aesCbcCryptor($argv[2], true));
}
$pnConfig->setUuid($pnUuid);

$pubnub = new PubNub($pnConfig);

$channelName = $argv[1];

// phpcs:ignore
class MySubscribeCallback extends SubscribeCallback
{
/**
* @param PubNub $pubnub
* @param PNStatus $status
* @return void
* @throws PubNubException
*/
public function status($pubnub, $status)
{
printf(
"Category: %s\nPublisher user_id: %s",
$status->getCategory(),
$status->getUuid()
);
}

/**
* @param PubNub $pubnub
* @param PNMessageResult $messageResult
* @return void
*/
public function message($pubnub, $messageResult)
{
printf(
"\nMessage %s\n Channel: %s\n Timetoken: %s\n Publisher: %s\n",
$messageResult->getMessage(),
$messageResult->getChannel(),
$messageResult->getTimetoken(),
$messageResult->getPublisher(),
);
if ($messageResult->isError()) {
printf('\nError occured during parsing the message: %s', $messageResult->getError()->getMessage());
}
}

/**
* @param PubNub $pubnub
* @param PNPresenceEventResult $presence
* @return void
*/
public function presence($pubnub, $presence)
{
print("{$presence->getEvent()}: {$presence->getUuid()}");
}

/**
* @param PubNub $pubnub
* @param PNSignalMessageResult $signal
* @return void
*/
public function signal($pubnub, $signal)
{
}
}

$subscribeCallback = new MySubscribeCallback();

$pubnub->addListener($subscribeCallback);

echo "subscribing to: $channelName\n";

$subResult = $pubnub->subscribe()
->channels($channelName)
->withTimetoken(true)
->withPresence(true)
->execute();

echo "done.\n";
16 changes: 9 additions & 7 deletions src/PubNub/Managers/SubscriptionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PubNub\Managers;


use PubNub\Exceptions\PubNubResponseParsingException;
use PubNub\Models\Consumer\PubSub\PNPresenceEventResult;
use PubNub\Builders\DTO\SubscribeOperation;
Expand All @@ -24,7 +23,6 @@
use PubNub\PubNub;
use PubNub\PubNubUtil;


class SubscriptionManager
{
/** @var PubNub */
Expand Down Expand Up @@ -251,17 +249,21 @@ protected function processIncomingPayload($message)
);

$this->listenerManager->announcePresence($pnPresenceResult);

} else {
$extractedMessage = $this->processMessage($message->getPayload());
$messageError = null;
try {
$extractedMessage = $this->processMessage($message->getPayload());
} catch (PubNubResponseParsingException $exception) {
$extractedMessage = $message->getPayload();
$messageError = $exception;
}
$publisher = $message->getIssuingClientId();

if ($extractedMessage === null) {
$this->pubnub->getLogger()->debug("unable to parse payload on #processIncomingMessages");
}

if (MessageType::SIGNAL == $message->getMessageType()) {

$pnSignalResult = new PNSignalMessageResult(
$extractedMessage,
$channel,
Expand All @@ -272,13 +274,13 @@ protected function processIncomingPayload($message)

$this->listenerManager->announceSignal($pnSignalResult);
} else {

$pnMessageResult = new PNMessageResult(
$extractedMessage,
$channel,
$subscriptionMatch,
$publishMetadata->getPublishTimetoken(),
$publisher
$publisher,
$messageError
);

$this->listenerManager->announceMessage($pnMessageResult);
Expand Down
17 changes: 11 additions & 6 deletions src/PubNub/Models/Consumer/History/PNHistoryItemResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PubNub\PubNubCryptoCore;


class PNHistoryItemResult
{
/** @var any */
Expand All @@ -16,6 +15,8 @@ class PNHistoryItemResult
/** @var int */
private $timetoken;

private $error = null;

/**
* PNHistoryItemResult constructor.
* @param string $entry
Expand All @@ -36,10 +37,14 @@ public function __toString()

public function decrypt()
{
if (is_string($this->entry)) {
$this->entry = $this->crypto->decrypt($this->entry);
} elseif (is_array($this->entry) and key_exists('pn_other', $this->entry)) {
$this->entry['pn_other'] = $this->crypto->decrypt($this->entry['pn_other']);
try {
if (is_string($this->entry)) {
$this->entry = $this->crypto->decrypt($this->entry);
} elseif (is_array($this->entry) and key_exists('pn_other', $this->entry)) {
$this->entry['pn_other'] = $this->crypto->decrypt($this->entry['pn_other']);
}
} catch (\Exception $error) {
$this->error = $error;
}
}

Expand All @@ -58,4 +63,4 @@ public function getTimetoken()
{
return $this->timetoken;
}
}
}
16 changes: 14 additions & 2 deletions src/PubNub/Models/Consumer/PubSub/PNMessageResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PubNub\Models\Consumer\PubSub;


class PNMessageResult
{
/** @var array */
Expand All @@ -20,6 +19,8 @@ class PNMessageResult
/** @var string */
private $publisher;

private $error;

/**
* PNMessageResult constructor.
* @param array $message
Expand All @@ -28,13 +29,14 @@ class PNMessageResult
* @param int $timetoken
* @param string $publisher
*/
public function __construct($message, $channel, $subscription, $timetoken, $publisher)
public function __construct($message, $channel, $subscription, $timetoken, $publisher, $error = null)
{
$this->message = $message;
$this->channel = $channel;
$this->subscription = $subscription;
$this->timetoken = $timetoken;
$this->publisher = $publisher;
$this->error = $error;
}

/**
Expand Down Expand Up @@ -76,4 +78,14 @@ public function getPublisher()
{
return $this->publisher;
}

public function isError(): bool
{
return !is_null($this->error);
}

public function getError()
{
return $this->error;
}
}
2 changes: 1 addition & 1 deletion src/PubNub/PubNub.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

class PubNub implements LoggerAwareInterface
{
protected const SDK_VERSION = "6.1.2";
protected const SDK_VERSION = "6.1.3";
protected const SDK_NAME = "PubNub-PHP";

public static $MAX_SEQUENCE = 65535;
Expand Down

0 comments on commit c2a2446

Please sign in to comment.