mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-22 20:15:51 +00:00
Cleanup some Json::Value methods:
* Rename isArray to isArrayOrNull * Rename isObject to isObjectOrNull * Introduce isArray and isObject * Change as many uses of isArrayorNull to isArray as possible * Change as many uses of isObjectorNull to isObject as possible * Reject null JSON arrays for subscribe and unsubscribe
This commit is contained in:
committed by
Mike Ellery
parent
20defb4844
commit
1a245234f1
@@ -1584,7 +1584,7 @@ ApplicationImp::loadLedgerFromFile (
|
|||||||
ledger = ledger.get()["accountState"];
|
ledger = ledger.get()["accountState"];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ledger.get().isArray ())
|
if (!ledger.get().isArrayOrNull ())
|
||||||
{
|
{
|
||||||
JLOG(m_journal.fatal())
|
JLOG(m_journal.fatal())
|
||||||
<< "State nodes must be an array";
|
<< "State nodes must be an array";
|
||||||
@@ -1599,7 +1599,7 @@ ApplicationImp::loadLedgerFromFile (
|
|||||||
{
|
{
|
||||||
Json::Value& entry = ledger.get()[index];
|
Json::Value& entry = ledger.get()[index];
|
||||||
|
|
||||||
if (!entry.isObject())
|
if (!entry.isObjectOrNull())
|
||||||
{
|
{
|
||||||
JLOG(m_journal.fatal())
|
JLOG(m_journal.fatal())
|
||||||
<< "Invalid entry in ledger";
|
<< "Invalid entry in ledger";
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ accountTxPage (
|
|||||||
bool bAdmin,
|
bool bAdmin,
|
||||||
std::uint32_t page_length)
|
std::uint32_t page_length)
|
||||||
{
|
{
|
||||||
bool lookingForMarker = !token.isNull() && token.isObject();
|
bool lookingForMarker = token.isObject();
|
||||||
|
|
||||||
std::uint32_t numberOfResults;
|
std::uint32_t numberOfResults;
|
||||||
|
|
||||||
|
|||||||
@@ -196,7 +196,7 @@ namespace {
|
|||||||
template <class Object>
|
template <class Object>
|
||||||
void doCopyFrom (Object& to, Json::Value const& from)
|
void doCopyFrom (Object& to, Json::Value const& from)
|
||||||
{
|
{
|
||||||
assert (from.isObject());
|
assert (from.isObjectOrNull());
|
||||||
auto members = from.getMemberNames();
|
auto members = from.getMemberNames();
|
||||||
for (auto& m: members)
|
for (auto& m: members)
|
||||||
to[m] = from[m];
|
to[m] = from[m];
|
||||||
|
|||||||
@@ -124,7 +124,7 @@ Reader::parse ( const char* beginDoc, const char* endDoc,
|
|||||||
Token token;
|
Token token;
|
||||||
skipCommentTokens ( token );
|
skipCommentTokens ( token );
|
||||||
|
|
||||||
if ( !root.isArray () && !root.isObject () )
|
if ( !root.isNull() && !root.isArray() && !root.isObject() )
|
||||||
{
|
{
|
||||||
// Set error location to start of doc, ideally should be first token found in doc
|
// Set error location to start of doc, ideally should be first token found in doc
|
||||||
token.type_ = tokenError;
|
token.type_ = tokenError;
|
||||||
|
|||||||
@@ -779,7 +779,7 @@ Value::operator bool () const
|
|||||||
if (isString ())
|
if (isString ())
|
||||||
{
|
{
|
||||||
auto s = asCString();
|
auto s = asCString();
|
||||||
return s && strlen(s);
|
return s && s[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
return ! (isArray() || isObject()) || size ();
|
return ! (isArray() || isObject()) || size ();
|
||||||
@@ -1093,6 +1093,12 @@ Value::isString () const
|
|||||||
|
|
||||||
bool
|
bool
|
||||||
Value::isArray() const
|
Value::isArray() const
|
||||||
|
{
|
||||||
|
return type_ == arrayValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
Value::isArrayOrNull () const
|
||||||
{
|
{
|
||||||
return type_ == nullValue || type_ == arrayValue;
|
return type_ == nullValue || type_ == arrayValue;
|
||||||
}
|
}
|
||||||
@@ -1100,6 +1106,12 @@ Value::isArray () const
|
|||||||
|
|
||||||
bool
|
bool
|
||||||
Value::isObject() const
|
Value::isObject() const
|
||||||
|
{
|
||||||
|
return type_ == objectValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
Value::isObjectOrNull () const
|
||||||
{
|
{
|
||||||
return type_ == nullValue || type_ == objectValue;
|
return type_ == nullValue || type_ == objectValue;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -263,7 +263,9 @@ public:
|
|||||||
bool isNumeric () const;
|
bool isNumeric () const;
|
||||||
bool isString () const;
|
bool isString () const;
|
||||||
bool isArray() const;
|
bool isArray() const;
|
||||||
|
bool isArrayOrNull () const;
|
||||||
bool isObject() const;
|
bool isObject() const;
|
||||||
|
bool isObjectOrNull () const;
|
||||||
|
|
||||||
bool isConvertibleTo ( ValueType other ) const;
|
bool isConvertibleTo ( ValueType other ) const;
|
||||||
|
|
||||||
|
|||||||
@@ -513,7 +513,7 @@ private:
|
|||||||
|
|
||||||
if (reader.parse (jvParams[1u].asString (), jvRequest))
|
if (reader.parse (jvParams[1u].asString (), jvRequest))
|
||||||
{
|
{
|
||||||
if (!jvRequest.isObject ())
|
if (!jvRequest.isObjectOrNull ())
|
||||||
return rpcError (rpcINVALID_PARAMS);
|
return rpcError (rpcINVALID_PARAMS);
|
||||||
|
|
||||||
jvRequest[jss::method] = jvParams[0u];
|
jvRequest[jss::method] = jvParams[0u];
|
||||||
@@ -544,7 +544,8 @@ private:
|
|||||||
jv.isMember(jss::id) && jv.isMember(jss::method))
|
jv.isMember(jss::id) && jv.isMember(jss::method))
|
||||||
{
|
{
|
||||||
if (jv.isMember(jss::params) &&
|
if (jv.isMember(jss::params) &&
|
||||||
!(jv[jss::params].isArray() || jv[jss::params].isObject()))
|
!(jv[jss::params].isNull() || jv[jss::params].isArray() ||
|
||||||
|
jv[jss::params].isObject()))
|
||||||
return false;
|
return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -806,7 +806,11 @@ amountFromJson (SField const& name, Json::Value const& v)
|
|||||||
Json::Value currency;
|
Json::Value currency;
|
||||||
Json::Value issuer;
|
Json::Value issuer;
|
||||||
|
|
||||||
if (v.isObject ())
|
if (v.isNull())
|
||||||
|
{
|
||||||
|
Throw<std::runtime_error> ("XRP may not be specified with a null Json value");
|
||||||
|
}
|
||||||
|
else if (v.isObject())
|
||||||
{
|
{
|
||||||
value = v[jss::value];
|
value = v[jss::value];
|
||||||
currency = v[jss::currency];
|
currency = v[jss::currency];
|
||||||
@@ -846,7 +850,7 @@ amountFromJson (SField const& name, Json::Value const& v)
|
|||||||
|
|
||||||
if (native)
|
if (native)
|
||||||
{
|
{
|
||||||
if (v.isObject ())
|
if (v.isObjectOrNull ())
|
||||||
Throw<std::runtime_error> ("XRP may not be specified as an object");
|
Throw<std::runtime_error> ("XRP may not be specified as an object");
|
||||||
issue = xrpIssue ();
|
issue = xrpIssue ();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -495,7 +495,7 @@ static boost::optional<detail::STVar> parseLeaf (
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case STI_VECTOR256:
|
case STI_VECTOR256:
|
||||||
if (! value.isArray ())
|
if (! value.isArrayOrNull ())
|
||||||
{
|
{
|
||||||
error = array_expected (json_name, fieldName);
|
error = array_expected (json_name, fieldName);
|
||||||
return ret;
|
return ret;
|
||||||
@@ -521,7 +521,7 @@ static boost::optional<detail::STVar> parseLeaf (
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case STI_PATHSET:
|
case STI_PATHSET:
|
||||||
if (!value.isArray ())
|
if (!value.isArrayOrNull ())
|
||||||
{
|
{
|
||||||
error = array_expected (json_name, fieldName);
|
error = array_expected (json_name, fieldName);
|
||||||
return ret;
|
return ret;
|
||||||
@@ -535,7 +535,7 @@ static boost::optional<detail::STVar> parseLeaf (
|
|||||||
{
|
{
|
||||||
STPath p;
|
STPath p;
|
||||||
|
|
||||||
if (!value[i].isArray ())
|
if (!value[i].isArrayOrNull ())
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << fieldName << "[" << i << "]";
|
ss << fieldName << "[" << i << "]";
|
||||||
@@ -709,7 +709,7 @@ static boost::optional <STObject> parseObject (
|
|||||||
int depth,
|
int depth,
|
||||||
Json::Value& error)
|
Json::Value& error)
|
||||||
{
|
{
|
||||||
if (! json.isObject ())
|
if (! json.isObjectOrNull ())
|
||||||
{
|
{
|
||||||
error = not_an_object (json_name);
|
error = not_an_object (json_name);
|
||||||
return boost::none;
|
return boost::none;
|
||||||
@@ -743,7 +743,7 @@ static boost::optional <STObject> parseObject (
|
|||||||
case STI_TRANSACTION:
|
case STI_TRANSACTION:
|
||||||
case STI_LEDGERENTRY:
|
case STI_LEDGERENTRY:
|
||||||
case STI_VALIDATION:
|
case STI_VALIDATION:
|
||||||
if (! value.isObject ())
|
if (! value.isObjectOrNull ())
|
||||||
{
|
{
|
||||||
error = not_an_object (json_name, fieldName);
|
error = not_an_object (json_name, fieldName);
|
||||||
return boost::none;
|
return boost::none;
|
||||||
@@ -816,7 +816,7 @@ static boost::optional <detail::STVar> parseArray (
|
|||||||
int depth,
|
int depth,
|
||||||
Json::Value& error)
|
Json::Value& error)
|
||||||
{
|
{
|
||||||
if (! json.isArray ())
|
if (! json.isArrayOrNull ())
|
||||||
{
|
{
|
||||||
error = not_an_array (json_name);
|
error = not_an_array (json_name);
|
||||||
return boost::none;
|
return boost::none;
|
||||||
@@ -834,11 +834,12 @@ static boost::optional <detail::STVar> parseArray (
|
|||||||
|
|
||||||
for (Json::UInt i = 0; json.isValidIndex (i); ++i)
|
for (Json::UInt i = 0; json.isValidIndex (i); ++i)
|
||||||
{
|
{
|
||||||
bool const isObject (json[i].isObject());
|
bool const isObjectOrNull (json[i].isObjectOrNull());
|
||||||
bool const singleKey (isObject ? json[i].size() == 1 : true);
|
bool const singleKey (isObjectOrNull ? json[i].size() == 1 : true);
|
||||||
|
|
||||||
if (!isObject || !singleKey)
|
if (!isObjectOrNull || !singleKey)
|
||||||
{
|
{
|
||||||
|
// null values are !singleKey
|
||||||
error = singleton_expected (json_name, i);
|
error = singleton_expected (json_name, i);
|
||||||
return boost::none;
|
return boost::none;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,22 +52,21 @@ Json::Value doBookOffers (RPC::Context& context)
|
|||||||
if (!context.params.isMember (jss::taker_gets))
|
if (!context.params.isMember (jss::taker_gets))
|
||||||
return RPC::missing_field_error (jss::taker_gets);
|
return RPC::missing_field_error (jss::taker_gets);
|
||||||
|
|
||||||
if (!context.params[jss::taker_pays].isObject ())
|
Json::Value const& taker_pays = context.params[jss::taker_pays];
|
||||||
|
Json::Value const& taker_gets = context.params[jss::taker_gets];
|
||||||
|
|
||||||
|
if (!taker_pays.isObjectOrNull ())
|
||||||
return RPC::object_field_error (jss::taker_pays);
|
return RPC::object_field_error (jss::taker_pays);
|
||||||
|
|
||||||
if (!context.params[jss::taker_gets].isObject ())
|
if (!taker_gets.isObjectOrNull ())
|
||||||
return RPC::object_field_error (jss::taker_gets);
|
return RPC::object_field_error (jss::taker_gets);
|
||||||
|
|
||||||
Json::Value const& taker_pays (context.params[jss::taker_pays]);
|
|
||||||
|
|
||||||
if (!taker_pays.isMember (jss::currency))
|
if (!taker_pays.isMember (jss::currency))
|
||||||
return RPC::missing_field_error ("taker_pays.currency");
|
return RPC::missing_field_error ("taker_pays.currency");
|
||||||
|
|
||||||
if (! taker_pays [jss::currency].isString ())
|
if (! taker_pays [jss::currency].isString ())
|
||||||
return RPC::expected_field_error ("taker_pays.currency", "string");
|
return RPC::expected_field_error ("taker_pays.currency", "string");
|
||||||
|
|
||||||
Json::Value const& taker_gets = context.params[jss::taker_gets];
|
|
||||||
|
|
||||||
if (! taker_gets.isMember (jss::currency))
|
if (! taker_gets.isMember (jss::currency))
|
||||||
return RPC::missing_field_error ("taker_gets.currency");
|
return RPC::missing_field_error ("taker_gets.currency");
|
||||||
|
|
||||||
|
|||||||
@@ -116,7 +116,8 @@ Json::Value doGatewayBalances (RPC::Context& context)
|
|||||||
Json::Value const& hw = params[jss::hotwallet];
|
Json::Value const& hw = params[jss::hotwallet];
|
||||||
bool valid = true;
|
bool valid = true;
|
||||||
|
|
||||||
if (hw.isArray())
|
// null is treated as a valid 0-sized array of hotwallet
|
||||||
|
if (hw.isArrayOrNull())
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < hw.size(); ++i)
|
for (unsigned i = 0; i < hw.size(); ++i)
|
||||||
valid &= addHotWallet (hw[i]);
|
valid &= addHotWallet (hw[i]);
|
||||||
|
|||||||
@@ -64,7 +64,11 @@ Json::Value doLedgerEntry (RPC::Context& context)
|
|||||||
}
|
}
|
||||||
else if (context.params.isMember (jss::directory))
|
else if (context.params.isMember (jss::directory))
|
||||||
{
|
{
|
||||||
if (!context.params[jss::directory].isObject ())
|
if (context.params[jss::directory].isNull())
|
||||||
|
{
|
||||||
|
jvResult[jss::error] = "malformedRequest";
|
||||||
|
}
|
||||||
|
else if (!context.params[jss::directory].isObject())
|
||||||
{
|
{
|
||||||
uNodeIndex.SetHex (context.params[jss::directory].asString ());
|
uNodeIndex.SetHex (context.params[jss::directory].asString ());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -195,8 +195,8 @@ Json::Value doSubscribe (RPC::Context& context)
|
|||||||
if (!j.isObject()
|
if (!j.isObject()
|
||||||
|| !j.isMember (jss::taker_pays)
|
|| !j.isMember (jss::taker_pays)
|
||||||
|| !j.isMember (jss::taker_gets)
|
|| !j.isMember (jss::taker_gets)
|
||||||
|| !j[jss::taker_pays].isObject ()
|
|| !j[jss::taker_pays].isObjectOrNull ()
|
||||||
|| !j[jss::taker_gets].isObject ())
|
|| !j[jss::taker_gets].isObjectOrNull ())
|
||||||
return rpcError (rpcINVALID_PARAMS);
|
return rpcError (rpcINVALID_PARAMS);
|
||||||
|
|
||||||
Book book;
|
Book book;
|
||||||
|
|||||||
@@ -139,8 +139,8 @@ Json::Value doUnsubscribe (RPC::Context& context)
|
|||||||
if (! jv.isObject() ||
|
if (! jv.isObject() ||
|
||||||
! jv.isMember(jss::taker_pays) ||
|
! jv.isMember(jss::taker_pays) ||
|
||||||
! jv.isMember(jss::taker_gets) ||
|
! jv.isMember(jss::taker_gets) ||
|
||||||
! jv[jss::taker_pays].isObject() ||
|
! jv[jss::taker_pays].isObjectOrNull() ||
|
||||||
! jv[jss::taker_gets].isObject())
|
! jv[jss::taker_gets].isObjectOrNull())
|
||||||
{
|
{
|
||||||
return rpcError(rpcINVALID_PARAMS);
|
return rpcError(rpcINVALID_PARAMS);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -334,7 +334,6 @@ ServerHandlerImp::onWSMessage(
|
|||||||
auto const size = boost::asio::buffer_size(buffers);
|
auto const size = boost::asio::buffer_size(buffers);
|
||||||
if (size > RPC::Tuning::maxRequestSize ||
|
if (size > RPC::Tuning::maxRequestSize ||
|
||||||
! Json::Reader{}.parse(jv, buffers) ||
|
! Json::Reader{}.parse(jv, buffers) ||
|
||||||
! jv ||
|
|
||||||
! jv.isObject())
|
! jv.isObject())
|
||||||
{
|
{
|
||||||
Json::Value jvResult(Json::objectValue);
|
Json::Value jvResult(Json::objectValue);
|
||||||
@@ -603,7 +602,7 @@ ServerHandlerImp::processRequest (Port const& port,
|
|||||||
if (jsonRPC.isMember(jss::params) &&
|
if (jsonRPC.isMember(jss::params) &&
|
||||||
jsonRPC[jss::params].isArray() &&
|
jsonRPC[jss::params].isArray() &&
|
||||||
jsonRPC[jss::params].size() > 0 &&
|
jsonRPC[jss::params].size() > 0 &&
|
||||||
jsonRPC[jss::params][Json::UInt(0)].isObject())
|
jsonRPC[jss::params][Json::UInt(0)].isObjectOrNull())
|
||||||
{
|
{
|
||||||
role = requestRole(
|
role = requestRole(
|
||||||
required,
|
required,
|
||||||
@@ -721,7 +720,7 @@ ServerHandlerImp::processRequest (Port const& port,
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
params = std::move (params[0u]);
|
params = std::move (params[0u]);
|
||||||
if (!params.isObject())
|
if (!params.isObjectOrNull())
|
||||||
{
|
{
|
||||||
usage.charge(Resource::feeInvalidRPC);
|
usage.charge(Resource::feeInvalidRPC);
|
||||||
HTTPReply (400, "params unparseable", output, rpcJ);
|
HTTPReply (400, "params unparseable", output, rpcJ);
|
||||||
|
|||||||
@@ -213,8 +213,7 @@ struct Regression_test : public beast::unit_test::suite
|
|||||||
std::vector<boost::asio::const_buffer> buffers;
|
std::vector<boost::asio::const_buffer> buffers;
|
||||||
buffers.emplace_back(buffer(request, 1024));
|
buffers.emplace_back(buffer(request, 1024));
|
||||||
buffers.emplace_back(buffer(request.data() + 1024, request.length() - 1024));
|
buffers.emplace_back(buffer(request.data() + 1024, request.length() - 1024));
|
||||||
BEAST_EXPECT(jrReader.parse(jvRequest, buffers) &&
|
BEAST_EXPECT(jrReader.parse(jvRequest, buffers) && jvRequest.isObject());
|
||||||
jvRequest && jvRequest.isObject());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void run() override
|
void run() override
|
||||||
|
|||||||
@@ -38,9 +38,7 @@ public:
|
|||||||
bool parseJSONString (std::string const& json, Json::Value& to)
|
bool parseJSONString (std::string const& json, Json::Value& to)
|
||||||
{
|
{
|
||||||
Json::Reader reader;
|
Json::Reader reader;
|
||||||
return reader.parse(json, to) &&
|
return reader.parse(json, to) && to.isObject();
|
||||||
bool (to) &&
|
|
||||||
to.isObject();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void testParseJSONArrayWithInvalidChildrenObjects ()
|
void testParseJSONArrayWithInvalidChildrenObjects ()
|
||||||
|
|||||||
@@ -462,11 +462,16 @@ public:
|
|||||||
BEAST_EXPECT(jr[jss::error_message] == "You don't have permission for this command.");
|
BEAST_EXPECT(jr[jss::error_message] == "You don't have permission for this command.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::initializer_list<Json::Value> const nonArrays {Json::nullValue,
|
||||||
|
Json::intValue, Json::uintValue, Json::realValue, "",
|
||||||
|
Json::booleanValue, Json::objectValue};
|
||||||
|
|
||||||
for (auto const& f : {jss::accounts_proposed, jss::accounts})
|
for (auto const& f : {jss::accounts_proposed, jss::accounts})
|
||||||
{
|
{
|
||||||
|
for (auto const& nonArray : nonArrays)
|
||||||
{
|
{
|
||||||
Json::Value jv;
|
Json::Value jv;
|
||||||
jv[f] = "";
|
jv[f] = nonArray;
|
||||||
auto jr = wsc->invoke(method, jv) [jss::result];
|
auto jr = wsc->invoke(method, jv) [jss::result];
|
||||||
BEAST_EXPECT(jr[jss::error] == "invalidParams");
|
BEAST_EXPECT(jr[jss::error] == "invalidParams");
|
||||||
BEAST_EXPECT(jr[jss::error_message] == "Invalid parameters.");
|
BEAST_EXPECT(jr[jss::error_message] == "Invalid parameters.");
|
||||||
@@ -481,9 +486,10 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (auto const& nonArray : nonArrays)
|
||||||
{
|
{
|
||||||
Json::Value jv;
|
Json::Value jv;
|
||||||
jv[jss::books] = "";
|
jv[jss::books] = nonArray;
|
||||||
auto jr = wsc->invoke(method, jv) [jss::result];
|
auto jr = wsc->invoke(method, jv) [jss::result];
|
||||||
BEAST_EXPECT(jr[jss::error] == "invalidParams");
|
BEAST_EXPECT(jr[jss::error] == "invalidParams");
|
||||||
BEAST_EXPECT(jr[jss::error_message] == "Invalid parameters.");
|
BEAST_EXPECT(jr[jss::error_message] == "Invalid parameters.");
|
||||||
@@ -608,9 +614,10 @@ public:
|
|||||||
BEAST_EXPECT(jr[jss::error_message] == "No such market.");
|
BEAST_EXPECT(jr[jss::error_message] == "No such market.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (auto const& nonArray : nonArrays)
|
||||||
{
|
{
|
||||||
Json::Value jv;
|
Json::Value jv;
|
||||||
jv[jss::streams] = "";
|
jv[jss::streams] = nonArray;
|
||||||
auto jr = wsc->invoke(method, jv) [jss::result];
|
auto jr = wsc->invoke(method, jv) [jss::result];
|
||||||
BEAST_EXPECT(jr[jss::error] == "invalidParams");
|
BEAST_EXPECT(jr[jss::error] == "invalidParams");
|
||||||
BEAST_EXPECT(jr[jss::error_message] == "Invalid parameters.");
|
BEAST_EXPECT(jr[jss::error_message] == "Invalid parameters.");
|
||||||
|
|||||||
Reference in New Issue
Block a user