@@ -153,6 +153,41 @@ static bool checkApiWriteAccess(bool is_read_only, Json::Value& jResponse)
153
153
return !is_read_only;
154
154
}
155
155
156
+ static bool parseRequestId (Json::Value& jRequest, Json::Value& jResponse)
157
+ {
158
+ const char *membername = " id" ;
159
+
160
+ // NOTE: all errors have the same code (-32600) indicating this is an invalid request
161
+
162
+ // be sure id is there and it's not empty, otherwise raise an error
163
+ if (!jRequest.isMember (membername) || jRequest[membername].empty ())
164
+ {
165
+ jResponse[membername] = Json::nullValue;
166
+ jResponse[" error" ][" code" ] = -32600 ;
167
+ jResponse[" error" ][" message" ] = " Invalid Request (missing or empty id)" ;
168
+ return false ;
169
+ }
170
+
171
+ // try to parse id as Uint
172
+ if (jRequest[membername].isUInt ())
173
+ {
174
+ jResponse[membername] = jRequest[membername].asUInt ();
175
+ return true ;
176
+ }
177
+
178
+ // try to parse id as String
179
+ if (jRequest[membername].isString ())
180
+ {
181
+ jResponse[membername] = jRequest[membername].asString ();
182
+ return true ;
183
+ }
184
+
185
+ // id has invalid type
186
+ jResponse[membername] = Json::nullValue;
187
+ jResponse[" error" ][" code" ] = -32600 ;
188
+ jResponse[" error" ][" message" ] = " Invalid Request (id has invalid type)" ;
189
+ return false ;
190
+ }
156
191
157
192
ApiServer::ApiServer (
158
193
boost::asio::io_service& io_service, int portnum, bool readonly, string password, Farm& f, PoolManager& mgr)
@@ -288,15 +323,8 @@ void ApiConnection::processRequest(Json::Value& jRequest, Json::Value& jResponse
288
323
jResponse[" jsonrpc" ] = " 2.0" ;
289
324
290
325
// Strict sanity checks over jsonrpc v2
291
- unsigned id;
292
- if (!getRequestValue (" id" , id, jRequest, false , jResponse))
293
- {
294
- jResponse[" id" ] = Json::nullValue;
295
- jResponse[" error" ][" code" ] = -32600 ;
296
- jResponse[" error" ][" message" ] = " Invalid Request" ;
297
- return ;
298
- }
299
- jResponse[" id" ] = id;
326
+ if (!parseRequestId (jRequest, jResponse))
327
+ return ;
300
328
301
329
std::string jsonrpc;
302
330
std::string _method;
0 commit comments