From c3b1ee799af65e7c2d128d9ae2e925009ebb1460 Mon Sep 17 00:00:00 2001 From: netkas Date: Wed, 12 Mar 2025 15:10:07 -0400 Subject: [PATCH] Refactor SettingsSignatureExists and SigningKeysManager to improve UUID validation and update parameter naming for clarity https://github.com/nosial/Socialbox-PHP/issues/67 --- .../Settings/SettingsSignatureExists.php | 22 +++++-------------- src/Socialbox/Managers/SigningKeysManager.php | 17 +++++++++++--- src/Socialbox/SocialClient.php | 6 ++--- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/Socialbox/Classes/StandardMethods/Settings/SettingsSignatureExists.php b/src/Socialbox/Classes/StandardMethods/Settings/SettingsSignatureExists.php index 86a7c9d..5c54f84 100644 --- a/src/Socialbox/Classes/StandardMethods/Settings/SettingsSignatureExists.php +++ b/src/Socialbox/Classes/StandardMethods/Settings/SettingsSignatureExists.php @@ -2,13 +2,9 @@ namespace Socialbox\Classes\StandardMethods\Settings; - use Exception; - use InvalidArgumentException; - use ncc\ThirdParty\Symfony\Uid\Uuid; use Socialbox\Abstracts\Method; use Socialbox\Enums\StandardError; use Socialbox\Exceptions\DatabaseOperationException; - use Socialbox\Exceptions\Standard\InvalidRpcArgumentException; use Socialbox\Exceptions\Standard\MissingRpcArgumentException; use Socialbox\Exceptions\Standard\StandardRpcException; use Socialbox\Interfaces\SerializableInterface; @@ -23,28 +19,20 @@ */ public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface { - if(!$rpcRequest->containsParameter('uuid')) + if(!$rpcRequest->containsParameter('signature_uuid')) { - throw new MissingRpcArgumentException('uuid'); + throw new MissingRpcArgumentException('signature_uuid'); } + $signatureUuid = (string)$rpcRequest->getParameter('signature_uuid'); + try { - $uuid = Uuid::fromString($rpcRequest->getParameter('uuid')); - } - catch(InvalidArgumentException) - { - throw new InvalidRpcArgumentException('uuid'); - } - - try - { - return $rpcRequest->produceResponse(SigningKeysManager::signingKeyExists($request->getPeer()->getUuid(), $uuid)); + return $rpcRequest->produceResponse(SigningKeysManager::signingKeyExists($request->getPeer()->getUuid(), $signatureUuid)); } catch(DatabaseOperationException $e) { throw new StandardRpcException('Failed to check the signing key existence', StandardError::INTERNAL_SERVER_ERROR, $e); } - } } \ No newline at end of file diff --git a/src/Socialbox/Managers/SigningKeysManager.php b/src/Socialbox/Managers/SigningKeysManager.php index 3dde942..15cf4bc 100644 --- a/src/Socialbox/Managers/SigningKeysManager.php +++ b/src/Socialbox/Managers/SigningKeysManager.php @@ -8,6 +8,7 @@ use PDOException; use Socialbox\Classes\Cryptography; use Socialbox\Classes\Database; + use Socialbox\Classes\Validator; use Socialbox\Enums\SigningKeyState; use Socialbox\Exceptions\CryptographyException; use Socialbox\Exceptions\DatabaseOperationException; @@ -199,16 +200,26 @@ * Checks if a signing key exists in the database using the provided UUID. * * @param string $peerUuid The UUID of the peer associated with the signing key. - * @param string $uuid The UUID of the signing key to check. + * @param string $signatureUuid The UUID of the signing key to check. * @return bool True if the signing key exists, false otherwise. * @throws DatabaseOperationException If a database error occurs during the operation. */ - public static function signingKeyExists(string $peerUuid, string $uuid): bool + public static function signingKeyExists(string $peerUuid, string $signatureUuid): bool { + if(!Validator::validateUuid($peerUuid)) + { + throw new InvalidArgumentException('The given internal peer UUID is not a valid UUID V4'); + } + + if(!Validator::validateUuid($signatureUuid)) + { + throw new InvalidArgumentException('The given signature UUID is not a valid UUID V4'); + } + try { $statement = Database::getConnection()->prepare("SELECT COUNT(*) FROM signing_keys WHERE uuid=:uuid AND peer_uuid=:peer_uuid"); - $statement->bindParam(':uuid', $uuid); + $statement->bindParam(':uuid', $signatureUuid); $statement->bindParam(':peer_uuid', $peerUuid); $statement->execute(); diff --git a/src/Socialbox/SocialClient.php b/src/Socialbox/SocialClient.php index 2f3971f..c4b2ba0 100644 --- a/src/Socialbox/SocialClient.php +++ b/src/Socialbox/SocialClient.php @@ -1168,15 +1168,15 @@ /** * Checks if a signature exists in the peer's profile, returns True if the signature exists. * - * @param string $uuid The UUID of the signature to check for it's existence + * @param string $signatureUuid The UUID of the signature to check for it's existence * @return bool Returns True if the signature exists, False otherwise * @throws RpcException Thrown if there was an error with the RPC request */ - public function settingsSignatureExists(string $uuid): bool + public function settingsSignatureExists(string $signatureUuid): bool { return $this->sendRequest( new RpcRequest(StandardMethods::SETTINGS_SIGNATURE_EXISTS, parameters: [ - 'uuid' => $uuid + 'signature_uuid' => $signatureUuid ]) )->getResponse()->getResult(); }