Skip to content

Commit 7a1f902

Browse files
committed
Fix http params handling discrepancy (#913)
Fixes #909
1 parent 7e621b2 commit 7a1f902

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

src/rpc/RPCEngine.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,9 @@ class RPCEngineBase
150150

151151
if (v)
152152
return v->as_object();
153-
else
154-
{
155-
notifyErrored(ctx.method);
156-
return Status{v.error()};
157-
}
153+
154+
notifyErrored(ctx.method);
155+
return Status{v.error()};
158156
}
159157
catch (data::DatabaseTimeout const& t)
160158
{

src/web/RPCServerHandler.h

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ class RPCServerHandler
8989
auto req = boost::json::parse(request).as_object();
9090
LOG(perfLog_.debug()) << connection->tag() << "Adding to work queue";
9191

92-
if (not connection->upgraded and not req.contains("params"))
93-
req["params"] = boost::json::array({boost::json::object{}});
92+
if (not connection->upgraded and shouldReplaceParams(req))
93+
req[JS(params)] = boost::json::array({boost::json::object{}});
9494

9595
if (!rpcEngine_->post(
9696
[this, request = std::move(req), connection](boost::asio::yield_context yield) mutable {
@@ -267,6 +267,27 @@ class RPCServerHandler
267267
return web::detail::ErrorHelper(connection, request).sendInternalError();
268268
}
269269
}
270+
271+
bool
272+
shouldReplaceParams(boost::json::object const& req) const
273+
{
274+
auto const hasParams = req.contains(JS(params));
275+
auto const paramsIsArray = hasParams and req.at(JS(params)).is_array();
276+
auto const paramsIsEmptyString =
277+
hasParams and req.at(JS(params)).is_string() and req.at(JS(params)).as_string().empty();
278+
auto const paramsIsEmptyObject =
279+
hasParams and req.at(JS(params)).is_object() and req.at(JS(params)).as_object().empty();
280+
auto const paramsIsNull = hasParams and req.at(JS(params)).is_null();
281+
auto const arrayIsEmpty = paramsIsArray and req.at(JS(params)).as_array().empty();
282+
auto const arrayIsNotEmpty = paramsIsArray and not req.at(JS(params)).as_array().empty();
283+
auto const firstArgIsNull = arrayIsNotEmpty and req.at(JS(params)).as_array().at(0).is_null();
284+
auto const firstArgIsEmptyString = arrayIsNotEmpty and req.at(JS(params)).as_array().at(0).is_string() and
285+
req.at(JS(params)).as_array().at(0).as_string().empty();
286+
287+
// Note: all this compatibility dance is to match `rippled` as close as possible
288+
return not hasParams or paramsIsEmptyString or paramsIsNull or paramsIsEmptyObject or arrayIsEmpty or
289+
firstArgIsEmptyString or firstArgIsNull;
290+
}
270291
};
271292

272293
} // namespace web

src/web/impl/HttpBase.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,10 @@ class HttpBase : public ConnectionBase
180180

181181
if (boost::beast::websocket::is_upgrade(req_))
182182
{
183-
upgraded = true;
184-
// Disable the timeout.
185-
// The websocket::stream uses its own timeout settings.
183+
// Disable the timeout. The websocket::stream uses its own timeout settings.
186184
boost::beast::get_lowest_layer(derived().stream()).expires_never();
185+
186+
upgraded = true;
187187
return derived().upgrade();
188188
}
189189

unittests/web/RPCServerHandlerTests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ TEST_F(WebRPCServerHandlerTest, HTTPParamsUnparseableNotArray)
549549
EXPECT_EQ(session->lastStatus, boost::beast::http::status::bad_request);
550550
}
551551

552-
TEST_F(WebRPCServerHandlerTest, HTTPParamsUnparseableEmptyArray)
552+
TEST_F(WebRPCServerHandlerTest, HTTPParamsUnparseableArrayWithDigit)
553553
{
554554
static auto constexpr response = "params unparseable";
555555

@@ -558,7 +558,7 @@ TEST_F(WebRPCServerHandlerTest, HTTPParamsUnparseableEmptyArray)
558558

559559
static auto constexpr requestJSON = R"({
560560
"method": "ledger",
561-
"params": []
561+
"params": [1]
562562
})";
563563

564564
EXPECT_CALL(*rpcEngine, notifyBadSyntax).Times(1);

0 commit comments

Comments
 (0)