From b7818c793572323a1ad90a6c6f4ebdb3b445f1c3 Mon Sep 17 00:00:00 2001 From: mcfnord Date: Sat, 1 Aug 2026 01:13:47 +0000 Subject: [PATCH] Reject unrecognised directoryType in jamulusserver/setDirectory DeserializeDirectoryType() returned AT_DEFAULT on a lookup miss, so any unrecognised directoryType replied "ok" while registering the server with the public any_genre_1 directory, and the existing "custom needs an address" guard could not fire for "CUSTOM". The lookup is now a bool with an out-parameter; an unrecognised value returns -32602 and leaves the directory setting unchanged. Doc comment and generated docs/JSON-RPC.md updated. Fixes #3915 Co-Authored-By: Claude Fable 5.1 --- docs/JSON-RPC.md | 4 ++-- src/serverrpc.cpp | 24 +++++++++++++++++------- src/serverrpc.h | 6 +++--- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/docs/JSON-RPC.md b/docs/JSON-RPC.md index 3501fbdc9d..2922037b40 100644 --- a/docs/JSON-RPC.md +++ b/docs/JSON-RPC.md @@ -507,14 +507,14 @@ Parameters: | Name | Type | Description | | --- | --- | --- | -| params.directoryType | string | The directory type as a string (see EDirectoryType and DeserializeDirectoryType). | +| params.directoryType | string | The directory type as a string. One of: none, any_genre_1, any_genre_2, any_genre_asia, genre_rock, genre_jazz, genre_classical_folk, genre_choral_barbershop, custom. The value is matched exactly: it is case-sensitive and is not trimmed. An unrecognised value is rejected. | | [params.directoryAddress] | string | (optional) The directory address, required if `directoryType` is "custom". | Results: | Name | Type | Description | | --- | --- | --- | -| result | string | Always "ok". | +| result | string | "ok" on success. An unrecognised `directoryType` returns error -32602 and leaves the directory setting unchanged. | ### jamulusserver/setRecordingDirectory diff --git a/src/serverrpc.cpp b/src/serverrpc.cpp index a35aaad17f..16dc447e01 100644 --- a/src/serverrpc.cpp +++ b/src/serverrpc.cpp @@ -251,9 +251,12 @@ CServerRpc::CServerRpc ( CServer* pServer, CRpcServer* pRpcServer, QObject* pare /// @rpc_method jamulusserver/setDirectory /// @brief Set the directory type and, for custom, the directory address. - /// @param {string} params.directoryType - The directory type as a string (see EDirectoryType and DeserializeDirectoryType). + /// @param {string} params.directoryType - The directory type as a string. One of: none, any_genre_1, any_genre_2, + /// any_genre_asia, genre_rock, genre_jazz, genre_classical_folk, genre_choral_barbershop, custom. + /// The value is matched exactly: it is case-sensitive and is not trimmed. An unrecognised value is rejected. /// @param {string} [params.directoryAddress] - (optional) The directory address, required if `directoryType` is "custom". - /// @result {string} result - Always "ok". + /// @result {string} result - "ok" on success. An unrecognised `directoryType` returns error -32602 and leaves the + /// directory setting unchanged. pRpcServer->HandleMethod ( "jamulusserver/setDirectory", [=] ( const QJsonObject& params, QJsonObject& response ) { auto jsonDirectoryType = params["directoryType"]; auto directoryAddress = params["directoryAddress"]; @@ -264,9 +267,15 @@ CServerRpc::CServerRpc ( CServer* pServer, CRpcServer* pRpcServer, QObject* pare response["error"] = CRpcServer::CreateJsonRpcError ( CRpcServer::iErrInvalidParams, "Invalid params: directory type is not a string" ); return; } - else + + const QString strDirectoryType = jsonDirectoryType.toString(); + + if ( !DeserializeDirectoryType ( strDirectoryType.toStdString(), directoryType ) ) { - directoryType = DeserializeDirectoryType ( jsonDirectoryType.toString().toStdString() ); + response["error"] = + CRpcServer::CreateJsonRpcError ( CRpcServer::iErrInvalidParams, + QString ( "Invalid params: unrecognised directoryType \"%1\"" ).arg ( strDirectoryType ) ); + return; } if ( !directoryAddress.isUndefined() ) @@ -424,13 +433,14 @@ const std::unordered_map CServerRpc::sumStringToDir { "custom", EDirectoryType::AT_CUSTOM }, }; -inline EDirectoryType CServerRpc::DeserializeDirectoryType ( std::string sAddrType ) +inline bool CServerRpc::DeserializeDirectoryType ( const std::string& sAddrType, EDirectoryType& eAddrType ) { auto found = sumStringToDirectoryType.find ( sAddrType ); if ( found == sumStringToDirectoryType.end() ) - return AT_DEFAULT; + return false; - return found->second; + eAddrType = found->second; + return true; } #if defined( Q_OS_MACOS ) && QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 ) diff --git a/src/serverrpc.h b/src/serverrpc.h index 4de40d1b7d..a0d0690fe3 100644 --- a/src/serverrpc.h +++ b/src/serverrpc.h @@ -74,7 +74,7 @@ class CServerRpc : public QObject const static std::unordered_map sumSvrRegStatusToString; #endif - QJsonValue SerializeDirectoryType ( EDirectoryType eAddrType ); - EDirectoryType DeserializeDirectoryType ( std::string sAddrType ); - QJsonValue SerializeRegistrationStatus ( ESvrRegStatus eSvrRegStatus ); + QJsonValue SerializeDirectoryType ( EDirectoryType eAddrType ); + bool DeserializeDirectoryType ( const std::string& sAddrType, EDirectoryType& eAddrType ); + QJsonValue SerializeRegistrationStatus ( ESvrRegStatus eSvrRegStatus ); };