Hotfix for account_info, ledger_data and ip() (#134)

* remove option to create account from seed

* catch errors in derived().ip()

* checks keys.size() before accessing
This commit is contained in:
Nathan Nichols
2022-03-25 15:57:32 -05:00
committed by GitHub
parent ac6c4c25d6
commit e526083456
8 changed files with 76 additions and 43 deletions

View File

@@ -253,9 +253,8 @@ BackendInterface::fetchLedgerPage(
bool reachedEnd = false;
while (keys.size() < limit && !reachedEnd)
{
ripple::uint256 const& curCursor = keys.size() ? keys.back()
: cursor ? *cursor
: firstKey;
ripple::uint256 const& curCursor =
keys.size() ? keys.back() : cursor ? *cursor : firstKey;
uint32_t seq = outOfOrder ? range->maxSequence : ledgerSequence;
auto succ = fetchSuccessorKey(curCursor, seq, yield);
if (!succ)
@@ -283,7 +282,7 @@ BackendInterface::fetchLedgerPage(
assert(false);
}
}
if (!reachedEnd)
if (keys.size() && !reachedEnd)
page.cursor = keys.back();
return page;

View File

@@ -48,16 +48,8 @@ doAccountInfo(Context const& context)
// Get info on account.
auto accountID = accountFromStringStrict(strIdent);
if (!accountID)
{
if (!request.contains("strict") || !request.at("strict").as_bool())
{
accountID = accountFromSeed(strIdent);
if (!accountID)
return Status{Error::rpcBAD_SEED};
}
else
return Status{Error::rpcACT_MALFORMED};
}
return Status{Error::rpcACT_MALFORMED};
assert(accountID.has_value());
auto key = ripple::keylet::account(accountID.value());

View File

@@ -192,6 +192,10 @@ public:
}
auto ip = derived().ip();
if (!ip)
return;
auto session = derived().shared_from_this();
// Requests are handed using coroutines. Here we spawn a coroutine
@@ -208,7 +212,7 @@ public:
etl_,
dosGuard_,
counters_,
ip,
*ip,
session);
});
}

View File

@@ -50,10 +50,17 @@ public:
return std::move(stream_);
}
std::string
std::optional<std::string>
ip()
{
return stream_.socket().remote_endpoint().address().to_string();
try
{
return stream_.socket().remote_endpoint().address().to_string();
}
catch (std::exception const&)
{
return {};
}
}
// Start the asynchronous operation

View File

@@ -58,15 +58,22 @@ public:
return ws_;
}
std::string
std::optional<std::string>
ip()
{
return ws()
.next_layer()
.socket()
.remote_endpoint()
.address()
.to_string();
try
{
return ws()
.next_layer()
.socket()
.remote_endpoint()
.address()
.to_string();
}
catch (std::exception const&)
{
return {};
}
}
~PlainWsSession() = default;

View File

@@ -51,14 +51,21 @@ public:
return std::move(stream_);
}
std::string
std::optional<std::string>
ip()
{
return stream_.next_layer()
.socket()
.remote_endpoint()
.address()
.to_string();
try
{
return stream_.next_layer()
.socket()
.remote_endpoint()
.address()
.to_string();
}
catch (std::exception const&)
{
return {};
}
}
// Start the asynchronous operation

View File

@@ -55,16 +55,24 @@ public:
{
return ws_;
}
std::string
std::optional<std::string>
ip()
{
return ws()
.next_layer()
.next_layer()
.socket()
.remote_endpoint()
.address()
.to_string();
try
{
return ws()
.next_layer()
.next_layer()
.socket()
.remote_endpoint()
.address()
.to_string();
}
catch (std::exception const&)
{
return {};
}
}
};

View File

@@ -213,6 +213,10 @@ public:
void
handle_request(std::string const&& msg, boost::asio::yield_context& yc)
{
auto ip = derived().ip();
if (!ip)
return;
boost::json::object response = {};
auto sendError = [this](auto error) {
send(boost::json::serialize(RPC::make_error(error)));
@@ -221,6 +225,7 @@ public:
{
boost::json::value raw = boost::json::parse(msg);
boost::json::object request = raw.as_object();
BOOST_LOG_TRIVIAL(debug) << " received request : " << request;
try
{
@@ -238,7 +243,7 @@ public:
shared_from_this(),
*range,
counters_,
derived().ip());
*ip);
if (!context)
return sendError(RPC::Error::rpcBAD_SYNTAX);
@@ -287,7 +292,7 @@ public:
}
std::string responseStr = boost::json::serialize(response);
dosGuard_.add(derived().ip(), responseStr.size());
dosGuard_.add(*ip, responseStr.size());
send(std::move(responseStr));
}
@@ -302,9 +307,13 @@ public:
std::string msg{
static_cast<char const*>(buffer_.data().data()), buffer_.size()};
auto ip = derived().ip();
if (!ip)
return;
BOOST_LOG_TRIVIAL(debug)
<< __func__ << " received request from ip = " << ip;
if (!dosGuard_.isOk(ip))
<< __func__ << " received request from ip = " << *ip;
if (!dosGuard_.isOk(*ip))
{
boost::json::object response;
response["error"] = "Too many requests. Slow down";
@@ -312,7 +321,7 @@ public:
BOOST_LOG_TRIVIAL(trace) << __func__ << " : " << responseStr;
dosGuard_.add(ip, responseStr.size());
dosGuard_.add(*ip, responseStr.size());
send(std::move(responseStr));
}
else