diff options
author | Sadie Powell <sadie@witchery.services> | 2025-02-25 23:13:54 +0000 |
---|---|---|
committer | Sadie Powell <sadie@witchery.services> | 2025-02-25 23:13:54 +0000 |
commit | 752f5e269e99acdd0c2795a2c2eedc44875d5fed (patch) | |
tree | b4b4ef22c9b5d8407612887b89051f9270f7c780 | |
parent | bb3e124bdb07cc12d51a0483b6cf913e4af58b8a (diff) |
Deduplicate RPC parameter count checks.
-rw-r--r-- | include/modules/rpc.h | 6 | ||||
-rw-r--r-- | modules/extra/xmlrpc.cpp | 12 | ||||
-rw-r--r-- | modules/rpc/jsonrpc.cpp | 11 | ||||
-rw-r--r-- | modules/rpc/rpc_data.cpp | 30 |
4 files changed, 29 insertions, 30 deletions
diff --git a/include/modules/rpc.h b/include/modules/rpc.h index 640eebda1..cd0578729 100644 --- a/include/modules/rpc.h +++ b/include/modules/rpc.h @@ -173,10 +173,12 @@ class RPC::Event { private: Anope::string event; + size_t minparams; protected: - Event(const Anope::string& e) + Event(const Anope::string& e, size_t mp = 0) : event(e) + , minparams(mp) { } @@ -185,6 +187,8 @@ public: const auto &GetEvent() const { return event; } + const auto &GetMinParams() const { return minparams; } + virtual bool Run(ServiceInterface *iface, HTTPClient *client, Request &request) = 0; }; diff --git a/modules/extra/xmlrpc.cpp b/modules/extra/xmlrpc.cpp index 94beb2088..00b7d92ac 100644 --- a/modules/extra/xmlrpc.cpp +++ b/modules/extra/xmlrpc.cpp @@ -145,7 +145,17 @@ public: } xmlrpc_DECREF(params); - if (!event->second->Run(this, client, request)) + auto *eh = event->second; + if (request.data.size() < eh->GetMinParams()) + { + auto error = Anope::printf("Not enough parameters (given %zu, expected %zu)", + request.data.size(), eh->GetMinParams()); + xmlrpc_env_set_fault(&env, RPC::ERR_INVALID_PARAMS, error.c_str()); + SendError(reply, env); + return true; + } + + if (!eh->Run(this, client, request)) return false; this->Reply(request); diff --git a/modules/rpc/jsonrpc.cpp b/modules/rpc/jsonrpc.cpp index fc4c40e83..1e7315984 100644 --- a/modules/rpc/jsonrpc.cpp +++ b/modules/rpc/jsonrpc.cpp @@ -150,7 +150,16 @@ public: return true; } - if (!event->second->Run(this, client, request)) + auto *eh = event->second; + if (request.data.size() < eh->GetMinParams()) + { + auto error = Anope::printf("Not enough parameters (given %zu, expected %zu)", + request.data.size(), eh->GetMinParams()); + SendError(reply, RPC::ERR_INVALID_PARAMS, error, id); + return true; + } + + if (!eh->Run(this, client, request)) return false; this->Reply(request); diff --git a/modules/rpc/rpc_data.cpp b/modules/rpc/rpc_data.cpp index 670b4c57e..059468fa0 100644 --- a/modules/rpc/rpc_data.cpp +++ b/modules/rpc/rpc_data.cpp @@ -38,18 +38,12 @@ class AnopeChannelRPCEvent final { public: AnopeChannelRPCEvent() - : RPC::Event("anope.channel") + : RPC::Event("anope.channel", 1) { } bool Run(RPC::ServiceInterface *iface, HTTPClient *client, RPC::Request &request) override { - if (request.data.empty()) - { - request.Error(RPC::ERR_INVALID_PARAMS, "Not enough parameters"); - return true; - } - auto *c = Channel::Find(request.data[0]); if (!c) { @@ -137,18 +131,12 @@ class AnopeOperRPCEvent final { public: AnopeOperRPCEvent() - : RPC::Event("anope.oper") + : RPC::Event("anope.oper", 1) { } bool Run(RPC::ServiceInterface *iface, HTTPClient *client, RPC::Request &request) override { - if (request.data.empty()) - { - request.Error(RPC::ERR_INVALID_PARAMS, "Not enough parameters"); - return true; - } - auto *o = Oper::Find(request.data[0]); if (!o) { @@ -230,12 +218,6 @@ public: bool Run(RPC::ServiceInterface *iface, HTTPClient *client, RPC::Request &request) override { - if (request.data.empty()) - { - request.Error(RPC::ERR_INVALID_PARAMS, "Not enough parameters"); - return true; - } - auto *s = Server::Find(request.data[0]); if (!s) { @@ -291,18 +273,12 @@ class AnopeUserRPCEvent final { public: AnopeUserRPCEvent() - : RPC::Event("anope.user") + : RPC::Event("anope.user", 1) { } bool Run(RPC::ServiceInterface *iface, HTTPClient *client, RPC::Request &request) override { - if (request.data.empty()) - { - request.Error(RPC::ERR_INVALID_PARAMS, "Not enough parameters"); - return true; - } - auto *u = User::Find(request.data[0]); if (!u) { |