Skip to content

Commit 6ff495f

Browse files
authored
Fix: Perform array size check (#6030)
The `ledger_entry` and `deposit_preauth` requests require an array of credentials. However, the array size is not checked before is gets processing. This fix adds checks and return errors in case array size is too big.
1 parent ad37461 commit 6ff495f

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/test/rpc/LedgerEntry_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,7 @@ class LedgerEntry_test : public beast::unit_test::suite
11031103
checkErrorValue(
11041104
jrr[jss::result],
11051105
"malformedAuthorizedCredentials",
1106-
"Invalid field 'authorized_credentials', not array.");
1106+
"Invalid field 'authorized_credentials', array empty.");
11071107
}
11081108

11091109
{
@@ -1144,7 +1144,7 @@ class LedgerEntry_test : public beast::unit_test::suite
11441144
checkErrorValue(
11451145
jrr[jss::result],
11461146
"malformedAuthorizedCredentials",
1147-
"Invalid field 'authorized_credentials', not array.");
1147+
"Invalid field 'authorized_credentials', array too long.");
11481148
}
11491149
}
11501150

src/xrpld/rpc/handlers/LedgerEntry.cpp

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
#include <xrpl/protocol/STXChainBridge.h>
1717
#include <xrpl/protocol/jss.h>
1818

19-
#include <functional>
20-
2119
namespace ripple {
2220

2321
static Expected<uint256, Json::Value>
@@ -178,18 +176,41 @@ static Expected<STArray, Json::Value>
178176
parseAuthorizeCredentials(Json::Value const& jv)
179177
{
180178
if (!jv.isArray())
179+
{
181180
return LedgerEntryHelpers::invalidFieldError(
182181
"malformedAuthorizedCredentials",
183182
jss::authorized_credentials,
184183
"array");
185-
STArray arr(sfAuthorizeCredentials, jv.size());
184+
}
185+
186+
std::uint32_t const n = jv.size();
187+
if (n > maxCredentialsArraySize)
188+
{
189+
return Unexpected(LedgerEntryHelpers::malformedError(
190+
"malformedAuthorizedCredentials",
191+
"Invalid field '" + std::string(jss::authorized_credentials) +
192+
"', array too long."));
193+
}
194+
195+
if (n == 0)
196+
{
197+
return Unexpected(LedgerEntryHelpers::malformedError(
198+
"malformedAuthorizedCredentials",
199+
"Invalid field '" + std::string(jss::authorized_credentials) +
200+
"', array empty."));
201+
}
202+
203+
STArray arr(sfAuthorizeCredentials, n);
186204
for (auto const& jo : jv)
187205
{
188206
if (!jo.isObject())
207+
{
189208
return LedgerEntryHelpers::invalidFieldError(
190209
"malformedAuthorizedCredentials",
191210
jss::authorized_credentials,
192211
"array");
212+
}
213+
193214
if (auto const value = LedgerEntryHelpers::hasRequired(
194215
jo,
195216
{jss::issuer, jss::credential_type},
@@ -260,13 +281,6 @@ parseDepositPreauth(Json::Value const& dp, Json::StaticString const fieldName)
260281
auto const arr = parseAuthorizeCredentials(ac);
261282
if (!arr.has_value())
262283
return Unexpected(arr.error());
263-
if (arr->empty() || (arr->size() > maxCredentialsArraySize))
264-
{
265-
return LedgerEntryHelpers::invalidFieldError(
266-
"malformedAuthorizedCredentials",
267-
jss::authorized_credentials,
268-
"array");
269-
}
270284

271285
auto const& sorted = credentials::makeSorted(arr.value());
272286
if (sorted.empty())

0 commit comments

Comments
 (0)