From baf6820f581c87cf7526297de14b48b610d108cf Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Tue, 24 Sep 2024 22:31:25 +0530 Subject: [PATCH] Added JSON error check while decoding socketId as per review comment --- src/Utils.php | 14 ++++++++++---- tests/UtilsTest.php | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/Utils.php b/src/Utils.php index 1a506d6..8c4d607 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -79,14 +79,20 @@ public static function base64url_decode($data) { ."'connectionKey' and 'clientId' as keys. 'clientId' is null if connection is not identified"; /** + * @return object * @throws AblyException */ - public static function decodeSocketId($socketId) { + public static function decodeSocketId($socketId): ?object + { $socketIdObject = null; if ($socketId) { - $socketIdObject = json_decode(self::base64url_decode($socketId)); - if (!$socketIdObject) { - throw new AblyException("SocketId decoding failed, ".self::SOCKET_ID_ERROR); + $socketIdJsonString = self::base64url_decode($socketId); + if (!$socketIdJsonString) { + throw new AblyException("Base64 decoding failed, ".self::SOCKET_ID_ERROR); + } + $socketIdObject = json_decode($socketIdJsonString); + if (json_last_error() !== JSON_ERROR_NONE) { + throw new AblyException("JSON decoding failed: " . json_last_error_msg() . ", " . self::SOCKET_ID_ERROR); } if (!isset($socketIdObject->connectionKey)) { throw new AblyException("ConnectionKey is not set, ".self::SOCKET_ID_ERROR); diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php index 3fb3663..a4362fd 100644 --- a/tests/UtilsTest.php +++ b/tests/UtilsTest.php @@ -53,7 +53,7 @@ public function testDecodeSocketId() { public function testExceptionOnDecodingInvalidSocketId() { self::expectException(AblyException::class); - self::expectExceptionMessage("SocketId decoding failed, ".Utils::SOCKET_ID_ERROR); + self::expectExceptionMessage("Base64 decoding failed, ".Utils::SOCKET_ID_ERROR); Utils::decodeSocketId("invalid_socket_id"); }